Class ClassUtils
- java.lang.Object
-
- io.microsphere.util.ClassUtils
-
- All Implemented Interfaces:
Utils
public abstract class ClassUtils extends java.lang.Object implements Utils
Classutility class- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Class,ClassLoaderUtils,ConstructorUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringARRAY_SUFFIXSuffix for array class names: "[]"static java.util.Set<java.lang.Class<?>>PRIMITIVE_ARRAY_TYPESstatic java.util.Set<java.lang.Class<?>>PRIMITIVE_TYPESstatic java.util.Set<java.lang.Class<?>>SIMPLE_TYPESSimple Types including:VoidBooleanCharacterByteIntegerFloatDoubleStringBigDecimalBigIntegerDateObjectstatic java.util.Set<java.lang.Class<?>>WRAPPER_TYPES
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanarrayTypeEquals(java.lang.Class<?> oneArrayType, java.lang.Class<?> anotherArrayType)Checks if two array types are equivalent, including nested arrays.static <T> Tcast(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 theClassof the given object.static java.lang.Class<?>[]getClasses(java.lang.Object... values)Get thetypesof the specified valuesstatic java.lang.StringgetSimpleName(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)Alias ofgetClass(Object)static java.lang.StringgetTypeName(java.lang.Class<?> type)Gets the type name of the given class.static java.lang.StringgetTypeName(java.lang.Object value)Gets the type name of the given object.static java.lang.Class[]getTypes(java.lang.Object... values)Alias ofgetTypes(Object...)static booleanisAbstractClass(java.lang.Class<?> type)Checks if the specified type is an abstract class.static booleanisArray(java.lang.Class<?> type)Checks if the specified type is an array.static booleanisArray(java.lang.Object object)Checks if the specified object is an array.static booleanisAssignableFrom(java.lang.Class<?> superType, java.lang.Class<?> targetType)Checks if thesuperTypeis assignable from thetargetType.static booleanisCharSequence(java.lang.Class<?> type)Checks if the given type is a subtype ofCharSequence.static booleanisCharSequence(java.lang.Object value)Checks if the given object is an instance ofCharSequence.static booleanisClass(java.lang.Object object)Checks if the given object is an instance ofClass.static booleanisConcreteClass(java.lang.Class<?> type)Checks if the specified type is a concrete class.static booleanisDerived(java.lang.Class<?> targetType, java.lang.Class<?>... superTypes)Checks if thetargetTypeis derived from any of the specifiedsuperTypes.static booleanisEnum(java.lang.Class<?> type)Checks if the specified class is an enum.static booleanisEnum(java.lang.Object object)Checks if the given object is an instance ofEnum.static booleanisFinal(java.lang.Class<?> type)Checks if the specified class is a final class.static booleanisGeneralClass(java.lang.Class<?> type)Checks if the specified type is a general class.protected static booleanisGeneralClass(java.lang.Class<?> type, java.lang.Boolean isAbstract)Is the specified type a general class or not?static booleanisNumber(java.lang.Class<?> type)Checks if the given type is a subtype ofNumber.static booleanisNumber(java.lang.Object value)Checks if the given object is an instance ofNumber.static booleanisPrimitive(java.lang.Class<?> type)Checks if the specified class is a primitive type.static booleanisSimpleType(java.lang.Class<?> type)Checks if the specified type is a simple type.static booleanisSimpleType(java.lang.Object object)Checks if the specified object is a simple type.static booleanisTopLevelClass(java.lang.Class<?> type)Checks if the specified class is a top-level class.static booleanisWrapperType(java.lang.Class<?> type)Checks if the specified class is a wrapper type.static booleanisWrapperType(java.lang.Object value)Checks if the specified object is a wrapper type.static <T> TnewInstance(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.StringresolveClassName(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.StringresolveClassName(java.lang.String resourceName)Resolves the fully qualified class name from a resource name.static java.lang.StringresolvePackageName(java.lang.Class<?> targetClass)Resolve the package name of the given class.static java.lang.StringresolvePackageName(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.
-
-
-
Field Detail
-
ARRAY_SUFFIX
@Nonnull @Immutable public static final java.lang.String ARRAY_SUFFIX
Suffix for array class names: "[]"- See Also:
- Constant Field Values
-
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:VoidBooleanCharacterByteIntegerFloatDoubleStringBigDecimalBigIntegerDateObject
- 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 benull- Returns:
trueif the specified object is an array,falseotherwise- 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 benull- Returns:
trueif the specified type is an array class,falseotherwise- 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 benull- Returns:
trueif the specified type is a concrete class,falseotherwise- 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 benull- Returns:
trueif the specified type is an abstract class,falseotherwise- 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 benull- Returns:
trueif the specified type is a general class,falseotherwise- 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 checktypeis abstract or not.- Parameters:
type- the typeisAbstract- 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 benull- Returns:
trueif the specified class is a top-level class,falseotherwise- 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
trueif the given class is one of the eight primitive types (boolean,byte,char,short,int,long,float,double) or their corresponding array types. It returnsfalseotherwise, 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 benull- Returns:
trueif the specified class is a primitive type or primitive array type,falseotherwise- 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
trueif the given class is marked with thefinalmodifier, andfalseotherwise. It also returnsfalseif the class isnull.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 benull- Returns:
trueif the specified class is a final class,falseotherwise- 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:
VoidBooleanCharacterByteShortIntegerLongFloatDoubleStringBigDecimalBigIntegerDateObject
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 benull- Returns:
trueif the specified object is a simple type,falseotherwise- 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:
VoidBooleanCharacterByteShortIntegerLongFloatDoubleStringBigDecimalBigIntegerDateObject
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 benull- Returns:
trueif the specified type is a simple type,falseotherwise- See Also:
SIMPLE_TYPES
-
isCharSequence
public static boolean isCharSequence(@Nullable java.lang.Object value)
Checks if the given object is an instance ofCharSequence.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 benull- Returns:
trueif the object is an instance ofCharSequence,falseotherwise- See Also:
isCharSequence(Class)
-
isCharSequence
public static boolean isCharSequence(@Nullable java.lang.Class<?> type)
Checks if the given type is a subtype ofCharSequence.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 benull- Returns:
trueif the type is a subtype ofCharSequence,falseotherwise
-
isNumber
public static boolean isNumber(@Nullable java.lang.Object value)
Checks if the given object is an instance ofNumber.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 benull- Returns:
trueif the object is an instance ofNumber,falseotherwise- See Also:
isNumber(Class)
-
isNumber
public static boolean isNumber(@Nullable java.lang.Class<?> type)
Checks if the given type is a subtype ofNumber.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 benull- Returns:
trueif the type is a subtype ofNumber,falseotherwise
-
isClass
public static boolean isClass(@Nullable java.lang.Object object)
Checks if the given object is an instance ofClass.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 benull- Returns:
trueif the object is an instance ofClass,falseotherwise
-
isEnum
public static boolean isEnum(@Nullable java.lang.Object object)
Checks if the given object is an instance ofEnum.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 benull- Returns:
trueif the object is an instance ofEnum,falseotherwise- 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
trueif the given class is an enum type, andfalseotherwise. It also returnsfalseif the class isnull.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 benull- Returns:
trueif the specified class is an enum,falseotherwise- 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,nullis 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 benull- 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,nullis 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 benull- 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 benull- 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 benulldefaultType- 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:
BooleanCharacterByteShortIntegerLongFloatDouble
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 benull- Returns:
trueif the specified object is a wrapper type,falseotherwise- 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:
BooleanCharacterByteShortIntegerLongFloatDouble
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 benull- Returns:
trueif the specified class is a wrapper type,falseotherwise- 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
trueif both classes are arrays and their component types are equal (considering nested arrays),falseotherwise.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 benullanotherArrayType- the second array type to compare, may benull- Returns:
trueif both types are arrays with equivalent component types,falseotherwise- 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
Classobject if a match is found, ornullotherwise.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 benull- Returns:
- the primitive class, or
nullif 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 returnsnull.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 benull- Returns:
- the package name of the given class, or
nullif the class is primitive ornull - 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 returnsnull.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 benull- Returns:
- the package name, or an empty string if the class is in the default package,
or
nullif the input isnull - 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
recursiveparameter is set totrue.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 benullrecursive- 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
recursiveparameter is set totrue.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 benullrecursive- 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
recursiveparameter is set totrue, 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 benullrecursive- 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
recursiveparameter is set totrue, 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 benullrecursive- 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 benullclassFile- the class file to resolve the name for, must not benull- 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 benull- Returns:
- the fully qualified class name, or
nullif the input isnull - 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 benull- Returns:
- non-null read-only
Listof 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 benull- Returns:
- non-null read-only
Listof 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 benull- Returns:
- non-null read-only
Listof 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 benull- Returns:
- non-null read-only
Listof 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 benull- Returns:
- non-null read-only
Listof 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
Predicatefilters. 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 benullclassFilters- optional filters to apply to the results- Returns:
- a non-null, read-only
Listof 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
Predicatefilters. 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 benullinterfaceFilters- optional filters to apply to the results- Returns:
- a non-null, read-only
Listof 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
Predicatefilters. 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 benullclassFilters- optional filters to apply to the results- Returns:
- a non-null, read-only
Listof 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
Predicatefilters. 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 benullclassFilters- optional filters to apply to the results- Returns:
- a non-null, read-only
Listof 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
Predicatefilters.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 benullincludeSelf- whether to include the type itself in the resultsincludeHierarchicalTypes- whether to include superclasses in the resultsincludeGenericSuperclass- whether to include the direct generic superclass in the resultsincludeGenericInterfaces- whether to include interfaces in the resultstypeFilters- optional filters to apply to the results- Returns:
- a non-null, read-only
Listof 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 thesuperTypeis assignable from thetargetType.This method is a null-safe variant of
Class.isAssignableFrom(Class). It returnsfalseif either of the arguments isnull.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 benulltargetType- the target type to check, may benull- Returns:
trueifsuperTypeis assignable fromtargetType,falseotherwise- See Also:
Class.isAssignableFrom(Class)
-
getType
@Nullable public static java.lang.Class<?> getType(@Nullable java.lang.Object value)
Alias ofgetClass(Object)- See Also:
getClass(Object)
-
getClass
@Nullable public static java.lang.Class<?> getClass(@Nullable java.lang.Object object)
Gets theClassof 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 returnsnull.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 benull- Returns:
- the Class of the given object, or
nullif the object isnull - See Also:
Object.getClass()
-
getTypes
@Nonnull public static java.lang.Class[] getTypes(@Nullable java.lang.Object... values)
Alias ofgetTypes(Object...)- See Also:
getTypes(Object...)
-
getClasses
@Nonnull public static java.lang.Class<?>[] getClasses(@Nullable java.lang.Object... values)
Get thetypesof 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 returnsnull.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 benull- Returns:
- the fully qualified name of the class of the given object, or
nullif the object isnull - 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 returnsnull.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 benull- Returns:
- the fully qualified name of the given class, or
nullif the class isnull - 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 returnsnull.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 benull- Returns:
- the simple name of the given class, or
nullif the class isnull - See Also:
Class.getSimpleName(),getSimpleName(Class, boolean)
-
isDerived
public static boolean isDerived(@Nullable java.lang.Class<?> targetType, @Nullable java.lang.Class<?>... superTypes)
Checks if thetargetTypeis derived from any of the specifiedsuperTypes.This method returns
trueif thetargetTypeis assignable from any of thesuperTypes, meaning thattargetTypeis the same as, or is a subclass or subinterface of, any of thesuperTypes. It returnsfalseiftargetTypeisnull, ifsuperTypesis empty, or if none of thesuperTypesare assignable fromtargetType.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 benullsuperTypes- the super types to check against, may benullor empty- Returns:
trueiftargetTypeis derived from any of thesuperTypes,falseotherwise- 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
IllegalArgumentExceptionis thrown. If a matching constructor is found, it is invoked usingConstructorUtils.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 instanceargs- 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 returnint.class. If the input is not an array or isnull, this method returnsnull.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 benull- Returns:
- the top-level component type of the array, or
nullif the input is not an array or isnull - 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 returnint.class. If the input is not an array or isnull, this method returnsnull.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 benull- Returns:
- the top-level component type of the array, or
nullif the input is not an array or isnull - 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,
nullis returned. This method is null-safe and will returnnullif either the object or the cast type isnull.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 benullcastType- the type to cast the object to, may benull- Returns:
- the casted object if it is an instance of the cast type, or
nullif the object is not an instance of the cast type, or if either the object or cast type isnull - See Also:
Class.isInstance(Object),Class.cast(Object)
-
-