类 ReflectionUtils
Only intended for internal use.
- 从以下版本开始:
- 1.2.2
- 作者:
- Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen
-
嵌套类概要
嵌套类修饰符和类型类说明static 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 method fields to be operated on by a method callback. -
字段概要
字段修饰符和类型字段说明static ReflectionUtils.FieldFilterPre-built FieldFilter that matches all non-static, non-final fields.static ReflectionUtils.MethodFilterPre-built MethodFilter that matches all non-bridge methods.static ReflectionUtils.MethodFilterPre-built MethodFilter that matches all non-bridge methods which are not declared onjava.lang.Object. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明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, 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 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, ReflectionUtils.MethodFilter mf) Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).static Fieldstatic Fieldstatic MethodfindMethod(Class<?> clazz, String name) Attempt to find aMethodon the supplied class with the supplied name and no parameters.static MethodfindMethod(Class<?> clazz, String name, Class<?>... 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 ObjectGet the field represented by the suppliedfield objecton the specifiedtarget object.static voidHandle the given invocation target exception.static voidHandle the given reflection exception.static ObjectinvokeJdbcMethod(Method method, Object target) Invoke the specified JDBC APIMethodagainst the supplied target object with no arguments.static ObjectinvokeJdbcMethod(Method method, Object target, Object... args) Invoke the specified JDBC APIMethodagainst the supplied target object with the supplied arguments.static ObjectinvokeMethod(Method method, Object target) Invoke the specifiedMethodagainst the supplied target object with no arguments.static ObjectinvokeMethod(Method method, Object target, Object... args) Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static booleanisEqualsMethod(Method method) Determine whether the given method is an "equals" method.static booleanisHashCodeMethod(Method method) Determine whether the given method is a "hashCode" method.static booleanisPublicStaticFinal(Field field) Determine whether the given field is a "public static final" constant.static booleanisToStringMethod(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 voidRethrow 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 suppliedfield objecton the specifiedtarget objectto 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.
-
字段详细资料
-
COPYABLE_FIELDS
Pre-built FieldFilter that matches all non-static, non-final fields. -
NON_BRIDGED_METHODS
Pre-built MethodFilter that matches all non-bridge methods. -
USER_DECLARED_METHODS
Pre-built MethodFilter that matches all non-bridge methods which are not declared onjava.lang.Object.
-
-
构造器详细资料
-
ReflectionUtils
public ReflectionUtils()
-
-
方法详细资料
-
findField
Attempt to find afieldon the suppliedClasswith the suppliedname. Searches all superclasses up toObject.- 参数:
clazz- the class to introspectname- the name of the field- 返回:
- the corresponding Field object, or
nullif not found
-
findField
Attempt to find afieldon the suppliedClasswith the suppliednameand/ortype. Searches all superclasses up toObject.- 参数:
clazz- the class to introspectname- the name of the field (may benullif type is specified)type- the type of the field (may benullif name is specified)- 返回:
- the corresponding Field object, or
nullif not found
-
setField
Set the field represented by the suppliedfield objecton the specifiedtarget objectto the specifiedvalue. In accordance withField.set(Object, Object)semantics, the new value is automatically unwrapped if the underlying field has a primitive type.Thrown exceptions are handled via a call to
handleReflectionException(Exception).- 参数:
field- the field to settarget- the target object on which to set the fieldvalue- the value to set; may benull
-
getField
Get 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).- 参数:
field- the field to gettarget- the target object from which to get the field- 返回:
- the field's current value
-
findMethod
Attempt to find aMethodon the supplied class with the supplied name and no parameters. Searches all superclasses up toObject.Returns
nullif noMethodcan be found.- 参数:
clazz- the class to introspectname- the name of the method- 返回:
- the Method object, or
nullif none found
-
findMethod
Attempt to find aMethodon the supplied class with the supplied name and parameter types. Searches all superclasses up toObject.Returns
nullif noMethodcan be found.- 参数:
clazz- the class to introspectname- the name of the methodparamTypes- the parameter types of the method (may benullto indicate any signature)- 返回:
- the Method object, or
nullif none found
-
invokeMethod
Invoke 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(java.lang.Exception).- 参数:
method- the method to invoketarget- the target object to invoke the method on- 返回:
- the invocation result, if any
- 另请参阅:
-
invokeMethod
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(java.lang.Exception).- 参数:
method- the method to invoketarget- the target object to invoke the method onargs- the invocation arguments (may benull)- 返回:
- the invocation result, if any
-
invokeJdbcMethod
Invoke the specified JDBC APIMethodagainst the supplied target object with no arguments.- 参数:
method- the method to invoketarget- the target object to invoke the method on- 返回:
- the invocation result, if any
- 抛出:
SQLException- the JDBC API SQLException to rethrow (if any)- 另请参阅:
-
invokeJdbcMethod
public static Object invokeJdbcMethod(Method method, Object target, Object... args) throws SQLException Invoke the specified JDBC APIMethodagainst the supplied target object with the supplied arguments.- 参数:
method- the method to invoketarget- the target object to invoke the method onargs- the invocation arguments (may benull)- 返回:
- the invocation result, if any
- 抛出:
SQLException- the JDBC API SQLException to rethrow (if any)- 另请参阅:
-
handleReflectionException
Handle the given reflection 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 an InvocationTargetException with such a root cause. Throws an IllegalStateException with an appropriate message else.
- 参数:
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 IllegalStateException else.
- 参数:
ex- the invocation target exception to handle
-
rethrowRuntimeException
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
RuntimeExceptionorErrorif appropriate; otherwise, throws anIllegalStateException.- 参数:
ex- the exception to rethrow- 抛出:
RuntimeException- the rethrown exception
-
rethrowException
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 anIllegalStateException.- 参数:
ex- the exception to rethrow- 抛出:
Exception- the rethrown exception (in case of a checked exception)
-
declaresException
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.- 参数:
method- the declaring methodexceptionType- the exception to throw- 返回:
trueif the exception can be thrown as-is;falseif it needs to be wrapped
-
isPublicStaticFinal
Determine whether the given field is a "public static final" constant.- 参数:
field- the field to check
-
isEqualsMethod
Determine whether the given method is an "equals" method.- 另请参阅:
-
isHashCodeMethod
Determine whether the given method is a "hashCode" method.- 另请参阅:
-
isToStringMethod
Determine whether the given method is a "toString" method.- 另请参阅:
-
makeAccessible
Make the given field accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- 参数:
field- the field to make accessible- 另请参阅:
-
makeAccessible
Make the given method accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- 参数:
method- the method to make accessible- 另请参阅:
-
makeAccessible
Make the given constructor accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- 参数:
ctor- the constructor to make accessible- 另请参阅:
-
doWithMethods
public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc) throws IllegalArgumentException 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.- 参数:
clazz- class to start looking atmc- the callback to invoke for each method- 抛出:
IllegalArgumentException- 另请参阅:
-
doWithMethods
public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf) throws IllegalArgumentException 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.- 参数:
clazz- class to start looking atmc- the callback to invoke for each methodmf- the filter that determines the methods to apply the callback to- 抛出:
IllegalArgumentException
-
getAllDeclaredMethods
Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first. -
doWithFields
public static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc) throws IllegalArgumentException Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.- 参数:
clazz- the target class to analyzefc- the callback to invoke for each field- 抛出:
IllegalArgumentException
-
doWithFields
public static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff) throws IllegalArgumentException Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.- 参数:
clazz- the target class to analyzefc- the callback to invoke for each fieldff- the filter that determines the fields to apply the callback to- 抛出:
IllegalArgumentException
-
shallowCopyFieldState
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.- 抛出:
IllegalArgumentException- if the arguments are incompatible
-