Top Description Inners Fields Constructors Methods
org.apache.commons.lang3

public Class ClassUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Method, .Modifier, java.util.ArrayList, .Collections, .Comparator, .HashMap, .HashSet, .Iterator, .LinkedHashSet, .List, .Map, .Objects, .Set, java.util.concurrent.atomic.AtomicReference, java.util.stream.Collectors

Operates on classes without using reflection.

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.

Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private static final Map<String, String>
ABBREVIATION_MAP

Maps a primitive class name to its corresponding abbreviation used in array class names.

private static final Comparator<Class<?>>
public static final String
INNER_CLASS_SEPARATOR

The inner class separator String: "$".

public static final char
INNER_CLASS_SEPARATOR_CHAR

The inner class separator character: '$' == {@value}.

private static final int
MAX_DIMENSIONS

The maximum number of array dimensions.

private static final Map<String, Class<?>>
NAME_PRIMITIVE_MAP

Maps names of primitives to their corresponding primitive Classes.

public static final String
PACKAGE_SEPARATOR

The package separator String: ".".

public static final char
PACKAGE_SEPARATOR_CHAR

The package separator character: '.' == {@value}.

private static final Map<Class<?>, Class<?>>
PRIMITIVE_WRAPPER_MAP

Maps primitive Classes to their corresponding wrapper Class.

private static final Map<String, String>
REVERSE_ABBREVIATION_MAP

Maps an abbreviation used in array class names to corresponding primitive class name.

private static final Map<Class<?>, Class<?>>
WRAPPER_PRIMITIVE_MAP

Maps wrapper Classes to their corresponding primitive types.

Constructor Summary

AccessConstructor and Description
public
ClassUtils()
Deprecated TODO Make private in 4.0.

ClassUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static Comparator<Class<?>>

Returns:

the class comparator.
comparator
()

Gets the class comparator, comparing by class name.

public static List<String>

Returns:

a List of class names corresponding to the Class objects, null if null input
convertClassesToClassNames
(final List<Class<?>>
the classes to change
classes
)

Given a List of Class objects, this method converts them into class names.

public static List<Class<?>>

Returns:

a List of Class objects corresponding to the class names, null if null input
convertClassNamesToClasses
(final List<String>
the classNames to change
classNames
)

Given a List of class names, this method converts them into classes.

public static String

Returns:

the abbreviated name or an empty string
getAbbreviatedName
(final Class<?>
the class to get the abbreviated name for, may be null
cls
,
final int
the desired length of the abbreviated name
lengthHint
)

Gets the abbreviated name of a Class.

public static String

Returns:

the abbreviated name or an empty string if the specified class name is null or empty string. The abbreviated name may be longer than the desired length if it cannot be abbreviated to the desired length.
getAbbreviatedName
(final String
the className to get the abbreviated name for, may be null
className
,
final int
the desired length of the abbreviated name
lengthHint
)

Gets the abbreviated class name from a String.

public static List<Class<?>>

Returns:

the List of interfaces in order, null if null input
getAllInterfaces
(final Class<?>
the class to look up, may be null
cls
)

Gets a List of all interfaces implemented by the given class and its superclasses.

private static void
getAllInterfaces(Class<?>
the class to look up, may be null
cls
,
final HashSet<Class<?>>
the Set of interfaces for the class
interfacesFound
)

Gets the interfaces for the specified class.

public static List<Class<?>>

Returns:

the List of superclasses in order going up from this one null if null input
getAllSuperclasses
(final Class<?>
the class to look up, may be null
cls
)

Gets a List of superclasses for the given class.

public static String

Returns:

the canonical name of the class, or the empty String
getCanonicalName
(final Class<?>
the class for which to get the canonical class name; may be null
cls
)

Gets the canonical class name for a Class.

public static String

Returns:

the canonical name of the class, or valueIfNull
getCanonicalName
(final Class<?>
the class for which to get the canonical class name; may be null
cls
,
final String
the return value if null
valueIfNull
)

Gets the canonical name for a Class.

public static String

Returns:

the canonical name of the object, or the empty String
getCanonicalName
(final Object
the object for which to get the canonical class name; may be null
object
)

Gets the canonical name for an Object.

public static String

Returns:

the canonical name of the object or valueIfNull
getCanonicalName
(final Object
the object for which to get the canonical class name; may be null
object
,
final String
the return value if null
valueIfNull
)

Gets the canonical name for an Object.

private static String

Returns:

canonical form of class name.
getCanonicalName
(final String
the name of class.
name
)

Converts a given name of class into canonical format.

public static Class<?>

Returns:

the class represented by className using the classLoader
getClass
(final ClassLoader
the class loader to use to load the class
classLoader
,
final String
the class name
className
)

Gets the (initialized) class represented by className using the classLoader.

public static Class<?>

Returns:

the class represented by className using the classLoader
getClass
(final ClassLoader
the class loader to use to load the class
classLoader
,
final String
the class name
className
,
final boolean
whether the class must be initialized
initialize
)

Gets the class represented by className using the classLoader.

public static Class<?>

Returns:

the class represented by className using the current thread's context class loader
getClass
(final String
the class name
className
)

Gets the (initialized) class represented by className using the current thread's context class loader.

public static Class<?>

Returns:

the class represented by className using the current thread's context class loader
getClass
(final String
the class name
className
,
final boolean
whether the class must be initialized
initialize
)

Gets the class represented by className using the current thread's context class loader.

public static <
The array class type.
T
>
Class<T>

Returns:

The array component type or null.
getComponentType
(final Class<T[]>
A class or null.
cls
)

Delegates to Class#getComponentType() using generics.

public static String

Returns:

the class name or the empty string in case the argument is null
getName
(final Class<?>
the class for which to get the class name; may be null
cls
)

Null-safe version of cls.getName()

public static String

Returns:

the class name or valueIfNull
getName
(final Class<?>
the class for which to get the class name; may be null
cls
,
final String
the return value if the argument cls is null
valueIfNull
)

Null-safe version of cls.getName()

pack-priv static String
getName(final Class<?> cls, final String valueIfNull, final boolean simple)

public static String

Returns:

the class name or the empty String
getName
(final Object
the object for which to get the class name; may be null
object
)

Null-safe version of object.getClass().getName()

public static String

Returns:

the class name or valueIfNull
getName
(final Object
the object for which to get the class name; may be null
object
,
final String
the value to return if object is null
valueIfNull
)

Null-safe version of object.getClass().getSimpleName()

public static String

Returns:

the package name or an empty string
getPackageCanonicalName
(final Class<?>
the class to get the package name for, may be null.
cls
)

Gets the package name from the canonical name of a Class.

public static String

Returns:

the package name of the object, or the null value
getPackageCanonicalName
(final Object
the class to get the package name for, may be null
object
,
final String
the value to return if null
valueIfNull
)

Gets the package name from the class name of an Object.

public static String

Returns:

the package name or an empty string
getPackageCanonicalName
(final String
the name to get the package name for, may be null
name
)

Gets the package name from the class name.

public static String

Returns:

the package name or an empty string
getPackageName
(final Class<?>
the class to get the package name for, may be null.
cls
)

Gets the package name of a Class.

public static String

Returns:

the package name of the object, or the null value
getPackageName
(final Object
the class to get the package name for, may be null
object
,
final String
the value to return if null
valueIfNull
)

Gets the package name of an Object.

public static String

Returns:

the package name or an empty string.
getPackageName
(String
the className to get the package name for, may be null.
className
)

Gets the package name from a String.

pack-priv static Class<?>

Returns:

the primitive class.
getPrimitiveClass
(final String
the primitive class for the given class name.
className
)

Gets the primitive class for the given class name, for example "byte".

public static Method

Returns:

the method
getPublicMethod
(final Class<?>
the class to check, not null
cls
,
final String
the name of the method
methodName
,
final Class<?>...
the list of parameters
parameterTypes
)

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.

public static String

Returns:

the canonical name without the package name or an empty string
getShortCanonicalName
(final Class<?>
the class for which to get the short canonical class name; may be null
cls
)

Gets the canonical name minus the package name from a Class.

public static String

Returns:

the canonical name of the object without the package name, or the null value
getShortCanonicalName
(final Object
the class to get the short name for, may be null
object
,
final String
the value to return if null
valueIfNull
)

Gets the canonical name minus the package name for an Object.

public static String

Returns:

the canonical name of the class without the package name or an empty string
getShortCanonicalName
(final String
the class name to get the short name for
canonicalName
)

Gets the canonical name minus the package name from a String.

public static String

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.
getShortClassName
(final Class<?>
the class to get the short name for.
cls
)

Gets the class name minus the package name from a Class.

public static String

Returns:

the class name of the object without the package name, or valueIfNull if the argument object is null
getShortClassName
(final Object
the class to get the short name for, may be null
object
,
final String
the value to return if the object is null
valueIfNull
)

Gets the class name of the object without the package name or names.

public static String

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.
getShortClassName
(String
the className to get the short name for. It has to be formatted as returned by Class.getName() and not Class.getCanonicalName()
className
)

Gets the class name minus the package name from a String.

public static String

Returns:

the simple class name or the empty string in case the argument is null
getSimpleName
(final Class<?>
the class for which to get the simple name; may be null
cls
)

Null-safe version of cls.getSimpleName()

public static String

Returns:

the simple class name or valueIfNull if the argument cls is null
getSimpleName
(final Class<?>
the class for which to get the simple name; may be null
cls
,
final String
the value to return if null
valueIfNull
)

Null-safe version of cls.getSimpleName()

public static String

Returns:

the simple class name or the empty string in case the argument is null
getSimpleName
(final Object
the object for which to get the simple class name; may be null
object
)

Null-safe version of 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.

public static String

Returns:

the simple class name or valueIfNull if the argument object is null
getSimpleName
(final Object
the object for which to get the simple class name; may be null
object
,
final String
the value to return if object is null
valueIfNull
)

Null-safe version of object.getClass().getSimpleName()

public static Iterable<Class<?>>

Returns:

Iterable an Iterable over the class hierarchy of the given class
hierarchy
(final Class<?>
the type to get the class hierarchy from
type
)

Gets an Iterable that can iterate over a class hierarchy in ascending (subclass to superclass) order, excluding interfaces.

public static Iterable<Class<?>>

Returns:

Iterable an Iterable over the class hierarchy of the given class
hierarchy
(final Class<?>
the type to get the class hierarchy from
type
,
final ClassUtils.Interfaces
switch indicating whether to include or exclude interfaces
interfacesBehavior
)

Gets an Iterable that can iterate over a class hierarchy in ascending (subclass to superclass) order.

public static boolean

Returns:

true if assignment possible
isAssignable
(final Class<?>
the Class to check, may be null
cls
,
final Class<?>
the Class to try to assign into, returns false if null
toClass
)

Tests whether one Class can be assigned to a variable of another Class.

public static boolean

Returns:

true if assignment possible
isAssignable
(Class<?>
the Class to check, may be null
cls
,
final Class<?>
the Class to try to assign into, returns false if null
toClass
,
final boolean
whether to use implicit autoboxing/unboxing between primitives and wrappers
autoboxing
)

Tests whether one Class can be assigned to a variable of another Class.

public static boolean

Returns:

true if assignment possible
isAssignable
(final Class<?>[]
the array of Classes to check, may be null
classArray
,
final Class<?>...
the array of Classes to try to assign into, may be null
toClassArray
)

Tests whether an array of Classes can be assigned to another array of Classes.

public static boolean

Returns:

true if assignment possible
isAssignable
(Class<?>[]
the array of Classes to check, may be null
classArray
,
Class<?>[]
the array of Classes to try to assign into, may be null
toClassArray
,
final boolean
whether to use implicit autoboxing/unboxing between primitives and wrappers
autoboxing
)

Tests whether an array of Classes can be assigned to another array of Classes.

public static boolean

Returns:

true if the class is an inner or static nested class, false if not or null
isInnerClass
(final Class<?>
the class to check, may be null
cls
)

Tests whether the specified class an inner class or static nested class.

public static boolean

Returns:

true if the given type is a primitive or primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).
isPrimitiveOrWrapper
(final Class<?>
The class to query or null.
type
)

Tests whether the given type is a primitive or primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).

public static boolean

Returns:

true if the given type is a primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).
isPrimitiveWrapper
(final Class<?>
The class to query or null.
type
)

Tests whether the given type is a primitive wrapper (Boolean, Byte, Character, Short, Integer, Long, Double, Float).

public static boolean

Returns:

true if cls is public.
isPublic
(final Class<?>
Class to test.
cls
)

Tests whether a Class is public.

public static Class<?>[]

Returns:

an array which contains for each given class, the wrapper class or the original class if class is not a primitive. null if null input. Empty array if an empty array passed in.
primitivesToWrappers
(final Class<?>...
the class array to convert, may be null or empty.
classes
)

Converts the specified array of primitive Class objects to an array of its corresponding wrapper Class objects.

public static Class<?>

Returns:

the wrapper class for cls or cls if cls is not a primitive. null if null input.
primitiveToWrapper
(final Class<?>
the class to convert, may be null.
cls
)

Converts the specified primitive Class object to its corresponding wrapper Class object.

private static String

Returns:

the converted name
toCanonicalName
(final String
the class name
className
)

Converts a class name to a JLS style class name.

public static Class<?>[]

Returns:

a Class array, null if null array input
toClass
(final Object...
an Object array
array
)

Converts an array of Object in to an array of Class objects.

private static boolean

Returns:

true if it can be kept in its original length false if the current part has to be abbreviated and
useFull
(final int
the target index (where the characters were copied to) pointing after the last character copied when the current part was copied
runAheadTarget
,
final int
the source index (where the characters were copied from) pointing after the last character copied when the current part was copied
source
,
final int
the original length of the class full name, which is abbreviated
originalLength
,
final int
the desired length of the abbreviated class name
desiredLength
)

Decides if the part that was just copied to its destination location in the work array can be kept as it was copied or must be abbreviated.

public static Class<?>[]

Returns:

an array which contains for each given class, the primitive class or null if the original class is not a wrapper class. null if null input. Empty array if an empty array passed in.
wrappersToPrimitives
(final Class<?>...
the class array to convert, may be null or empty
classes
)

Converts the specified array of wrapper Class objects to an array of its corresponding primitive Class objects.

public static Class<?>

Returns:

the corresponding primitive type if cls is a wrapper class, null otherwise
wrapperToPrimitive
(final Class<?>
the class to convert, may be null
cls
)

Converts the specified wrapper class to its corresponding primitive class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait