public abstract class
ReflectionUtils
extends
Object
Simple utility class for working with the reflection API and handling
reflection exceptions.
Only intended for internal use.
Since:
1.2.2
Author:
Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
Callback optionally used to filter fields to be operated on by a field callback.
static interface
Action to take on each method.
static interface
Callback optionally used to filter methods to be operated on by a method callback.
Pre-built FieldFilter that matches all non-static, non-final fields.
Obtain an accessible constructor for the given class and parameters.
static void
Clear the internal method/field cache.
static boolean
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.
static void
Invoke the given callback on all fields in the target class, going up the
class hierarchy to get all declared fields.
static void
Invoke the given callback on all fields in the target class, going up the
class hierarchy to get all declared fields.
static void
Invoke the given callback on all locally declared fields in the given class.
static void
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).
static void
Perform the given callback operation on all matching methods of the given
class and superclasses.
static void
Perform the given callback operation on all matching methods of the given
class and superclasses (or given interface and super-interfaces).
Attempt to find a
field
on the supplied
Class
with the
supplied
name
.
Attempt to find a
field
on the supplied
Class
with the
supplied
name
and/or
type
.
Attempt to find a
Method
on the supplied class with the supplied name
and no parameters.
Attempt to find a
Method
on the supplied class with the supplied name
and parameter types.
Get all declared methods on the leaf class and all superclasses.
Get the unique set of declared methods on the leaf class and all superclasses.
Get the unique set of declared methods on the leaf class and all superclasses.
static void
Handle the given invocation target exception.
static void
Handle the given reflection exception.
Invoke the specified
Method
against the supplied target object with no arguments.
Invoke the specified
Method
against the supplied target object with the
supplied arguments.
static boolean
Determine whether the given method is a CGLIB 'renamed' method,
following the pattern "CGLIB$methodName$0".
static boolean
Determine whether the given method is an "equals" method.
static boolean
Determine whether the given method is a "hashCode" method.
static boolean
Determine whether the given method is originally declared by
Object
.
static boolean
Determine whether the given field is a "public static final" constant.
static boolean
Determine whether the given method is a "toString" method.
static void
Make the given constructor accessible, explicitly setting it accessible
if necessary.
static void
Make the given field accessible, explicitly setting it accessible if
necessary.
static void
Make the given method accessible, explicitly setting it accessible if
necessary.
static void
static void
static void
static void
Given the source object and the destination, which must be the same class
or a subclass, copy all fields, including inherited fields.
USER_DECLARED_METHODS
Since:
3.0.5
handleReflectionException
public static
void
handleReflectionException
(
Exception
ex)
Handle the given reflection exception.
Should only be called if no checked exception is expected to be thrown
by a target method, or if an error occurs while accessing a method or field.
Throws the underlying RuntimeException or Error in case of an
InvocationTargetException with such a root cause. Throws an
IllegalStateException with an appropriate message or
UndeclaredThrowableException otherwise.
Parameters:
ex
- the reflection exception to handle
handleInvocationTargetException
Handle the given invocation target exception. Should only be called if no
checked exception is expected to be thrown by the target method.
Throws the underlying RuntimeException or Error in case of such a root
cause. Throws an UndeclaredThrowableException otherwise.
Parameters:
ex
- the invocation target exception to handle
rethrowRuntimeException
public static
void
rethrowRuntimeException
(
Throwable
ex)
Parameters:
ex
- the exception to rethrow
Throws:
RuntimeException
- the rethrown exception
rethrowException
Parameters:
throwable
- the exception to rethrow
Throws:
Exception
- the rethrown exception (in case of a checked exception)
accessibleConstructor
Obtain an accessible constructor for the given class and parameters.
Parameters:
clazz
- the clazz to check
parameterTypes
- the parameter types of the desired constructor
Returns:
the constructor reference
Throws:
NoSuchMethodException
- if no such constructor exists
Since:
makeAccessible
public static
void
makeAccessible
(
Constructor
<?> ctor)
Make the given constructor accessible, explicitly setting it accessible
if necessary. The
setAccessible(true)
method is only called
when actually necessary, to avoid unnecessary conflicts.
Parameters:
ctor
- the constructor to make accessible
See Also:
Constructor.setAccessible(boolean)
public static
Method
findMethod
(
Class
<?> clazz,
String
name)
Attempt to find a
Method
on the supplied class with the supplied name
and no parameters. Searches all superclasses up to
Object
.
Returns
null
if no
Method
can be found.
Parameters:
clazz
- the class to introspect
name
- the name of the method
Returns:
the Method object, or
null
if none found
@Nullable
Class
<?>... paramTypes)
Attempt to find a
Method
on the supplied class with the supplied name
and parameter types. Searches all superclasses up to
Object
.
Returns
null
if no
Method
can be found.
Parameters:
clazz
- the class to introspect
name
- the name of the method
paramTypes
- the parameter types of the method
(may be
null
to indicate any signature)
Returns:
the Method object, or
null
if none found
@Nullable
Object
target)
Parameters:
method
- the method to invoke
target
- the target object to invoke the method on
Returns:
the invocation result, if any
See Also:
invokeMethod(java.lang.reflect.Method, Object, Object[])
@Nullable
Object
... args)
Parameters:
method
- the method to invoke
target
- the target object to invoke the method on
args
- the invocation arguments (may be
null
)
Returns:
the invocation result, if any
declaresException
public static
boolean
declaresException
(
Method
method,
Class
<?> 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.
Parameters:
method
- the declaring method
exceptionType
- the exception to throw
Returns:
true
if the exception can be thrown as-is;
false
if it needs to be wrapped
doWithLocalMethods
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).
Parameters:
clazz
- the class to introspect
mc
- the callback to invoke for each method
Throws:
IllegalStateException
- if introspection fails
Since:
See Also:
doWithMethods(java.lang.Class<?>, org.springframework.util.ReflectionUtils.MethodCallback)
doWithMethods
Perform the given callback operation on all matching methods of the given
class and superclasses.
The same named method occurring on subclass and superclass will appear
twice, unless excluded by a
ReflectionUtils.MethodFilter
.
Parameters:
clazz
- the class to introspect
mc
- the callback to invoke for each method
Throws:
IllegalStateException
- if introspection fails
See Also:
doWithMethods(Class, MethodCallback, MethodFilter)
doWithMethods
Perform the given callback operation on all matching methods of the given
class and superclasses (or given interface and super-interfaces).
The same named method occurring on subclass and superclass will appear
twice, unless excluded by the specified
ReflectionUtils.MethodFilter
.
Parameters:
clazz
- the class to introspect
mc
- the callback to invoke for each method
mf
- the filter that determines the methods to apply the callback to
Throws:
IllegalStateException
- if introspection fails
getAllDeclaredMethods
public static
Method
[]
getAllDeclaredMethods
(
Class
<?> leafClass)
Get all declared methods on the leaf class and all superclasses.
Leaf class methods are included first.
Parameters:
leafClass
- the class to introspect
Throws:
IllegalStateException
- if introspection fails
getUniqueDeclaredMethods
public static
Method
[]
getUniqueDeclaredMethods
(
Class
<?> leafClass)
Get the unique set of declared methods on the leaf class and all superclasses.
Leaf class methods are included first and while traversing the superclass hierarchy
any methods found with signatures matching a method already included are filtered out.
Parameters:
leafClass
- the class to introspect
Throws:
IllegalStateException
- if introspection fails
getUniqueDeclaredMethods
Get the unique set of declared methods on the leaf class and all superclasses.
Leaf class methods are included first and while traversing the superclass hierarchy
any methods found with signatures matching a method already included are filtered out.
Parameters:
leafClass
- the class to introspect
mf
- the filter that determines the methods to take into account
Throws:
IllegalStateException
- if introspection fails
Since:
getDeclaredMethods
public static
Method
[]
getDeclaredMethods
(
Class
<?> clazz)
Variant of
Class.getDeclaredMethods()
that uses a local cache in
order to avoid new Method instances. In addition, it also includes Java 8
default methods from locally implemented interfaces, since those are
effectively to be treated just like declared methods.
Parameters:
clazz
- the class to introspect
Returns:
the cached array of methods
Throws:
IllegalStateException
- if introspection fails
Since:
See Also:
Class.getDeclaredMethods()
Determine whether the given method is an "equals" method.
See Also:
Object.equals(Object)
Determine whether the given method is a "hashCode" method.
See Also:
Object.hashCode()
Determine whether the given method is a "toString" method.
See Also:
Object.toString()
isCglibRenamedMethod
public static
boolean
isCglibRenamedMethod
(
Method
renamedMethod)
Determine whether the given method is a CGLIB 'renamed' method,
following the pattern "CGLIB$methodName$0".
Parameters:
renamedMethod
- the method to check
makeAccessible
public static
void
makeAccessible
(
Method
method)
Make the given method accessible, explicitly setting it accessible if
necessary. The
setAccessible(true)
method is only called
when actually necessary, to avoid unnecessary conflicts.
Parameters:
method
- the method to make accessible
See Also:
Method.setAccessible(boolean)
public static
Field
findField
(
Class
<?> clazz,
String
name)
Attempt to find a
field
on the supplied
Class
with the
supplied
name
. Searches all superclasses up to
Object
.
Parameters:
clazz
- the class to introspect
name
- the name of the field
Returns:
the corresponding Field object, or
null
if not found
Class
<?> type)
Attempt to find a
field
on the supplied
Class
with the
supplied
name
and/or
type
. Searches all superclasses
up to
Object
.
Parameters:
clazz
- the class to introspect
name
- the name of the field (may be
null
if type is specified)
type
- the type of the field (may be
null
if name is specified)
Returns:
the corresponding Field object, or
null
if not found
Object
value)
Parameters:
field
- the field to set
target
- the target object on which to set the field
(or
null
for a static field)
value
- the value to set (may be
null
)
Object
target)
Parameters:
field
- the field to get
target
- the target object from which to get the field
(or
null
for a static field)
Returns:
the field's current value
doWithLocalFields
Invoke the given callback on all locally declared fields in the given class.
Parameters:
clazz
- the target class to analyze
fc
- the callback to invoke for each field
Throws:
IllegalStateException
- if introspection fails
Since:
See Also:
doWithFields(java.lang.Class<?>, org.springframework.util.ReflectionUtils.FieldCallback)
doWithFields
Invoke the given callback on all fields in the target class, going up the
class hierarchy to get all declared fields.
Parameters:
clazz
- the target class to analyze
fc
- the callback to invoke for each field
Throws:
IllegalStateException
- if introspection fails
doWithFields
Invoke the given callback on all fields in the target class, going up the
class hierarchy to get all declared fields.
Parameters:
clazz
- the target class to analyze
fc
- the callback to invoke for each field
ff
- the filter that determines the fields to apply the callback to
Throws:
IllegalStateException
- if introspection fails
shallowCopyFieldState
public static
void
shallowCopyFieldState
(
Object
src,
Object
dest)
Given the source object and the destination, which must be the same class
or a subclass, copy all fields, including inherited fields. Designed to
work on objects with public no-arg constructors.
Throws:
IllegalStateException
- if introspection fails
isPublicStaticFinal
public static
boolean
isPublicStaticFinal
(
Field
field)
Determine whether the given field is a "public static final" constant.
Parameters:
field
- the field to check
makeAccessible
public static
void
makeAccessible
(
Field
field)
Make the given field accessible, explicitly setting it accessible if
necessary. The
setAccessible(true)
method is only called
when actually necessary, to avoid unnecessary conflicts.
Parameters:
field
- the field to make accessible
See Also:
Field.setAccessible(boolean)