/** * Build a String that consists of the names of the classes/interfaces * in the given array. * <p>Basically like {@code AbstractCollection.toString()}, but stripping * the "class "/"interface " prefix before every class name. * * @param classes a Collection of Class objects (may be {@code null}) * @return a String of form "[com.foo.Bar, com.foo.Baz]" * @see java.util.AbstractCollection#toString() */ public static String classNamesToString(Class<?>... classes) { return classNamesToString(Arrays.asList(classes)); }
/** * Determine whether the {@link Class} identified by the supplied name is present * and can be loaded. Will return {@code false} if either the class or * one of its dependencies is not present or cannot be loaded. * * @param className the name of the class to check * @param classLoader the class loader to use * (may be {@code null}, which indicates the default class loader) * @return whether the specified class is present */ public static boolean isPresent(String className, ClassLoader classLoader) { try { forName(className, classLoader); return true; } catch (Throwable ex) { // Class or one of its dependencies is not present... return false; } }
/** * Return all interfaces that the given class implements as array, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * * @param clazz the class to analyze for interfaces * @return all interfaces that the given object implements as array */ public static Class<?>[] getAllInterfacesForClass(Class<?> clazz) { return getAllInterfacesForClass(clazz, null); }
Assert.notNull(name, "Name must not be null"); Class<?> clazz = resolvePrimitiveClassName(name); if (clazz == null) { clazz = commonClassCache.get(name); Class<?> elementClass = forName(elementClassName, classLoader); return Array.newInstance(elementClass, 0).getClass(); Class<?> elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); Class<?> elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); clToUse = getDefaultClassLoader();
/** * Create a new DefaultResourceLoader. * <p>ClassLoader access will happen using the thread context class loader * at the time of this ResourceLoader's initialization. * * @see java.lang.Thread#getContextClassLoader() */ public DefaultResourceLoader() { this.classLoader = ClassUtils.getDefaultClassLoader(); }
/** * Return all interfaces that the given class implements as Set, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * * @param clazz the class to analyze for interfaces * @param classLoader the ClassLoader that the interfaces need to be visible in * (may be {@code null} when accepting all declared interfaces) * @return all interfaces that the given object implements as Set */ public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, ClassLoader classLoader) { Assert.notNull(clazz, "Class must not be null"); if (clazz.isInterface() && isVisible(clazz, classLoader)) { return Collections.<Class<?>>singleton(clazz); } Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>(); while (clazz != null) { Class<?>[] ifcs = clazz.getInterfaces(); for (Class<?> ifc : ifcs) { interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader)); } clazz = clazz.getSuperclass(); } return interfaces; }
/** * Return all interfaces that the given class implements as Set, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces * @return all interfaces that the given object implements as Set */ public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz) { return getAllInterfacesForClassAsSet(clazz, null); }
/** * Return a path suitable for use with {@code ClassLoader.getResource} * (also suitable for use with {@code Class.getResource} by prepending a * slash ('/') to the return value). Built by taking the package of the specified * class file, converting all dots ('.') to slashes ('/'), adding a trailing slash * if necessary, and concatenating the specified resource name to this. * <br> As such, this function may be used to build a path suitable for * loading a resource file that is in the same package as a class file, * although ClassPathResource is usually * even more convenient. * @param clazz the Class whose package will be used as the base * @param resourceName the resource name to append. A leading slash is optional. * @return the built-up resource path * @see ClassLoader#getResource * @see Class#getResource */ public static String addResourcePathToPackagePath(Class<?> clazz, String resourceName) { Assert.notNull(resourceName, "Resource name must not be null"); if (!resourceName.startsWith("/")) { return classPackageAsResourcePath(clazz) + "/" + resourceName; } return classPackageAsResourcePath(clazz) + resourceName; }
/** * Determine the name of the package of the given class, * e.g. "java.lang" for the {@code java.lang.String} class. * * @param clazz the class * @return the package name, or the empty String if the class * is defined in the default package */ public static String getPackageName(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); return getPackageName(clazz.getName()); }
/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
/** * Determine whether the given class has a public method with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param methodName the name of the method * @param paramTypes the parameter types of the method * @return whether the class has a corresponding method * @see Class#getMethod */ public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { return (getMethodIfAvailable(clazz, methodName, paramTypes) != null); }
/** * Return the number of methods with a given name (with any argument types), * for the given class and/or its superclasses. Includes non-public methods. * * @param clazz the clazz to check * @param methodName the name of the method * @return the number of methods with the given name */ public static int getMethodCountForName(Class<?> clazz, String methodName) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(methodName, "Method name must not be null"); int count = 0; Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method method : declaredMethods) { if (methodName.equals(method.getName())) { count++; } } Class<?>[] ifcs = clazz.getInterfaces(); for (Class<?> ifc : ifcs) { count += getMethodCountForName(ifc, methodName); } if (clazz.getSuperclass() != null) { count += getMethodCountForName(clazz.getSuperclass(), methodName); } return count; }
Assert.notNull(name, "Name must not be null"); Class<?> clazz = resolvePrimitiveClassName(name); if (clazz == null) { clazz = commonClassCache.get(name); Class<?> elementClass = forName(elementClassName, classLoader); return Array.newInstance(elementClass, 0).getClass(); Class<?> elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); Class<?> elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); clToUse = getDefaultClassLoader();
/** * Create a new DefaultResourceLoader. * <p>ClassLoader access will happen using the thread context class loader * at the time of this ResourceLoader's initialization. * * @see java.lang.Thread#getContextClassLoader() */ public DefaultResourceLoader() { this.classLoader = ClassUtils.getDefaultClassLoader(); }
/** * Return all interfaces that the given class implements as Set, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces * @param classLoader the ClassLoader that the interfaces need to be visible in * (may be {@code null} when accepting all declared interfaces) * @return all interfaces that the given object implements as Set */ public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, ClassLoader classLoader) { Assert.notNull(clazz, "Class must not be null"); if (clazz.isInterface() && isVisible(clazz, classLoader)) { return Collections.<Class<?>>singleton(clazz); } Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>(); while (clazz != null) { Class<?>[] ifcs = clazz.getInterfaces(); for (Class<?> ifc : ifcs) { interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader)); } clazz = clazz.getSuperclass(); } return interfaces; }
/** * Return all interfaces that the given class implements as Set, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * * @param clazz the class to analyze for interfaces * @return all interfaces that the given object implements as Set */ public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz) { return getAllInterfacesForClassAsSet(clazz, null); }
/** * Return a path suitable for use with {@code ClassLoader.getResource} * (also suitable for use with {@code Class.getResource} by prepending a * slash ('/') to the return value). Built by taking the package of the specified * class file, converting all dots ('.') to slashes ('/'), adding a trailing slash * if necessary, and concatenating the specified resource name to this. * <br> As such, this function may be used to build a path suitable for * loading a resource file that is in the same package as a class file, * although ClassPathResource is usually * even more convenient. * * @param clazz the Class whose package will be used as the base * @param resourceName the resource name to append. A leading slash is optional. * @return the built-up resource path * @see ClassLoader#getResource * @see Class#getResource */ public static String addResourcePathToPackagePath(Class<?> clazz, String resourceName) { Assert.notNull(resourceName, "Resource name must not be null"); if (!resourceName.startsWith("/")) { return classPackageAsResourcePath(clazz) + "/" + resourceName; } return classPackageAsResourcePath(clazz) + resourceName; }
/** * Determine the name of the package of the given class, * e.g. "java.lang" for the {@code java.lang.String} class. * @param clazz the class * @return the package name, or the empty String if the class * is defined in the default package */ public static String getPackageName(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); return getPackageName(clazz.getName()); }
/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
/** * Determine whether the given class has a public method with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * * @param clazz the clazz to analyze * @param methodName the name of the method * @param paramTypes the parameter types of the method * @return whether the class has a corresponding method * @see Class#getMethod */ public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { return (getMethodIfAvailable(clazz, methodName, paramTypes) != null); }