Class ClassUtils
- java.lang.Object
-
- org.apache.commons.lang3.ClassUtils
-
public class ClassUtils extends java.lang.ObjectOperates on classes without using reflection.
This class handles invalid
nullinputs as best it can. Each method documents its behaviour in more detail.The notion of a
canonical nameincludes the human readable name for the type, for exampleint[]. The non-canonical method variants work with the JVM names, such as[I.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classClassUtils.InterfacesInclusivity literals forhierarchy(Class, Interfaces).
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.String,java.lang.String>abbreviationMapMaps a primitive class name to its corresponding abbreviation used in array class names.static java.lang.StringINNER_CLASS_SEPARATORThe inner class separator String:"$".static charINNER_CLASS_SEPARATOR_CHARThe inner class separator character:'$' == 36.private static java.util.Map<java.lang.String,java.lang.Class<?>>namePrimitiveMapMaps names of primitives to their corresponding primitiveClasses.static java.lang.StringPACKAGE_SEPARATORThe package separator String:".".static charPACKAGE_SEPARATOR_CHARThe package separator character:'.' == 46.private static java.util.Map<java.lang.Class<?>,java.lang.Class<?>>primitiveWrapperMapMaps primitiveClasses to their corresponding wrapperClass.private static java.util.Map<java.lang.String,java.lang.String>reverseAbbreviationMapMaps an abbreviation used in array class names to corresponding primitive class name.private static java.util.Map<java.lang.Class<?>,java.lang.Class<?>>wrapperPrimitiveMapMaps wrapperClasses to their corresponding primitive types.
-
Constructor Summary
Constructors Constructor Description ClassUtils()ClassUtils instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<java.lang.String>convertClassesToClassNames(java.util.List<java.lang.Class<?>> classes)Given aListofClassobjects, this method converts them into class names.static java.util.List<java.lang.Class<?>>convertClassNamesToClasses(java.util.List<java.lang.String> classNames)Given aListof class names, this method converts them into classes.static java.lang.StringgetAbbreviatedName(java.lang.Class<?> cls, int len)Gets the abbreviated name of aClass.static java.lang.StringgetAbbreviatedName(java.lang.String className, int len)Gets the abbreviated class name from aString.static java.util.List<java.lang.Class<?>>getAllInterfaces(java.lang.Class<?> cls)Gets aListof all interfaces implemented by the given class and its superclasses.private static voidgetAllInterfaces(java.lang.Class<?> cls, java.util.HashSet<java.lang.Class<?>> interfacesFound)Get the interfaces for the specified class.static java.util.List<java.lang.Class<?>>getAllSuperclasses(java.lang.Class<?> cls)Gets aListof superclasses for the given class.static java.lang.StringgetCanonicalName(java.lang.Class<?> cls)Gets the canonical class name for aClass.static java.lang.StringgetCanonicalName(java.lang.Class<?> cls, java.lang.String valueIfNull)Gets the canonical name for aClass.static java.lang.StringgetCanonicalName(java.lang.Object object)Gets the canonical name for anObject.static java.lang.StringgetCanonicalName(java.lang.Object object, java.lang.String valueIfNull)Gets the canonical name for anObject.private static java.lang.StringgetCanonicalName(java.lang.String className)Converts a given name of class into canonical format.static java.lang.Class<?>getClass(java.lang.ClassLoader classLoader, java.lang.String className)Returns the (initialized) class represented byclassNameusing theclassLoader.static java.lang.Class<?>getClass(java.lang.ClassLoader classLoader, java.lang.String className, boolean initialize)Returns the class represented byclassNameusing theclassLoader.static java.lang.Class<?>getClass(java.lang.String className)Returns the (initialized) class represented byclassNameusing the current thread's context class loader.static java.lang.Class<?>getClass(java.lang.String className, boolean initialize)Returns the class represented byclassNameusing the current thread's context class loader.static java.lang.StringgetName(java.lang.Class<?> cls)Null-safe version ofClass.getName()static java.lang.StringgetName(java.lang.Class<?> cls, java.lang.String valueIfNull)Null-safe version ofaClass.getName()static java.lang.StringgetName(java.lang.Object object)Null-safe version ofClass.getName()static java.lang.StringgetName(java.lang.Object object, java.lang.String valueIfNull)Null-safe version ofaClass.getSimpleName()static java.lang.StringgetPackageCanonicalName(java.lang.Class<?> cls)Gets the package name from the canonical name of aClass.static java.lang.StringgetPackageCanonicalName(java.lang.Object object, java.lang.String valueIfNull)Gets the package name from the canonical name of anObject.static java.lang.StringgetPackageCanonicalName(java.lang.String canonicalName)Gets the package name from the canonical name.static java.lang.StringgetPackageName(java.lang.Class<?> cls)Gets the package name of aClass.static java.lang.StringgetPackageName(java.lang.Object object, java.lang.String valueIfNull)Gets the package name of anObject.static java.lang.StringgetPackageName(java.lang.String className)Gets the package name from aString.static java.lang.reflect.MethodgetPublicMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)Returns the desired Method much likeClass.getMethod, however it ensures that the returned Method is from a public class or interface and not from an anonymous inner class.static java.lang.StringgetShortCanonicalName(java.lang.Class<?> cls)Gets the canonical name minus the package name from aClass.static java.lang.StringgetShortCanonicalName(java.lang.Object object, java.lang.String valueIfNull)Gets the canonical name minus the package name for anObject.static java.lang.StringgetShortCanonicalName(java.lang.String canonicalName)Gets the canonical name minus the package name from a String.static java.lang.StringgetShortClassName(java.lang.Class<?> cls)Gets the class name minus the package name from aClass.static java.lang.StringgetShortClassName(java.lang.Object object, java.lang.String valueIfNull)Gets the class name minus the package name for anObject.static java.lang.StringgetShortClassName(java.lang.String className)Gets the class name minus the package name from a String.static java.lang.StringgetSimpleName(java.lang.Class<?> cls)Null-safe version ofaClass.getSimpleName()static java.lang.StringgetSimpleName(java.lang.Class<?> cls, java.lang.String valueIfNull)Null-safe version ofaClass.getSimpleName()static java.lang.StringgetSimpleName(java.lang.Object object)Null-safe version ofaClass.getSimpleName()static java.lang.StringgetSimpleName(java.lang.Object object, java.lang.String valueIfNull)Null-safe version ofaClass.getSimpleName()static java.lang.Iterable<java.lang.Class<?>>hierarchy(java.lang.Class<?> type)Get anIterablethat can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.static java.lang.Iterable<java.lang.Class<?>>hierarchy(java.lang.Class<?> type, ClassUtils.Interfaces interfacesBehavior)Get anIterablethat can iterate over a class hierarchy in ascending (subclass to superclass) order.static booleanisAssignable(java.lang.Class<?>[] classArray, java.lang.Class<?>... toClassArray)Checks if an array of Classes can be assigned to another array of Classes.static booleanisAssignable(java.lang.Class<?>[] classArray, java.lang.Class<?>[] toClassArray, boolean autoboxing)Checks if an array of Classes can be assigned to another array of Classes.static booleanisAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass)Checks if oneClasscan be assigned to a variable of anotherClass.static booleanisAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass, boolean autoboxing)Checks if oneClasscan be assigned to a variable of anotherClass.static booleanisInnerClass(java.lang.Class<?> cls)Is the specified class an inner class or static nested class.static booleanisPrimitiveOrWrapper(java.lang.Class<?> type)Returns whether the giventypeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).static booleanisPrimitiveWrapper(java.lang.Class<?> type)Returns whether the giventypeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).static java.lang.Class<?>[]primitivesToWrappers(java.lang.Class<?>... classes)Converts the specified array of primitive Class objects to an array of its corresponding wrapper Class objects.static java.lang.Class<?>primitiveToWrapper(java.lang.Class<?> cls)Converts the specified primitive Class object to its corresponding wrapper Class object.private static java.lang.StringtoCanonicalName(java.lang.String className)Converts a class name to a JLS style class name.static java.lang.Class<?>[]toClass(java.lang.Object... array)Converts an array ofObjectin to an array ofClassobjects.static java.lang.Class<?>[]wrappersToPrimitives(java.lang.Class<?>... classes)Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.static java.lang.Class<?>wrapperToPrimitive(java.lang.Class<?> cls)Converts the specified wrapper class to its corresponding primitive class.
-
-
-
Field Detail
-
PACKAGE_SEPARATOR_CHAR
public static final char PACKAGE_SEPARATOR_CHAR
The package separator character:'.' == 46.- See Also:
- Constant Field Values
-
PACKAGE_SEPARATOR
public static final java.lang.String PACKAGE_SEPARATOR
The package separator String:".".
-
INNER_CLASS_SEPARATOR_CHAR
public static final char INNER_CLASS_SEPARATOR_CHAR
The inner class separator character:'$' == 36.- See Also:
- Constant Field Values
-
INNER_CLASS_SEPARATOR
public static final java.lang.String INNER_CLASS_SEPARATOR
The inner class separator String:"$".
-
namePrimitiveMap
private static final java.util.Map<java.lang.String,java.lang.Class<?>> namePrimitiveMap
Maps names of primitives to their corresponding primitiveClasses.
-
primitiveWrapperMap
private static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> primitiveWrapperMap
Maps primitiveClasses to their corresponding wrapperClass.
-
wrapperPrimitiveMap
private static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> wrapperPrimitiveMap
Maps wrapperClasses to their corresponding primitive types.
-
abbreviationMap
private static final java.util.Map<java.lang.String,java.lang.String> abbreviationMap
Maps a primitive class name to its corresponding abbreviation used in array class names.
-
reverseAbbreviationMap
private static final java.util.Map<java.lang.String,java.lang.String> reverseAbbreviationMap
Maps an abbreviation used in array class names to corresponding primitive class name.
-
-
Method Detail
-
getShortClassName
public static java.lang.String getShortClassName(java.lang.Object object, java.lang.String valueIfNull)Gets the class name minus the package name for an
Object.- Parameters:
object- the class to get the short name for, may be nullvalueIfNull- the value to return if null- Returns:
- the class name of the object without the package name, or the null value
-
getShortClassName
public static java.lang.String getShortClassName(java.lang.Class<?> cls)
Gets the class name minus the package name from a
Class.Consider using the Java 5 API
Class.getSimpleName()instead. The one known difference is that this code will return"Map.Entry"while thejava.lang.Classvariant will simply return"Entry".- Parameters:
cls- the class to get the short name for.- Returns:
- the class name without the package name or an empty string
-
getShortClassName
public static java.lang.String getShortClassName(java.lang.String className)
Gets the class name minus the package name from a String.
The string passed in is assumed to be a class name - it is not checked.
Note that this method differs from Class.getSimpleName() in that this will return
"Map.Entry"whilst thejava.lang.Classvariant will simply return"Entry".- Parameters:
className- the className to get the short name for- Returns:
- the class name of the class without the package name or an empty string
-
getSimpleName
public static java.lang.String getSimpleName(java.lang.Class<?> cls)
Null-safe version of
aClass.getSimpleName()- Parameters:
cls- the class for which to get the simple name; may be null- Returns:
- the simple class name.
- Since:
- 3.0
- See Also:
Class.getSimpleName()
-
getSimpleName
public static java.lang.String getSimpleName(java.lang.Class<?> cls, java.lang.String valueIfNull)Null-safe version of
aClass.getSimpleName()- Parameters:
cls- the class for which to get the simple name; may be nullvalueIfNull- the value to return if null- Returns:
- the simple class name or
valueIfNull - Since:
- 3.0
- See Also:
Class.getSimpleName()
-
getSimpleName
public static java.lang.String getSimpleName(java.lang.Object object)
Null-safe version of
aClass.getSimpleName()- Parameters:
object- the object for which to get the simple class name; may be null- Returns:
- the simple class name or the empty String
- Since:
- 3.7
- See Also:
Class.getSimpleName()
-
getSimpleName
public static java.lang.String getSimpleName(java.lang.Object object, java.lang.String valueIfNull)Null-safe version of
aClass.getSimpleName()- Parameters:
object- the object for which to get the simple class name; may be nullvalueIfNull- the value to return ifobjectisnull- Returns:
- the simple class name or
valueIfNull - Since:
- 3.0
- See Also:
Class.getSimpleName()
-
getName
public static java.lang.String getName(java.lang.Class<?> cls)
Null-safe version of
Class.getName()- Parameters:
cls- the class for which to get the class name; may be null- Returns:
- the class name or the empty String.
- Since:
- 3.7
- See Also:
Class.getSimpleName()
-
getName
public static java.lang.String getName(java.lang.Class<?> cls, java.lang.String valueIfNull)Null-safe version of
aClass.getName()- Parameters:
cls- the class for which to get the class name; may be nullvalueIfNull- the return value ifclsisnull- Returns:
- the class name or
valueIfNull - Since:
- 3.7
- See Also:
Class.getName()
-
getName
public static java.lang.String getName(java.lang.Object object)
Null-safe version of
Class.getName()- Parameters:
object- the object for which to get the class name; may be null- Returns:
- the class name or the empty String
- Since:
- 3.7
- See Also:
Class.getSimpleName()
-
getName
public static java.lang.String getName(java.lang.Object object, java.lang.String valueIfNull)Null-safe version of
aClass.getSimpleName()- Parameters:
object- the object for which to get the class name; may be nullvalueIfNull- the value to return ifobjectisnull- Returns:
- the class name or
valueIfNull - Since:
- 3.0
- See Also:
Class.getName()
-
getPackageName
public static java.lang.String getPackageName(java.lang.Object object, java.lang.String valueIfNull)Gets the package name of an
Object.- Parameters:
object- the class to get the package name for, may be nullvalueIfNull- the value to return if null- Returns:
- the package name of the object, or the null value
-
getPackageName
public static java.lang.String getPackageName(java.lang.Class<?> cls)
Gets the package name of a
Class.- Parameters:
cls- the class to get the package name for, may benull.- Returns:
- the package name or an empty string
-
getPackageName
public static java.lang.String getPackageName(java.lang.String className)
Gets the package name from a
String.The string passed in is assumed to be a class name - it is not checked.
If the class is unpackaged, return an empty string.
- Parameters:
className- the className to get the package name for, may benull- Returns:
- the package name or an empty string
-
getAbbreviatedName
public static java.lang.String getAbbreviatedName(java.lang.Class<?> cls, int len)Gets the abbreviated name of a
Class.- Parameters:
cls- the class to get the abbreviated name for, may benulllen- the desired length of the abbreviated name- Returns:
- the abbreviated name or an empty string
- Throws:
java.lang.IllegalArgumentException- if len <= 0- Since:
- 3.4
- See Also:
getAbbreviatedName(String, int)
-
getAbbreviatedName
public static java.lang.String getAbbreviatedName(java.lang.String className, int len)Gets the abbreviated class name from a
String.The string passed in is assumed to be a class name - it is not checked.
The abbreviation algorithm will shorten the class name, usually without significant loss of meaning.
The abbreviated class name will always include the complete package hierarchy. If enough space is available, rightmost sub-packages will be displayed in full length.
Examples className len return null 1 "" "java.lang.String" 5 "j.l.String" "java.lang.String" 15 "j.lang.String" "java.lang.String" 30 "java.lang.String" - Parameters:
className- the className to get the abbreviated name for, may benulllen- the desired length of the abbreviated name- Returns:
- the abbreviated name or an empty string
- Throws:
java.lang.IllegalArgumentException- if len <= 0- Since:
- 3.4
-
getAllSuperclasses
public static java.util.List<java.lang.Class<?>> getAllSuperclasses(java.lang.Class<?> cls)
Gets a
Listof superclasses for the given class.- Parameters:
cls- the class to look up, may benull- Returns:
- the
Listof superclasses in order going up from this onenullif null input
-
getAllInterfaces
public static java.util.List<java.lang.Class<?>> getAllInterfaces(java.lang.Class<?> cls)
Gets a
Listof all interfaces implemented by the given class and its superclasses.The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order is maintained.
- Parameters:
cls- the class to look up, may benull- Returns:
- the
Listof interfaces in order,nullif null input
-
getAllInterfaces
private static void getAllInterfaces(java.lang.Class<?> cls, java.util.HashSet<java.lang.Class<?>> interfacesFound)Get the interfaces for the specified class.- Parameters:
cls- the class to look up, may benullinterfacesFound- theSetof interfaces for the class
-
convertClassNamesToClasses
public static java.util.List<java.lang.Class<?>> convertClassNamesToClasses(java.util.List<java.lang.String> classNames)
Given a
Listof class names, this method converts them into classes.A new
Listis returned. If the class name cannot be found,nullis stored in theList. If the class name in theListisnull,nullis stored in the outputList.- Parameters:
classNames- the classNames to change- Returns:
- a
Listof Class objects corresponding to the class names,nullif null input - Throws:
java.lang.ClassCastException- if classNames contains a non String entry
-
convertClassesToClassNames
public static java.util.List<java.lang.String> convertClassesToClassNames(java.util.List<java.lang.Class<?>> classes)
Given a
ListofClassobjects, this method converts them into class names.A new
Listis returned.nullobjects will be copied into the returned list asnull.- Parameters:
classes- the classes to change- Returns:
- a
Listof class names corresponding to the Class objects,nullif null input - Throws:
java.lang.ClassCastException- ifclassescontains a non-Classentry
-
isAssignable
public static boolean isAssignable(java.lang.Class<?>[] classArray, java.lang.Class<?>... toClassArray)Checks if an array of Classes can be assigned to another array of Classes.
This method calls
isAssignablefor each Class pair in the input arrays. It can be used to check if a set of arguments (the first parameter) are suitably compatible with a set of method parameter types (the second parameter).Unlike the
Class.isAssignableFrom(java.lang.Class)method, this method takes into account widenings of primitive classes andnulls.Primitive widenings allow an int to be assigned to a
long,floatordouble. This method returns the correct result for these cases.Nullmay be assigned to any reference type. This method will returntrueifnullis passed in and the toClass is non-primitive.Specifically, this method tests whether the type represented by the specified
Classparameter can be converted to the type represented by thisClassobject via an identity conversion widening primitive or widening reference conversion. See The Java Language Specification, sections 5.1.1, 5.1.2 and 5.1.4 for details.Since Lang 3.0, this method will default behavior for calculating assignability between primitive and wrapper types corresponding to the running Java version; i.e. autoboxing will be the default behavior in VMs running Java versions > 1.5.
- Parameters:
classArray- the array of Classes to check, may benulltoClassArray- the array of Classes to try to assign into, may benull- Returns:
trueif assignment possible
-
isAssignable
public static boolean isAssignable(java.lang.Class<?>[] classArray, java.lang.Class<?>[] toClassArray, boolean autoboxing)Checks if an array of Classes can be assigned to another array of Classes.
This method calls
isAssignablefor each Class pair in the input arrays. It can be used to check if a set of arguments (the first parameter) are suitably compatible with a set of method parameter types (the second parameter).Unlike the
Class.isAssignableFrom(java.lang.Class)method, this method takes into account widenings of primitive classes andnulls.Primitive widenings allow an int to be assigned to a
long,floatordouble. This method returns the correct result for these cases.Nullmay be assigned to any reference type. This method will returntrueifnullis passed in and the toClass is non-primitive.Specifically, this method tests whether the type represented by the specified
Classparameter can be converted to the type represented by thisClassobject via an identity conversion widening primitive or widening reference conversion. See The Java Language Specification, sections 5.1.1, 5.1.2 and 5.1.4 for details.- Parameters:
classArray- the array of Classes to check, may benulltoClassArray- the array of Classes to try to assign into, may benullautoboxing- whether to use implicit autoboxing/unboxing between primitives and wrappers- Returns:
trueif assignment possible
-
isPrimitiveOrWrapper
public static boolean isPrimitiveOrWrapper(java.lang.Class<?> type)
Returns whether the giventypeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).- Parameters:
type- The class to query or null.- Returns:
- true if the given
typeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float). - Since:
- 3.1
-
isPrimitiveWrapper
public static boolean isPrimitiveWrapper(java.lang.Class<?> type)
Returns whether the giventypeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).- Parameters:
type- The class to query or null.- Returns:
- true if the given
typeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float). - Since:
- 3.1
-
isAssignable
public static boolean isAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass)Checks if one
Classcan be assigned to a variable of anotherClass.Unlike the
Class.isAssignableFrom(java.lang.Class)method, this method takes into account widenings of primitive classes andnulls.Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct result for these cases.
Nullmay be assigned to any reference type. This method will returntrueifnullis passed in and the toClass is non-primitive.Specifically, this method tests whether the type represented by the specified
Classparameter can be converted to the type represented by thisClassobject via an identity conversion widening primitive or widening reference conversion. See The Java Language Specification, sections 5.1.1, 5.1.2 and 5.1.4 for details.Since Lang 3.0, this method will default behavior for calculating assignability between primitive and wrapper types corresponding to the running Java version; i.e. autoboxing will be the default behavior in VMs running Java versions > 1.5.
- Parameters:
cls- the Class to check, may be nulltoClass- the Class to try to assign into, returns false if null- Returns:
trueif assignment possible
-
isAssignable
public static boolean isAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass, boolean autoboxing)Checks if one
Classcan be assigned to a variable of anotherClass.Unlike the
Class.isAssignableFrom(java.lang.Class)method, this method takes into account widenings of primitive classes andnulls.Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct result for these cases.
Nullmay be assigned to any reference type. This method will returntrueifnullis passed in and the toClass is non-primitive.Specifically, this method tests whether the type represented by the specified
Classparameter can be converted to the type represented by thisClassobject via an identity conversion widening primitive or widening reference conversion. See The Java Language Specification, sections 5.1.1, 5.1.2 and 5.1.4 for details.- Parameters:
cls- the Class to check, may be nulltoClass- the Class to try to assign into, returns false if nullautoboxing- whether to use implicit autoboxing/unboxing between primitives and wrappers- Returns:
trueif assignment possible
-
primitiveToWrapper
public static java.lang.Class<?> primitiveToWrapper(java.lang.Class<?> cls)
Converts the specified primitive Class object to its corresponding wrapper Class object.
NOTE: From v2.2, this method handles
Void.TYPE, returningVoid.TYPE.- Parameters:
cls- the class to convert, may be null- Returns:
- the wrapper class for
clsorclsifclsis not a primitive.nullif null input. - Since:
- 2.1
-
primitivesToWrappers
public static java.lang.Class<?>[] primitivesToWrappers(java.lang.Class<?>... classes)
Converts the specified array of primitive Class objects to an array of its corresponding wrapper Class objects.
- Parameters:
classes- the class array to convert, may be null or empty- Returns:
- an array which contains for each given class, the wrapper class or
the original class if class is not a primitive.
nullif null input. Empty array if an empty array passed in. - Since:
- 2.1
-
wrapperToPrimitive
public static java.lang.Class<?> wrapperToPrimitive(java.lang.Class<?> cls)
Converts the specified wrapper class to its corresponding primitive class.
This method is the counter part of
primitiveToWrapper(). If the passed in class is a wrapper class for a primitive type, this primitive type will be returned (e.g.Integer.TYPEforInteger.class). For other classes, or if the parameter is null, the return value is null.- Parameters:
cls- the class to convert, may be null- Returns:
- the corresponding primitive type if
clsis a wrapper class, null otherwise - Since:
- 2.4
- See Also:
primitiveToWrapper(Class)
-
wrappersToPrimitives
public static java.lang.Class<?>[] wrappersToPrimitives(java.lang.Class<?>... classes)
Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.
This method invokes
wrapperToPrimitive()for each element of the passed in array.- Parameters:
classes- the class array to convert, may be null or empty- Returns:
- an array which contains for each given class, the primitive class or
null if the original class is not a wrapper class.
nullif null input. Empty array if an empty array passed in. - Since:
- 2.4
- See Also:
wrapperToPrimitive(Class)
-
isInnerClass
public static boolean isInnerClass(java.lang.Class<?> cls)
Is the specified class an inner class or static nested class.
- Parameters:
cls- the class to check, may be null- Returns:
trueif the class is an inner or static nested class, false if not ornull
-
getClass
public static java.lang.Class<?> getClass(java.lang.ClassLoader classLoader, java.lang.String className, boolean initialize) throws java.lang.ClassNotFoundExceptionReturns the class represented byclassNameusing theclassLoader. This implementation supports the syntaxes "java.util.Map.Entry[]", "java.util.Map$Entry[]", "[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".- Parameters:
classLoader- the class loader to use to load the classclassName- the class nameinitialize- whether the class must be initialized- Returns:
- the class represented by
classNameusing theclassLoader - Throws:
java.lang.ClassNotFoundException- if the class is not found
-
getClass
public static java.lang.Class<?> getClass(java.lang.ClassLoader classLoader, java.lang.String className) throws java.lang.ClassNotFoundExceptionReturns the (initialized) class represented byclassNameusing theclassLoader. This implementation supports the syntaxes "java.util.Map.Entry[]", "java.util.Map$Entry[]", "[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".- Parameters:
classLoader- the class loader to use to load the classclassName- the class name- Returns:
- the class represented by
classNameusing theclassLoader - Throws:
java.lang.ClassNotFoundException- if the class is not found
-
getClass
public static java.lang.Class<?> getClass(java.lang.String className) throws java.lang.ClassNotFoundExceptionReturns the (initialized) class represented byclassNameusing the current thread's context class loader. This implementation supports the syntaxes "java.util.Map.Entry[]", "java.util.Map$Entry[]", "[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".- Parameters:
className- the class name- Returns:
- the class represented by
classNameusing the current thread's context class loader - Throws:
java.lang.ClassNotFoundException- if the class is not found
-
getClass
public static java.lang.Class<?> getClass(java.lang.String className, boolean initialize) throws java.lang.ClassNotFoundExceptionReturns the class represented byclassNameusing the current thread's context class loader. This implementation supports the syntaxes "java.util.Map.Entry[]", "java.util.Map$Entry[]", "[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;".- Parameters:
className- the class nameinitialize- whether the class must be initialized- Returns:
- the class represented by
classNameusing the current thread's context class loader - Throws:
java.lang.ClassNotFoundException- if the class is not found
-
getPublicMethod
public static java.lang.reflect.Method getPublicMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodExceptionReturns the desired Method much like
Class.getMethod, however it ensures that the returned Method is from a public class or interface and not from an anonymous inner class. This means that the Method is invokable and doesn't fall foul of Java bug 4071957).Set set = Collections.unmodifiableSet(...); Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty", new Class[0]); Object result = method.invoke(set, new Object[]);- Parameters:
cls- the class to check, not nullmethodName- the name of the methodparameterTypes- the list of parameters- Returns:
- the method
- Throws:
java.lang.NullPointerException- if the class is nulljava.lang.SecurityException- if a security violation occurredjava.lang.NoSuchMethodException- if the method is not found in the given class or if the method doesn't conform with the requirements
-
toCanonicalName
private static java.lang.String toCanonicalName(java.lang.String className)
Converts a class name to a JLS style class name.- Parameters:
className- the class name- Returns:
- the converted name
-
toClass
public static java.lang.Class<?>[] toClass(java.lang.Object... array)
Converts an array of
Objectin to an array ofClassobjects. If any of these objects is null, a null element will be inserted into the array.This method returns
nullfor anullinput array.- Parameters:
array- anObjectarray- Returns:
- a
Classarray,nullif null array input - Since:
- 2.4
-
getShortCanonicalName
public static java.lang.String getShortCanonicalName(java.lang.Object object, java.lang.String valueIfNull)Gets the canonical name minus the package name for an
Object.- Parameters:
object- the class to get the short name for, may be nullvalueIfNull- the value to return if null- Returns:
- the canonical name of the object without the package name, or the null value
- Since:
- 2.4
-
getCanonicalName
public static java.lang.String getCanonicalName(java.lang.Class<?> cls)
Gets the canonical class name for a
Class.- Parameters:
cls- the class for which to get the canonical class name; may be null- Returns:
- the canonical name of the class, or the empty String
- Since:
- 3.7
- See Also:
Class.getCanonicalName()
-
getCanonicalName
public static java.lang.String getCanonicalName(java.lang.Class<?> cls, java.lang.String valueIfNull)Gets the canonical name for a
Class.- Parameters:
cls- the class for which to get the canonical class name; may be nullvalueIfNull- the return value if null- Returns:
- the canonical name of the class, or
valueIfNull - Since:
- 3.7
- See Also:
Class.getCanonicalName()
-
getCanonicalName
public static java.lang.String getCanonicalName(java.lang.Object object)
Gets the canonical name for an
Object.- Parameters:
object- the object for which to get the canonical class name; may be null- Returns:
- the canonical name of the object, or the empty String
- Since:
- 3.7
- See Also:
Class.getCanonicalName()
-
getCanonicalName
public static java.lang.String getCanonicalName(java.lang.Object object, java.lang.String valueIfNull)Gets the canonical name for an
Object.- Parameters:
object- the object for which to get the canonical class name; may be nullvalueIfNull- the return value if null- Returns:
- the canonical name of the object or
valueIfNull - Since:
- 3.7
- See Also:
Class.getCanonicalName()
-
getShortCanonicalName
public static java.lang.String getShortCanonicalName(java.lang.Class<?> cls)
Gets the canonical name minus the package name from a
Class.- Parameters:
cls- the class for which to get the short canonical class name; may be null- Returns:
- the canonical name without the package name or an empty string
- Since:
- 2.4
-
getShortCanonicalName
public static java.lang.String getShortCanonicalName(java.lang.String canonicalName)
Gets the canonical name minus the package name from a String.
The string passed in is assumed to be a canonical name - it is not checked.
- Parameters:
canonicalName- the class name to get the short name for- Returns:
- the canonical name of the class without the package name or an empty string
- Since:
- 2.4
-
getPackageCanonicalName
public static java.lang.String getPackageCanonicalName(java.lang.Object object, java.lang.String valueIfNull)Gets the package name from the canonical name of an
Object.- Parameters:
object- the class to get the package name for, may be nullvalueIfNull- the value to return if null- Returns:
- the package name of the object, or the null value
- Since:
- 2.4
-
getPackageCanonicalName
public static java.lang.String getPackageCanonicalName(java.lang.Class<?> cls)
Gets the package name from the canonical name of a
Class.- Parameters:
cls- the class to get the package name for, may benull.- Returns:
- the package name or an empty string
- Since:
- 2.4
-
getPackageCanonicalName
public static java.lang.String getPackageCanonicalName(java.lang.String canonicalName)
Gets the package name from the canonical name.
The string passed in is assumed to be a canonical name - it is not checked.
If the class is unpackaged, return an empty string.
- Parameters:
canonicalName- the canonical name to get the package name for, may benull- Returns:
- the package name or an empty string
- Since:
- 2.4
-
getCanonicalName
private static java.lang.String getCanonicalName(java.lang.String className)
Converts a given name of class into canonical format. If name of class is not a name of array class it returns unchanged name.
Example:
getCanonicalName("[I") = "int[]"getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"getCanonicalName("java.lang.String") = "java.lang.String"
- Parameters:
className- the name of class- Returns:
- canonical form of class name
- Since:
- 2.4
-
hierarchy
public static java.lang.Iterable<java.lang.Class<?>> hierarchy(java.lang.Class<?> type)
Get anIterablethat can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.- Parameters:
type- the type to get the class hierarchy from- Returns:
- Iterable an Iterable over the class hierarchy of the given class
- Since:
- 3.2
-
hierarchy
public static java.lang.Iterable<java.lang.Class<?>> hierarchy(java.lang.Class<?> type, ClassUtils.Interfaces interfacesBehavior)Get anIterablethat can iterate over a class hierarchy in ascending (subclass to superclass) order.- Parameters:
type- the type to get the class hierarchy frominterfacesBehavior- switch indicating whether to include or exclude interfaces- Returns:
- Iterable an Iterable over the class hierarchy of the given class
- Since:
- 3.2
-
-