| java.lang.Object org.springframework.util.ReflectionUtils
ReflectionUtils | abstract public class ReflectionUtils (Code) | | Simple utility class for working with the reflection API and handling
reflection exceptions.
Only intended for internal use.
author: Juergen Hoeller author: Rob Harrop author: Rod Johnson author: Costin Leau author: Sam Brannen since: 1.2.2 |
Inner Class :public static interface MethodCallback | |
Inner Class :public static interface MethodFilter | |
Inner Class :public static interface FieldCallback | |
Inner Class :public static interface FieldFilter | |
Field Summary | |
public static FieldFilter | COPYABLE_FIELDS Pre-built FieldFilter that matches all non-static, non-final fields. |
Method Summary | |
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. | public static void | doWithFields(Class targetClass, FieldCallback 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 targetClass, FieldCallback fc, FieldFilter 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 | doWithMethods(Class targetClass, MethodCallback mc) Perform the given callback operation on all matching methods of the
given class and superclasses. | public static void | doWithMethods(Class targetClass, MethodCallback mc, MethodFilter mf) Perform the given callback operation on all matching methods of the
given class and superclasses. | public static Field | findField(Class clazz, String name, Class type) Attempt to find a
Field field on the supplied
Class with
the supplied name and
Class type . | public static Method | findMethod(Class clazz, String name) Attempt to find a
Method on the supplied class with the supplied name
and no parameters. | public static Method | findMethod(Class clazz, String name, Class[] paramTypes) Attempt to find a
Method on the supplied class with the supplied name
and parameter types. | public static Method[] | getAllDeclaredMethods(Class leafClass) Get all declared methods on the leaf class and all superclasses. | public static void | handleInvocationTargetException(InvocationTargetException ex) Handle the given invocation target exception. | public static void | handleReflectionException(Exception ex) Handle the given reflection exception. | public static Object | invokeMethod(Method method, Object target) Invoke the specified
Method against the supplied target object
with no arguments. | public static Object | invokeMethod(Method method, Object target, Object[] args) Invoke the specified
Method against the supplied target object
with the supplied arguments. | public static boolean | isPublicStaticFinal(Field field) Determine whether the given field is a "public static final" constant. | public static void | makeAccessible(Field field) Make the given field accessible, explicitly setting it accessible if necessary. | public static void | makeAccessible(Method method) Make the given method accessible, explicitly setting it accessible if necessary. | public static void | makeAccessible(Constructor ctor) Make the given constructor accessible, explicitly setting it accessible if necessary. | public static void | rethrowException(Throwable ex) Rethrow the given
Throwable exception , which is presumably the
target exception of an
InvocationTargetException . | public static void | setField(Field field, Object target, Object value) Set the field represented by the supplied
Field field object on
the specified
Object target object to the specified
value . | 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. |
COPYABLE_FIELDS | public static FieldFilter COPYABLE_FIELDS(Code) | | Pre-built FieldFilter that matches all non-static, non-final fields.
|
declaresException | public static boolean declaresException(Method method, Class exceptionType)(Code) | | 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 Parameters: exceptionType - the exception to throw true if the exception can be thrown as-is;false if it needs to be wrapped |
doWithFields | public static void doWithFields(Class targetClass, FieldCallback fc) throws IllegalArgumentException(Code) | | Invoke the given callback on all fields in the target class,
going up the class hierarchy to get all declared fields.
Parameters: targetClass - the target class to analyze Parameters: fc - the callback to invoke for each field |
doWithFields | public static void doWithFields(Class targetClass, FieldCallback fc, FieldFilter ff) throws IllegalArgumentException(Code) | | Invoke the given callback on all fields in the target class,
going up the class hierarchy to get all declared fields.
Parameters: targetClass - the target class to analyze Parameters: fc - the callback to invoke for each field Parameters: ff - the filter that determines the fields to apply the callback to |
doWithMethods | public static void doWithMethods(Class targetClass, MethodCallback mc) throws IllegalArgumentException(Code) | | 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
MethodFilter .
Parameters: targetClass - class to start looking at Parameters: mc - the callback to invoke for each method See Also: ReflectionUtils.doWithMethods(Class,MethodCallback,MethodFilter) |
doWithMethods | public static void doWithMethods(Class targetClass, MethodCallback mc, MethodFilter mf) throws IllegalArgumentException(Code) | | 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 the specified
MethodFilter .
Parameters: targetClass - class to start looking at Parameters: mc - the callback to invoke for each method Parameters: mf - the filter that determines the methods to apply the callback to |
findField | public static Field findField(Class clazz, String name, Class type)(Code) | | Attempt to find a
Field field on the supplied
Class with
the supplied name and
Class type . Searches all
superclasses up to
Object .
Parameters: clazz - the class to introspect Parameters: name - the name of the field Parameters: type - the type of the field the corresponding Field object, or null if not found throws: IllegalArgumentException - if the class or field type isnull or if the field name is empty |
findMethod | public static Method findMethod(Class clazz, String name)(Code) | | 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 Parameters: name - the name of the method the Method object, or null if none found |
findMethod | public static Method findMethod(Class clazz, String name, Class[] paramTypes)(Code) | | 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 Parameters: name - the name of the method Parameters: paramTypes - the parameter types of the method the Method object, or null if none found |
getAllDeclaredMethods | public static Method[] getAllDeclaredMethods(Class leafClass) throws IllegalArgumentException(Code) | | Get all declared methods on the leaf class and all superclasses.
Leaf class methods are included first.
|
handleInvocationTargetException | public static void handleInvocationTargetException(InvocationTargetException ex)(Code) | | 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.
Parameters: ex - the invocation target exception to handle |
handleReflectionException | public static void handleReflectionException(Exception ex)(Code) | | 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.
Parameters: ex - the reflection exception to handle |
isPublicStaticFinal | public static boolean isPublicStaticFinal(Field field)(Code) | | Determine whether the given field is a "public static final" constant.
Parameters: field - the field to check |
makeAccessible | public static void makeAccessible(Field field)(Code) | | 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 with a JVM SecurityManager (if active).
Parameters: field - the field to make accessible See Also: java.lang.reflect.Field.setAccessible |
makeAccessible | public static void makeAccessible(Method method)(Code) | | 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 with a JVM SecurityManager (if active).
Parameters: method - the method to make accessible See Also: java.lang.reflect.Method.setAccessible |
makeAccessible | public static void makeAccessible(Constructor ctor)(Code) | | 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 with a JVM SecurityManager (if active).
Parameters: ctor - the constructor to make accessible See Also: java.lang.reflect.Constructor.setAccessible |
rethrowException | public static void rethrowException(Throwable ex) throws Exception(Code) | | Rethrow the given
Throwable exception , which is presumably the
target exception of an
InvocationTargetException .
Should only be called if no checked exception is expected to be thrown by
the target method.
Rethrows the underlying exception cast to an
Exception or
Error if appropriate; otherwise, throws an
IllegalStateException .
Parameters: ex - the exception to rethrow throws: Exception - the rethrown exception (in case of a checked exception) |
setField | public static void setField(Field field, Object target, Object value)(Code) | | Set the field represented by the supplied
Field field object on
the specified
Object target object to the specified
value . In accordance with
Field.set(ObjectObject) semantics, the new value is
automatically unwrapped if the underlying field has a primitive type.
Thrown exceptions are handled via a call to
ReflectionUtils.handleReflectionException(Exception) .
Parameters: field - the field to set Parameters: target - the target object on which to set the field Parameters: value - the value to set; may be null |
shallowCopyFieldState | public static void shallowCopyFieldState(Object src, Object dest) throws IllegalArgumentException(Code) | | 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: IllegalArgumentException - if the arguments are incompatible |
|
|