Top Description Fields Constructors Methods
org.springframework.util

public abstract Class ClassUtils

extends Object
Class Inheritance
Imports
java.io.Closeable, .Externalizable, .File, .Serializable, java.lang.reflect.Constructor, .Method, .Modifier, .Proxy, java.net.InetAddress, .URI, .URL, java.nio.charset.Charset, java.nio.file.Path, java.time.ZoneId, java.time.temporal.Temporal, java.util.Arrays, .Collection, .Collections, .Currency, .Date, .Enumeration, .HashMap, .HashSet, .IdentityHashMap, .Iterator, .LinkedHashSet, .List, .Locale, .Map, .Optional, .Set, .StringJoiner, .TimeZone, .UUID, java.util.regex.Pattern, org.springframework.lang.Nullable

Miscellaneous java.lang.Class utility methods.

Mainly for internal use within the framework.

Authors
Juergen Hoeller, Keith Donald, Rob Harrop, Sam Brannen, Sebastien Deleuze
Since
1.1
See Also
TypeUtils, ReflectionUtils

Field Summary

Modifier and TypeField and Description
public static final String
ARRAY_SUFFIX

Suffix for array class names: "[]".

public static final String
CGLIB_CLASS_SEPARATOR

The CGLIB class separator: "$$".

public static final String
CLASS_FILE_SUFFIX

The ".class" file suffix.

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

Map with common Java language class name as key and corresponding Class as value.

private static final Class<?>[]
EMPTY_CLASS_ARRAY

A reusable empty class array constant.

private static final Map<Method, Method>
interfaceMethodCache

Cache for equivalent methods on an interface implemented by the declaring class.

private static final String
INTERNAL_ARRAY_PREFIX

Prefix for internal array class names: "[".

private static final Set<Class<?>>
javaLanguageInterfaces

Common Java language interfaces which are supposed to be ignored when searching for 'primary' user-level interfaces.

private static final char
NESTED_CLASS_SEPARATOR

The nested class separator character: '$'.

private static final int
NON_OVERRIDABLE_MODIFIER

Precomputed value for the combination of private, static and final modifiers.

private static final String
NON_PRIMITIVE_ARRAY_PREFIX

Prefix for internal non-primitive array class names: "[L".

private static final int
OVERRIDABLE_MODIFIER

Precomputed value for the combination of public and protected modifiers.

private static final char
PACKAGE_SEPARATOR

The package separator character: '.'.

private static final char
PATH_SEPARATOR

The path separator character: '/'.

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

Map with primitive type name as key and corresponding primitive type as value, for example: "int" -> int.class.

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

Map with primitive type as key and corresponding wrapper type as value, for example: int.class -> Integer.class.

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

Map with primitive wrapper type as key and corresponding primitive type as value, for example: Integer.class -> int.class.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the built-up resource path
addResourcePathToPackagePath
(Class<?>
the Class whose package will be used as the base
clazz
,
String
the resource name to append. A leading slash is optional.
resourceName
)

Return a path suitable for use with ClassLoader.getResource (also suitable for use with Class.getResource by prepending a slash ('/') to the return value).

public static String

Returns:

a String of form "[com.foo.Bar, com.foo.Baz]"
classNamesToString
(Class<?>...
an array of Class objects
classes
)

Build a String that consists of the names of the classes/interfaces in the given array.

public static String

Returns:

a String of form "[com.foo.Bar, com.foo.Baz]"
classNamesToString
(Collection<Class<?>>
a Collection of Class objects (may be null)
classes
)

Build a String that consists of the names of the classes/interfaces in the given collection.

public static String

Returns:

a path which represents the package name
classPackageAsResourcePath
(Class<?>
the input class. A null value or the default (empty) package will result in an empty string ("") being returned.
clazz
)

Given an input class object, return a string which consists of the class's package name as a pathname, i.e., all dots ('.') are replaced by slashes ('/').

public static String

Returns:

the corresponding resource path, pointing to the class
convertClassNameToResourcePath
(String
the fully qualified class name
className
)

Convert a "."-based fully qualified class name to a "/"-based resource path.

public static String

Returns:

the corresponding fully qualified class name
convertResourcePathToClassName
(String
the resource path pointing to a class
resourcePath
)

Convert a "/"-based resource path to a "."-based fully qualified class name.

public static Class<?>

Returns:

the merged interface as Class
createCompositeInterface
(Class<?>[]
the interfaces to merge
interfaces
,
ClassLoader
the ClassLoader to create the composite Class in
classLoader
)

Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.

public static Class<?>

Returns:

the common ancestor (i.e. common superclass, one interface extending the other), or null if none found. If any of the given classes is null, the other class will be returned.
determineCommonAncestor
(Class<?>
the class to introspect
clazz1
,
Class<?>
the other class to introspect
clazz2
)

Determine the common ancestor of the given classes, if any.

private static Method
findInterfaceMethodIfPossible(Method method, Class<?> startClass, Class<?> endClass)

private static Set<Method>
findMethodCandidatesByName(Class<?> clazz, String methodName)

public static Class<?>

Returns:

a class instance for the supplied name
forName
(String
the name of the Class
name
,
ClassLoader
the class loader to use (may be null, which indicates the default class loader)
classLoader
)

Replacement for Class.forName() that also returns Class instances for primitives (e.g. "int") and array class names (e.g. "String[]").

public static Class<?>[]

Returns:

all interfaces that the given instance implements as an array
getAllInterfaces
(Object
the instance to analyze for interfaces
instance
)

Return all interfaces that the given instance implements as an array, including ones implemented by superclasses.

public static Set<Class<?>>

Returns:

all interfaces that the given instance implements as a Set
getAllInterfacesAsSet
(Object
the instance to analyze for interfaces
instance
)

Return all interfaces that the given instance implements as a Set, including ones implemented by superclasses.

public static Class<?>[]

Returns:

all interfaces that the given object implements as an array
getAllInterfacesForClass
(Class<?>
the class to analyze for interfaces
clazz
)

Return all interfaces that the given class implements as an array, including ones implemented by superclasses.

public static Class<?>[]

Returns:

all interfaces that the given object implements as an array
getAllInterfacesForClass
(Class<?>
the class to analyze for interfaces
clazz
,
ClassLoader
the ClassLoader that the interfaces need to be visible in (may be null when accepting all declared interfaces)
classLoader
)

Return all interfaces that the given class implements as an array, including ones implemented by superclasses.

public static Set<Class<?>>

Returns:

all interfaces that the given object implements as a Set
getAllInterfacesForClassAsSet
(Class<?>
the class to analyze for interfaces
clazz
)

Return all interfaces that the given class implements as a Set, including ones implemented by superclasses.

public static Set<Class<?>>

Returns:

all interfaces that the given object implements as a Set
getAllInterfacesForClassAsSet
(Class<?>
the class to analyze for interfaces
clazz
,
ClassLoader
the ClassLoader that the interfaces need to be visible in (may be null when accepting all declared interfaces)
classLoader
)

Return all interfaces that the given class implements as a Set, including ones implemented by superclasses.

public static String

Returns:

the file name of the ".class" file
getClassFileName
(Class<?>
the class
clazz
)

Determine the name of the class file, relative to the containing package: e.g. "String.class"

public static <T> Constructor<T>

Returns:

the constructor, or null if not found
getConstructorIfAvailable
(Class<T>
the clazz to analyze
clazz
,
Class<?>...
the parameter types of the method
paramTypes
)

Determine whether the given class has a public constructor with the given signature, and return it if available (else return null).

public static ClassLoader

Returns:

the default ClassLoader (only null if even the system ClassLoader isn't accessible)
getDefaultClassLoader
()

Return the default ClassLoader to use: typically the thread context ClassLoader, if available; the ClassLoader that loaded the ClassUtils class will be used as fallback.

public static String

Returns:

the qualified name of the class
getDescriptiveType
(Object
the value to introspect
value
)

Return a descriptive name for the given object's type: usually simply the class name, but component type class name + "[]" for arrays, and an appended list of implemented interfaces for JDK proxies.

public static Method

Returns:

the corresponding interface method, or the original method if none found
getInterfaceMethodIfPossible
(Method
the method to be invoked, potentially from an implementation class
method
)

Determine a corresponding interface method for the given method handle, if possible.
public static Method

Returns:

the corresponding interface method, or the original method if none found
getInterfaceMethodIfPossible
(Method
the method to be invoked, potentially from an implementation class
method
,
Class<?>
the target class to check for declared interfaces
targetClass
)

Determine a corresponding interface method for the given method handle, if possible.

public static Method

Returns:

the method (never null)
getMethod
(Class<?>
the clazz to analyze
clazz
,
String
the name of the method
methodName
,
Class<?>...
the parameter types of the method (may be null to indicate any signature)
paramTypes
)

Determine whether the given class has a public method with the given signature, and return it if available (else throws an IllegalStateException).

public static int

Returns:

the number of methods with the given name
getMethodCountForName
(Class<?>
the clazz to check
clazz
,
String
the name of the method
methodName
)

Return the number of methods with a given name (with any argument types), for the given class and/or its superclasses.

public static Method

Returns:

the method, or null if not found
getMethodIfAvailable
(Class<?>
the clazz to analyze
clazz
,
String
the name of the method
methodName
,
Class<?>...
the parameter types of the method (may be null to indicate any signature)
paramTypes
)

Determine whether the given class has a public method with the given signature, and return it if available (else return null).

private static Method
getMethodOrNull(Class<?> clazz, String methodName, Class<?>[] paramTypes)

public static Method

Returns:

the specific target method, or the original method if the targetClass does not implement it
getMostSpecificMethod
(Method
the method to be invoked, which may come from an interface
method
,
Class<?>
the target class for the current invocation (may be null or may not even implement the method)
targetClass
)

Given a method, which may come from an interface, and a target class used in the current reflective invocation, find the corresponding target method if there is one — for example, the method may be IFoo.bar(), and the target class may be DefaultFoo.

public static String

Returns:

the package name, or the empty String if the class is defined in the default package
getPackageName
(Class<?>
the class
clazz
)

Determine the name of the package of the given class, e.g. "java.lang" for the java.lang.String class.

public static String

Returns:

the package name, or the empty String if the class is defined in the default package
getPackageName
(String
the fully-qualified class name
fqClassName
)

Determine the name of the package of the given fully-qualified class name, e.g. "java.lang" for the java.lang.String class name.

public static String

Returns:

the qualified name of the method
getQualifiedMethodName
(Method
the method
method
)

Return the qualified name of the given method, consisting of fully qualified interface/class name + "." + method name.

public static String

Returns:

the qualified name of the method
getQualifiedMethodName
(Method
the method
method
,
Class<?>
the clazz that the method is being invoked on (may be null to indicate the method's declaring class)
clazz
)

Return the qualified name of the given method, consisting of fully qualified interface/class name + "." + method name.

public static String

Returns:

the qualified name of the class
getQualifiedName
(Class<?>
the class
clazz
)

Return the qualified name of the given class: usually simply the class name, but component type class name + "[]" for arrays.

public static String

Returns:

the class name of the class without the package name
getShortName
(String
the className to get the short name for
className
)

Get the class name without the qualified package name.

public static String

Returns:

the class name of the class without the package name
getShortName
(Class<?>
the class to get the short name for
clazz
)

Get the class name without the qualified package name.

public static String

Returns:

the short name rendered in a standard JavaBeans property format
getShortNameAsProperty
(Class<?>
the class
clazz
)

Return the short string name of a Java class in uncapitalized JavaBeans property format.

public static Method

Returns:

the static method, or null if no static method was found
getStaticMethod
(Class<?>
the class which defines the method
clazz
,
String
the static method name
methodName
,
Class<?>...
the parameter types to the method
args
)

Return a public static method of a class.

public static Class<?>

Returns:

the user-defined class
getUserClass
(Object
the instance to check
instance
)

Return the user-defined class for the given instance: usually simply the class of the given instance, but the original class in case of a CGLIB-generated subclass.

public static Class<?>

Returns:

the user-defined class
getUserClass
(Class<?>
the class to check
clazz
)

Return the user-defined class for the given class: usually simply the given class, but the original class in case of a CGLIB-generated subclass.

public static boolean

Returns:

whether there is at least one method with the given name
hasAtLeastOneMethodWithName
(Class<?>
the clazz to check
clazz
,
String
the name of the method
methodName
)

Does the given class or one of its superclasses at least have one or more methods with the supplied name (with any argument types)?

public static boolean

Returns:

whether the class has a corresponding constructor
hasConstructor
(Class<?>
the clazz to analyze
clazz
,
Class<?>...
the parameter types of the method
paramTypes
)

Determine whether the given class has a public constructor with the given signature.

public static boolean

Returns:

whether the class has a corresponding method
hasMethod
(Class<?>
the clazz to analyze
clazz
,
Method
the method to look for
method
)

Determine whether the given class has a public method with the given signature.

public static boolean

Returns:

whether the class has a corresponding method
hasMethod
(Class<?>
the clazz to analyze
clazz
,
String
the name of the method
methodName
,
Class<?>...
the parameter types of the method
paramTypes
)

Determine whether the given class has a public method with the given signature.

public static boolean

Returns:

true if rhsType is assignable to lhsType
isAssignable
(Class<?>
the target type (left-hand side (LHS) type)
lhsType
,
Class<?>
the value type (right-hand side (RHS) type) that should be assigned to the target type
rhsType
)

Check if the right-hand side type may be assigned to the left-hand side type, assuming setting by reflection.

public static boolean

Returns:

if the type is assignable from the value
isAssignableValue
(Class<?>
the target type
type
,
Object
the value that should be assigned to the type
value
)

Determine if the given type is assignable from the given value, assuming setting by reflection.

public static boolean
isCacheSafe(Class<?>
the class to analyze
clazz
,
ClassLoader
the ClassLoader to potentially cache metadata in (may be null which indicates the system class loader)
classLoader
)

Check whether the given class is cache-safe in the given context, i.e. whether it is loaded by the given ClassLoader or a parent of it.

public static boolean
isCglibProxy(Object
the object to check
object
)

Deprecated as of 5.2, in favor of custom (possibly narrower) checks such as for a Spring AOP proxy
Check whether the given object is a CGLIB proxy.
public static boolean
isCglibProxyClass(Class<?>
the class to check
clazz
)

Deprecated as of 5.2, in favor of custom (possibly narrower) checks or simply a check for containing CGLIB_CLASS_SEPARATOR
Check whether the specified class is a CGLIB-generated class.
public static boolean
isCglibProxyClassName(String
the class name to check
className
)

Deprecated as of 5.2, in favor of custom (possibly narrower) checks or simply a check for containing CGLIB_CLASS_SEPARATOR
Check whether the specified class name is a CGLIB-generated class.
private static boolean
public static boolean

Returns:

true if the supplied class is an inner class
isInnerClass
(Class<?> clazz)

Determine if the supplied class is an inner class, i.e. a non-static member of an enclosing class.

public static boolean
isJavaLanguageInterface(Class<?>
the interface to check
ifc
)

Determine whether the given interface is a common Java language interface: Serializable, Externalizable, Closeable, AutoCloseable, Cloneable, Comparable - all of which can be ignored when looking for 'primary' user-level interfaces.

public static boolean

Returns:

true if the class is a lambda implementation class
isLambdaClass
(Class<?>
the class to check
clazz
)

Determine if the supplied Class is a JVM-generated implementation class for a lambda expression or method reference.

private static boolean
isLoadable(Class<?>
the class to check (typically an interface)
clazz
,
ClassLoader
the ClassLoader to check against
classLoader
)

Check whether the given class is loadable in the given ClassLoader.

private static boolean
isOverridable(Method
the method to check
method
,
Class<?>
the target class to check against
targetClass
)

Determine whether the given method is overridable in the given target class.

public static boolean

Returns:

whether the specified class is present (including all of its superclasses and interfaces)
isPresent
(String
the name of the class to check
className
,
ClassLoader
the class loader to use (may be null which indicates the default class loader)
classLoader
)

Determine whether the Class identified by the supplied name is present and can be loaded.

public static boolean

Returns:

whether the given class is a primitive array class
isPrimitiveArray
(Class<?>
the class to check
clazz
)

Check if the given class represents an array of primitives, i.e. boolean, byte, char, short, int, long, float, or double.

public static boolean

Returns:

true if the given class represents a primitive, void, or a wrapper class
isPrimitiveOrWrapper
(Class<?>
the class to check
clazz
)

Check if the given class represents a primitive (i.e. boolean, byte, char, short, int, long, float, or double), void, or a wrapper for those types (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, Double, or Void).

public static boolean

Returns:

whether the given class is a primitive wrapper class
isPrimitiveWrapper
(Class<?>
the class to check
clazz
)

Check if the given class represents a primitive wrapper, i.e. Boolean, Byte, Character, Short, Integer, Long, Float, Double, or Void.

public static boolean

Returns:

whether the given class is a primitive wrapper array class
isPrimitiveWrapperArray
(Class<?>
the class to check
clazz
)

Check if the given class represents an array of primitive wrappers, i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.

public static boolean

Returns:

whether the given type represents a "simple" value type, suggesting value-based data binding and toString output
isSimpleValueType
(Class<?>
the type to check
type
)

Delegate for org.springframework.beans.BeanUtils#isSimpleValueType.

public static boolean

Returns:

true if the supplied class is a static class
isStaticClass
(Class<?> clazz)

Determine if the supplied class is a static class.

public static boolean

Returns:

true if the method can be considered as user-declared; false otherwise
isUserLevelMethod
(Method
the method to check
method
)

Determine whether the given method is declared by the user or at least pointing to a user-declared method.

public static boolean
isVisible(Class<?>
the class to check (typically an interface)
clazz
,
ClassLoader
the ClassLoader to check against (may be null in which case this method will always return true)
classLoader
)

Check whether the given class is visible in the given ClassLoader.

public static boolean

Returns:

true if the type represents Void or void
isVoidType
(Class<?>
the type to check
type
)

Determine if the given type represents either Void or void.

public static boolean
matchesTypeName(Class<?>
the class to check
clazz
,
String
the type name to match
typeName
)

Check whether the given class matches the user-specified type name.

public static ClassLoader

Returns:

the original thread context ClassLoader, or null if not overridden
overrideThreadContextClassLoader
(ClassLoader
the actual ClassLoader to use for the thread context
classLoaderToUse
)

Override the thread context ClassLoader with the environment's bean ClassLoader if necessary, i.e. if the bean ClassLoader is not equivalent to the thread context ClassLoader already.

private static void
registerCommonClasses(Class<?>... commonClasses)

Register the given common classes with the ClassUtils cache.

public static Class<?>

Returns:

a class instance for the supplied name
resolveClassName
(String
the name of the Class
className
,
ClassLoader
the class loader to use (may be null, which indicates the default class loader)
classLoader
)

Resolve the given class name into a Class instance.

public static Class<?>

Returns:

the primitive class, or null if the name does not denote a primitive class or primitive array class
resolvePrimitiveClassName
(String
the name of the potentially primitive class
name
)

Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.

public static Class<?>

Returns:

the original class, or a primitive wrapper for the original primitive type
resolvePrimitiveIfNecessary
(Class<?>
the class to check
clazz
)

Resolve the given class if it is a primitive class, returning the corresponding primitive wrapper type instead.

public static Class<?>[]

Returns:

the Class array
toClassArray
(Collection<Class<?>>
the Collection to copy
collection
)

Copy the given Collection into a Class array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait