Top Description Inners Fields Constructors Methods
org.springframework.util

public abstract Class ReflectionUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .Field, .InvocationTargetException, .Method, .Modifier, .UndeclaredThrowableException, java.util.ArrayList, .Arrays, .List, .Map, org.springframework.lang.Nullable

Simple utility class for working with the reflection API and handling reflection exceptions.

Only intended for internal use.

Authors
Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
Since
1.2.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ReflectionUtils.FieldCallback

Callback interface invoked on each field in the hierarchy.

public static interface
ReflectionUtils.FieldFilter

Callback optionally used to filter fields to be operated on by a field callback.

public static interface
ReflectionUtils.MethodCallback

Action to take on each method.

public static interface
ReflectionUtils.MethodFilter

Callback optionally used to filter methods to be operated on by a method callback.

Field Summary

Modifier and TypeField and Description
private static final String
CGLIB_RENAMED_METHOD_PREFIX

Naming prefix for CGLIB-renamed methods.

public static final ReflectionUtils.FieldFilter
COPYABLE_FIELDS

Pre-built FieldFilter that matches all non-static, non-final fields.

private static final Map<Class<?>, Field[]>
declaredFieldsCache

Cache for Class#getDeclaredFields(), allowing for fast iteration.

private static final Map<Class<?>, Method[]>
declaredMethodsCache

Cache for Class#getDeclaredMethods() plus equivalent default methods from Java 8 based interfaces, allowing for fast iteration.

private static final Class<?>[]
private static final Field[]
private static final Method[]
private static final Object[]
public static final ReflectionUtils.MethodFilter
USER_DECLARED_METHODS

Pre-built MethodFilter that matches all non-bridge non-synthetic methods which are not declared on java.lang.Object.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <T> Constructor<T>

Returns:

the constructor reference
accessibleConstructor
(Class<T>
the clazz to check
clazz
,
Class<?>...
the parameter types of the desired constructor
parameterTypes
)

Obtain an accessible constructor for the given class and parameters.

public static void
clearCache()

Clear the internal method/field cache.

public static boolean

Returns:

true if the exception can be thrown as-is; false if it needs to be wrapped
declaresException
(Method
the declaring method
method
,
Class<?>
the exception to throw
exceptionType
)

Determine whether the given method explicitly declares the given exception or one of its superclasses, which means that an exception of that type can be propagated as-is within a reflective invocation.

public static void
doWithFields(Class<?>
the target class to analyze
clazz
,
ReflectionUtils.FieldCallback
the callback to invoke for each field
fc
)

Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

public static void
doWithFields(Class<?>
the target class to analyze
clazz
,
ReflectionUtils.FieldCallback
the callback to invoke for each field
fc
,
ReflectionUtils.FieldFilter
the filter that determines the fields to apply the callback to
ff
)

Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.

public static void
doWithLocalFields(Class<?>
the target class to analyze
clazz
,
ReflectionUtils.FieldCallback
the callback to invoke for each field
fc
)

Invoke the given callback on all locally declared fields in the given class.

public static void
doWithLocalMethods(Class<?>
the class to introspect
clazz
,
ReflectionUtils.MethodCallback
the callback to invoke for each method
mc
)

Perform the given callback operation on all matching methods of the given class, as locally declared or equivalent thereof (such as default methods on Java 8 based interfaces that the given class implements).

public static void
doWithMethods(Class<?>
the class to introspect
clazz
,
ReflectionUtils.MethodCallback
the callback to invoke for each method
mc
)

Perform the given callback operation on all matching methods of the given class and superclasses.

public static void
doWithMethods(Class<?>
the class to introspect
clazz
,
ReflectionUtils.MethodCallback
the callback to invoke for each method
mc
,
ReflectionUtils.MethodFilter
the filter that determines the methods to apply the callback to
mf
)

Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

private static List<Method>
public static Field

Returns:

the corresponding Field object, or null if not found
findField
(Class<?>
the class to introspect
clazz
,
String
the name of the field
name
)

Attempt to find a field on the supplied Class with the supplied name.

public static Field

Returns:

the corresponding Field object, or null if not found
findField
(Class<?>
the class to introspect
clazz
,
String
the name of the field (may be null if type is specified)
name
,
Class<?>
the type of the field (may be null if name is specified)
type
)

Attempt to find a field on the supplied Class with the supplied name and/or type.

public static Field

Returns:

the corresponding Field object, or null if not found
findFieldIgnoreCase
(Class<?>
the class to introspect
clazz
,
String
the name of the field (with upper/lower case to be ignored)
name
)

Attempt to find a field on the supplied Class with the supplied name.

public static Method

Returns:

the Method object, or null if none found
findMethod
(Class<?>
the class to introspect
clazz
,
String
the name of the method
name
)

Attempt to find a Method on the supplied class with the supplied name and no parameters.

public static Method

Returns:

the Method object, or null if none found
findMethod
(Class<?>
the class to introspect
clazz
,
String
the name of the method
name
,
Class<?>...
the parameter types of the method (may be null to indicate any signature)
paramTypes
)

Attempt to find a Method on the supplied class with the supplied name and parameter types.

public static Method[]
getAllDeclaredMethods(Class<?>
the class to introspect
leafClass
)

Get all declared methods on the leaf class and all superclasses.

private static Field[]

Returns:

the cached array of fields
getDeclaredFields
(Class<?>
the class to introspect
clazz
)

This variant retrieves Class#getDeclaredFields() from a local cache in order to avoid defensive array copying.

public static Method[]

Returns:

the cached array of methods
getDeclaredMethods
(Class<?>
the class to introspect
clazz
)

Variant of Class#getDeclaredMethods() that uses a local cache in order to avoid new Method instances.

private static Method[]
getDeclaredMethods(Class<?> clazz, boolean defensive)

public static Object

Returns:

the field's current value
getField
(Field
the field to get
field
,
Object
the target object from which to get the field (or null for a static field)
target
)

Get the field represented by the supplied field object on the specified target object.

public static Method[]
getUniqueDeclaredMethods(Class<?>
the class to introspect
leafClass
)

Get the unique set of declared methods on the leaf class and all superclasses.

public static Method[]
getUniqueDeclaredMethods(Class<?>
the class to introspect
leafClass
,
ReflectionUtils.MethodFilter
the filter that determines the methods to take into account
mf
)

Get the unique set of declared methods on the leaf class and all superclasses.

public static void
handleInvocationTargetException(InvocationTargetException
the invocation target exception to handle
ex
)

Handle the given invocation target exception.

public static void
handleReflectionException(Exception
the reflection exception to handle
ex
)

Handle the given reflection exception.

private static boolean
hasSameParams(Method method, Class<?>[] paramTypes)

public static Object

Returns:

the invocation result, if any
invokeMethod
(Method
the method to invoke
method
,
Object
the target object to invoke the method on
target
)

Invoke the specified Method against the supplied target object with no arguments.

public static Object

Returns:

the invocation result, if any
invokeMethod
(Method
the method to invoke
method
,
Object
the target object to invoke the method on
target
,
Object...
the invocation arguments (may be null)
args
)

Invoke the specified Method against the supplied target object with the supplied arguments.

public static boolean
isCglibRenamedMethod(Method
the method to check
renamedMethod
)

Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".

public static boolean
isEqualsMethod(Method method)

Determine whether the given method is an "equals" method.

public static boolean
isHashCodeMethod(Method method)

Determine whether the given method is a "hashCode" method.

public static boolean
isObjectMethod(Method method)

Determine whether the given method is originally declared by java.lang.Object.

public static boolean
isPublicStaticFinal(Field
the field to check
field
)

Determine whether the given field is a "public static final" constant.

public static boolean
isToStringMethod(Method method)

Determine whether the given method is a "toString" method.

public static void
makeAccessible(Constructor<?>
the constructor to make accessible
ctor
)

Make the given constructor accessible, explicitly setting it accessible if necessary.

public static void
makeAccessible(Method
the method to make accessible
method
)

Make the given method accessible, explicitly setting it accessible if necessary.

public static void
makeAccessible(Field
the field to make accessible
field
)

Make the given field accessible, explicitly setting it accessible if necessary.

public static void
rethrowException(Throwable
the exception to rethrow
throwable
)

Rethrow the given exception, which is presumably the target exception of an InvocationTargetException.

public static void
rethrowRuntimeException(Throwable
the exception to rethrow
ex
)

Rethrow the given exception, which is presumably the target exception of an InvocationTargetException.

public static void
setField(Field
the field to set
field
,
Object
the target object on which to set the field (or null for a static field)
target
,
Object
the value to set (may be null)
value
)

Set the field represented by the supplied field object on the specified target object to the specified value.

public static void
shallowCopyFieldState(final Object src, final Object dest)

Given the source object and the destination, which must be the same class or a subclass, copy all fields, including inherited fields.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait