Class ClassUtils


  • public final class ClassUtils
    extends Object
    Utility methods to aid in class/resource loading.
    Author:
    Kevin Conner
    • Method Detail

      • forName

        public static Class<?> forName​(String className,
                                       Class<?> caller)
                                throws ClassNotFoundException
        Load the specified class.
        Parameters:
        className - The name of the class to load.
        caller - The class of the caller.
        Returns:
        The specified class.
        Throws:
        ClassNotFoundException - If the class cannot be found.
      • getResourceAsStream

        public static InputStream getResourceAsStream​(String resourceName,
                                                      Class<?> caller)
        Get the specified resource as a stream.
        Parameters:
        resourceName - The name of the class to load.
        caller - The class of the caller.
        Returns:
        The input stream for the resource or null if not found.
      • getResourceAsStream

        public static InputStream getResourceAsStream​(String resourceName,
                                                      ClassLoader classLoader)
        Get the specified resource as a stream.
        Parameters:
        resourceName - The name of the class to load.
        classLoader - The ClassLoader to use, if the resource is not located via the Thread context ClassLoader.
        Returns:
        The input stream for the resource or null if not found.
      • findInstancesOf

        public static List<Class<?>> findInstancesOf​(Class<?> type)
      • getClasses

        public static <T> List<Class<T>> getClasses​(String fileName,
                                                    Class<T> instanceOf)
        Will try to create a List of classes that are listed in the passed in file. The fileName is expected to be found on the classpath.
        Parameters:
        fileName - The name of the file containing the list of classes, one class name per line.
        instanceOf - The instanceof filter.
        Returns:
        List> list of the classes contained in the file.
      • toFilePath

        public static String toFilePath​(Package aPackage)
      • containsAssignableClass

        public static boolean containsAssignableClass​(Class<?> toFind,
                                                      Class<?>... classes)
        Checks if the class in the first parameter is assignable to one of the classes in the second or any later parameter.
        Parameters:
        toFind - The class to check for assignment compatibility.
        classes - The classes against which toFind should be checked.
        Returns:
        true if toFind is assignable to any one of the specified classes.
      • indexOfFirstAssignableClass

        public static int indexOfFirstAssignableClass​(Class<?> toFind,
                                                      Class<?>... classes)
        Gets the array index of the first class within an array of classes to which a specified class is assignable.
        Parameters:
        toFind - The class to check for assignment compatibility.
        classes - The classes against which toFind should be checked.
        Returns:
        The array index of the first class within classes to which toFind is assignable, if it can be assigned to one of the specified classes, -1 otherwise.
      • toSetterName

        public static String toSetterName​(String property)
      • toGetterName

        public static String toGetterName​(String property)
      • toIsGetterName

        public static String toIsGetterName​(String property)
      • getSetterMethod

        public static Method getSetterMethod​(String setterName,
                                             Object bean,
                                             Class<?> setterParamType)
      • getSetterMethod

        public static Method getSetterMethod​(String setterName,
                                             Class beanclass,
                                             Class<?> setterParamType)
      • getGetterMethod

        public static Method getGetterMethod​(String getterName,
                                             Object bean,
                                             Class<?> returnType)
      • getGetterMethodByProperty

        public static Method getGetterMethodByProperty​(String propertyName,
                                                       Class<?> beanClass,
                                                       Class<?> returnType)
      • getLongMemberName

        public static String getLongMemberName​(Member field)
      • toClassName

        public static String toClassName​(String fileName)
        Convert the Java-class-file-name to the equivalent Java-class-name (dot delimited package name).

        EG:
        a/b/c/X.class converts to a.b.c.X
        a/b/c/X converts to a.b.c.X
        a.b.c.X converts to a.b.c.X
        a.b.c.X.class converts to a.b.c.X

        Parameters:
        fileName - The file name String to be translated.
        Returns:
        Java Class runtime name representation of the supplied file name String.