The Spring Framework

org.springframework.util
Class ReflectionUtils

java.lang.Object
  extended by org.springframework.util.ReflectionUtils

public abstract class ReflectionUtils
extends Object

Simple utility class for handling reflection exceptions. Only intended for internal use.

Since:
1.2.2
Author:
Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau

Nested Class Summary
static interface ReflectionUtils.FieldCallback
          Callback interface invoked on each field in the hierarchy.
static interface ReflectionUtils.FieldFilter
          Callback optionally used to filter fields to be operated on by a field callback.
static interface ReflectionUtils.MethodCallback
          Action to take on each method
static interface ReflectionUtils.MethodFilter
          Callback optionally used to method fields to be operated on by a method callback.
 
Field Summary
static ReflectionUtils.FieldFilter COPYABLE_FIELDS
          FieldFilter that matches all non-static, non-final fields.
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static void doWithFields(Class targetClass, ReflectionUtils.FieldCallback fc)
          Invoke the given callback on all private fields in the target class, going up the class hierarchy to get all declared fields.
static void doWithFields(Class targetClass, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)
          Invoke the given callback on all private fields in the target class, going up the class hierarchy to get all declared fields.
static void doWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc)
          Perform the given callback operation on all matching methods of the given class and superclasses.
static void doWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf)
          Perform the given callback operation on all matching methods of the given class and superclasses.
static Method findMethod(Class type, String name, Class[] paramTypes)
          Attempt to find a Method on the supplied type with the supplied name and parameter types.
static Method[] getAllDeclaredMethods(Class leafClass)
          Get all declared methods on the leaf class and all superclasses.
static void handleInvocationTargetException(InvocationTargetException ex)
          Handle the given invocation target exception.
static void handleReflectionException(Exception ex)
          Handle the given reflection exception.
static Object invokeMethod(Method method, Object target)
          Invoke the specified Method against the supplied target object with no arguments The target object can be null when invoking a static Method.
static Object invokeMethod(Method method, Object target, Object[] args)
          Invoke the specified Method against the supplied target object with the supplied arguments The target object can be null when invoking a static Method.
static boolean isPublicStaticFinal(Field field)
          Determine whether the given field is a "public static final" constant.
static void makeAccessible(Field field)
          Make the given field accessible, explicitly setting it accessible if necessary.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYABLE_FIELDS

public static ReflectionUtils.FieldFilter COPYABLE_FIELDS
FieldFilter that matches all non-static, non-final fields.

Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

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 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

handleInvocationTargetException

public static void handleInvocationTargetException(InvocationTargetException ex)
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

findMethod

public static Method findMethod(Class type,
                                String name,
                                Class[] paramTypes)
Attempt to find a Method on the supplied type with the supplied name and parameter types. Searches all superclasses up to Object. Returns 'null' if no Method can be found.


invokeMethod

public static Object invokeMethod(Method method,
                                  Object target)
Invoke the specified Method against the supplied target object with no arguments The target object can be null when invoking a static Method.

See Also:
invokeMethod(java.lang.reflect.Method, Object, Object[])

invokeMethod

public static Object invokeMethod(Method method,
                                  Object target,
                                  Object[] args)
Invoke the specified Method against the supplied target object with the supplied arguments The target object can be null when invoking a static Method.

Thrown exceptions are handled via a call to handleReflectionException(Exception).

See Also:
invokeMethod(java.lang.reflect.Method, Object, Object[])

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 with a JVM SecurityManager (if active).

Parameters:
field - the field to make accessible
See Also:
AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)

doWithMethods

public static void doWithMethods(Class targetClass,
                                 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 the MethodFilter

Parameters:
targetClass - class to start looking at
mc - the callback to invoke for each method
Throws:
IllegalArgumentException

doWithMethods

public static void doWithMethods(Class targetClass,
                                 ReflectionUtils.MethodCallback mc,
                                 ReflectionUtils.MethodFilter mf)
                          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 the MethodFilter

Parameters:
targetClass - class to start looking at
mc - the callback to invoke for each method
mf - the filter that determines the methods to apply the callback to
Throws:
IllegalArgumentException

getAllDeclaredMethods

public static Method[] getAllDeclaredMethods(Class leafClass)
                                      throws IllegalArgumentException
Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.

Throws:
IllegalArgumentException

doWithFields

public static void doWithFields(Class targetClass,
                                ReflectionUtils.FieldCallback fc)
                         throws IllegalArgumentException
Invoke the given callback on all private fields in the target class, going up the class hierarchy to get all declared fields.

Parameters:
targetClass - the target class to analyze
fc - the callback to invoke for each field
Throws:
IllegalArgumentException

doWithFields

public static void doWithFields(Class targetClass,
                                ReflectionUtils.FieldCallback fc,
                                ReflectionUtils.FieldFilter ff)
                         throws IllegalArgumentException
Invoke the given callback on all private fields in the target class, going up the class hierarchy to get all declared fields.

Parameters:
targetClass - 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:
IllegalArgumentException

shallowCopyFieldState

public static void shallowCopyFieldState(Object src,
                                         Object dest)
                                  throws IllegalArgumentException
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 arguments are incompatible or either is null

The Spring Framework

Copyright © 2002-2006 The Spring Framework.