Class ClassUtils

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ARRAY_SUFFIX
      Suffix for array class names: "[]"
      static java.util.Set<java.lang.Class<?>> PRIMITIVE_ARRAY_TYPES  
      static java.util.Set<java.lang.Class<?>> PRIMITIVE_TYPES  
      static java.util.Set<java.lang.Class<?>> SIMPLE_TYPES
      Simple Types including: Void Boolean Character Byte Integer Float Double String BigDecimal BigInteger Date Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean arrayTypeEquals​(java.lang.Class<?> oneArrayType, java.lang.Class<?> anotherArrayType)  
      static <T> T cast​(java.lang.Object object, java.lang.Class<T> castType)
      Cast the given object to the specified type
      static java.util.List<java.lang.Class<?>> findAllClasses​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
      Find all classes from the specified type with filters
      static java.util.List<java.lang.Class<?>> findAllInheritedClasses​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
      Find all inherited classes from the specified type
      static java.util.List<java.lang.Class<?>> findAllInterfaces​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... interfaceFilters)
      find all interfaces from the specified type
      static java.util.List<java.lang.Class<?>> findAllSuperClasses​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
      Find all super classes from the specified type
      static java.util.Set<java.lang.String> findClassNamesInClassPath​(java.io.File classPath, boolean recursive)
      Find all class names in class path
      static java.util.Set<java.lang.String> findClassNamesInClassPath​(java.lang.String classPath, boolean recursive)
      Find all class names in class path
      static java.util.Set<java.lang.String> findClassNamesInDirectory​(java.io.File classesDirectory, boolean recursive)
      Find all class names in directory
      static java.util.Set<java.lang.String> findClassNamesInJarFile​(java.io.File jarFile, boolean recursive)
      Find all class names in jar file
      protected static java.util.List<java.lang.Class<?>> findTypes​(java.lang.Class<?> type, boolean includeSelf, boolean includeHierarchicalTypes, boolean includeGenericSuperclass, boolean includeGenericInterfaces, java.util.function.Predicate<? super java.lang.Class<?>>... typeFilters)  
      static java.util.List<java.lang.Class<?>> getAllClasses​(java.lang.Class<?> type)
      Get all classes from the specified type
      static java.util.List<java.lang.Class<?>> getAllInheritedClasses​(java.lang.Class<?> type)
      Get all inherited classes from the specified type
      static java.util.List<java.lang.Class<?>> getAllInheritedTypes​(java.lang.Class<?> type)
      Get all inherited types from the specified type
      static java.util.List<java.lang.Class<?>> getAllInterfaces​(java.lang.Class<?> type)
      Get all interfaces from the specified type
      static java.util.List<java.lang.Class<?>> getAllSuperClasses​(java.lang.Class<?> type)
      Get all super classes from the specified type
      static java.lang.String getSimpleName​(java.lang.Class<?> type)
      Get the simple name of the specified type
      static java.lang.Class<?> getTopComponentType​(java.lang.Class<?> arrayType)  
      static java.lang.Class<?> getTopComponentType​(java.lang.Object array)  
      static java.lang.String getTypeName​(java.lang.Class<?> type)
      Get the name of the specified type
      static java.lang.Class[] getTypes​(java.lang.Object... values)
      Resolve the types of the specified values
      static boolean isAbstractClass​(java.lang.Class<?> type)
      Is the specified type a abstract class or not?
      static boolean isArray​(java.lang.Class<?> type)
      The specified type is array or not?*
      static boolean isAssignableFrom​(java.lang.Class<?> superType, java.lang.Class<?> targetType)
      the semantics is same as Class.isAssignableFrom(Class)
      static boolean isConcreteClass​(java.lang.Class<?> type)
      Is the specified type a concrete class or not?
      static boolean isDerived​(java.lang.Class<?> targetType, java.lang.Class<?>... superTypes)
      the semantics is same as Class.isAssignableFrom(Class)
      static boolean isFinal​(java.lang.Class<?> type)  
      static boolean isGeneralClass​(java.lang.Class<?> type)
      Is the specified type a general class or not?
      protected static boolean isGeneralClass​(java.lang.Class<?> type, java.lang.Boolean isAbstract)
      Is the specified type a general class or not?
      static boolean isPrimitive​(java.lang.Class<?> type)
      The specified type is primitive type or simple type
      static boolean isSimpleType​(java.lang.Class<?> type)
      The specified type is simple type or not
      static boolean isTopLevelClass​(java.lang.Class<?> type)  
      static boolean isWrapperType​(java.lang.Class<?> type)  
      static <T> T newInstance​(java.lang.Class<T> type, java.lang.Object... args)  
      protected static java.lang.String resolveClassName​(java.io.File classesDirectory, java.io.File classFile)  
      static java.lang.String resolveClassName​(java.lang.String resourceName)
      Resolve resource name to class name
      static java.lang.String resolvePackageName​(java.lang.Class<?> targetClass)
      Resolve package name under specified class
      static java.lang.String resolvePackageName​(java.lang.String className)
      Resolve package name under specified class name
      static java.lang.Class<?> resolvePrimitiveClassName​(java.lang.String name)
      Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.
      static java.lang.Class<?> resolvePrimitiveType​(java.lang.Class<?> type)
      Resolve the primitive class from the specified type
      static java.lang.Class[] resolveTypes​(java.lang.Object... values)
      Resolve the types of the specified values
      static java.lang.Class<?> resolveWrapperType​(java.lang.Class<?> primitiveType)
      Resolve the wrapper class from the primitive type
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ARRAY_SUFFIX

        public static final java.lang.String ARRAY_SUFFIX
        Suffix for array class names: "[]"
        See Also:
        Constant Field Values
      • SIMPLE_TYPES

        public static final java.util.Set<java.lang.Class<?>> SIMPLE_TYPES
        Simple Types including:
        • Void
        • Boolean
        • Character
        • Byte
        • Integer
        • Float
        • Double
        • String
        • BigDecimal
        • BigInteger
        • Date
        • Object
        See Also:
        SimpleType
      • PRIMITIVE_TYPES

        public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_TYPES
      • PRIMITIVE_ARRAY_TYPES

        public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_ARRAY_TYPES
    • Method Detail

      • isArray

        public static boolean isArray​(java.lang.Class<?> type)
        The specified type is array or not?*
        Parameters:
        type - the type to test
        Returns:
        true if the specified type is an array class, false otherwise
        See Also:
        Class.isArray()
      • isConcreteClass

        public static boolean isConcreteClass​(java.lang.Class<?> type)
        Is the specified type a concrete class or not?
        Parameters:
        type - type to check
        Returns:
        true if concrete class, false otherwise.
      • isAbstractClass

        public static boolean isAbstractClass​(java.lang.Class<?> type)
        Is the specified type a abstract class or not?

        Parameters:
        type - the type
        Returns:
        true if type is a abstract class, false otherwise.
      • isGeneralClass

        public static boolean isGeneralClass​(java.lang.Class<?> type)
        Is the specified type a general class or not?

        Parameters:
        type - the type
        Returns:
        true if type is a general class, false otherwise.
      • isGeneralClass

        protected static boolean isGeneralClass​(java.lang.Class<?> type,
                                                java.lang.Boolean isAbstract)
        Is the specified type a general class or not?

        If isAbstract == null, it will not check type is abstract or not.

        Parameters:
        type - the type
        isAbstract - optional abstract flag
        Returns:
        true if type is a general (abstract) class, false otherwise.
      • isTopLevelClass

        public static boolean isTopLevelClass​(java.lang.Class<?> type)
      • isPrimitive

        public static boolean isPrimitive​(java.lang.Class<?> type)
        The specified type is primitive type or simple type

        It's an optimized implementation for Class.isPrimitive().

        Parameters:
        type - the type to test
        Returns:
        See Also:
        Class.isPrimitive()
      • isFinal

        public static boolean isFinal​(java.lang.Class<?> type)
      • isSimpleType

        public static boolean isSimpleType​(java.lang.Class<?> type)
        The specified type is simple type or not
        Parameters:
        type - the type to test
        Returns:
        if type is one element of SIMPLE_TYPES, return true, or false
        See Also:
        SIMPLE_TYPES
      • resolvePrimitiveType

        public static java.lang.Class<?> resolvePrimitiveType​(java.lang.Class<?> type)
        Resolve the primitive class from the specified type
        Parameters:
        type - the specified type
        Returns:
        null if not found
      • resolveWrapperType

        public static java.lang.Class<?> resolveWrapperType​(java.lang.Class<?> primitiveType)
        Resolve the wrapper class from the primitive type
        Parameters:
        primitiveType - the primitive type
        Returns:
        null if not found
      • isWrapperType

        public static boolean isWrapperType​(java.lang.Class<?> type)
      • arrayTypeEquals

        public static boolean arrayTypeEquals​(java.lang.Class<?> oneArrayType,
                                              java.lang.Class<?> anotherArrayType)
      • resolvePrimitiveClassName

        public static java.lang.Class<?> resolvePrimitiveClassName​(java.lang.String name)
        Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.

        Also supports the JVM's internal class names for primitive arrays. Does not support the "[]" suffix notation for primitive arrays; this is only supported by #forName.

        Parameters:
        name - the name of the potentially primitive class
        Returns:
        the primitive class, or null if the name does not denote a primitive class or primitive array class
      • resolvePackageName

        @Nullable
        public static java.lang.String resolvePackageName​(java.lang.Class<?> targetClass)
        Resolve package name under specified class
        Parameters:
        targetClass - target class
        Returns:
        package name
      • resolvePackageName

        @Nullable
        public static java.lang.String resolvePackageName​(java.lang.String className)
        Resolve package name under specified class name
        Parameters:
        className - class name
        Returns:
        package name
      • findClassNamesInClassPath

        @Nonnull
        public static java.util.Set<java.lang.String> findClassNamesInClassPath​(java.lang.String classPath,
                                                                                boolean recursive)
        Find all class names in class path
        Parameters:
        classPath - class path
        recursive - is recursive on sub directories
        Returns:
        all class names in class path
      • findClassNamesInClassPath

        public static java.util.Set<java.lang.String> findClassNamesInClassPath​(java.io.File classPath,
                                                                                boolean recursive)
        Find all class names in class path
        Parameters:
        classPath - JarFile or class patch directory
        recursive - is recursive on sub directories
        Returns:
        all class names in class path
      • findClassNamesInDirectory

        public static java.util.Set<java.lang.String> findClassNamesInDirectory​(java.io.File classesDirectory,
                                                                                boolean recursive)
        Find all class names in directory
        Parameters:
        classesDirectory - a directory to be found
        recursive - is recursive on sub directories
        Returns:
        all class names in directory
      • findClassNamesInJarFile

        public static java.util.Set<java.lang.String> findClassNamesInJarFile​(java.io.File jarFile,
                                                                              boolean recursive)
        Find all class names in jar file
        Parameters:
        jarFile - jar file
        recursive - is recursive on sub directories
        Returns:
        all class names in jar file
      • resolveClassName

        protected static java.lang.String resolveClassName​(java.io.File classesDirectory,
                                                           java.io.File classFile)
      • resolveClassName

        public static java.lang.String resolveClassName​(java.lang.String resourceName)
        Resolve resource name to class name
        Parameters:
        resourceName - resource name
        Returns:
        class name
      • getAllSuperClasses

        public static java.util.List<java.lang.Class<?>> getAllSuperClasses​(java.lang.Class<?> type)
        Get all super classes from the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null read-only List
      • getAllInterfaces

        public static java.util.List<java.lang.Class<?>> getAllInterfaces​(java.lang.Class<?> type)
        Get all interfaces from the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null read-only List
      • getAllInheritedTypes

        public static java.util.List<java.lang.Class<?>> getAllInheritedTypes​(java.lang.Class<?> type)
        Get all inherited types from the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null read-only List
      • getAllInheritedClasses

        public static java.util.List<java.lang.Class<?>> getAllInheritedClasses​(java.lang.Class<?> type)
        Get all inherited classes from the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null read-only List
      • getAllClasses

        public static java.util.List<java.lang.Class<?>> getAllClasses​(java.lang.Class<?> type)
        Get all classes from the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null read-only List
      • findAllSuperClasses

        public static java.util.List<java.lang.Class<?>> findAllSuperClasses​(java.lang.Class<?> type,
                                                                             java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
        Find all super classes from the specified type
        Parameters:
        type - the specified type
        classFilters - the filters for classes
        Returns:
        non-null read-only List
      • findAllInterfaces

        public static java.util.List<java.lang.Class<?>> findAllInterfaces​(java.lang.Class<?> type,
                                                                           java.util.function.Predicate<? super java.lang.Class<?>>... interfaceFilters)
        find all interfaces from the specified type
        Parameters:
        type - the specified type
        interfaceFilters - the filters for interfaces
        Returns:
        non-null read-only List
      • findAllInheritedClasses

        public static java.util.List<java.lang.Class<?>> findAllInheritedClasses​(java.lang.Class<?> type,
                                                                                 java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
        Find all inherited classes from the specified type
        Parameters:
        type - the specified type
        classFilters - the filters for types
        Returns:
        non-null read-only List
      • findAllClasses

        public static java.util.List<java.lang.Class<?>> findAllClasses​(java.lang.Class<?> type,
                                                                        java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
        Find all classes from the specified type with filters
        Parameters:
        type - the specified type
        classFilters - class filters
        Returns:
        non-null read-only List
      • findTypes

        protected static java.util.List<java.lang.Class<?>> findTypes​(java.lang.Class<?> type,
                                                                      boolean includeSelf,
                                                                      boolean includeHierarchicalTypes,
                                                                      boolean includeGenericSuperclass,
                                                                      boolean includeGenericInterfaces,
                                                                      java.util.function.Predicate<? super java.lang.Class<?>>... typeFilters)
      • isAssignableFrom

        public static boolean isAssignableFrom​(java.lang.Class<?> superType,
                                               java.lang.Class<?> targetType)
        the semantics is same as Class.isAssignableFrom(Class)
        Parameters:
        superType - the super type
        targetType - the target type
        Returns:
        see Class.isAssignableFrom(Class)
      • getTypes

        public static java.lang.Class[] getTypes​(java.lang.Object... values)
        Resolve the types of the specified values
        Parameters:
        values - the values
        Returns:
        If can't be resolved, return empty class array
      • resolveTypes

        public static java.lang.Class[] resolveTypes​(java.lang.Object... values)
        Resolve the types of the specified values
        Parameters:
        values - the values
        Returns:
        If can't be resolved, return empty class array
      • getTypeName

        public static java.lang.String getTypeName​(java.lang.Class<?> type)
        Get the name of the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null
      • getSimpleName

        public static java.lang.String getSimpleName​(java.lang.Class<?> type)
        Get the simple name of the specified type
        Parameters:
        type - the specified type
        Returns:
        non-null
      • isDerived

        public static boolean isDerived​(java.lang.Class<?> targetType,
                                        java.lang.Class<?>... superTypes)
        the semantics is same as Class.isAssignableFrom(Class)
        Parameters:
        targetType - the target type
        superTypes - the super types
        Returns:
        see Class.isAssignableFrom(Class)
      • newInstance

        public static <T> T newInstance​(java.lang.Class<T> type,
                                        java.lang.Object... args)
      • getTopComponentType

        public static java.lang.Class<?> getTopComponentType​(java.lang.Object array)
      • getTopComponentType

        public static java.lang.Class<?> getTopComponentType​(java.lang.Class<?> arrayType)
      • cast

        public static <T> T cast​(java.lang.Object object,
                                 java.lang.Class<T> castType)
        Cast the given object to the specified type
        Type Parameters:
        T - the type to cast
        Parameters:
        object - the object
        castType - the type to cast
        Returns:
        the casted instance if and only if object is an instance of castType , null otherwise