public class ClassUtils
extends java.lang.Object
This class handles invalid null
inputs as best it can. Each method documents its behavior 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 | Class and Description |
---|---|
static class |
ClassUtils.Interfaces
Inclusivity literals for
hierarchy(Class, Interfaces) . |
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:
'$' == {@value} . |
static java.lang.String |
PACKAGE_SEPARATOR
The package separator String:
"." . |
static char |
PACKAGE_SEPARATOR_CHAR
The package separator character:
'.' == {@value} . |
Constructor and Description |
---|
ClassUtils()
ClassUtils instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static java.util.Comparator<java.lang.Class<?>> |
comparator()
Gets the class comparator, comparing by class name.
|
static java.util.List<java.lang.String> |
convertClassesToClassNames(java.util.List<java.lang.Class<?>> classes)
Given a
List of Class objects, this method converts them into class names. |
static java.util.List<java.lang.Class<?>> |
convertClassNamesToClasses(java.util.List<java.lang.String> classNames)
Given a
List of class names, this method converts them into classes. |
static java.lang.String |
getAbbreviatedName(java.lang.Class<?> cls,
int lengthHint)
Gets the abbreviated name of a
Class . |
static java.lang.String |
getAbbreviatedName(java.lang.String className,
int lengthHint)
Gets the abbreviated class name from a
String . |
static java.util.List<java.lang.Class<?>> |
getAllInterfaces(java.lang.Class<?> cls)
Gets a
List of all interfaces implemented by the given class and its superclasses. |
static java.util.List<java.lang.Class<?>> |
getAllSuperclasses(java.lang.Class<?> cls)
Gets a
List of superclasses for the given class. |
static java.lang.String |
getCanonicalName(java.lang.Class<?> cls)
Gets the canonical class name for a
Class . |
static java.lang.String |
getCanonicalName(java.lang.Class<?> cls,
java.lang.String valueIfNull)
Gets the canonical name for a
Class . |
static java.lang.String |
getCanonicalName(java.lang.Object object)
Gets the canonical name for an
Object . |
static java.lang.String |
getCanonicalName(java.lang.Object object,
java.lang.String valueIfNull)
Gets the canonical name for an
Object . |
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 <T> java.lang.Class<T> |
getComponentType(java.lang.Class<T[]> cls)
Delegates to
Class.getComponentType() using generics. |
static java.lang.String |
getName(java.lang.Class<?> cls)
Null-safe version of
cls.getName() |
static java.lang.String |
getName(java.lang.Class<?> cls,
java.lang.String valueIfNull)
Null-safe version of
cls.getName() |
static java.lang.String |
getName(java.lang.Object object)
Null-safe version of
object.getClass().getName() |
static java.lang.String |
getName(java.lang.Object object,
java.lang.String valueIfNull)
Null-safe version of
object.getClass().getSimpleName() |
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 class name of an
Object . |
static java.lang.String |
getPackageCanonicalName(java.lang.String name)
Gets the package name from the class 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 of the
object without the package name or names. |
static java.lang.String |
getShortClassName(java.lang.String className)
Gets the class name minus the package name from a String.
|
static java.lang.String |
getSimpleName(java.lang.Class<?> cls)
Null-safe version of
cls.getSimpleName() |
static java.lang.String |
getSimpleName(java.lang.Class<?> cls,
java.lang.String valueIfNull)
Null-safe version of
cls.getSimpleName() |
static java.lang.String |
getSimpleName(java.lang.Object object)
Null-safe version of
object.getClass().getSimpleName() |
static java.lang.String |
getSimpleName(java.lang.Object object,
java.lang.String valueIfNull)
Null-safe version of
object.getClass().getSimpleName() |
static java.lang.Iterable<java.lang.Class<?>> |
hierarchy(java.lang.Class<?> type)
Gets an
Iterable that 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)
Gets an
Iterable that can iterate over a class hierarchy in ascending (subclass to superclass) order. |
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 boolean |
isPrimitiveOrWrapper(java.lang.Class<?> type)
Returns whether the given
type is a primitive or primitive wrapper (Boolean , Byte ,
Character , Short , Integer , Long , Double , Float ). |
static boolean |
isPrimitiveWrapper(java.lang.Class<?> type)
Returns whether the given
type is a primitive wrapper (Boolean , Byte , Character ,
Short , Integer , Long , Double , Float ). |
static boolean |
isPublic(java.lang.Class<?> cls)
Tests whether a
Class is 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.
|
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
'.' == {@value}
.public static final java.lang.String PACKAGE_SEPARATOR
"."
.public static final char INNER_CLASS_SEPARATOR_CHAR
'$' == {@value}
.public static final java.lang.String INNER_CLASS_SEPARATOR
"$"
.public ClassUtils()
ClassUtils.getShortClassName(cls)
.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static java.util.Comparator<java.lang.Class<?>> comparator()
public static java.util.List<java.lang.String> convertClassesToClassNames(java.util.List<java.lang.Class<?>> classes)
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 java.util.List<java.lang.Class<?>> convertClassNamesToClasses(java.util.List<java.lang.String> classNames)
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.lang.String getAbbreviatedName(java.lang.Class<?> cls, int lengthHint)
Class
.cls
- the class to get the abbreviated name for, may be null
lengthHint
- the desired length of the abbreviated namejava.lang.IllegalArgumentException
- if len <= 0getAbbreviatedName(String, int)
public static java.lang.String getAbbreviatedName(java.lang.String className, int lengthHint)
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. The abbreviated package names will be shortened to a single character.
Only package names are shortened, the class simple name remains untouched. (See examples.)
The result will be longer than the desired length only if all the package names shortened to a single character plus the class simple name with the separating dots together are longer than the desired length. In other words, when the class name cannot be shortened to the desired length.
If the class name can be shortened then the final length will be at most lengthHint
characters.
If the lengthHint
is zero or negative then the method throws exception. If you want to achieve the shortest
possible version then use 1
as a lengthHint
.
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" |
"org.apache.commons.lang3.ClassUtils" | 18 | "o.a.c.l.ClassUtils" |
className
- the className to get the abbreviated name for, may be null
lengthHint
- the desired length of the abbreviated namenull
or empty string. The
abbreviated name may be longer than the desired length if it cannot be abbreviated to the desired length.java.lang.IllegalArgumentException
- if len <= 0
public static java.util.List<java.lang.Class<?>> getAllInterfaces(java.lang.Class<?> cls)
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<java.lang.Class<?>> getAllSuperclasses(java.lang.Class<?> cls)
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.lang.String getCanonicalName(java.lang.Class<?> cls)
Class
.cls
- the class for which to get the canonical class name; may be nullClass.getCanonicalName()
public static java.lang.String getCanonicalName(java.lang.Class<?> cls, java.lang.String valueIfNull)
Class
.cls
- the class for which to get the canonical class name; may be nullvalueIfNull
- the return value if nullvalueIfNull
Class.getCanonicalName()
public static java.lang.String getCanonicalName(java.lang.Object object)
Object
.object
- the object for which to get the canonical class name; may be nullClass.getCanonicalName()
public static java.lang.String getCanonicalName(java.lang.Object object, java.lang.String valueIfNull)
Object
.object
- the object for which to get the canonical class name; may be nullvalueIfNull
- the return value if nullvalueIfNull
Class.getCanonicalName()
public 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.NullPointerException
- if the className is nulljava.lang.ClassNotFoundException
- if the class is not foundpublic 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.NullPointerException
- if the className is nulljava.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.NullPointerException
- if the className is nulljava.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.NullPointerException
- if the className is nulljava.lang.ClassNotFoundException
- if the class is not foundpublic static <T> java.lang.Class<T> getComponentType(java.lang.Class<T[]> cls)
Class.getComponentType()
using generics.T
- The array class type.cls
- A class or null.Class.getComponentType()
public static java.lang.String getName(java.lang.Class<?> cls)
cls.getName()
cls
- the class for which to get the class name; may be nullnull
Class.getSimpleName()
public static java.lang.String getName(java.lang.Class<?> cls, java.lang.String valueIfNull)
cls.getName()
cls
- the class for which to get the class name; may be nullvalueIfNull
- the return value if the argument cls
is null
valueIfNull
Class.getName()
public static java.lang.String getName(java.lang.Object object)
object.getClass().getName()
object
- the object for which to get the class name; may be nullClass.getSimpleName()
public static java.lang.String getName(java.lang.Object object, java.lang.String valueIfNull)
object.getClass().getSimpleName()
object
- the object for which to get the class name; may be nullvalueIfNull
- the value to return if object
is null
valueIfNull
Class.getName()
public static java.lang.String getPackageCanonicalName(java.lang.Class<?> cls)
Class
.cls
- the class to get the package name for, may be null
.public static java.lang.String getPackageCanonicalName(java.lang.Object object, java.lang.String valueIfNull)
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.String name)
The string passed in is assumed to be a class name - it is not checked.
If the class is in the default package, return an empty string.
name
- the name to get the package name for, may be null
public static java.lang.String getPackageName(java.lang.Class<?> cls)
Class
.cls
- the class to get the package name for, may be null
.public static java.lang.String getPackageName(java.lang.Object object, java.lang.String valueIfNull)
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.String className)
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.lang.reflect.Method getPublicMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException
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 security violation occurredjava.lang.NoSuchMethodException
- if the method is not found in the given class or if the method doesn't conform with the
requirementspublic static java.lang.String getShortCanonicalName(java.lang.Class<?> cls)
Class
.cls
- the class for which to get the short canonical class name; may be nullClass.getCanonicalName()
public static java.lang.String getShortCanonicalName(java.lang.Object object, java.lang.String valueIfNull)
Object
.object
- the class to get the short name for, may be nullvalueIfNull
- the value to return if nullClass.getCanonicalName()
public static java.lang.String getShortCanonicalName(java.lang.String canonicalName)
The string passed in is assumed to be a class name - it is not checked.
Note that this method is mainly designed to handle the arrays and primitives properly. If the class is an inner class
then the result value will not contain the outer classes. This way the behavior of this method is different from
getShortClassName(String)
. The argument in that case is class name and not canonical name and the return
value retains the outer classes.
Note that there is no way to reliably identify the part of the string representing the package hierarchy and the part that is the outer class or classes in case of an inner class. Trying to find the class would require reflective call and the class itself may not even be on the class path. Relying on the fact that class names start with capital letter and packages with lower case is heuristic.
It is recommended to use getShortClassName(String)
for cases when the class is an inner class and use this
method for cases it is designed for.
return value | input |
"" |
(String)null |
"Map.Entry" |
java.util.Map.Entry.class.getName() |
"Entry" |
java.util.Map.Entry.class.getCanonicalName() |
"ClassUtils" |
"org.apache.commons.lang3.ClassUtils" |
"ClassUtils[]" |
"[Lorg.apache.commons.lang3.ClassUtils;" |
"ClassUtils[][]" |
"[[Lorg.apache.commons.lang3.ClassUtils;" |
"ClassUtils[]" |
"org.apache.commons.lang3.ClassUtils[]" |
"ClassUtils[][]" |
"org.apache.commons.lang3.ClassUtils[][]" |
"int[]" |
"[I" |
"int[]" |
int[].class.getCanonicalName() |
"int[]" |
int[].class.getName() |
"int[][]" |
"[[I" |
"int[]" |
"int[]" |
"int[][]" |
"int[][]" |
canonicalName
- the class name to get the short name forpublic static java.lang.String getShortClassName(java.lang.Class<?> cls)
Class
.
This method simply gets the name using Class.getName()
and then calls getShortClassName(Class)
. See
relevant notes there.
cls
- the class to get the short name for..
(dot) character.public static java.lang.String getShortClassName(java.lang.Object object, java.lang.String valueIfNull)
object
without the package name or names.
The method looks up the class of the object and then converts the name of the class invoking
getShortClassName(Class)
(see relevant notes there).
object
- the class to get the short name for, may be null
valueIfNull
- the value to return if the object is null
valueIfNull
if the argument object
is null
public static java.lang.String getShortClassName(java.lang.String className)
The string passed in is assumed to be a class name - it is not checked. The string has to be formatted the way as the
JDK method Class.getName()
returns it, and not the usual way as we write it, for example in import
statements, or as it is formatted by Class.getCanonicalName()
.
The difference is is significant only in case of classes that are inner classes of some other classes. In this case
the separator between the outer and inner class (possibly on multiple hierarchy level) has to be $
(dollar
sign) and not .
(dot), as it is returned by Class.getName()
Note that this method is called from the getShortClassName(Class)
method using the string returned by
Class.getName()
.
Note that this method differs from getSimpleName(Class)
in that this will return, for example
"Map.Entry"
whilst the java.lang.Class
variant will simply return "Entry"
. In this example
the argument className
is the string java.util.Map$Entry
(note the $
sign.
className
- the className to get the short name for. It has to be formatted as returned by
Class.getName()
and not Class.getCanonicalName()
.
(dot) character.public static java.lang.String getSimpleName(java.lang.Class<?> cls)
cls.getSimpleName()
cls
- the class for which to get the simple name; may be nullnull
Class.getSimpleName()
public static java.lang.String getSimpleName(java.lang.Class<?> cls, java.lang.String valueIfNull)
cls.getSimpleName()
cls
- the class for which to get the simple name; may be nullvalueIfNull
- the value to return if nullvalueIfNull
if the argument cls
is null
Class.getSimpleName()
public static java.lang.String getSimpleName(java.lang.Object object)
object.getClass().getSimpleName()
It is to note that this method is overloaded and in case the argument object
is a Class
object then
the getSimpleName(Class)
will be invoked. If this is a significant possibility then the caller should check
this case and call getSimpleName(Class.class)
or just simply use the string literal "Class"
, which is the result of the method
in that case.
object
- the object for which to get the simple class name; may be nullnull
Class.getSimpleName()
public static java.lang.String getSimpleName(java.lang.Object object, java.lang.String valueIfNull)
object.getClass().getSimpleName()
object
- the object for which to get the simple class name; may be nullvalueIfNull
- the value to return if object
is null
valueIfNull
if the argument object
is null
Class.getSimpleName()
public static java.lang.Iterable<java.lang.Class<?>> hierarchy(java.lang.Class<?> type)
Iterable
that can iterate over a class hierarchy in ascending (subclass to superclass) order,
excluding interfaces.type
- the type to get the class hierarchy frompublic static java.lang.Iterable<java.lang.Class<?>> hierarchy(java.lang.Class<?> type, ClassUtils.Interfaces interfacesBehavior)
Iterable
that can iterate over a class hierarchy in ascending (subclass to superclass) order.type
- the type to get the class hierarchy frominterfacesBehavior
- switch indicating whether to include or exclude interfacespublic static boolean isAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass)
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.
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.
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)
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 boolean isAssignable(java.lang.Class<?>[] classArray, java.lang.Class<?>... toClassArray)
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.
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.
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)
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 isInnerClass(java.lang.Class<?> cls)
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 boolean isPublic(java.lang.Class<?> cls)
Class
is public.cls
- Class to test.true
if cls
is public.public static boolean isPrimitiveOrWrapper(java.lang.Class<?> type)
type
is a primitive or primitive wrapper (Boolean
, Byte
,
Character
, Short
, Integer
, Long
, Double
, Float
).type
- The class to query or null.type
is a primitive or primitive wrapper (Boolean
, Byte
,
Character
, Short
, Integer
, Long
, Double
, Float
).public static boolean isPrimitiveWrapper(java.lang.Class<?> type)
type
is a primitive wrapper (Boolean
, Byte
, Character
,
Short
, Integer
, Long
, Double
, Float
).type
- The class to query or null.type
is a primitive wrapper (Boolean
, Byte
, Character
,
Short
, Integer
, Long
, Double
, Float
).public static java.lang.Class<?>[] primitivesToWrappers(java.lang.Class<?>... classes)
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<?> primitiveToWrapper(java.lang.Class<?> cls)
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<?>[] toClass(java.lang.Object... array)
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.Class<?>[] wrappersToPrimitives(java.lang.Class<?>... classes)
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 java.lang.Class<?> wrapperToPrimitive(java.lang.Class<?> cls)
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)
Copyright © 2010 - 2023 Adobe. All Rights Reserved