Class ClassUtils


  • public class ClassUtils
    extends Object
    Utilities for handling classes. These are to supplement the methods provided by the Class object.
    • Constructor Detail

      • ClassUtils

        public ClassUtils()
    • Method Detail

      • newInstance

        public static <T> T newInstance​(Class<T> type,
                                        Class[] parameterTypes,
                                        Object[] parameters)
        Accessor for a new instance of an object. Uses reflection to generate the instance using the passed constructor parameter arguments.
        Type Parameters:
        T - Type of the new instance
        Parameters:
        type - Type of object (the class).
        parameterTypes - Classes of params for the constructor
        parameters - The parameters for the constructor
        Returns:
        The object
        Throws:
        NucleusException - If an error occurs creating the instance
      • getConstructorWithArguments

        public static <T> Constructor<T> getConstructorWithArguments​(Class<T> cls,
                                                                     Class[] argTypes)
        Convenience method to return the constructor of the passed class that accepts the supplied argument types. Allows for primitive to primitive wrapper conversion. Typically used by the JDOQL/JPQL ResultClass mapping process.
        Type Parameters:
        T - Type that the constructor is for
        Parameters:
        cls - The class
        argTypes - The constructor argument types. If we know we need a parameter yet don't know the type then this will have a null for that argument type.
        Returns:
        The constructor
      • getConstructorWithArguments

        public static <T> Constructor<T> getConstructorWithArguments​(Class<T> cls,
                                                                     Class[] argTypes,
                                                                     boolean[] argTypeCheck)
        Convenience method to return the constructor of the passed class that accepts the supplied argument types. Allows for primitive to primitive wrapper conversion. Typically used by the JDOQL/JPQL ResultClass mapping process.
        Type Parameters:
        T - Type that the constructor is for
        Parameters:
        cls - The class
        argTypes - The constructor argument types. If we know we need a parameter yet don't know the type then this will have a null for that argument type.
        argTypeCheck - Whether to check the type of the different arguments. Useful where we don't know the result type of an argument until processing results
        Returns:
        The constructor
      • getMethodWithArgument

        public static Method getMethodWithArgument​(Class cls,
                                                   String methodName,
                                                   Class argType)
        Obtain a method from a class or superclasses using reflection. The method will have the specified name and will take a single argument. Allows for the argument type being primitive or its associated wrapper.
        Parameters:
        cls - the class to find the declared fields and populate the map
        methodName - the method name to find
        argType - the argument type
        Returns:
        The Method
      • getMethodForClass

        public static Method getMethodForClass​(Class cls,
                                               String methodName,
                                               Class[] argtypes)
        Obtain a method from a class or superclasses using reflection
        Parameters:
        cls - the class where to look for the method
        methodName - the method name to find
        argtypes - the classes argument of the method
        Returns:
        The Method
      • getClassFilesForDirectory

        public static Collection<File> getClassFilesForDirectory​(File dir,
                                                                 boolean includeNormal,
                                                                 boolean includeInner)
        Method to return the class files below the specified directory.
        Parameters:
        dir - The directory
        includeNormal - Whether to include normal classes
        includeInner - Whether to include inner classes
        Returns:
        The class files (Collection of File objects).
      • getFilesForDirectory

        public static Collection<File> getFilesForDirectory​(File dir)
        Method to return the files below the specified directory.
        Parameters:
        dir - The directory
        Returns:
        The files
      • getClassNamesForJarFile

        public static String[] getClassNamesForJarFile​(String jarFileName)
        Convenience accessor for the names of all class files in the jar file with the specified name. The returned class names are of the form "org.datanucleus.MyClass".
        Parameters:
        jarFileName - Name of the jar file
        Returns:
        The class names
      • getClassNamesForJarFile

        public static String[] getClassNamesForJarFile​(URL jarFileURL)
        Convenience accessor for the names of all class files in the jar file with the specified URL. The returned class names are of the form "org.datanucleus.MyClass".
        Parameters:
        jarFileURL - URL for the jar file
        Returns:
        The class names
      • getClassNamesForJarFile

        public static String[] getClassNamesForJarFile​(URI jarFileURI)
        Convenience accessor for the names of all class files in the jar file with the specified URL. The returned class names are of the form "org.datanucleus.MyClass".
        Parameters:
        jarFileURI - URI for the jar file
        Returns:
        The class names
      • getPackageJdoFilesForJarFile

        public static String[] getPackageJdoFilesForJarFile​(String jarFileName)
        Convenience accessor for the names of all "package.jdo" files in the jar file with the specified name.
        Parameters:
        jarFileName - Name of the jar file
        Returns:
        The "package.jdo" file names
      • getPackageJdoFilesForJarFile

        public static String[] getPackageJdoFilesForJarFile​(URL jarFileURL)
        Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.
        Parameters:
        jarFileURL - URL for the jar file
        Returns:
        The "package.jdo" file names
      • getPackageJdoFilesForJarFile

        public static String[] getPackageJdoFilesForJarFile​(URI jarFileURI)
        Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.
        Parameters:
        jarFileURI - URI for the jar file
        Returns:
        The "package.jdo" file names
      • getClassNamesForDirectoryAndBelow

        public static String[] getClassNamesForDirectoryAndBelow​(File dir)
        Convenience method to return the names of classes specified in the directory and below. All inner classes are ignored.
        Parameters:
        dir - Directory that we should look below (root of classpath)
        Returns:
        The class names
      • isInnerClass

        public static boolean isInnerClass​(String class_name)
        Method to check whether a classname is for an inner class. Currently checks for the presence of $ in the name.
        Parameters:
        class_name - The class name
        Returns:
        Whether it is an inner class
      • hasDefaultConstructor

        public static boolean hasDefaultConstructor​(Class cls)
        Method to check for a default constructor on a class. Doesn't check superclasses for the default constructor.
        Parameters:
        cls - The class
        Returns:
        Whether it has a default constructor
      • getSuperclasses

        public static Collection<Class<?>> getSuperclasses​(Class<?> cls)
        Method to return the superclasses for a class. The superclasses will be ordered.
        Parameters:
        cls - The class
        Returns:
        The superclass of this class.
      • getSuperinterfaces

        public static Collection<Class<?>> getSuperinterfaces​(Class<?> cls)
        Method to return the superinterfaces for a class. The superinterfaces will be ordered and unique.
        Parameters:
        cls - The class
        Returns:
        The superinterfaces of this class.
      • getFieldForClass

        public static Field getFieldForClass​(Class cls,
                                             String fieldName)
        Obtain a field from a class or superclasses using reflection.
        Parameters:
        cls - the class to find the field from
        fieldName - the field name to find
        Returns:
        The Field
      • getGetterMethodForClass

        public static Method getGetterMethodForClass​(Class cls,
                                                     String beanName)
        Obtain a (Java bean) getter method from a class or superclasses using reflection. Any 'get...' method will take precedence over 'is...' methods.
        Parameters:
        cls - the class to find the getter from
        beanName - the name of the java bean to find the getter for
        Returns:
        The getter Method
      • getSetterMethodForClass

        public static Method getSetterMethodForClass​(Class cls,
                                                     String beanName,
                                                     Class type)
        Obtain a (Java bean) setter method from a class or superclasses using reflection.
        Parameters:
        cls - the class to find the setter from
        beanName - the name of the java bean to find the setter for
        type - the type of the java bean to find the setter for
        Returns:
        The setter Method
      • getWrapperTypeNameForPrimitiveTypeName

        public static String getWrapperTypeNameForPrimitiveTypeName​(String typeName)
        Convenience method to return the object wrapper type for a primitive type name. If the type is not a primitive then just returns the type name
        Parameters:
        typeName - The primitive type name
        Returns:
        The object wrapper type name for this primitive
      • getWrapperTypeForPrimitiveType

        public static Class getWrapperTypeForPrimitiveType​(Class type)
        Convenience method to return the object wrapper type for a primitive type.
        Parameters:
        type - The primitive type
        Returns:
        The object wrapper type for this primitive
      • getPrimitiveTypeForType

        public static Class getPrimitiveTypeForType​(Class type)
        Method to return the primitive equivalent of the specified type (if any). Returns null if there is no primitive equivalent.
        Parameters:
        type - The type
        Returns:
        The primitive equivalent.
      • isPrimitiveWrapperType

        public static boolean isPrimitiveWrapperType​(String typeName)
        Convenience method to return if the passed type (name) is a primitive wrapper type.
        Parameters:
        typeName - Name of the type
        Returns:
        Whether it is a primitive wrapper
      • isPrimitiveArrayType

        public static boolean isPrimitiveArrayType​(String typeName)
        Convenience method to return if the passed type (name) is a primitive array type.
        Parameters:
        typeName - Name of the type
        Returns:
        Whether it is a primitive array
      • isPrimitiveType

        public static boolean isPrimitiveType​(String typeName)
        Convenience method to return if the passed type (name) is a primitive type.
        Parameters:
        typeName - Name of the type
        Returns:
        Whether it is a primitive
      • typesAreCompatible

        public static boolean typesAreCompatible​(Class cls1,
                                                 String clsName2,
                                                 ClassLoaderResolver clr)
        Convenience method to return if two types are compatible. Returns true if both types are primitive/wrappers and are of the same type. Returns true if clsName2 is the same or a subclass of cls1. Otherwise returns false;
        Parameters:
        cls1 - First class
        clsName2 - Name of the second class
        clr - ClassLoader resolver to use
        Returns:
        Whether they are compatible
      • typesAreCompatible

        public static boolean typesAreCompatible​(Class cls1,
                                                 Class cls2)
        Convenience method to return if two types are compatible. Returns true if both types are primitive/wrappers and are of the same type. Returns true if cls2 is the same or a subclass of cls1. Otherwise returns false;
        Parameters:
        cls1 - First class
        cls2 - Second class
        Returns:
        Whether they are compatible
      • createFullClassName

        public static String createFullClassName​(String pkg_name,
                                                 String cls_name)
        Utility to create the full class name given the package and class name. Some examples
         packageName=test className=Test, returns result=test.Test
         packageName=test className=test1.Test, returns result=test1.Test
         packageName=<null> className=Test, returns result=Test
         packageName=<null> className=test1.Test, returns result=test1.Test
         
        Parameters:
        pkg_name - package name.
        cls_name - class name.
        Returns:
        generated full class name.
      • getJavaLangClassForType

        public static String getJavaLangClassForType​(String type)
        Convenience method to return the passed type as a java.lang type wherever possible. The passed type will be stripped of any package name and will be checked if it is a known java.lang class. This is used where the user has specified a class name for a collection or map element/key/value type and meant a java.lang class but didn't fully qualify it.
        Parameters:
        type - The type name
        Returns:
        The java.lang equivalent (or the input type if not possible)
      • classesAreDescendents

        public static boolean classesAreDescendents​(ClassLoaderResolver clr,
                                                    String class_name_1,
                                                    String class_name_2)
        Method to check if 2 classes are direct descendants, so whether one of them is a superclass of the other.
        Parameters:
        clr - ClassLoaderResolver for loading the classes
        class_name_1 - Name of first class
        class_name_2 - Name of second class
        Returns:
        Whether they are direct descendants.
      • dumpClassInformation

        public static void dumpClassInformation​(Class cls)
        Utility to use Reflection to dump out the details of a class. Will list all superclasses, interfaces, methods and fields. Can be used, for example, in checking the methods adding by the enhancement process. The information is dumped out the GENERAL log.
        Parameters:
        cls - The class to dump out to the log
      • getJavaBeanGetterName

        public static String getJavaBeanGetterName​(String fieldName,
                                                   boolean isBoolean)
        Generate a JavaBeans compatible getter name
        Parameters:
        fieldName - the field name
        isBoolean - whether the field is primitive boolean type
        Returns:
        the getter name
      • getJavaBeanSetterName

        public static String getJavaBeanSetterName​(String fieldName)
        Generate a JavaBeans compatible setter name
        Parameters:
        fieldName - the field name
        Returns:
        the setter name
      • getFieldNameForJavaBeanGetter

        public static String getFieldNameForJavaBeanGetter​(String methodName)
        Generate a field name for JavaBeans compatible getter method
        Parameters:
        methodName - the method name
        Returns:
        the field name
      • getFieldNameForJavaBeanSetter

        public static String getFieldNameForJavaBeanSetter​(String methodName)
        Generate a field name for JavaBeans compatible setter method
        Parameters:
        methodName - the method name
        Returns:
        the field name
      • getClassNameForFileName

        public static String getClassNameForFileName​(String fileName,
                                                     ClassLoaderResolver clr)
        Convenience method to try to find the class name stored in the specified file. Moves along the file, chopping off components from the front until it finds a class in the class path. This is typically used as a fallback after trying the method after this.
        Parameters:
        fileName - Name of file
        clr - ClassLoader resolver
        Returns:
        The class name (if found)
      • getClassNameForFileURL

        public static String getClassNameForFileURL​(URL fileURL)
                                             throws ClassNotFoundException
        Utility to find the class name of a class given the absolute file name of its class file. Creates a loader and loads the class directly to find it.
        Parameters:
        fileURL - URL for the class file
        Returns:
        The name of the class
        Throws:
        ClassNotFoundException - Thrown when the file is not found
      • getPackageNameForClass

        public static String getPackageNameForClass​(Class cls)
        Utility to return the package name for a class. Allows for the result of class.getPackage() being null.
        Parameters:
        cls - The class
        Returns:
        The name of its package (or null if no package e.g a primitive)
      • getPackageNameForClassName

        public static String getPackageNameForClassName​(String clsName)
        Utility to return the package name for a class name.
        Parameters:
        clsName - The class
        Returns:
        The name of its package (or null if no package e.g a primitive)
      • getClassNameForClass

        public static String getClassNameForClass​(Class cls)
        Utility to return the class name without the package name for a class.
        Parameters:
        cls - The class
        Returns:
        The name of the class without its package
      • getClassForGenericType

        public static Class getClassForGenericType​(Type genericType,
                                                   int pos)
        Convenience method to attempt to return the class for the provided generic type.
        Parameters:
        genericType - The generic type
        pos - The position of the generic arg (in case of multiple)
        Returns:
        The class (if determinable), or null
      • getCollectionElementType

        public static String getCollectionElementType​(Field field)
        Convenience method to extract the element type of a collection when using JDK1.5 generics given the input field.
        Parameters:
        field - The field
        Returns:
        The name of the element class
      • getCollectionElementType

        public static String getCollectionElementType​(Method method)
        Convenience method to extract the element type of a collection when using JDK1.5 generics, given the input method (getter).
        Parameters:
        method - The method
        Returns:
        The name of the element class
      • getCollectionElementType

        public static Class getCollectionElementType​(Class type,
                                                     Type genericType)
        Convenience method to extract the element type of a collection when using JDK1.5 generics given the input field.
        Parameters:
        type - the field type
        genericType - the generic type
        Returns:
        The element class
      • getMapKeyType

        public static String getMapKeyType​(Field field)
        Convenience method to extract the key type of a map when using JDK1.5 generics given the input field.
        Parameters:
        field - The field
        Returns:
        The name of the key class
      • getMapKeyType

        public static String getMapKeyType​(Method method)
        Convenience method to extract the key type of a map when using JDK1.5 generics given the input method.
        Parameters:
        method - The method
        Returns:
        The name of the key class
      • getMapKeyType

        public static Class getMapKeyType​(Class type,
                                          Type genericType)
        Convenience method to extract the key type of a map when using JDK1.5 generics given the input field.
        Parameters:
        type - the field type
        genericType - the generic type
        Returns:
        The name of the key class
      • getMapValueType

        public static String getMapValueType​(Field field)
        Convenience method to extract the value type of a map when using JDK1.5 generics given the input field
        Parameters:
        field - The field
        Returns:
        The name of the value class
      • getMapValueType

        public static String getMapValueType​(Method method)
        Convenience method to extract the value type of a map when using JDK1.5 generics given the input method.
        Parameters:
        method - The method
        Returns:
        The name of the value class
      • getMapValueType

        public static Class getMapValueType​(Class type,
                                            Type genericType)
        Convenience method to extract the value type of a map when using JDK1.5 generics given the input field
        Parameters:
        type - the field type
        genericType - the generic type
        Returns:
        The name of the value class
      • getModifiersForFieldOfClass

        public static int getModifiersForFieldOfClass​(ClassLoaderResolver clr,
                                                      String className,
                                                      String fieldName)
        Convenience accessor for the modifiers of a field in a class.
        Parameters:
        clr - ClassLoader resolver
        className - Name of the class
        fieldName - Name of the field
        Returns:
        The modifiers
      • isReferenceType

        public static boolean isReferenceType​(Class cls)
        Method to return whether the passes type is a "reference" type. A "reference" type is either an interface or an Object, so can be used as a reference to a persistable object.
        Parameters:
        cls - The type
        Returns:
        Whether it is a reference type
      • isClassPresent

        public static boolean isClassPresent​(String className,
                                             ClassLoaderResolver clr)
        Convenience method to say whether a class is present.
        Parameters:
        className - The class name
        clr - ClassLoader resolver
        Returns:
        Whether it is present
      • assertClassForJarExistsInClasspath

        public static void assertClassForJarExistsInClasspath​(ClassLoaderResolver clr,
                                                              String className,
                                                              String jarName)
        Convenience method to throw a NucleusUserException if the specified class is not loadable from the ClassLoaderResolver.
        Parameters:
        clr - ClassLoader resolver
        className - Name of the class
        jarName - Name of the jar containing the class
        Throws:
        NucleusUserException - if the class is not found
      • stringArrayContainsValue

        public static boolean stringArrayContainsValue​(String[] array,
                                                       String value)
        Convenience method to return if a String array contains a value.
        Parameters:
        array - The String array
        value - The value
        Returns:
        Whether it is contained
      • getValueOfMethodByReflection

        public static Object getValueOfMethodByReflection​(Object object,
                                                          String methodName,
                                                          Object... args)
        Helper method to return the value returned by a method on an object using reflection.
        Parameters:
        object - The object
        methodName - Name of the method
        args - The arguments
        Returns:
        The value
      • getValueOfFieldByReflection

        public static Object getValueOfFieldByReflection​(Object object,
                                                         String fieldName)
        Helper method to return the value of a field of an object using reflection.
        Parameters:
        object - The object
        fieldName - Name of the field
        Returns:
        The value
      • getValueForIdentityField

        public static Object getValueForIdentityField​(Object id,
                                                      String fieldName)
        Method to find the value of a field of the provided user-defined identity. The field has to be either accessible, or have a Java-bean style getter.
        Parameters:
        id - The identity
        fieldName - Name of the field
        Returns:
        The value of the field in this identity
        Throws:
        NucleusUserException - if not possible to get the value
      • getClassForMemberOfClass

        public static Class getClassForMemberOfClass​(Class cls,
                                                     String memberName)
        Method that returns the class type of a member of the specified class (if present). The member can either be a field, or a Java bean name (for a getter).
        Parameters:
        cls - The class
        memberName - The member name
        Returns:
        The member class (or null if not present)
      • isJavaBeanGetterMethod

        public static boolean isJavaBeanGetterMethod​(Method method)
        Convenience method to return if the supplied method is a valid java bean getter method. Checks that it is not static, that its name starts "get" or "is", that it has a return type and that it takes no arguments.
        Parameters:
        method - The method
        Returns:
        Whether this method is a java bean getter
      • clearFlags

        public static void clearFlags​(boolean[] flags)
        Utility to clear the supplied flags.
        Parameters:
        flags - The flags array to clear
      • clearFlags

        public static void clearFlags​(boolean[] flags,
                                      int[] fields)
        Utility to clear the supplied flags.
        Parameters:
        flags - Flags to clear
        fields - fields numbers where the flags will be cleared
      • getFlagsSetTo

        public static int[] getFlagsSetTo​(boolean[] flags,
                                          boolean state)
        Returns an array of integers containing the indices of all elements in flags that are in the state passed as argument.
        Parameters:
        flags - Array of flags (true or false)
        state - The state to search (true or false)
        Returns:
        The settings of the flags
      • getFlagsSetTo

        public static int[] getFlagsSetTo​(boolean[] flags,
                                          int[] indices,
                                          boolean state)
        Returns an array of integers containing the indices of all elements in flags whose index occurs in indices and whose value is state.
        Parameters:
        flags - the boolean array
        indices - The positions in the array
        state - The state that we want to match
        Returns:
        The positions of flags that are set to this state
      • getBitFromInt

        public static boolean getBitFromInt​(int bits,
                                            int bitIndex)
        Convenience method to get the value of a bit from an int when we are storing (up to 32) boolean in an int for memory utilisation purposes.
        Parameters:
        bits - The int storing the bits
        bitIndex - The index of this bit
        Returns:
        The value of this bit (as a boolean)
      • setBitInInt

        public static int setBitInInt​(int bits,
                                      int bitIndex,
                                      boolean flag)
        Convenience method to set a boolean as a bit in the specified int, for memory utilisation purposes.
        Parameters:
        bits - The int storing the bits
        bitIndex - The index of this bit
        flag - The boolean value to store
        Returns:
        The int with this bit set
      • getIndexesOfCollectionInList

        public static int[] getIndexesOfCollectionInList​(List delegate,
                                                         Collection elements)
        Convenience method to return the list index positions in "delegate" where the passed "elements" are located. The index positions are in reverse numeric order (i.e highest first).
        Parameters:
        delegate - The delegate list
        elements - The elements to check for
        Returns:
        The index positions (highest first)