ClassUtils
- public class org.apache.commons.lang3.ClassUtils[source]
Operates on classes without using reflection.
This class handles invalid
nullinputs as best it can. Each method documents its behavior 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
Constructors
Deprecated. ClassUtils instances should NOT be constructed in standard programming. |
- @Deprecated public ClassUtils()[source]
Deprecated: TODO Make private in 4.0.
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.
Fields and constants
The inner class separator String: |
|
The inner class separator character: |
|
The package separator String: |
|
The package separator character: |
Methods
Gets the class comparator, comparing by class name. |
|
Given a |
|
Given a |
|
Gets the abbreviated name of a |
|
Gets the abbreviated class name from a |
|
Gets a |
|
Gets a |
|
Gets the canonical class name for a |
|
Gets the canonical name for a |
|
Gets the canonical name for an |
|
Gets the canonical name for an |
|
Gets the (initialized) class represented by |
|
Gets the class represented by |
|
Gets the (initialized) class represented by |
|
Gets the class represented by |
|
Gets the class represented by |
|
Gets the (initialized) class represented by |
|
Gets the array component type using |
|
Gets the class name, handling |
|
Gets the class name, handling |
|
Gets the object’s class name, handling |
|
Gets the object’s class name, handling |
|
Gets the package name from the canonical name of a |
|
Gets the package name from the class name of an |
|
Gets the package name from the class name. |
|
Gets the package name of a |
|
Gets the package name of an |
|
Gets the package name from a |
|
Gets the desired Method much like |
|
Gets the canonical name minus the package name from a |
|
Gets the canonical name minus the package name for an |
|
Gets the canonical name minus the package name from a String. |
|
Gets the class name minus the package name from a |
|
Gets the class name of the |
|
Gets the class name minus the package name from a String. |
|
Gets the simple class name, handling |
|
Gets the simple class name, handling |
|
Gets the object’s simple class name, handling |
|
Gets the object’s simple class name, handling |
|
Gets an |
|
Gets an |
|
Tests whether one |
|
Tests whether one |
|
Tests whether an array of Classes can be assigned to another array of Classes. |
|
Tests whether an array of Classes can be assigned to another array of Classes. |
|
Tests whether the specified class an inner class or static nested class. |
|
Tests whether the given |
|
Tests whether the given |
|
Tests whether a |
|
Converts the specified primitive Class object to its corresponding wrapper Class object. |
|
Converts the specified array of primitive Class objects to an array of its corresponding wrapper Class objects. |
|
Converts an array of |
|
Converts the specified wrapper class to its corresponding primitive class. |
|
Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects. |
- public static Comparator<Class<?>> comparator()[source]
Gets the class comparator, comparing by class name.
- Returns:
The class comparator.
- Since:
3.13.0
- public static List<Class<?>> convertClassNamesToClasses(List<String> classNames)[source]
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 – Thrown if classNames contains a non String entry.
- public static List<String> convertClassesToClassNames(List<Class<?>> classes)[source]
Given a
ListofClassobjects, this method converts them into class names.A new
Listis returned.nullobjects will be copied into the returned list asnull.
- public static String getAbbreviatedName(Class<?> cls, int lengthHint)[source]
Gets the abbreviated name of a
Class.- Parameters:
cls – The class to get the abbreviated name for, may be
null.lengthHint – The desired length of the abbreviated name.
- Returns:
The abbreviated name or an empty string.
- Throws:
java.lang.IllegalArgumentException – Thrown if len <= 0.
- See also:
- Since:
3.4
- public static String getAbbreviatedName(String className, int lengthHint)[source]
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. 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
lengthHintcharacters.If the
lengthHintis zero or negative then the method throws exception. If you want to achieve the shortest possible version then use1as alengthHint.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”
“org.apache.commons.lang3.ClassUtils”
18
“o.a.c.l.ClassUtils”
- Parameters:
className – The className to get the abbreviated name for, may be
null.lengthHint – The desired length of the abbreviated name.
- Returns:
The abbreviated name or an empty string if the specified class name is
nullor empty string. The abbreviated name may be longer than the desired length if it cannot be abbreviated to the desired length.- Throws:
java.lang.IllegalArgumentException – Thrown if
len <= 0.- Since:
3.4
- public static List<Class<?>> getAllInterfaces(Class<?> cls)[source]
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 be
null.- Returns:
The
Listof interfaces in order,nullif null input.
- public static List<Class<?>> getAllSuperclasses(Class<?> cls)[source]
Gets a
Listof superclasses for the given class.The first entry is the superclass of the given class.
The last entry is
Object’s class.
- Parameters:
cls – The class to look up, may be
null.- Returns:
The
Listof superclasses in order going up from this onenullif null input.
- public static String getCanonicalName(Class<?> cls)[source]
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:
- public static String getCanonicalName(Class<?> cls, String valueIfNull)[source]
Gets the canonical name for a
Class.- Parameters:
cls – The class for which to get the canonical class name; may be null.
valueIfNull – The return value if null.
- Returns:
The canonical name of the class, or
valueIfNull.- Since:
3.7
- See also:
- public static String getCanonicalName(Object object)[source]
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:
- public static String getCanonicalName(Object object, String valueIfNull)[source]
Gets the canonical name for an
Object.- Parameters:
object – The object for which to get the canonical class name; may be null.
valueIfNull – The return value if null.
- Returns:
The canonical name of the object or
valueIfNull.- Since:
3.7
- See also:
- public static Class<?> getClass(ClassLoader classLoader, String className) throws ClassNotFoundException[source]
Gets the (initialized) class represented by
classNameusing theclassLoader. This implementation supports the syntaxes “java.util.Map.Entry[]”, “java.util.Map$Entry[]”, “[Ljava.util.Map.Entry;”, and “[Ljava.util.Map$Entry;”.The provided class name is normalized by removing all whitespace. This is especially helpful when handling XML element values in which whitespace has not been collapsed.
Security note: because all whitespace is deleted before the class is resolved (and a failing
'.'may be retried as'$'for inner classes), many distinct input strings resolve to the same class, whileClass#forName(String)performs no such normalization. Validating an untrusted class name by string comparison before calling this method is therefore unsound: for example," java.lang.Runtime"fails a naivestartsWith("java.")denylist check on the raw string, yet loadsjava.lang.Runtime. Validate the class name after normalization, validate the resolvedClassobject itself, or use#getClassStrict(ClassLoader, String, boolean)which performs no whitespace normalization.- Parameters:
classLoader – The class loader to use to load the class.
className – The class name.
- Returns:
The class represented by
classNameusing theclassLoader.- Throws:
java.lang.NullPointerException – Thrown if the className is null.
java.lang.ClassNotFoundException – Thrown if the class is not found.
java.lang.IllegalArgumentException – Thrown if the class name represents an array with more dimensions than the JVM supports, 255.
Thrown if the class name length is greater than 65,535.
- See also:
- public static Class<?> getClass(ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException[source]
Gets the class represented by
classNameusing theclassLoader. This implementation supports the syntaxes “java.util.Map.Entry[]”, “java.util.Map$Entry[]”, “[Ljava.util.Map.Entry;”, and “[Ljava.util.Map$Entry;”.The provided class name is normalized by removing all whitespace. This is especially helpful when handling XML element values in which whitespace has not been collapsed.
Security note: because all whitespace is deleted before the class is resolved (and a failing
'.'may be retried as'$'for inner classes), many distinct input strings resolve to the same class, whileClass#forName(String)performs no such normalization. Validating an untrusted class name by string comparison before calling this method is therefore unsound: for example," java.lang.Runtime"fails a naivestartsWith("java.")denylist check on the raw string, yet loadsjava.lang.Runtime. Validate the class name after normalization, validate the resolvedClassobject itself, or use#getClassStrict(ClassLoader, String, boolean)which performs no whitespace normalization.- Parameters:
classLoader – The class loader to use to load the class.
className – The class name.
initialize – whether the class must be initialized.
- Returns:
The class represented by
classNameusing theclassLoader.- Throws:
java.lang.NullPointerException – Thrown if the className is null.
java.lang.ClassNotFoundException – Thrown if the class is not found.
java.lang.IllegalArgumentException – Thrown if the class name represents an array with more dimensions than the JVM supports, 255.
Thrown if the class name length is greater than 65,535.
- See also:
- public static Class<?> getClass(String className) throws ClassNotFoundException[source]
Gets the (initialized) class represented by
classNameusing 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;”.The provided class name is normalized by removing all whitespace. This is especially helpful when handling XML element values in which whitespace has not been collapsed.
Security note: because all whitespace is deleted before the class is resolved (and a failing
'.'may be retried as'$'for inner classes), many distinct input strings resolve to the same class, whileClass#forName(String)performs no such normalization. Validating an untrusted class name by string comparison before calling this method is therefore unsound: for example," java.lang.Runtime"fails a naivestartsWith("java.")denylist check on the raw string, yet loadsjava.lang.Runtime. Validate the class name after normalization, validate the resolvedClassobject itself, or use#getClassStrict(ClassLoader, String, boolean)which performs no whitespace normalization.- Parameters:
className – The class name
- Returns:
The class represented by
classNameusing the current thread’s context class loader- Throws:
java.lang.NullPointerException – Thrown if the className is null.
java.lang.ClassNotFoundException – Thrown if the class is not found.
java.lang.IllegalArgumentException – Thrown if the class name represents an array with more dimensions than the JVM supports, 255.
Thrown if the class name length is greater than 65,535.
- See also:
- public static Class<?> getClass(String className, boolean initialize) throws ClassNotFoundException[source]
Gets the class represented by
classNameusing 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;”.The provided class name is normalized by removing all whitespace. This is especially helpful when handling XML element values in which whitespace has not been collapsed.
Security note: because all whitespace is deleted before the class is resolved (and a failing
'.'may be retried as'$'for inner classes), many distinct input strings resolve to the same class, whileClass#forName(String)performs no such normalization. Validating an untrusted class name by string comparison before calling this method is therefore unsound: for example," java.lang.Runtime"fails a naivestartsWith("java.")denylist check on the raw string, yet loadsjava.lang.Runtime. Validate the class name after normalization, validate the resolvedClassobject itself, or use#getClassStrict(ClassLoader, String, boolean)which performs no whitespace normalization.- Parameters:
className – The class name.
initialize – whether the class must be initialized.
- Returns:
The class represented by
classNameusing the current thread’s context class loader.- Throws:
java.lang.NullPointerException – Thrown if the className is null.
java.lang.ClassNotFoundException – Thrown if the class is not found.
java.lang.IllegalArgumentException – Thrown if the class name represents an array with more dimensions than the JVM supports, 255.
Thrown if the class name length is greater than 65,535.
- See also:
- public static Class<?> getClassStrict(ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException[source]
Gets the class represented by
classNameusing theclassLoader, without normalizing the class name.Unlike
#getClass(ClassLoader, String, boolean), this method does not delete whitespace from the class name: a name that differs from the intended binary name only by whitespace throwsClassNotFoundException, matchingClass#forName(String, boolean, ClassLoader). Use this variant when the class name may come from an untrusted source, so that host-side string validation of the raw name cannot be bypassed through whitespace the loader would otherwise silently remove.The syntaxes “
java.util.Map.Entry[]”, “java.util.Map$Entry[]”, “[Ljava.util.Map.Entry;”, and “[Ljava.util.Map$Entry;” are still supported: a failing'.'is retried as'$'to find inner classes, so more than one dotted spelling can still resolve to the same inner class. When validating untrusted names, prefer validating the resolvedClassobject.- Parameters:
classLoader – The class loader to use to load the class.
className – The class name.
initialize – whether the class must be initialized.
- Returns:
The class represented by
classNameusing theclassLoader.- Throws:
java.lang.NullPointerException – Thrown if the className is null.
java.lang.ClassNotFoundException – Thrown if the class is not found.
java.lang.IllegalArgumentException – Thrown if the class name represents an array with more dimensions than the JVM supports, 255.
Thrown if the class name length is greater than 65,535.
- See also:
- Since:
3.21.0
- public static Class<?> getClassStrict(String className) throws ClassNotFoundException[source]
Gets the (initialized) class represented by
classNameusing the current thread’s context class loader, without normalizing the class name.Unlike
#getClass(String), this method does not delete whitespace from the class name: a name that differs from the intended binary name only by whitespace throwsClassNotFoundException, matchingClass#forName(String). Use this variant when the class name may come from an untrusted source, so that host-side string validation of the raw name cannot be bypassed through whitespace the loader would otherwise silently remove.The syntaxes “
java.util.Map.Entry[]”, “java.util.Map$Entry[]”, “[Ljava.util.Map.Entry;”, and “[Ljava.util.Map$Entry;” are still supported: a failing'.'is retried as'$'to find inner classes, so more than one dotted spelling can still resolve to the same inner class. When validating untrusted names, prefer validating the resolvedClassobject.- Parameters:
className – The class name.
- Returns:
The class represented by
classNameusing the current thread’s context class loader.- Throws:
java.lang.NullPointerException – Thrown if the className is null.
java.lang.ClassNotFoundException – Thrown if the class is not found.
java.lang.IllegalArgumentException – Thrown if the class name represents an array with more dimensions than the JVM supports, 255.
Thrown if the class name length is greater than 65,535.
- See also:
- Since:
3.21.0
- @SuppressWarnings({"unchecked"}) public static <T> Class<T> getComponentType(Class<T[]> cls)[source]
Gets the array component type using
Class#getComponentType()with generics.- Type parameters:
<T> – The array class type.
- Parameters:
cls – A class or null.
- Returns:
The array component type or null.
- See also:
- Since:
3.13.0
- public static String getName(Class<?> cls)[source]
Gets the class name, handling
nullsafely.- Parameters:
cls – The class for which to get the class name; may be null.
- Returns:
The class name or the empty string in case the argument is
null.- Since:
3.7
- See also:
- public static String getName(Class<?> cls, String valueIfNull)[source]
Gets the class name, handling
nullsafely.- Parameters:
cls – The class for which to get the class name; may be null.
valueIfNull – The return value if the argument
clsisnull.
- Returns:
The class name or
valueIfNull- Since:
3.7
- See also:
- public static String getName(Object object)[source]
Gets the object’s class name, handling
nullsafely.- 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:
- public static String getName(Object object, String valueIfNull)[source]
Gets the object’s class name, handling
nullsafely.- Parameters:
object – The object for which to get the class name; may be null.
valueIfNull – The value to return if
objectisnull.
- Returns:
The class name or
valueIfNull.- Since:
3.0
- See also:
- public static String getPackageCanonicalName(Class<?> cls)[source]
Gets the package name from the canonical name of a
Class.- Parameters:
cls – The class to get the package name for, may be
null.- Returns:
The package name or an empty string.
- Since:
2.4
- public static String getPackageCanonicalName(Object object, String valueIfNull)[source]
Gets the package name from the class name of an
Object.- Parameters:
object – The class to get the package name for, may be null.
valueIfNull – The value to return if null.
- Returns:
The package name of the object, or the null value.
- Since:
2.4
- public static String getPackageCanonicalName(String name)[source]
Gets the package name from the class 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.
- Parameters:
name – The name to get the package name for, may be
null.- Returns:
The package name or an empty string.
- Since:
2.4
- public static String getPackageName(Class<?> cls)[source]
Gets the package name of a
Class.- Parameters:
cls – The class to get the package name for, may be
null.- Returns:
The package name or an empty string
- public static String getPackageName(Object object, String valueIfNull)[source]
Gets the package name of an
Object.- Parameters:
object – The class to get the package name for, may be null.
valueIfNull – The value to return if null.
- Returns:
The package name of the object, or the null value.
- public static String getPackageName(String className)[source]
Gets the package name from a
String.The string passed in is assumed to be a class name.
If the class is unpackaged, return an empty string.
- Parameters:
className – The className to get the package name for, may be
null.- Returns:
The package name or an empty string.
- public static Method getPublicMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException[source]
Gets 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 null.
methodName – The name of the method.
parameterTypes – The list of parameters.
- Returns:
The method.
- Throws:
java.lang.NullPointerException – Thrown if the class is null.
java.lang.SecurityException – Thrown if a security violation occurred.
java.lang.NoSuchMethodException – Thrown if the method is not found in the given class or if the method doesn’t conform with the requirements.
- public static String getShortCanonicalName(Class<?> cls)[source]
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
- See also:
- public static String getShortCanonicalName(Object object, String valueIfNull)[source]
Gets the canonical name minus the package name for an
Object.- Parameters:
object – The class to get the short name for, may be null.
valueIfNull – The value to return if null.
- Returns:
The canonical name of the object without the package name, or the null value.
- Since:
2.4
- See also:
- public static String getShortCanonicalName(String canonicalName)[source]
Gets the canonical 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 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.Examples 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[][]"- 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
- public static String getShortClassName(Class<?> cls)[source]
Gets the class name minus the package name from a
Class.- Parameters:
cls – The class to get the short name for.
- Returns:
The class name without the package name or an empty string. If the class is an inner class then the returned value will contain the outer class or classes separated with
.(dot) character.
- public static String getShortClassName(Object object, String valueIfNull)[source]
Gets the class name of the
objectwithout the package name or names.- Parameters:
object – The class to get the short name for, may be
null.valueIfNull – The value to return if the object is
null.
- Returns:
The class name of the object without the package name, or
valueIfNullif the argumentobjectisnull.
- public static String getShortClassName(String className)[source]
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. 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 byClass.getCanonicalName().The difference 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 byClass.getName()Note that this method is called from the
#getShortClassName(Class)method using the string returned byClass.getName().Note that this method differs from
#getSimpleName(Class)in that this will return, for example"Map.Entry"whilst theClassvariant will simply return"Entry". In this example the argumentclassNameis the stringjava.util.Map$Entry(note the$sign).- Parameters:
className – The className to get the short name for. It has to be formatted as returned by
Class.getName()and notClass.getCanonicalName().- Returns:
The class name of the class without the package name or an empty string. If the class is an inner class then value contains the outer class or classes and the separator is replaced to be
.(dot) character.
- public static String getSimpleName(Class<?> cls)[source]
Gets the simple class name, handling
nullsafely.- Parameters:
cls – The class for which to get the simple name; may be null.
- Returns:
The simple class name or the empty string in case the argument is
null.- Since:
3.0
- See also:
- public static String getSimpleName(Class<?> cls, String valueIfNull)[source]
Gets the simple class name, handling
nullsafely.- Parameters:
cls – The class for which to get the simple name; may be null.
valueIfNull – The value to return if null.
- Returns:
The simple class name or
valueIfNullif the argumentclsisnull.- Since:
3.0
- See also:
- public static String getSimpleName(Object object)[source]
Gets the object’s simple class name, handling
nullsafely.It is to note that this method is overloaded and in case the argument
objectis aClassobject then the#getSimpleName(Class)will be invoked. If this is a significant possibility then the caller should check this case and callgetSimpleName(Class.class)or just simply use the string literal"Class", which is the result of the method in that case.- Parameters:
object – The object for which to get the simple class name; may be null.
- Returns:
The simple class name or the empty string in case the argument is
null.- Since:
3.7
- See also:
- public static String getSimpleName(Object object, String valueIfNull)[source]
Gets the object’s simple class name, handling
nullsafely.- Parameters:
object – The object for which to get the simple class name; may be null.
valueIfNull – The value to return if
objectisnull.
- Returns:
The simple class name or
valueIfNullif the argumentobjectisnull.- Since:
3.0
- See also:
- public static Iterable<Class<?>> hierarchy(Class<?> type)[source]
Gets an
Iterablethat 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
- public static Iterable<Class<?>> hierarchy(Class<?> type, ClassUtils.Interfaces interfacesBehavior)[source]
Gets an
Iterablethat can iterate over a class hierarchy in ascending (subclass to superclass) order.- Parameters:
type – The type to get the class hierarchy from.
interfacesBehavior – switch indicating whether to include or exclude interfaces.
- Returns:
Iterable an Iterable over the class hierarchy of the given class.
- Since:
3.2
- public static boolean isAssignable(Class<?> cls, Class<?> toClass)[source]
Tests whether 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 null.
toClass – The Class to try to assign into, returns false if null.
- Returns:
trueif assignment possible.
- public static boolean isAssignable(Class<?> cls, Class<?> toClass, boolean autoboxing)[source]
Tests whether 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 null.
toClass – The Class to try to assign into, returns false if null.
autoboxing – whether to use implicit autoboxing/unboxing between primitives and wrappers.
- Returns:
trueif assignment possible.
- public static boolean isAssignable(Class<?>[] classArray, Class<?>... toClassArray)[source]
Tests whether 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 be
null.toClassArray – The array of Classes to try to assign into, may be
null.
- Returns:
trueif assignment possible.
- public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArray, boolean autoboxing)[source]
Tests whether 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 be
nulltoClassArray – The array of Classes to try to assign into, may be
nullautoboxing – whether to use implicit autoboxing/unboxing between primitives and wrappers
- Returns:
trueif assignment possible
- public static boolean isInnerClass(Class<?> cls)[source]
Tests whether 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.
- public static boolean isPrimitiveOrWrapper(Class<?> type)[source]
Tests whether the given
typeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).
- public static boolean isPrimitiveWrapper(Class<?> type)[source]
Tests whether the given
typeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).
- public static boolean isPublic(Class<?> cls)[source]
Tests whether a
Classis public.- Parameters:
cls – Class to test.
- Returns:
trueifclsis public.- Since:
3.13.0
- public static Class<?> primitiveToWrapper(Class<?> cls)[source]
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
- public static Class<?>[] primitivesToWrappers(Class<?>... classes)[source]
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
- public static Class<?>[] toClass(Object... array)[source]
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.
- public static Class<?> wrapperToPrimitive(Class<?> cls)[source]
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 isnull, the return value isnull.- Parameters:
cls – The class to convert, may be
null.- Returns:
The corresponding primitive type if
clsis a wrapper class,nullotherwise.- See also:
- Since:
2.4
- public static Class<?>[] wrappersToPrimitives(Class<?>... classes)[source]
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
nullif the original class is not a wrapper class.nullif null input. Empty array if an empty array passed in.- See also:
- Since:
2.4