-
public class ClassUtilsOperates on classes without using reflection.
This class handles invalid
{@code null}inputs as best it can. Each method documents its behaviour in more detail.The notion of a
{@code canonical name}includes the human readable name for the type, for example{@code int[]}. The non-canonical method variants work with the JVM names, such as{@code [I}.
-
-
Field Summary
Fields Modifier and Type Field Description public final static charPACKAGE_SEPARATOR_CHARpublic final static StringPACKAGE_SEPARATORpublic final static charINNER_CLASS_SEPARATOR_CHARpublic final static StringINNER_CLASS_SEPARATOR
-
Constructor Summary
Constructors Constructor Description ClassUtils()ClassUtils instances should NOT be constructed in standard programming.
-
Method Summary
Modifier and Type Method Description static StringgetShortClassName(Object object, String valueIfNull)Gets the class name minus the package name for an {@code Object}.static StringgetShortClassName(Class<out Object> cls)Gets the class name minus the package name from a {@code Class}.static StringgetShortClassName(String className)Gets the class name minus the package name from a String. static StringgetSimpleName(Class<out Object> cls)Null-safe version of aClass.getSimpleName()static StringgetSimpleName(Object object, String valueIfNull)Null-safe version of aClass.getSimpleName()static StringgetPackageName(Object object, String valueIfNull)Gets the package name of an {@code Object}.static StringgetPackageName(Class<out Object> cls)Gets the package name of a {@code Class}.static StringgetPackageName(String className)Gets the package name from a {@code String}.static List<Class<out Object>>getAllSuperclasses(Class<out Object> cls)Gets a {@code List}of superclasses for the given class.static List<Class<out Object>>getAllInterfaces(Class<out Object> cls)Gets a {@code List}of all interfaces implemented by the givenclass and its superclasses.static List<Class<out Object>>convertClassNamesToClasses(List<String> classNames)Given a {@code List}of class names, this method converts them into classes.static List<String>convertClassesToClassNames(List<Class<out Object>> classes)Given a {@code List}of{@code Class}objects, this method convertsthem into class names.static booleanisAssignable(Array<Class<out Object>> classArray, Array<Class<out Object>> toClassArray)Checks if an array of Classes can be assigned to another array of Classes. static booleanisAssignable(Array<Class<out Object>> classArray, Array<Class<out Object>> toClassArray, boolean autoboxing)Checks if an array of Classes can be assigned to another array of Classes. static booleanisPrimitiveOrWrapper(Class<out Object> type)Returns whether the given {@code type}is a primitive or primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).static booleanisPrimitiveWrapper(Class<out Object> type)Returns whether the given {@code type}is a primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).static booleanisAssignable(Class<out Object> cls, Class<out Object> toClass)Checks if one {@code Class}can be assigned to a variable ofanother{@code Class}.static booleanisAssignable(Class<out Object> cls, Class<out Object> toClass, boolean autoboxing)Checks if one {@code Class}can be assigned to a variable ofanother{@code Class}.static Class<out Object>primitiveToWrapper(Class<out Object> cls)Converts the specified primitive Class object to its correspondingwrapper Class object. static Array<Class<out Object>>primitivesToWrappers(Array<Class<out Object>> classes)Converts the specified array of primitive Class objects to an array ofits corresponding wrapper Class objects. static Class<out Object>wrapperToPrimitive(Class<out Object> cls)Converts the specified wrapper class to its corresponding primitiveclass. static Array<Class<out Object>>wrappersToPrimitives(Array<Class<out Object>> classes)Converts the specified array of wrapper Class objects to an array ofits corresponding primitive Class objects. static booleanisInnerClass(Class<out Object> cls)Is the specified class an inner class or static nested class. static Class<out Object>getClass(ClassLoader classLoader, String className, boolean initialize)Returns the class represented by {@code className}using the{@code classLoader}.static Class<out Object>getClass(ClassLoader classLoader, String className)Returns the (initialized) class represented by {@code className}using the{@code classLoader}.static Class<out Object>getClass(String className)Returns the (initialized) class represented by {@code className}using the current thread's context class loader.static Class<out Object>getClass(String className, boolean initialize)Returns the class represented by {@code className}using thecurrent thread's context class loader.static MethodgetPublicMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> parameterTypes)Returns the desired Method much like {@code Class.getMethod}, howeverit ensures that the returned Method is from a public class or interface and notfrom an anonymous inner class.static Array<Class<out Object>>toClass(Array<Object> array)Converts an array of {@code Object}in to an array of{@code Class}objects.If any of these objects is null, a null element will be inserted into the array.static StringgetShortCanonicalName(Object object, String valueIfNull)Gets the canonical name minus the package name for an {@code Object}.static StringgetShortCanonicalName(Class<out Object> cls)Gets the canonical name minus the package name from a {@code Class}.static StringgetShortCanonicalName(String canonicalName)Gets the canonical name minus the package name from a String. static StringgetPackageCanonicalName(Object object, String valueIfNull)Gets the package name from the canonical name of an {@code Object}.static StringgetPackageCanonicalName(Class<out Object> cls)Gets the package name from the canonical name of a {@code Class}.static StringgetPackageCanonicalName(String canonicalName)Gets the package name from the canonical name. -
-
Method Detail
-
getShortClassName
static String getShortClassName(Object object, String valueIfNull)
Gets the class name minus the package name for an
{@code Object}.- Parameters:
object- the class to get the short name for, may be nullvalueIfNull- the value to return if null
-
getShortClassName
static String getShortClassName(Class<out Object> cls)
Gets the class name minus the package name from a
{@code Class}.Consider using the Java 5 API getSimpleName instead. The one known difference is that this code will return
{@code "Map.Entry"}while the{@code java.lang.Class}variant will simply return{@code "Entry"}.- Parameters:
cls- the class to get the short name for.
-
getShortClassName
static String getShortClassName(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
{@code "Map.Entry"}whilst the{@code java.lang.Class}variant will simply return{@code "Entry"}.- Parameters:
className- the className to get the short name for
-
getSimpleName
static String getSimpleName(Class<out Object> cls)
Null-safe version of
aClass.getSimpleName()- Parameters:
cls- the class for which to get the simple name.
-
getSimpleName
static String getSimpleName(Object object, String valueIfNull)
Null-safe version of
aClass.getSimpleName()- Parameters:
object- the object for which to get the simple class name.valueIfNull- the value to return ifobjectisnull
-
getPackageName
static String getPackageName(Object object, String valueIfNull)
Gets the package name of an
{@code Object}.- Parameters:
object- the class to get the package name for, may be nullvalueIfNull- the value to return if null
-
getPackageName
static String getPackageName(Class<out Object> cls)
Gets the package name of a
{@code Class}.- Parameters:
cls- the class to get the package name for, may be{@code null}.
-
getPackageName
static String getPackageName(String className)
Gets the package name from a
{@code 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 be{@code null}
-
getAllSuperclasses
static List<Class<out Object>> getAllSuperclasses(Class<out Object> cls)
Gets a
{@code List}of superclasses for the given class.- Parameters:
cls- the class to look up, may be{@code null}
-
getAllInterfaces
static List<Class<out Object>> getAllInterfaces(Class<out Object> cls)
Gets a
{@code List}of all interfaces implemented by the givenclass and its superclasses.The order is determined by looking through each interface in turn asdeclared in the source file and following its hierarchy up. Then eachsuperclass is considered in the same way. Later duplicates are ignored,so the order is maintained.
- Parameters:
cls- the class to look up, may be{@code null}
-
convertClassNamesToClasses
static List<Class<out Object>> convertClassNamesToClasses(List<String> classNames)
Given a
{@code List}of class names, this method converts them into classes.A new
{@code List}is returned. If the class name cannot be found,{@code null}is stored in the{@code List}. If the class name in the{@code List}is{@code null},{@code null}is stored in the output{@code List}.- Parameters:
classNames- the classNames to change
-
convertClassesToClassNames
static List<String> convertClassesToClassNames(List<Class<out Object>> classes)
Given a
{@code List}of{@code Class}objects, this method convertsthem into class names.A new
{@code List}is returned.{@code null}objects will be copied intothe returned list as{@code null}.- Parameters:
classes- the classes to change
-
isAssignable
static boolean isAssignable(Array<Class<out Object>> classArray, Array<Class<out Object>> toClassArray)
Checks if an array of Classes can be assigned to another array of Classes.
This method calls isAssignable for eachClass 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 isAssignableFrom method, thismethod takes into account widenings of primitive classes and
{@code null}s.Primitive widenings allow an int to be assigned to a
{@code long},{@code float}or{@code double}. This method returns the correctresult for these cases.{@code Null}may be assigned to any reference type. This method willreturn{@code true}if{@code null}is passed in and the toClass isnon-primitive.Specifically, this method tests whether the type represented by thespecified
{@code Class}parameter can be converted to the typerepresented by this{@code Class}object via an identity conversionwidening primitive or widening reference conversion. SeeThe 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 forcalculating assignability between primitive and wrapper types correspondingto the running Java version; i.e. autoboxing will be the defaultbehavior in VMs running Java versions >= 1.5.
- Parameters:
classArray- the array of Classes to check, may be{@code null}toClassArray- the array of Classes to try to assign into, may be{@code null}
-
isAssignable
static boolean isAssignable(Array<Class<out Object>> classArray, Array<Class<out Object>> toClassArray, boolean autoboxing)
Checks if an array of Classes can be assigned to another array of Classes.
This method calls isAssignable for eachClass 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 isAssignableFrom method, thismethod takes into account widenings of primitive classes and
{@code null}s.Primitive widenings allow an int to be assigned to a
{@code long},{@code float}or{@code double}. This method returns the correctresult for these cases.{@code Null}may be assigned to any reference type. This method willreturn{@code true}if{@code null}is passed in and the toClass isnon-primitive.Specifically, this method tests whether the type represented by thespecified
{@code Class}parameter can be converted to the typerepresented by this{@code Class}object via an identity conversionwidening primitive or widening reference conversion. SeeThe 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 be{@code null}toClassArray- the array of Classes to try to assign into, may be{@code null}autoboxing- whether to use implicit autoboxing/unboxing between primitives and wrappers
-
isPrimitiveOrWrapper
static boolean isPrimitiveOrWrapper(Class<out Object> type)
Returns whether the given
{@code type}is a primitive or primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).- Parameters:
type- The class to query or null.
-
isPrimitiveWrapper
static boolean isPrimitiveWrapper(Class<out Object> type)
Returns whether the given
{@code type}is a primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).- Parameters:
type- The class to query or null.
-
isAssignable
static boolean isAssignable(Class<out Object> cls, Class<out Object> toClass)
Checks if one
{@code Class}can be assigned to a variable ofanother{@code Class}.Unlike the isAssignableFrom method,this method takes into account widenings of primitive classes and
{@code null}s.Primitive widenings allow an int to be assigned to a long, float ordouble. This method returns the correct result for these cases.
{@code Null}may be assigned to any reference type. This methodwill return{@code true}if{@code null}is passed in and thetoClass is non-primitive.Specifically, this method tests whether the type represented by thespecified
{@code Class}parameter can be converted to the typerepresented by this{@code Class}object via an identity conversionwidening primitive or widening reference conversion. SeeThe 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 forcalculating assignability between primitive and wrapper types correspondingto the running Java version; i.e. autoboxing will be the defaultbehavior 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
-
isAssignable
static boolean isAssignable(Class<out Object> cls, Class<out Object> toClass, boolean autoboxing)
Checks if one
{@code Class}can be assigned to a variable ofanother{@code Class}.Unlike the isAssignableFrom method,this method takes into account widenings of primitive classes and
{@code null}s.Primitive widenings allow an int to be assigned to a long, float ordouble. This method returns the correct result for these cases.
{@code Null}may be assigned to any reference type. This methodwill return{@code true}if{@code null}is passed in and thetoClass is non-primitive.Specifically, this method tests whether the type represented by thespecified
{@code Class}parameter can be converted to the typerepresented by this{@code Class}object via an identity conversionwidening primitive or widening reference conversion. SeeThe 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
-
primitiveToWrapper
static Class<out Object> primitiveToWrapper(Class<out Object> cls)
Converts the specified primitive Class object to its correspondingwrapper Class object.
NOTE: From v2.2, this method handles
{@code Void.TYPE},returning{@code Void.TYPE}.- Parameters:
cls- the class to convert, may be null
-
primitivesToWrappers
static Array<Class<out Object>> primitivesToWrappers(Array<Class<out Object>> classes)
Converts the specified array of primitive Class objects to an array ofits corresponding wrapper Class objects.
- Parameters:
classes- the class array to convert, may be null or empty
-
wrapperToPrimitive
static Class<out Object> wrapperToPrimitive(Class<out Object> cls)
Converts the specified wrapper class to its corresponding primitiveclass.
This method is the counter part of
{@code primitiveToWrapper()}.If the passed in class is a wrapper class for a primitive type, thisprimitive type will be returned (e.g.{@code Integer.TYPE}for{@code Integer.class}). For other classes, or if the parameter isnull, the return value is null.- Parameters:
cls- the class to convert, may be null
-
wrappersToPrimitives
static Array<Class<out Object>> wrappersToPrimitives(Array<Class<out Object>> classes)
Converts the specified array of wrapper Class objects to an array ofits corresponding primitive Class objects.
This method invokes
{@code wrapperToPrimitive()}for each elementof the passed in array.- Parameters:
classes- the class array to convert, may be null or empty
-
isInnerClass
static boolean isInnerClass(Class<out Object> cls)
Is the specified class an inner class or static nested class.
- Parameters:
cls- the class to check, may be null
-
getClass
static Class<out Object> getClass(ClassLoader classLoader, String className, boolean initialize)
Returns the class represented by
{@code className}using the{@code classLoader}. This implementation supports the syntaxes"{@code java.util.Map.Entry[]}", "{@code java.util.Map$Entry[]}","{@code [Ljava.util.Map.Entry;}", and "{@code [Ljava.util.Map$Entry;}".- Parameters:
classLoader- the class loader to use to load the classclassName- the class nameinitialize- whether the class must be initialized
-
getClass
static Class<out Object> getClass(ClassLoader classLoader, String className)
Returns the (initialized) class represented by
{@code className}using the{@code classLoader}. This implementation supportsthe syntaxes "{@code java.util.Map.Entry[]}","{@code java.util.Map$Entry[]}", "{@code [Ljava.util.Map.Entry;}",and "{@code [Ljava.util.Map$Entry;}".- Parameters:
classLoader- the class loader to use to load the classclassName- the class name
-
getClass
static Class<out Object> getClass(String className)
Returns the (initialized) class represented by
{@code className}using the current thread's context class loader. This implementationsupports the syntaxes "{@code java.util.Map.Entry[]}","{@code java.util.Map$Entry[]}", "{@code [Ljava.util.Map.Entry;}",and "{@code [Ljava.util.Map$Entry;}".- Parameters:
className- the class name
-
getClass
static Class<out Object> getClass(String className, boolean initialize)
Returns the class represented by
{@code className}using thecurrent thread's context class loader. This implementation supports thesyntaxes "{@code java.util.Map.Entry[]}", "{@code java.util.Map$Entry[]}","{@code [Ljava.util.Map.Entry;}", and "{@code [Ljava.util.Map$Entry;}".- Parameters:
className- the class nameinitialize- whether the class must be initialized
-
getPublicMethod
static Method getPublicMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> parameterTypes)
Returns the desired Method much like
{@code Class.getMethod}, howeverit ensures that the returned Method is from a public class or interface and notfrom an anonymous inner class. This means that the Method is invokable anddoesn't fall foul of Java bug4071957).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
-
toClass
static Array<Class<out Object>> toClass(Array<Object> array)
Converts an array of
{@code Object}in to an array of{@code Class}objects.If any of these objects is null, a null element will be inserted into the array.This method returns
{@code null}for a{@code null}input array.- Parameters:
array- an{@code Object}array
-
getShortCanonicalName
static String getShortCanonicalName(Object object, String valueIfNull)
Gets the canonical name minus the package name for an
{@code Object}.- Parameters:
object- the class to get the short name for, may be nullvalueIfNull- the value to return if null
-
getShortCanonicalName
static String getShortCanonicalName(Class<out Object> cls)
Gets the canonical name minus the package name from a
{@code Class}.- Parameters:
cls- the class to get the short name for.
-
getShortCanonicalName
static String getShortCanonicalName(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
-
getPackageCanonicalName
static String getPackageCanonicalName(Object object, String valueIfNull)
Gets the package name from the canonical name of an
{@code Object}.- Parameters:
object- the class to get the package name for, may be nullvalueIfNull- the value to return if null
-
getPackageCanonicalName
static String getPackageCanonicalName(Class<out Object> cls)
Gets the package name from the canonical name of a
{@code Class}.- Parameters:
cls- the class to get the package name for, may be{@code null}.
-
getPackageCanonicalName
static String getPackageCanonicalName(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 be{@code null}
-
-
-
-