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
      static java.util.Set<java.lang.Class<?>> WRAPPER_TYPES  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean arrayTypeEquals​(java.lang.Class<?> oneArrayType, java.lang.Class<?> anotherArrayType)
      Checks if two array types are equivalent, including nested arrays.
      static <T> T cast​(java.lang.Object object, java.lang.Class<T> castType)
      Casts the given object to the specified type if possible.
      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, optionally filtering the results.
      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, optionally filtering the results.
      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, optionally filtering the results.
      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, optionally filtering the results.
      static java.util.Set<java.lang.String> findClassNamesInClassPath​(java.io.File classPath, boolean recursive)
      Finds all class names in the specified class path.
      static java.util.Set<java.lang.String> findClassNamesInClassPath​(java.lang.String classPath, boolean recursive)
      Finds all class names in the specified class path.
      static java.util.Set<java.lang.String> findClassNamesInDirectory​(java.io.File classesDirectory, boolean recursive)
      Finds all class names in the specified directory.
      static java.util.Set<java.lang.String> findClassNamesInJarFile​(java.io.File jarFile, boolean recursive)
      Finds all class names in the specified 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)
      Finds types based on the specified criteria.
      static java.util.List<java.lang.Class<?>> getAllClasses​(java.lang.Class<?> type)
      Get all classes from the specified type, including the type itself, its superclasses, and interfaces.
      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.Class<?> getClass​(java.lang.Object object)
      Gets the Class of the given object.
      static java.lang.Class<?>[] getClasses​(java.lang.Object... values)
      Get the types of the specified values
      static java.lang.String getSimpleName​(java.lang.Class<?> type)
      Gets the simple name of the given class.
      static java.lang.Class<?> getTopComponentType​(java.lang.Class<?> arrayType)
      Gets the top-level component type of the given array type.
      static java.lang.Class<?> getTopComponentType​(java.lang.Object array)
      Gets the top-level component type of the given array object.
      static java.lang.Class<?> getType​(java.lang.Object value)
      static java.lang.String getTypeName​(java.lang.Class<?> type)
      Gets the type name of the given class.
      static java.lang.String getTypeName​(java.lang.Object value)
      Gets the type name of the given object.
      static java.lang.Class[] getTypes​(java.lang.Object... values)
      static boolean isAbstractClass​(java.lang.Class<?> type)
      Checks if the specified type is an abstract class.
      static boolean isArray​(java.lang.Class<?> type)
      Checks if the specified type is an array.
      static boolean isArray​(java.lang.Object object)
      Checks if the specified object is an array.
      static boolean isAssignableFrom​(java.lang.Class<?> superType, java.lang.Class<?> targetType)
      Checks if the superType is assignable from the targetType.
      static boolean isCharSequence​(java.lang.Class<?> type)
      Checks if the given type is a subtype of CharSequence.
      static boolean isCharSequence​(java.lang.Object value)
      Checks if the given object is an instance of CharSequence.
      static boolean isClass​(java.lang.Object object)
      Checks if the given object is an instance of Class.
      static boolean isConcreteClass​(java.lang.Class<?> type)
      Checks if the specified type is a concrete class.
      static boolean isDerived​(java.lang.Class<?> targetType, java.lang.Class<?>... superTypes)
      Checks if the targetType is derived from any of the specified superTypes.
      static boolean isEnum​(java.lang.Class<?> type)
      Checks if the specified class is an enum.
      static boolean isEnum​(java.lang.Object object)
      Checks if the given object is an instance of Enum.
      static boolean isFinal​(java.lang.Class<?> type)
      Checks if the specified class is a final class.
      static boolean isGeneralClass​(java.lang.Class<?> type)
      Checks if the specified type is a general class.
      protected static boolean isGeneralClass​(java.lang.Class<?> type, java.lang.Boolean isAbstract)
      Is the specified type a general class or not?
      static boolean isNumber​(java.lang.Class<?> type)
      Checks if the given type is a subtype of Number.
      static boolean isNumber​(java.lang.Object value)
      Checks if the given object is an instance of Number.
      static boolean isPrimitive​(java.lang.Class<?> type)
      Checks if the specified class is a primitive type.
      static boolean isSimpleType​(java.lang.Class<?> type)
      Checks if the specified type is a simple type.
      static boolean isSimpleType​(java.lang.Object object)
      Checks if the specified object is a simple type.
      static boolean isTopLevelClass​(java.lang.Class<?> type)
      Checks if the specified class is a top-level class.
      static boolean isWrapperType​(java.lang.Class<?> type)
      Checks if the specified class is a wrapper type.
      static boolean isWrapperType​(java.lang.Object value)
      Checks if the specified object is a wrapper type.
      static <T> T newInstance​(java.lang.Class<T> type, java.lang.Object... args)
      Creates a new instance of the specified class by finding a suitable constructor that matches the provided arguments and invoking it.
      protected static java.lang.String resolveClassName​(java.io.File classesDirectory, java.io.File classFile)
      Resolves the fully qualified class name from a class file within a classes directory.
      static java.lang.String resolveClassName​(java.lang.String resourceName)
      Resolves the fully qualified class name from a resource name.
      static java.lang.String resolvePackageName​(java.lang.Class<?> targetClass)
      Resolve the package name of the given class.
      static java.lang.String resolvePackageName​(java.lang.String className)
      Resolve the package name from the given class name.
      static java.lang.Class<?> resolvePrimitiveClassForName​(java.lang.String name)
      Resolve the given class name as a primitive class, if appropriate, according to the JVM's naming rules for primitive classes.
      static java.lang.Class<?> resolvePrimitiveType​(java.lang.Class<?> type)
      Resolves the primitive type corresponding to the given type.
      static java.lang.Class<?> resolveWrapperType​(java.lang.Class<?> primitiveType)
      Resolve the wrapper class from the primitive type.
      static java.lang.Class<?> tryResolveWrapperType​(java.lang.Class<?> primitiveType)
      Attempts to resolve the wrapper type for the given primitive type.
      protected static java.lang.Class<?> tryResolveWrapperType​(java.lang.Class<?> type, java.lang.Class<?> defaultType)
      Attempts to resolve the wrapper type for the given primitive type.
      • Methods inherited from class java.lang.Object

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

      • PRIMITIVE_TYPES

        @Nonnull
        @Immutable
        public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_TYPES
      • WRAPPER_TYPES

        @Nonnull
        @Immutable
        public static final java.util.Set<java.lang.Class<?>> WRAPPER_TYPES
      • PRIMITIVE_ARRAY_TYPES

        @Nonnull
        @Immutable
        public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_ARRAY_TYPES
      • SIMPLE_TYPES

        @Immutable
        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
    • Method Detail

      • isArray

        public static boolean isArray​(java.lang.Object object)
        Checks if the specified object is an array.

        Example Usage

        
         boolean result1 = ClassUtils.isArray(new int[]{1, 2, 3});  // returns true
         boolean result2 = ClassUtils.isArray("Hello");             // returns false
         boolean result3 = ClassUtils.isArray(null);                // returns false
         
        Parameters:
        object - the object to check, may be null
        Returns:
        true if the specified object is an array, false otherwise
        See Also:
        isArray(Class), Class.isArray()
      • isArray

        public static boolean isArray​(@Nullable
                                      java.lang.Class<?> type)
        Checks if the specified type is an array.

        Example Usage

        
         boolean result1 = ClassUtils.isArray(int[].class);     // returns true
         boolean result2 = ClassUtils.isArray(String.class);    // returns false
         boolean result3 = ClassUtils.isArray(null);           // returns false
         
        Parameters:
        type - the type to check, may be null
        Returns:
        true if the specified type is an array class, false otherwise
        See Also:
        Class.isArray()
      • isConcreteClass

        public static boolean isConcreteClass​(@Nullable
                                              java.lang.Class<?> type)
        Checks if the specified type is a concrete class.

        A concrete class is a class that:

        • Is not an interface
        • Is not an annotation
        • Is not an enum
        • Is not a synthetic class
        • Is not a primitive type
        • Is not an array
        • Is not abstract

        Example Usage

        
         boolean result1 = ClassUtils.isConcreteClass(String.class);  // returns true
         boolean result2 = ClassUtils.isConcreteClass(List.class);    // returns false (interface)
         boolean result3 = ClassUtils.isConcreteClass(null);         // returns false
         
        Parameters:
        type - the type to check, may be null
        Returns:
        true if the specified type is a concrete class, false otherwise
        See Also:
        isGeneralClass(Class, Boolean)
      • isAbstractClass

        public static boolean isAbstractClass​(@Nullable
                                              java.lang.Class<?> type)
        Checks if the specified type is an abstract class.

        An abstract class is a class that:

        • Is not an interface
        • Is not an annotation
        • Is not an enum
        • Is not a synthetic class
        • Is not a primitive type
        • Is not an array
        • Is abstract

        Example Usage

        
         abstract class AbstractExample { }
         class ConcreteExample { }
        
         boolean result1 = ClassUtils.isAbstractClass(AbstractExample.class); // returns true
         boolean result2 = ClassUtils.isAbstractClass(ConcreteExample.class); // returns false
         boolean result3 = ClassUtils.isAbstractClass(null);                  // returns false
         
        Parameters:
        type - the type to check, may be null
        Returns:
        true if the specified type is an abstract class, false otherwise
        See Also:
        isGeneralClass(Class, Boolean)
      • isGeneralClass

        public static boolean isGeneralClass​(@Nullable
                                             java.lang.Class<?> type)
        Checks if the specified type is a general class.

        A general class is a class that:

        • Is not an interface
        • Is not an annotation
        • Is not an enum
        • Is not a synthetic class
        • Is not a primitive type
        • Is not an array
        • Is not abstract (by default)

        Example Usage

        
         class ExampleClass {}
         abstract class AbstractExample {}
         interface ExampleInterface {}
        
         boolean result1 = ClassUtils.isGeneralClass(ExampleClass.class);      // returns true
         boolean result2 = ClassUtils.isGeneralClass(AbstractExample.class);   // returns false (abstract)
         boolean result3 = ClassUtils.isGeneralClass(ExampleInterface.class);  // returns false (interface)
         boolean result4 = ClassUtils.isGeneralClass(null);                   // returns false
         
        Parameters:
        type - the type to check, may be null
        Returns:
        true if the specified type is a general class, false otherwise
        See Also:
        isGeneralClass(Class, Boolean)
      • isGeneralClass

        protected static boolean isGeneralClass​(@Nullable
                                                java.lang.Class<?> type,
                                                @Nullable
                                                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​(@Nullable
                                              java.lang.Class<?> type)
        Checks if the specified class is a top-level class.

        A top-level class is a class that is not a local class and not a member class.

        Example Usage

        
         class TopLevelClass {}
        
         class OuterClass {
             static class StaticNestedClass {}
             class InnerClass {}
        
             void method() {
                 class LocalClass {}
             }
         }
        
         boolean result1 = ClassUtils.isTopLevelClass(TopLevelClass.class);     // returns true
         boolean result2 = ClassUtils.isTopLevelClass(OuterClass.InnerClass.class); // returns false
         boolean result3 = ClassUtils.isTopLevelClass(OuterClass.StaticNestedClass.class); // returns true
         boolean result4 = ClassUtils.isTopLevelClass(null);                    // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the specified class is a top-level class, false otherwise
        See Also:
        Class.isLocalClass(), Class.isMemberClass()
      • isPrimitive

        public static boolean isPrimitive​(@Nullable
                                          java.lang.Class<?> type)
        Checks if the specified class is a primitive type.

        This method returns true if the given class is one of the eight primitive types (boolean, byte, char, short, int, long, float, double) or their corresponding array types. It returns false otherwise, including for wrapper classes.

        Example Usage

        
         boolean result1 = ClassUtils.isPrimitive(int.class);        // returns true
         boolean result2 = ClassUtils.isPrimitive(Integer.class);    // returns false (wrapper class)
         boolean result3 = ClassUtils.isPrimitive(int[].class);      // returns true (primitive array)
         boolean result4 = ClassUtils.isPrimitive(String.class);     // returns false
         boolean result5 = ClassUtils.isPrimitive(null);            // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the specified class is a primitive type or primitive array type, false otherwise
        See Also:
        PRIMITIVE_TYPES, Class.isPrimitive()
      • isFinal

        public static boolean isFinal​(@Nullable
                                      java.lang.Class<?> type)
        Checks if the specified class is a final class.

        A final class is a class that cannot be extended by other classes. This method returns true if the given class is marked with the final modifier, and false otherwise. It also returns false if the class is null.

        Example Usage

        
         final class FinalExample {}
         class NonFinalExample {}
        
         boolean result1 = ClassUtils.isFinal(FinalExample.class);   // returns true
         boolean result2 = ClassUtils.isFinal(NonFinalExample.class); // returns false
         boolean result3 = ClassUtils.isFinal(null);                 // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the specified class is a final class, false otherwise
        See Also:
        Modifier.isFinal(int)
      • isSimpleType

        public static boolean isSimpleType​(@Nullable
                                           java.lang.Object object)
        Checks if the specified object is a simple type.

        A simple type is defined as one of the following:

        • Void
        • Boolean
        • Character
        • Byte
        • Short
        • Integer
        • Long
        • Float
        • Double
        • String
        • BigDecimal
        • BigInteger
        • Date
        • Object

        Example Usage

        
         boolean result1 = ClassUtils.isSimpleType("Hello");  // returns true
         boolean result2 = ClassUtils.isSimpleType(123);      // returns true
         boolean result3 = ClassUtils.isSimpleType(new ArrayList<>()); // returns false
         boolean result4 = ClassUtils.isSimpleType(null);     // returns false
         
        Parameters:
        object - the object to check, may be null
        Returns:
        true if the specified object is a simple type, false otherwise
        See Also:
        isSimpleType(Class)
      • isSimpleType

        public static boolean isSimpleType​(@Nullable
                                           java.lang.Class<?> type)
        Checks if the specified type is a simple type.

        A simple type is defined as one of the following:

        • Void
        • Boolean
        • Character
        • Byte
        • Short
        • Integer
        • Long
        • Float
        • Double
        • String
        • BigDecimal
        • BigInteger
        • Date
        • Object

        Example Usage

        
         boolean result1 = ClassUtils.isSimpleType(String.class);  // returns true
         boolean result2 = ClassUtils.isSimpleType(List.class);    // returns false
         boolean result3 = ClassUtils.isSimpleType(null);         // returns false
         
        Parameters:
        type - the type to check, may be null
        Returns:
        true if the specified type is a simple type, false otherwise
        See Also:
        SIMPLE_TYPES
      • isCharSequence

        public static boolean isCharSequence​(@Nullable
                                             java.lang.Object value)
        Checks if the given object is an instance of CharSequence.

        Example Usage

        
         boolean result1 = ClassUtils.isCharSequence("Hello");     // returns true
         boolean result2 = ClassUtils.isCharSequence(new StringBuilder("World")); // returns true
         boolean result3 = ClassUtils.isCharSequence(123);         // returns false
         boolean result4 = ClassUtils.isCharSequence(null);        // returns false
         
        Parameters:
        value - the object to check, may be null
        Returns:
        true if the object is an instance of CharSequence, false otherwise
        See Also:
        isCharSequence(Class)
      • isCharSequence

        public static boolean isCharSequence​(@Nullable
                                             java.lang.Class<?> type)
        Checks if the given type is a subtype of CharSequence.

        Example Usage

        
             boolean result = ClassUtils.isCharSequence(String.class); // returns true
             boolean result2 = ClassUtils.isCharSequence(Integer.class); // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the type is a subtype of CharSequence, false otherwise
      • isNumber

        public static boolean isNumber​(@Nullable
                                       java.lang.Object value)
        Checks if the given object is an instance of Number.

        Example Usage

        
         boolean result1 = ClassUtils.isNumber(Integer.valueOf(10));  // returns true
         boolean result2 = ClassUtils.isNumber(10.5);                 // returns true
         boolean result3 = ClassUtils.isNumber("123");                // returns false
         boolean result4 = ClassUtils.isNumber(null);                 // returns false
         
        Parameters:
        value - the object to check, may be null
        Returns:
        true if the object is an instance of Number, false otherwise
        See Also:
        isNumber(Class)
      • isNumber

        public static boolean isNumber​(@Nullable
                                       java.lang.Class<?> type)
        Checks if the given type is a subtype of Number.

        Example Usage

        
             boolean result = ClassUtils.isNumber(Integer.class); // returns true
             boolean result2 = ClassUtils.isNumber(String.class); // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the type is a subtype of Number, false otherwise
      • isClass

        public static boolean isClass​(@Nullable
                                      java.lang.Object object)
        Checks if the given object is an instance of Class.

        Example Usage

        
         boolean result1 = ClassUtils.isClass(String.class); // returns true
         boolean result2 = ClassUtils.isClass("Hello");      // returns false
         boolean result3 = ClassUtils.isClass(null);         // returns false
         
        Parameters:
        object - the object to check, may be null
        Returns:
        true if the object is an instance of Class, false otherwise
      • isEnum

        public static boolean isEnum​(@Nullable
                                     java.lang.Object object)
        Checks if the given object is an instance of Enum.

        Example Usage

        
         enum Color { RED, GREEN, BLUE }
        
         boolean result1 = ClassUtils.isEnum(Color.RED);  // returns true
         boolean result2 = ClassUtils.isEnum("RED");      // returns false
         boolean result3 = ClassUtils.isEnum(null);       // returns false
         
        Parameters:
        object - the object to check, may be null
        Returns:
        true if the object is an instance of Enum, false otherwise
        See Also:
        isEnum(Class)
      • isEnum

        public static boolean isEnum​(@Nullable
                                     java.lang.Class<?> type)
        Checks if the specified class is an enum.

        This method returns true if the given class is an enum type, and false otherwise. It also returns false if the class is null.

        Example Usage

        
         enum Color { RED, GREEN, BLUE }
        
         boolean result1 = ClassUtils.isEnum(Color.class);  // returns true
         boolean result2 = ClassUtils.isEnum(String.class); // returns false
         boolean result3 = ClassUtils.isEnum(null);         // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the specified class is an enum, false otherwise
        See Also:
        Class.isEnum()
      • resolvePrimitiveType

        @Nullable
        public static java.lang.Class<?> resolvePrimitiveType​(@Nullable
                                                              java.lang.Class<?> type)
        Resolves the primitive type corresponding to the given type.

        If the provided type is already a primitive type, it is returned as-is. If the provided type is a wrapper type (e.g., Integer, Boolean), the corresponding primitive type (e.g., int, boolean) is returned. If the provided type is neither a primitive nor a wrapper type, null is returned.

        Example Usage

        
         Class<?> result1 = ClassUtils.resolvePrimitiveType(int.class);      // returns int.class
         Class<?> result2 = ClassUtils.resolvePrimitiveType(Integer.class);  // returns int.class
         Class<?> result3 = ClassUtils.resolvePrimitiveType(String.class);   // returns null
         Class<?> result4 = ClassUtils.resolvePrimitiveType(null);          // returns null
         
        Parameters:
        type - the type to resolve, may be null
        Returns:
        the corresponding primitive type if the input is a wrapper or primitive type, otherwise null
        See Also:
        isPrimitive(Class), WRAPPER_TO_PRIMITIVE_TYPES_MAP
      • resolveWrapperType

        @Nullable
        public static java.lang.Class<?> resolveWrapperType​(@Nullable
                                                            java.lang.Class<?> primitiveType)
        Resolve the wrapper class from the primitive type.

        If the provided type is already a wrapper type, it is returned as-is. If the provided type is a primitive type (e.g., int, boolean), the corresponding wrapper type (e.g., Integer, Boolean) is returned. If the provided type is neither a primitive nor a wrapper type, null is returned.

        Example Usage

        
         Class<?> result1 = ClassUtils.resolveWrapperType(int.class);      // returns Integer.class
         Class<?> result2 = ClassUtils.resolveWrapperType(Integer.class);  // returns Integer.class
         Class<?> result3 = ClassUtils.resolveWrapperType(String.class);   // returns null
         Class<?> result4 = ClassUtils.resolveWrapperType(null);          // returns null
         
        Parameters:
        primitiveType - the primitive type to resolve, may be null
        Returns:
        the corresponding wrapper type if the input is a primitive or wrapper type, otherwise null
        See Also:
        isWrapperType(Class), PRIMITIVE_TO_WRAPPER_TYPES_MAP
      • tryResolveWrapperType

        public static java.lang.Class<?> tryResolveWrapperType​(@Nullable
                                                               java.lang.Class<?> primitiveType)
        Attempts to resolve the wrapper type for the given primitive type.

        If the provided type is already a wrapper type, it is returned as-is. If the provided type is a primitive type (e.g., int, boolean), the corresponding wrapper type (e.g., Integer, Boolean) is returned. If the provided type is neither a primitive nor a wrapper type, the primitive type itself is returned.

        Example Usage

        
         Class<?> result1 = ClassUtils.tryResolveWrapperType(int.class);      // returns Integer.class
         Class<?> result2 = ClassUtils.tryResolveWrapperType(Integer.class);  // returns Integer.class
         Class<?> result3 = ClassUtils.tryResolveWrapperType(String.class);   // returns String.class
         Class<?> result4 = ClassUtils.tryResolveWrapperType(null);          // returns null
         
        Parameters:
        primitiveType - the primitive type to resolve, may be null
        Returns:
        the corresponding wrapper type if the input is a primitive type, the input type itself if it is a wrapper type, or the input type itself if it is neither
        See Also:
        isWrapperType(Class), PRIMITIVE_TO_WRAPPER_TYPES_MAP
      • tryResolveWrapperType

        protected static java.lang.Class<?> tryResolveWrapperType​(@Nullable
                                                                  java.lang.Class<?> type,
                                                                  @Nullable
                                                                  java.lang.Class<?> defaultType)
        Attempts to resolve the wrapper type for the given primitive type.

        If the provided type is already a wrapper type, it is returned as-is. If the provided type is a primitive type (e.g., int, boolean), the corresponding wrapper type (e.g., Integer, Boolean) is returned. If the provided type is neither a primitive nor a wrapper type, the specified default type is returned.

        Example Usage

        
         Class<?> result1 = ClassUtils.tryResolveWrapperType(int.class, Object.class);      // returns Integer.class
         Class<?> result2 = ClassUtils.tryResolveWrapperType(Integer.class, Object.class);  // returns Integer.class
         Class<?> result3 = ClassUtils.tryResolveWrapperType(String.class, Object.class);   // returns Object.class
         Class<?> result4 = ClassUtils.tryResolveWrapperType(null, Object.class);          // returns Object.class
         
        Parameters:
        type - the type to resolve, may be null
        defaultType - the default type to return if the input type is neither primitive nor wrapper type
        Returns:
        the corresponding wrapper type if the input is a primitive type, the input type itself if it is a wrapper type, or the specified default type if it is neither
        See Also:
        isWrapperType(Class), PRIMITIVE_TO_WRAPPER_TYPES_MAP
      • isWrapperType

        public static boolean isWrapperType​(@Nullable
                                            java.lang.Object value)
        Checks if the specified object is a wrapper type.

        A wrapper type is one of the following:

        • Boolean
        • Character
        • Byte
        • Short
        • Integer
        • Long
        • Float
        • Double

        Example Usage

        
         boolean result1 = ClassUtils.isWrapperType(Integer.valueOf(10));  // returns true
         boolean result2 = ClassUtils.isWrapperType(10);                   // returns false (primitive type)
         boolean result3 = ClassUtils.isWrapperType("Hello");              // returns false
         boolean result4 = ClassUtils.isWrapperType(null);                 // returns false
         
        Parameters:
        value - the object to check, may be null
        Returns:
        true if the specified object is a wrapper type, false otherwise
        See Also:
        isWrapperType(Class)
      • isWrapperType

        public static boolean isWrapperType​(@Nullable
                                            java.lang.Class<?> type)
        Checks if the specified class is a wrapper type.

        A wrapper type is one of the following:

        • Boolean
        • Character
        • Byte
        • Short
        • Integer
        • Long
        • Float
        • Double

        Example Usage

        
         boolean result1 = ClassUtils.isWrapperType(Integer.class);  // returns true
         boolean result2 = ClassUtils.isWrapperType(int.class);      // returns false (primitive type)
         boolean result3 = ClassUtils.isWrapperType(String.class);   // returns false
         boolean result4 = ClassUtils.isWrapperType(null);          // returns false
         
        Parameters:
        type - the class to check, may be null
        Returns:
        true if the specified class is a wrapper type, false otherwise
        See Also:
        WRAPPER_TYPES
      • arrayTypeEquals

        public static boolean arrayTypeEquals​(@Nullable
                                              java.lang.Class<?> oneArrayType,
                                              @Nullable
                                              java.lang.Class<?> anotherArrayType)
        Checks if two array types are equivalent, including nested arrays.

        This method compares the component types of two array classes recursively. It returns true if both classes are arrays and their component types are equal (considering nested arrays), false otherwise.

        Example Usage

        
         boolean result1 = ClassUtils.arrayTypeEquals(int[].class, int[].class);           // returns true
         boolean result2 = ClassUtils.arrayTypeEquals(int[][].class, int[][].class);       // returns true
         boolean result3 = ClassUtils.arrayTypeEquals(int[].class, long[].class);          // returns false
         boolean result4 = ClassUtils.arrayTypeEquals(int[].class, int.class);             // returns false
         boolean result5 = ClassUtils.arrayTypeEquals(null, int[].class);                  // returns false
         
        Parameters:
        oneArrayType - the first array type to compare, may be null
        anotherArrayType - the second array type to compare, may be null
        Returns:
        true if both types are arrays with equivalent component types, false otherwise
        See Also:
        Class.getComponentType(), Objects.equals(Object, Object)
      • resolvePrimitiveClassForName

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

        This method checks if the provided name corresponds to a primitive type (e.g., "int", "boolean") or a primitive array type (e.g., "[I", "[Z"). It returns the corresponding Class object if a match is found, or null otherwise.

        Note: This method does not support the "[]" suffix notation for primitive arrays; this is only supported by #forName(String).

        Example Usage

        
         Class<?> intClass = ClassUtils.resolvePrimitiveClassForName("int");     // returns int.class
         Class<?> boolArrayClass = ClassUtils.resolvePrimitiveClassForName("[Z"); // returns boolean[].class
         Class<?> stringClass = ClassUtils.resolvePrimitiveClassForName("String"); // returns null
         Class<?> nullResult = ClassUtils.resolvePrimitiveClassForName(null);    // returns null
         
        Parameters:
        name - the name of the potentially primitive class, may be null
        Returns:
        the primitive class, or null if the name does not denote a primitive class or primitive array class
        See Also:
        NAME_TO_TYPE_PRIMITIVE_MAP
      • resolvePackageName

        @Nullable
        public static java.lang.String resolvePackageName​(@Nullable
                                                          java.lang.Class<?> targetClass)
        Resolve the package name of the given class.

        This method returns the package name of the specified class. If the class is a primitive type, or if the class is null, this method returns null.

        Example Usage

        
         String packageName1 = ClassUtils.resolvePackageName(String.class);
         // returns "java.lang"
        
         String packageName2 = ClassUtils.resolvePackageName(int.class);
         // returns null (primitive type)
        
         String packageName3 = ClassUtils.resolvePackageName((Class<?>) null);
         // returns null
         
        Parameters:
        targetClass - the class to resolve the package name for, may be null
        Returns:
        the package name of the given class, or null if the class is primitive or null
        See Also:
        resolvePackageName(String), isPrimitive(Class), getTypeName(Class)
      • resolvePackageName

        @Nonnull
        public static java.lang.String resolvePackageName​(@Nullable
                                                          java.lang.String className)
        Resolve the package name from the given class name.

        This method extracts the package name from a fully qualified class name. If the class name does not contain a package (i.e., it's a default package), this method returns an empty string. If the input is null, this method returns null.

        Example Usage

        
         String packageName1 = ClassUtils.resolvePackageName("java.lang.String");
         // returns "java.lang"
        
         String packageName2 = ClassUtils.resolvePackageName("String");
         // returns "" (empty string for default package)
        
         String packageName3 = ClassUtils.resolvePackageName(null);
         // returns null
         
        Parameters:
        className - the fully qualified class name, may be null
        Returns:
        the package name, or an empty string if the class is in the default package, or null if the input is null
        See Also:
        StringUtils.substringBeforeLast(String, String)
      • findClassNamesInClassPath

        @Nonnull
        @Immutable
        public static java.util.Set<java.lang.String> findClassNamesInClassPath​(@Nullable
                                                                                java.lang.String classPath,
                                                                                boolean recursive)
        Finds all class names in the specified class path.

        This method scans the given class path for class files and returns their fully qualified names. The class path can be a directory or a JAR file. If the class path is a directory, the method will recursively scan subdirectories if the recursive parameter is set to true.

        Example Usage

        
         // Find all class names in a directory, recursively
         Set<String> classNames = ClassUtils.findClassNamesInClassPath("/path/to/classes", true);
        
         // Find all class names in a JAR file
         Set<String> jarClassNames = ClassUtils.findClassNamesInClassPath("file:/path/to/library.jar", false);
         
        Parameters:
        classPath - the class path to scan, may be null
        recursive - whether to scan subdirectories recursively
        Returns:
        a set of fully qualified class names found in the class path, or an empty set if none found
        See Also:
        findClassNamesInClassPath(File, boolean)
      • findClassNamesInClassPath

        @Nonnull
        @Immutable
        public static java.util.Set<java.lang.String> findClassNamesInClassPath​(@Nullable
                                                                                java.io.File classPath,
                                                                                boolean recursive)
        Finds all class names in the specified class path.

        This method scans the given class path for class files and returns their fully qualified names. The class path can be a directory or a JAR file. If the class path is a directory, the method will recursively scan subdirectories if the recursive parameter is set to true.

        Example Usage

        
         // Find all class names in a directory, recursively
         Set<String> classNames = ClassUtils.findClassNamesInClassPath(new File("/path/to/classes"), true);
        
         // Find all class names in a JAR file
         Set<String> jarClassNames = ClassUtils.findClassNamesInClassPath(new File("/path/to/library.jar"), false);
         
        Parameters:
        classPath - the class path to scan, may be null
        recursive - whether to scan subdirectories recursively
        Returns:
        a set of fully qualified class names found in the class path, or an empty set if none found
        See Also:
        findClassNamesInDirectory(File, boolean), findClassNamesInJarFile(File, boolean)
      • findClassNamesInDirectory

        @Nonnull
        @Immutable
        public static java.util.Set<java.lang.String> findClassNamesInDirectory​(@Nullable
                                                                                java.io.File classesDirectory,
                                                                                boolean recursive)
        Finds all class names in the specified directory.

        This method scans the given directory for class files and returns their fully qualified names. If the recursive parameter is set to true, the method will recursively scan subdirectories. Only files with the ".class" extension are considered.

        Example Usage

        
         // Find all class names in a directory, recursively
         Set<String> classNames = ClassUtils.findClassNamesInDirectory(new File("/path/to/classes"), true);
        
         // Find all class names in a directory, non-recursively
         Set<String> nonRecursiveClassNames = ClassUtils.findClassNamesInDirectory(new File("/path/to/classes"), false);
         
        Parameters:
        classesDirectory - the directory to scan for class files, may be null
        recursive - whether to scan subdirectories recursively
        Returns:
        a set of fully qualified class names found in the directory, or an empty set if none found
        See Also:
        SimpleFileScanner.scan(File, boolean, IOFileFilter), FileExtensionFilter.of(String)
      • findClassNamesInJarFile

        @Nonnull
        @Immutable
        public static java.util.Set<java.lang.String> findClassNamesInJarFile​(@Nullable
                                                                              java.io.File jarFile,
                                                                              boolean recursive)
        Finds all class names in the specified JAR file.

        This method scans the given JAR file for class files and returns their fully qualified names. If the recursive parameter is set to true, the method will recursively scan subdirectories within the JAR file. Only entries with the ".class" extension are considered.

        Example Usage

        
         // Find all class names in a JAR file, recursively
         Set<String> classNames = ClassUtils.findClassNamesInJarFile(new File("/path/to/library.jar"), true);
        
         // Find all class names in a JAR file, non-recursively
         Set<String> nonRecursiveClassNames = ClassUtils.findClassNamesInJarFile(new File("/path/to/library.jar"), false);
         
        Parameters:
        jarFile - the JAR file to scan for class files, may be null
        recursive - whether to scan subdirectories recursively
        Returns:
        a set of fully qualified class names found in the JAR file, or an empty set if none found
        See Also:
        ClassFileJarEntryFilter.INSTANCE, SimpleJarEntryScanner.INSTANCE
      • resolveClassName

        protected static java.lang.String resolveClassName​(java.io.File classesDirectory,
                                                           java.io.File classFile)
        Resolves the fully qualified class name from a class file within a classes directory.

        This method calculates the relative path of the class file from the classes directory, then converts the path to a fully qualified class name by replacing path separators with dots and removing the ".class" extension.

        Example Usage

        
         File classesDir = new File("/project/target/classes");
         File classFile = new File("/project/target/classes/com/example/MyClass.class");
         String className = ClassUtils.resolveClassName(classesDir, classFile);
         // Returns: "com.example.MyClass"
         
        Parameters:
        classesDirectory - the root directory containing compiled classes, must not be null
        classFile - the class file to resolve the name for, must not be null
        Returns:
        the fully qualified class name, never null
        See Also:
        #resolveRelativePath(File, File), resolveClassName(String)
      • resolveClassName

        @Nullable
        public static java.lang.String resolveClassName​(@Nullable
                                                        java.lang.String resourceName)
        Resolves the fully qualified class name from a resource name.

        This method converts a resource name (using '/' as separator) to a fully qualified class name (using '.' as separator) by replacing slashes with dots and removing the ".class" extension if present. It also handles cases where the resource name might start with leading dots.

        Example Usage

        
         String className1 = ClassUtils.resolveClassName("com/example/MyClass.class");
         // Returns: "com.example.MyClass"
        
         String className2 = ClassUtils.resolveClassName("com/example/MyClass");
         // Returns: "com.example.MyClass"
        
         String className3 = ClassUtils.resolveClassName("./com/example/MyClass.class");
         // Returns: "com.example.MyClass"
        
         String className4 = ClassUtils.resolveClassName(null);
         // Returns: null
         
        Parameters:
        resourceName - the resource name to resolve, may be null
        Returns:
        the fully qualified class name, or null if the input is null
        See Also:
        resolveClassName(File, File)
      • getAllSuperClasses

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> getAllSuperClasses​(@Nullable
                                                                            java.lang.Class<?> type)
        Get all super classes from the specified type.

        Example Usage

        
         List<Class<?>> superClasses = ClassUtils.getAllSuperClasses(String.class);
         // Returns a list containing: [Object.class]
        
         List<Class<?>> noSuperClasses = ClassUtils.getAllSuperClasses(Object.class);
         // Returns an empty list since Object has no superclass
        
         List<Class<?>> nullResult = ClassUtils.getAllSuperClasses(null);
         // Returns an empty list since the input is null
         
        Parameters:
        type - the specified type, may be null
        Returns:
        non-null read-only List of super classes
        See Also:
        findAllSuperClasses(Class, Predicate[])
      • getAllInterfaces

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> getAllInterfaces​(@Nullable
                                                                          java.lang.Class<?> type)
        Get all interfaces from the specified type.

        This method returns a list of all interfaces implemented by the given class or interface, including interfaces inherited from superclasses and superinterfaces. The returned list is read-only and immutable.

        Example Usage

        
         // For a class implementing multiple interfaces
         class Example implements Runnable, Cloneable {}
         List<Class<?>> interfaces = ClassUtils.getAllInterfaces(Example.class);
         // Returns a list containing: [Runnable.class, Cloneable.class]
        
         // For an interface extending other interfaces
         interface SubInterface extends Serializable, Comparable<String> {}
         List<Class<?>> superInterfaces = ClassUtils.getAllInterfaces(SubInterface.class);
         // Returns a list containing: [Serializable.class, Comparable.class]
        
         // For a class with no interfaces
         class PlainClass {}
         List<Class<?>> noInterfaces = ClassUtils.getAllInterfaces(PlainClass.class);
         // Returns an empty list
        
         // For null input
         List<Class<?>> nullResult = ClassUtils.getAllInterfaces(null);
         // Returns an empty list
         
        Parameters:
        type - the specified type, may be null
        Returns:
        non-null read-only List of interfaces
        See Also:
        findAllInterfaces(Class, Predicate[])
      • getAllInheritedTypes

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> getAllInheritedTypes​(@Nullable
                                                                              java.lang.Class<?> type)
        Get all inherited types from the specified type.

        This method returns a list of all types inherited by the given class or interface, including superclasses and interfaces. The returned list is read-only and immutable.

        Example Usage

        
         class Parent {}
         interface MyInterface {}
         class Child extends Parent implements MyInterface {}
        
         List<Class<?>> inheritedTypes = ClassUtils.getAllInheritedTypes(Child.class);
         // Returns a list containing Parent.class and MyInterface.class
        
         List<Class<?>> noInheritedTypes = ClassUtils.getAllInheritedTypes(Object.class);
         // Returns an empty list since Object has no superclass or interfaces in this context
        
         List<Class<?>> nullResult = ClassUtils.getAllInheritedTypes(null);
         // Returns an empty list since the input is null
         
        Parameters:
        type - the specified type, may be null
        Returns:
        non-null read-only List of inherited types
        See Also:
        getAllInheritedClasses(Class)
      • getAllInheritedClasses

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> getAllInheritedClasses​(@Nullable
                                                                                java.lang.Class<?> type)
        Get all inherited classes from the specified type.

        This method returns a list of all classes inherited by the given class, including its superclasses. It does not include interfaces. The returned list is read-only and immutable.

        Example Usage

        
         class Parent {}
         class Child extends Parent {}
        
         List<Class<?>> inheritedClasses = ClassUtils.getAllInheritedClasses(Child.class);
         // Returns a list containing Parent.class
        
         List<Class<?>> noInheritedClasses = ClassUtils.getAllInheritedClasses(Object.class);
         // Returns an empty list since Object has no superclass
        
         List<Class<?>> nullResult = ClassUtils.getAllInheritedClasses(null);
         // Returns an empty list since the input is null
         
        Parameters:
        type - the specified type, may be null
        Returns:
        non-null read-only List of inherited classes
        See Also:
        findAllInheritedClasses(Class, Predicate[])
      • getAllClasses

        public static java.util.List<java.lang.Class<?>> getAllClasses​(@Nullable
                                                                       java.lang.Class<?> type)
        Get all classes from the specified type, including the type itself, its superclasses, and interfaces.

        This method returns a list of all classes related to the given class or interface, including the class itself, its superclasses, and interfaces. The returned list is read-only and immutable.

        Example Usage

        
         class Parent {}
         interface MyInterface {}
         class Child extends Parent implements MyInterface {}
        
         List<Class<?>> allClasses = ClassUtils.getAllClasses(Child.class);
         // Returns a list containing Child.class, Parent.class, MyInterface.class, and Object.class
        
         List<Class<?>> objectClasses = ClassUtils.getAllClasses(Object.class);
         // Returns a list containing Object.class
        
         List<Class<?>> nullResult = ClassUtils.getAllClasses(null);
         // Returns an empty list since the input is null
         
        Parameters:
        type - the specified type, may be null
        Returns:
        non-null read-only List of all classes related to the specified type
        See Also:
        findAllClasses(Class, Predicate[])
      • findAllSuperClasses

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> findAllSuperClasses​(@Nullable
                                                                             java.lang.Class<?> type,
                                                                             @Nullable
                                                                             java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
        Find all super classes from the specified type, optionally filtering the results.

        This method traverses up the inheritance hierarchy of the given class, collecting all its superclasses. The results can be filtered using the provided Predicate filters. If no filters are provided, all superclasses are returned.

        Example Usage

        
         // Find all superclasses of String
         List<Class<?>> superClasses = ClassUtils.findAllSuperClasses(String.class);
         // Returns: [java.lang.Object]
        
         // Find superclasses with a filter
         List<Class<?>> filtered = ClassUtils.findAllSuperClasses(ArrayList.class, cls -> !cls.equals(Object.class));
         // Returns: [java.util.AbstractList, java.util.AbstractCollection]
        
         // Null input returns empty list
         List<Class<?>> nullResult = ClassUtils.findAllSuperClasses(null);
         // Returns: []
         
        Parameters:
        type - the specified type to find superclasses for, may be null
        classFilters - optional filters to apply to the results
        Returns:
        a non-null, read-only List of superclasses
        See Also:
        findTypes(Class, boolean, boolean, boolean, boolean, Predicate[])
      • findAllInterfaces

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> findAllInterfaces​(@Nullable
                                                                           java.lang.Class<?> type,
                                                                           @Nullable
                                                                           java.util.function.Predicate<? super java.lang.Class<?>>... interfaceFilters)
        Find all interfaces from the specified type, optionally filtering the results.

        This method traverses the inheritance hierarchy of the given class or interface, collecting all interfaces it implements or extends. The results can be filtered using the provided Predicate filters. If no filters are provided, all interfaces are returned.

        Example Usage

        
         // Find all interfaces implemented by ArrayList
         List<Class<?>> interfaces = ClassUtils.findAllInterfaces(ArrayList.class);
         // Returns: [java.util.List, java.util.Collection, java.lang.Iterable, java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable]
        
         // Find interfaces with a filter
         List<Class<?>> filtered = ClassUtils.findAllInterfaces(ArrayList.class, cls -> cls.getSimpleName().startsWith("C"));
         // Returns: [java.util.Collection, java.lang.Cloneable]
        
         // Null input returns empty list
         List<Class<?>> nullResult = ClassUtils.findAllInterfaces(null);
         // Returns: []
         
        Parameters:
        type - the specified type to find interfaces for, may be null
        interfaceFilters - optional filters to apply to the results
        Returns:
        a non-null, read-only List of interfaces
        See Also:
        findTypes(Class, boolean, boolean, boolean, boolean, Predicate[])
      • findAllInheritedClasses

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> findAllInheritedClasses​(@Nullable
                                                                                 java.lang.Class<?> type,
                                                                                 @Nullable
                                                                                 java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
        Find all inherited classes from the specified type, optionally filtering the results.

        This method traverses the inheritance hierarchy of the given class, collecting all its superclasses and interfaces. The results can be filtered using the provided Predicate filters. If no filters are provided, all inherited classes are returned.

        Example Usage

        
         // Find all inherited classes of ArrayList
         List<Class<?>> inherited = ClassUtils.findAllInheritedClasses(ArrayList.class);
         // Returns: [java.util.AbstractList, java.util.AbstractCollection, java.lang.Object,
         //           java.util.List, java.util.Collection, java.lang.Iterable,
         //           java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable]
        
         // Find inherited classes with a filter
         List<Class<?>> filtered = ClassUtils.findAllInheritedClasses(ArrayList.class,
                 cls -> cls.getSimpleName().startsWith("A") || cls.getSimpleName().endsWith("able"));
         // Returns: [java.util.AbstractList, java.util.AbstractCollection,
         //           java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable]
        
         // Null input returns empty list
         List<Class<?>> nullResult = ClassUtils.findAllInheritedClasses(null);
         // Returns: []
         
        Parameters:
        type - the specified type to find inherited classes for, may be null
        classFilters - optional filters to apply to the results
        Returns:
        a non-null, read-only List of inherited classes
        See Also:
        findTypes(Class, boolean, boolean, boolean, boolean, Predicate[])
      • findAllClasses

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.Class<?>> findAllClasses​(@Nullable
                                                                        java.lang.Class<?> type,
                                                                        @Nullable
                                                                        java.util.function.Predicate<? super java.lang.Class<?>>... classFilters)
        Find all classes from the specified type, optionally filtering the results.

        This method collects all classes related to the given class or interface, including the class itself, its superclasses, and interfaces. The results can be filtered using the provided Predicate filters. If no filters are provided, all related classes are returned.

        Example Usage

        
         // Find all classes related to ArrayList
         List<Class<?>> allClasses = ClassUtils.findAllClasses(ArrayList.class);
         // Returns: [java.util.ArrayList, java.util.AbstractList, java.util.AbstractCollection,
         //           java.lang.Object, java.util.List, java.util.Collection, java.lang.Iterable,
         //           java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable]
        
         // Find classes with a filter
         List<Class<?>> filtered = ClassUtils.findAllClasses(ArrayList.class,
                 cls -> cls.getSimpleName().startsWith("A") || cls.getSimpleName().endsWith("able"));
         // Returns: [java.util.ArrayList, java.util.AbstractList, java.util.AbstractCollection,
         //           java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable]
        
         // Null input returns empty list
         List<Class<?>> nullResult = ClassUtils.findAllClasses(null);
         // Returns: []
         
        Parameters:
        type - the specified type to find classes for, may be null
        classFilters - optional filters to apply to the results
        Returns:
        a non-null, read-only List of all classes related to the specified type
        See Also:
        findTypes(Class, boolean, boolean, boolean, boolean, Predicate[])
      • findTypes

        @Nonnull
        @Immutable
        protected static java.util.List<java.lang.Class<?>> findTypes​(@Nullable
                                                                      java.lang.Class<?> type,
                                                                      boolean includeSelf,
                                                                      boolean includeHierarchicalTypes,
                                                                      boolean includeGenericSuperclass,
                                                                      boolean includeGenericInterfaces,
                                                                      @Nullable
                                                                      java.util.function.Predicate<? super java.lang.Class<?>>... typeFilters)
        Finds types based on the specified criteria.

        This method is a core utility for traversing class hierarchies and collecting types according to various flags. It can include the original type, hierarchical types (superclasses), generic superclass, and/or interfaces. The results can be filtered using the provided Predicate filters.

        Example Usage

        
         // Find all superclasses of ArrayList
         List<Class<?>> superClasses = ClassUtils.findTypes(ArrayList.class, false, true, true, false);
         // Returns: [java.util.AbstractList, java.util.AbstractCollection, java.lang.Object]
        
         // Find all interfaces implemented by ArrayList
         List<Class<?>> interfaces = ClassUtils.findTypes(ArrayList.class, false, true, false, true);
         // Returns: [java.util.List, java.util.Collection, java.lang.Iterable,
         //           java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable]
        
         // Find the type itself and all its superclasses
         List<Class<?>> selfAndSuper = ClassUtils.findTypes(String.class, true, true, true, false);
         // Returns: [java.lang.String, java.lang.Object]
        
         // Null input returns empty list
         List<Class<?>> nullResult = ClassUtils.findTypes(null, true, true, true, true);
         // Returns: []
         
        Parameters:
        type - the specified type to find related types for, may be null
        includeSelf - whether to include the type itself in the results
        includeHierarchicalTypes - whether to include superclasses in the results
        includeGenericSuperclass - whether to include the direct generic superclass in the results
        includeGenericInterfaces - whether to include interfaces in the results
        typeFilters - optional filters to apply to the results
        Returns:
        a non-null, read-only List of classes based on the specified criteria
        See Also:
        TypeFinder.classFinder(Class, boolean, boolean, boolean, boolean)
      • isAssignableFrom

        public static boolean isAssignableFrom​(@Nullable
                                               java.lang.Class<?> superType,
                                               @Nullable
                                               java.lang.Class<?> targetType)
        Checks if the superType is assignable from the targetType.

        This method is a null-safe variant of Class.isAssignableFrom(Class). It returns false if either of the arguments is null.

        Example Usage

        
         boolean result1 = ClassUtils.isAssignableFrom(List.class, ArrayList.class);  // returns true
         boolean result2 = ClassUtils.isAssignableFrom(String.class, Object.class);   // returns false
         boolean result3 = ClassUtils.isAssignableFrom(null, String.class);           // returns false
         boolean result4 = ClassUtils.isAssignableFrom(String.class, null);           // returns false
         
        Parameters:
        superType - the super type to check against, may be null
        targetType - the target type to check, may be null
        Returns:
        true if superType is assignable from targetType, false otherwise
        See Also:
        Class.isAssignableFrom(Class)
      • getClass

        @Nullable
        public static java.lang.Class<?> getClass​(@Nullable
                                                  java.lang.Object object)
        Gets the Class of the given object.

        This method returns the Class object that represents the runtime class of the specified object. If the object is null, this method returns null.

        Example Usage

        
         String str = "Hello";
         Class<?> clazz = ClassUtils.getClass(str);
         System.out.println(clazz); // prints: class java.lang.String
        
         Object obj = null;
         Class<?> nullClazz = ClassUtils.getClass(obj);
         System.out.println(nullClazz); // prints: null
         
        Parameters:
        object - the object to get the class from, may be null
        Returns:
        the Class of the given object, or null if the object is null
        See Also:
        Object.getClass()
      • getClasses

        @Nonnull
        public static java.lang.Class<?>[] getClasses​(@Nullable
                                                      java.lang.Object... values)
        Get the types of the specified values
        Parameters:
        values - the values
        Returns:
        If can't be resolved, return empty class array
      • getTypeName

        @Nullable
        public static java.lang.String getTypeName​(@Nullable
                                                   java.lang.Object value)
        Gets the type name of the given object.

        This method returns the fully qualified name of the class of the specified object. If the object is null, this method returns null.

        Example Usage

        
         String str = "Hello";
         String typeName = ClassUtils.getTypeName(str);
         System.out.println(typeName); // prints: java.lang.String
        
         Object obj = null;
         String nullTypeName = ClassUtils.getTypeName(obj);
         System.out.println(nullTypeName); // prints: null
         
        Parameters:
        value - the object to get the type name from, may be null
        Returns:
        the fully qualified name of the class of the given object, or null if the object is null
        See Also:
        getTypeName(Class)
      • getTypeName

        @Nullable
        public static java.lang.String getTypeName​(@Nullable
                                                   java.lang.Class<?> type)
        Gets the type name of the given class.

        This method returns the fully qualified name of the specified class. For array types, it returns the component type name followed by the appropriate number of square brackets ("[]"). If the class is null, this method returns null.

        Example Usage

        
         String typeName = ClassUtils.getTypeName(String.class);
         System.out.println(typeName); // prints: java.lang.String
        
         String arrayTypeName = ClassUtils.getTypeName(int[].class);
         System.out.println(arrayTypeName); // prints: int[]
        
         String nullTypeName = ClassUtils.getTypeName(null);
         System.out.println(nullTypeName); // prints: null
         
        Parameters:
        type - the class to get the type name from, may be null
        Returns:
        the fully qualified name of the given class, or null if the class is null
        See Also:
        Class.getName(), Class.getComponentType()
      • getSimpleName

        @Nullable
        public static java.lang.String getSimpleName​(@Nullable
                                                     java.lang.Class<?> type)
        Gets the simple name of the given class.

        This method returns the simple name of the specified class as defined in the Java Language Specification. For array types, it returns the component type's simple name followed by the appropriate number of square brackets ("[]"). If the class is null, this method returns null.

        Example Usage

        
         String simpleName = ClassUtils.getSimpleName(String.class);
         System.out.println(simpleName); // prints: String
        
         String arraySimpleName = ClassUtils.getSimpleName(int[].class);
         System.out.println(arraySimpleName); // prints: int[]
        
         String nullSimpleName = ClassUtils.getSimpleName(null);
         System.out.println(nullSimpleName); // prints: null
         
        Parameters:
        type - the class to get the simple name from, may be null
        Returns:
        the simple name of the given class, or null if the class is null
        See Also:
        Class.getSimpleName(), getSimpleName(Class, boolean)
      • isDerived

        public static boolean isDerived​(@Nullable
                                        java.lang.Class<?> targetType,
                                        @Nullable
                                        java.lang.Class<?>... superTypes)
        Checks if the targetType is derived from any of the specified superTypes.

        This method returns true if the targetType is assignable from any of the superTypes, meaning that targetType is the same as, or is a subclass or subinterface of, any of the superTypes. It returns false if targetType is null, if superTypes is empty, or if none of the superTypes are assignable from targetType.

        Example Usage

        
         boolean result1 = ClassUtils.isDerived(ArrayList.class, List.class, Collection.class);
         // returns true because ArrayList implements List and Collection
        
         boolean result2 = ClassUtils.isDerived(String.class, Number.class);
         // returns false because String does not implement or extend Number
        
         boolean result3 = ClassUtils.isDerived(null, Object.class);
         // returns false because targetType is null
        
         boolean result4 = ClassUtils.isDerived(String.class);
         // returns false because superTypes is empty
         
        Parameters:
        targetType - the target type to check, may be null
        superTypes - the super types to check against, may be null or empty
        Returns:
        true if targetType is derived from any of the superTypes, false otherwise
        See Also:
        isAssignableFrom(Class, Class)
      • newInstance

        @Nonnull
        public static <T> T newInstance​(@Nonnull
                                        java.lang.Class<T> type,
                                        java.lang.Object... args)
        Creates a new instance of the specified class by finding a suitable constructor that matches the provided arguments and invoking it.

        This method searches for a declared constructor in the given class that matches the number and types of the provided arguments. It handles primitive types by resolving their wrapper types for comparison. If no matching constructor is found, an IllegalArgumentException is thrown. If a matching constructor is found, it is invoked using ConstructorUtils.newInstance(Constructor, Object...).

        Example Usage

        
         // Creating an instance of a class with a parameterized constructor
         MyClass instance = ClassUtils.newInstance(MyClass.class, "example", 42);
        
         // Creating an instance of a class with a no-arg constructor
         MyClass instance2 = ClassUtils.newInstance(MyClass.class);
        
         // This will throw an IllegalArgumentException if no matching constructor exists
         try {
             ClassUtils.newInstance(MyClass.class, "invalid", "arguments");
         } catch (IllegalArgumentException e) {
             System.out.println("No matching constructor found: " + e.getMessage());
         }
         
        Type Parameters:
        T - the type of the object to create
        Parameters:
        type - the class of which to create a new instance
        args - the arguments to pass to the constructor
        Returns:
        a new instance of the specified class
        Throws:
        java.lang.IllegalArgumentException - if no constructor matching the provided arguments is found
        See Also:
        ConstructorUtils.newInstance(Constructor, Object...)
      • getTopComponentType

        @Nullable
        public static java.lang.Class<?> getTopComponentType​(@Nullable
                                                             java.lang.Object array)
        Gets the top-level component type of the given array object.

        This method recursively retrieves the component type of an array until it reaches the base (non-array) type. For example, for a 2D array int[][], this method will return int.class. If the input is not an array or is null, this method returns null.

        Example Usage

        
         int[] array1D = {1, 2, 3};
         Class<?> componentType1 = ClassUtils.getTopComponentType(array1D);
         // Returns: int.class
        
         int[][] array2D = {{1, 2}, {3, 4}};
         Class<?> componentType2 = ClassUtils.getTopComponentType(array2D);
         // Returns: int.class
        
         String[] stringArray = {"a", "b"};
         Class<?> componentType3 = ClassUtils.getTopComponentType(stringArray);
         // Returns: String.class
        
         Object nonArray = "not an array";
         Class<?> componentType4 = ClassUtils.getTopComponentType(nonArray);
         // Returns: null
        
         Class<?> componentType5 = ClassUtils.getTopComponentType(null);
         // Returns: null
         
        Parameters:
        array - the array object to get the top-level component type from, may be null
        Returns:
        the top-level component type of the array, or null if the input is not an array or is null
        See Also:
        getTopComponentType(Class)
      • getTopComponentType

        @Nullable
        public static java.lang.Class<?> getTopComponentType​(@Nullable
                                                             java.lang.Class<?> arrayType)
        Gets the top-level component type of the given array type.

        This method recursively retrieves the component type of an array until it reaches the base (non-array) type. For example, for a 2D array int[][], this method will return int.class. If the input is not an array or is null, this method returns null.

        Example Usage

        
         Class<?> componentType1 = ClassUtils.getTopComponentType(int[].class);
         // Returns: int.class
        
         Class<?> componentType2 = ClassUtils.getTopComponentType(int[][].class);
         // Returns: int.class
        
         Class<?> componentType3 = ClassUtils.getTopComponentType(String[].class);
         // Returns: String.class
        
         Class<?> componentType4 = ClassUtils.getTopComponentType(String.class);
         // Returns: null
        
         Class<?> componentType5 = ClassUtils.getTopComponentType(null);
         // Returns: null
         
        Parameters:
        arrayType - the array type to get the top-level component type from, may be null
        Returns:
        the top-level component type of the array, or null if the input is not an array or is null
        See Also:
        isArray(Class), Class.getComponentType()
      • cast

        @Nullable
        public static <T> T cast​(@Nullable
                                 java.lang.Object object,
                                 @Nullable
                                 java.lang.Class<T> castType)
        Casts the given object to the specified type if possible.

        This method checks if the provided object is an instance of the target cast type. If it is, the object is cast to the target type and returned. Otherwise, null is returned. This method is null-safe and will return null if either the object or the cast type is null.

        Example Usage

        
         String str = "Hello";
         CharSequence charSeq = ClassUtils.cast(str, CharSequence.class);
         // Returns: "Hello" as CharSequence
        
         Integer num = 42;
         String strNum = ClassUtils.cast(num, String.class);
         // Returns: null (Integer is not an instance of String)
        
         Object nullObj = null;
         String nullResult = ClassUtils.cast(nullObj, String.class);
         // Returns: null (object is null)
        
         String str2 = "World";
         String result = ClassUtils.cast(str2, null);
         // Returns: null (castType is null)
         
        Type Parameters:
        T - the type to cast to
        Parameters:
        object - the object to cast, may be null
        castType - the type to cast the object to, may be null
        Returns:
        the casted object if it is an instance of the cast type, or null if the object is not an instance of the cast type, or if either the object or cast type is null
        See Also:
        Class.isInstance(Object), Class.cast(Object)