Class ReflectionUtils
Only intended for internal use.
- Since:
- 1.2.2
- Author:
- Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface invoked on each field in the hierarchy.static interfaceCallback optionally used to filter fields to be operated on by a field callback.static interfaceAction to take on each method.static interfaceCallback optionally used to filter methods to be operated on by a method callback.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final ReflectionUtils.FieldFilterPre-built FieldFilter that matches all non-static, non-final fields.static final ReflectionUtils.MethodFilterPre-builtReflectionUtils.MethodFilterthat matches all non-bridge non-synthetic methods which are not declared onjava.lang.Object.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) Obtain an accessible constructor for the given class and parameters.static voidClear the internal method/field cache.static booleandeclaresException(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.static voiddoWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.static voiddoWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, @Nullable ReflectionUtils.FieldFilter ff) Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.static voiddoWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) Invoke the given callback on all locally declared fields in the given class.static voiddoWithLocalMethods(Class<?> clazz, ReflectionUtils.MethodCallback 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).static voiddoWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc) Perform the given callback operation on all matching methods of the given class and superclasses.static voiddoWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, @Nullable ReflectionUtils.MethodFilter mf) Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).findFieldIgnoreCase(Class<?> clazz, String name) findMethod(Class<?> clazz, String name) Attempt to find aMethodon the supplied class with the supplied name and no parameters.findMethod(Class<?> clazz, String name, Class<?> @Nullable ... paramTypes) Attempt to find aMethodon the supplied class with the supplied name and parameter types.static Method[]getAllDeclaredMethods(Class<?> leafClass) Get all declared methods on the leaf class and all superclasses.static Method[]getDeclaredMethods(Class<?> clazz) Variant ofClass.getDeclaredMethods()that uses a local cache in order to avoid new Method instances.Get the field represented by the suppliedfield objecton the specifiedtarget object.static Method[]getUniqueDeclaredMethods(Class<?> leafClass) Get the unique set of declared methods on the leaf class and all superclasses.static Method[]getUniqueDeclaredMethods(Class<?> leafClass, @Nullable ReflectionUtils.MethodFilter mf) Get the unique set of declared methods on the leaf class and all superclasses.static voidHandle the given invocation target exception.static voidHandle the given reflection exception.invokeMethod(Method method, @Nullable Object target) Invoke the specifiedMethodagainst the supplied target object with no arguments.Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static booleanisCglibRenamedMethod(Method renamedMethod) Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".static booleanisEqualsMethod(@Nullable Method method) Determine whether the given method is an "equals" method.static booleanisHashCodeMethod(@Nullable Method method) Determine whether the given method is a "hashCode" method.static booleanisObjectMethod(@Nullable Method method) Determine whether the given method is originally declared byObject.static booleanisPublicStaticFinal(Field field) Determine whether the given field is a "public static final" constant.static booleanisToStringMethod(@Nullable Method method) Determine whether the given method is a "toString" method.static voidmakeAccessible(Constructor<?> ctor) Make the given constructor accessible, explicitly setting it accessible if necessary.static voidmakeAccessible(Field field) Make the given field accessible, explicitly setting it accessible if necessary.static voidmakeAccessible(Method method) Make the given method accessible, explicitly setting it accessible if necessary.static voidrethrowException(@Nullable Throwable throwable) Rethrow the givenexception, which is presumably the target exception of anInvocationTargetException.static voidRethrow the givenexception, which is presumably the target exception of anInvocationTargetException.static voidSet the field represented by the supplied field object on the specified target object to the specifiedvalue.static voidshallowCopyFieldState(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.
- 
Field Details- 
USER_DECLARED_METHODSPre-builtReflectionUtils.MethodFilterthat matches all non-bridge non-synthetic methods which are not declared onjava.lang.Object.- Since:
- 3.0.5
 
- 
COPYABLE_FIELDSPre-built FieldFilter that matches all non-static, non-final fields.
 
- 
- 
Constructor Details- 
ReflectionUtilspublic ReflectionUtils()
 
- 
- 
Method Details- 
handleReflectionExceptionHandle 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
 
- 
handleInvocationTargetExceptionHandle 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
 
- 
rethrowRuntimeExceptionRethrow the givenexception, which is presumably the target exception of anInvocationTargetException. Should only be called if no checked exception is expected to be thrown by the target method.Rethrows the underlying exception cast to a RuntimeExceptionorErrorif appropriate; otherwise, throws anUndeclaredThrowableException.- Parameters:
- ex- the exception to rethrow
- Throws:
- RuntimeException- the rethrown exception
 
- 
rethrowException@Contract("_ -> fail") public static void rethrowException(@Nullable Throwable throwable) throws Exception Rethrow the givenexception, which is presumably the target exception of anInvocationTargetException. Should only be called if no checked exception is expected to be thrown by the target method.Rethrows the underlying exception cast to an ExceptionorErrorif appropriate; otherwise, throws anUndeclaredThrowableException.- Parameters:
- throwable- the exception to rethrow
- Throws:
- Exception- the rethrown exception (in case of a checked exception)
 
- 
accessibleConstructorpublic static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) throws NoSuchMethodException 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:
- 5.0
 
- 
makeAccessibleMake the given constructor accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts.- Parameters:
- ctor- the constructor to make accessible
- See Also:
 
- 
findMethod
- 
findMethodpublic static @Nullable Method findMethod(Class<?> clazz, String name, Class<?> @Nullable ... paramTypes) Attempt to find aMethodon the supplied class with the supplied name and parameter types. Searches all superclasses up toObject.Returns nullif noMethodcan be found.- Parameters:
- clazz- the class to introspect
- name- the name of the method
- paramTypes- the parameter types of the method (may be- nullto indicate any signature)
- Returns:
- the Method object, or nullif none found
 
- 
invokeMethodInvoke the specifiedMethodagainst the supplied target object with no arguments. The target object can benullwhen invoking a staticMethod.Thrown exceptions are handled via a call to handleReflectionException(Exception).- Parameters:
- method- the method to invoke
- target- the target object to invoke the method on
- Returns:
- the invocation result, if any
- See Also:
 
- 
invokeMethodpublic static @Nullable Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) Invoke the specifiedMethodagainst the supplied target object with the supplied arguments. The target object can benullwhen invoking a staticMethod.Thrown exceptions are handled via a call to handleReflectionException(Exception).- 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
 
- 
declaresExceptionDetermine 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:
- trueif the exception can be thrown as-is;- falseif it needs to be wrapped
 
- 
doWithLocalMethodsPerform 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:
- 4.2
- See Also:
 
- 
doWithMethodsPerform 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:
 
- 
doWithMethodspublic static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, @Nullable ReflectionUtils.MethodFilter mf) 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
 
- 
getAllDeclaredMethodsGet 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
 
- 
getUniqueDeclaredMethodsGet 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
 
- 
getUniqueDeclaredMethodspublic static Method[] getUniqueDeclaredMethods(Class<?> leafClass, @Nullable ReflectionUtils.MethodFilter mf) 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:
- 5.2
 
- 
getDeclaredMethodsVariant ofClass.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:
- 5.2
- See Also:
 
- 
isEqualsMethod
- 
isHashCodeMethod
- 
isToStringMethod
- 
isObjectMethod
- 
isCglibRenamedMethodDetermine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".- Parameters:
- renamedMethod- the method to check
 
- 
makeAccessibleMake the given method accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts.- Parameters:
- method- the method to make accessible
- See Also:
 
- 
findField
- 
findField@Contract("_, null, null -> fail") public static @Nullable Field findField(Class<?> clazz, @Nullable String name, @Nullable Class<?> type) Attempt to find afieldon the suppliedClasswith the suppliednameand/ortype. Searches all superclasses up toObject.- Parameters:
- clazz- the class to introspect
- name- the name of the field (may be- nullif type is specified)
- type- the type of the field (may be- nullif name is specified)
- Returns:
- the corresponding Field object, or nullif not found
 
- 
findFieldIgnoreCase
- 
setFieldSet the field represented by the supplied field object on the specified target object to the specifiedvalue.In accordance with Field.set(Object, Object)semantics, the new value is automatically unwrapped if the underlying field has a primitive type.This method does not support setting static finalfields.Thrown exceptions are handled via a call to handleReflectionException(Exception).- Parameters:
- field- the field to set
- target- the target object on which to set the field (or- nullfor a static field)
- value- the value to set (may be- null)
 
- 
getFieldGet the field represented by the suppliedfield objecton the specifiedtarget object. In accordance withField.get(Object)semantics, the returned value is automatically wrapped if the underlying field has a primitive type.Thrown exceptions are handled via a call to handleReflectionException(Exception).- Parameters:
- field- the field to get
- target- the target object from which to get the field (or- nullfor a static field)
- Returns:
- the field's current value
 
- 
doWithLocalFieldsInvoke 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:
- 4.2
- See Also:
 
- 
doWithFieldsInvoke 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
 
- 
doWithFieldspublic static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, @Nullable ReflectionUtils.FieldFilter ff) 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
 
- 
shallowCopyFieldStateGiven 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
 
- 
isPublicStaticFinalDetermine whether the given field is a "public static final" constant.- Parameters:
- field- the field to check
 
- 
makeAccessibleMake the given field accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts.- Parameters:
- field- the field to make accessible
- See Also:
 
- 
clearCachepublic static void clearCache()Clear the internal method/field cache.- Since:
- 4.2.4
 
 
-