public class ClassUtils
extends java.lang.Object
Operates on classes without using reflection.
This class handles invalid null
inputs as best it can.
Each method documents its behaviour in more detail.
The notion of a canonical name
includes the human
readable name for the type, for example int[]
. The
non-canonical method variants work with the JVM names, such as
[I
.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
INNER_CLASS_SEPARATOR
The inner class separator String:
"$" . |
static char |
INNER_CLASS_SEPARATOR_CHAR
The inner class separator character:
'$' == 36 . |
static java.lang.String |
PACKAGE_SEPARATOR
The package separator String:
"." . |
static char |
PACKAGE_SEPARATOR_CHAR
The package separator character:
'.' == 46 . |
Constructor and Description |
---|
ClassUtils()
ClassUtils instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static java.util.List |
convertClassesToClassNames(java.util.List classes)
Given a
List of Class objects, this method converts
them into class names. |
static java.util.List |
convertClassNamesToClasses(java.util.List classNames)
Given a
List of class names, this method converts them into classes. |
static java.util.List |
getAllInterfaces(java.lang.Class cls)
Gets a
List of all interfaces implemented by the given
class and its superclasses. |
static java.util.List |
getAllSuperclasses(java.lang.Class cls)
Gets a
List of superclasses for the given class. |
static java.lang.Class |
getClass(java.lang.ClassLoader classLoader,
java.lang.String className)
Returns the (initialized) class represented by
className
using the classLoader . |
static java.lang.Class |
getClass(java.lang.ClassLoader classLoader,
java.lang.String className,
boolean initialize)
Returns the class represented by
className using the
classLoader . |
static java.lang.Class |
getClass(java.lang.String className)
Returns the (initialized) class represented by
className
using the current thread's context class loader. |
static java.lang.Class |
getClass(java.lang.String className,
boolean initialize)
Returns the class represented by
className using the
current thread's context class loader. |
static java.lang.String |
getPackageCanonicalName(java.lang.Class cls)
Gets the package name from the canonical name of a
Class . |
static java.lang.String |
getPackageCanonicalName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the package name from the canonical name of an
Object . |
static java.lang.String |
getPackageCanonicalName(java.lang.String canonicalName)
Gets the package name from the canonical name.
|
static java.lang.String |
getPackageName(java.lang.Class cls)
Gets the package name of a
Class . |
static java.lang.String |
getPackageName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the package name of an
Object . |
static java.lang.String |
getPackageName(java.lang.String className)
Gets the package name from a
String . |
static java.lang.reflect.Method |
getPublicMethod(java.lang.Class cls,
java.lang.String methodName,
java.lang.Class[] parameterTypes)
Returns 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. |
static java.lang.String |
getShortCanonicalName(java.lang.Class cls)
Gets the canonical name minus the package name from a
Class . |
static java.lang.String |
getShortCanonicalName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the canonical name minus the package name for an
Object . |
static java.lang.String |
getShortCanonicalName(java.lang.String canonicalName)
Gets the canonical name minus the package name from a String.
|
static java.lang.String |
getShortClassName(java.lang.Class cls)
Gets the class name minus the package name from a
Class . |
static java.lang.String |
getShortClassName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the class name minus the package name for an
Object . |
static java.lang.String |
getShortClassName(java.lang.String className)
Gets the class name minus the package name from a String.
|
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.
|
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.
|
static boolean |
isAssignable(java.lang.Class cls,
java.lang.Class toClass)
Checks if one
Class can be assigned to a variable of
another Class . |
static boolean |
isAssignable(java.lang.Class cls,
java.lang.Class toClass,
boolean autoboxing)
Checks if one
Class can be assigned to a variable of
another Class . |
static boolean |
isInnerClass(java.lang.Class cls)
Is the specified class an inner class or static nested class.
|
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.
|
static java.lang.Class[] |
toClass(java.lang.Object[] array)
Converts an array of
Object in to an array of Class objects. |
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.
|
public static final char PACKAGE_SEPARATOR_CHAR
The package separator character: '.' == 46
.
public static final java.lang.String PACKAGE_SEPARATOR
The package separator String: "."
.
public static final char INNER_CLASS_SEPARATOR_CHAR
The inner class separator character: '$' == 36
.
public static final java.lang.String INNER_CLASS_SEPARATOR
The inner class separator String: "$"
.
public ClassUtils()
ClassUtils instances should NOT be constructed in standard programming.
Instead, the class should be used as
ClassUtils.getShortClassName(cls)
.
This constructor is public to permit tools that require a JavaBean instance to operate.
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
.
object
- the class to get the short name for, may be nullvalueIfNull
- the value to return if nullpublic static java.lang.String getShortClassName(java.lang.Class cls)
Gets the class name minus the package name from a Class
.
cls
- the class to get the short name for.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.
className
- the className to get the short name forpublic static java.lang.String getPackageName(java.lang.Object object, java.lang.String valueIfNull)
Gets the package name of an Object
.
object
- the class to get the package name for, may be nullvalueIfNull
- the value to return if nullpublic static java.lang.String getPackageName(java.lang.Class cls)
Gets the package name of a Class
.
cls
- the class to get the package name for, may be null
.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.
className
- the className to get the package name for, may be null
public static java.util.List getAllSuperclasses(java.lang.Class cls)
Gets a List
of superclasses for the given class.
cls
- the class to look up, may be null
List
of superclasses in order going up from this one
null
if null inputpublic static java.util.List getAllInterfaces(java.lang.Class cls)
Gets a List
of 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.
cls
- the class to look up, may be null
List
of interfaces in order,
null
if null inputpublic static java.util.List convertClassNamesToClasses(java.util.List classNames)
Given a List
of class names, this method converts them into classes.
A new List
is returned. If the class name cannot be found, null
is stored in the List
. If the class name in the List
is
null
, null
is stored in the output List
.
classNames
- the classNames to changeList
of Class objects corresponding to the class names,
null
if null inputjava.lang.ClassCastException
- if classNames contains a non String entrypublic static java.util.List convertClassesToClassNames(java.util.List classes)
Given a List
of Class
objects, this method converts
them into class names.
A new List
is returned. null
objects will be copied into
the returned list as null
.
classes
- the classes to changeList
of class names corresponding to the Class objects,
null
if null inputjava.lang.ClassCastException
- if classes
contains a non-Class
entrypublic 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 isAssignable
for 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 and
null
s.
Primitive widenings allow an int to be assigned to a long
,
float
or double
. This method returns the correct
result for these cases.
Null
may be assigned to any reference type. This method will
return true
if null
is passed in and the toClass is
non-primitive.
Specifically, this method tests whether the type represented by the
specified Class
parameter can be converted to the type
represented by this Class
object 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.
classArray
- the array of Classes to check, may be null
toClassArray
- the array of Classes to try to assign into, may be null
true
if assignment possiblepublic 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 isAssignable
for 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 and
null
s.
Primitive widenings allow an int to be assigned to a long
,
float
or double
. This method returns the correct
result for these cases.
Null
may be assigned to any reference type. This method will
return true
if null
is passed in and the toClass is
non-primitive.
Specifically, this method tests whether the type represented by the
specified Class
parameter can be converted to the type
represented by this Class
object 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.
classArray
- the array of Classes to check, may be null
toClassArray
- the array of Classes to try to assign into, may be null
autoboxing
- whether to use implicit autoboxing/unboxing between primitives and wrapperstrue
if assignment possiblepublic static boolean isAssignable(java.lang.Class cls, java.lang.Class toClass)
Checks if one Class
can be assigned to a variable of
another Class
.
Unlike the Class.isAssignableFrom(java.lang.Class)
method,
this method takes into account widenings of primitive classes and
null
s.
Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct result for these cases.
Null
may be assigned to any reference type. This method
will return true
if null
is passed in and the
toClass is non-primitive.
Specifically, this method tests whether the type represented by the
specified Class
parameter can be converted to the type
represented by this Class
object 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.
cls
- the Class to check, may be nulltoClass
- the Class to try to assign into, returns false if nulltrue
if assignment possiblepublic static boolean isAssignable(java.lang.Class cls, java.lang.Class toClass, boolean autoboxing)
Checks if one Class
can be assigned to a variable of
another Class
.
Unlike the Class.isAssignableFrom(java.lang.Class)
method,
this method takes into account widenings of primitive classes and
null
s.
Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct result for these cases.
Null
may be assigned to any reference type. This method
will return true
if null
is passed in and the
toClass is non-primitive.
Specifically, this method tests whether the type represented by the
specified Class
parameter can be converted to the type
represented by this Class
object 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.
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 wrapperstrue
if assignment possiblepublic 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
,
returning Void.TYPE
.
cls
- the class to convert, may be nullcls
or cls
if
cls
is not a primitive. null
if null input.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.
classes
- the class array to convert, may be null or emptynull
if null input.
Empty array if an empty array passed in.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.TYPE
for
Integer.class
). For other classes, or if the parameter is
null, the return value is null.
cls
- the class to convert, may be nullcls
is a
wrapper class, null otherwiseprimitiveToWrapper(Class)
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.
classes
- the class array to convert, may be null or emptynull
if null input.
Empty array if an empty array passed in.wrapperToPrimitive(Class)
public static boolean isInnerClass(java.lang.Class cls)
Is the specified class an inner class or static nested class.
cls
- the class to check, may be nulltrue
if the class is an inner or static nested class,
false if not or null
public static java.lang.Class getClass(java.lang.ClassLoader classLoader, java.lang.String className, boolean initialize) throws java.lang.ClassNotFoundException
className
using the
classLoader
. This implementation supports the syntaxes
"java.util.Map.Entry[]
", "java.util.Map$Entry[]
",
"[Ljava.util.Map.Entry;
", and "[Ljava.util.Map$Entry;
".classLoader
- the class loader to use to load the classclassName
- the class nameinitialize
- whether the class must be initializedclassName
using the classLoader
java.lang.ClassNotFoundException
- if the class is not foundpublic static java.lang.Class getClass(java.lang.ClassLoader classLoader, java.lang.String className) throws java.lang.ClassNotFoundException
className
using the classLoader
. This implementation supports
the syntaxes "java.util.Map.Entry[]
",
"java.util.Map$Entry[]
", "[Ljava.util.Map.Entry;
",
and "[Ljava.util.Map$Entry;
".classLoader
- the class loader to use to load the classclassName
- the class nameclassName
using the classLoader
java.lang.ClassNotFoundException
- if the class is not foundpublic static java.lang.Class getClass(java.lang.String className) throws java.lang.ClassNotFoundException
className
using 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;
".className
- the class nameclassName
using the current thread's context class loaderjava.lang.ClassNotFoundException
- if the class is not foundpublic static java.lang.Class getClass(java.lang.String className, boolean initialize) throws java.lang.ClassNotFoundException
className
using 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;
".className
- the class nameinitialize
- whether the class must be initializedclassName
using the current thread's context class loaderjava.lang.ClassNotFoundException
- if the class is not foundpublic static java.lang.reflect.Method getPublicMethod(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] parameterTypes) throws java.lang.SecurityException, java.lang.NoSuchMethodException
Returns 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[]);
cls
- the class to check, not nullmethodName
- the name of the methodparameterTypes
- the list of parametersjava.lang.NullPointerException
- if the class is nulljava.lang.SecurityException
- if a a security violation occuredjava.lang.NoSuchMethodException
- if the method is not found in the given class
or if the metothod doen't conform with the requirementspublic static java.lang.Class[] toClass(java.lang.Object[] array)
Converts an array of Object
in to an array of Class
objects.
If any of these objects is null, a null element will be inserted into the array.
This method returns null
for a null
input array.
array
- an Object
arrayClass
array, null
if null array inputpublic static java.lang.String getShortCanonicalName(java.lang.Object object, java.lang.String valueIfNull)
Gets the canonical name minus the package name for an Object
.
object
- the class to get the short name for, may be nullvalueIfNull
- the value to return if nullpublic static java.lang.String getShortCanonicalName(java.lang.Class cls)
Gets the canonical name minus the package name from a Class
.
cls
- the class to get the short name for.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.
canonicalName
- the class name to get the short name forpublic static java.lang.String getPackageCanonicalName(java.lang.Object object, java.lang.String valueIfNull)
Gets the package name from the canonical name of an Object
.
object
- the class to get the package name for, may be nullvalueIfNull
- the value to return if nullpublic static java.lang.String getPackageCanonicalName(java.lang.Class cls)
Gets the package name from the canonical name of a Class
.
cls
- the class to get the package name for, may be null
.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.
canonicalName
- the canonical name to get the package name for, may be null
Copyright © 2010 - 2020 Adobe. All Rights Reserved