public abstract class ReflectionUtils
extends java.lang.Object
Title: ReflectionUtils.java
Description:
bboss workgroup
Copyright (c) 2008
| 限定符和类型 | 类和说明 |
|---|---|
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.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static ReflectionUtils.FieldFilter |
COPYABLE_FIELDS
Pre-built FieldFilter that matches all non-static, non-final fields.
|
| 构造器和说明 |
|---|
ReflectionUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static boolean |
declaresException(java.lang.reflect.Method method,
java.lang.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 void |
doWithMethods(java.lang.Class targetClass,
ReflectionUtils.MethodCallback mc)
Perform the given callback operation on all matching methods of the
given class and superclasses.
|
static void |
doWithMethods(java.lang.Class targetClass,
ReflectionUtils.MethodCallback mc,
ReflectionUtils.MethodFilter mf)
Perform the given callback operation on all matching methods of the
given class and superclasses.
|
static java.lang.reflect.Field |
findField(java.lang.Class clazz,
java.lang.String name)
Attempt to find a
field on the supplied Class with
the supplied name. |
static java.lang.reflect.Method |
findMethod(java.lang.Class clazz,
java.lang.String name)
Attempt to find a
Method on the supplied class with the supplied name
and no parameters. |
static java.lang.reflect.Method |
findMethod(java.lang.Class clazz,
java.lang.String name,
java.lang.Class... paramTypes)
Attempt to find a
Method on the supplied class with the supplied name
and parameter types. |
static java.lang.reflect.Method[] |
getAllDeclaredMethods(java.lang.Class leafClass)
Get all declared methods on the leaf class and all superclasses.
|
static java.lang.Object |
getField(java.lang.reflect.Field field,
java.lang.Object target)
Get the field represented by the supplied
field object on
the specified target object. |
static void |
handleInvocationTargetException(java.lang.reflect.InvocationTargetException ex)
Handle the given invocation target exception.
|
static void |
handleReflectionException(java.lang.Exception ex)
Handle the given reflection exception.
|
static java.lang.Object |
invokeJdbcMethod(java.lang.reflect.Method method,
java.lang.Object target)
Invoke the specified JDBC API
Method against the supplied
target object with no arguments. |
static java.lang.Object |
invokeJdbcMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
Invoke the specified JDBC API
Method against the supplied
target object with the supplied arguments. |
static java.lang.Object |
invokeMethod(java.lang.reflect.Method method,
java.lang.Object target)
Invoke the specified
Method against the supplied target object
with no arguments. |
static java.lang.Object |
invokeMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
Invoke the specified
Method against the supplied target object
with the supplied arguments. |
static boolean |
isAccessible(java.lang.reflect.Method method) |
static boolean |
isEqualsMethod(java.lang.reflect.Method method)
Determine whether the given method is an "equals" method.
|
static boolean |
isHashCodeMethod(java.lang.reflect.Method method)
Determine whether the given method is a "hashCode" method.
|
static boolean |
isPublicStaticFinal(java.lang.reflect.Field field)
Determine whether the given field is a "public static final" constant.
|
static boolean |
isToStringMethod(java.lang.reflect.Method method)
Determine whether the given method is a "toString" method.
|
static void |
makeAccessible(java.lang.reflect.Constructor ctor)
Make the given constructor accessible, explicitly setting it accessible if necessary.
|
static void |
makeAccessible(java.lang.reflect.Field field)
Make the given field accessible, explicitly setting it accessible if necessary.
|
static void |
makeAccessible(java.lang.reflect.Method method)
Make the given method accessible, explicitly setting it accessible if necessary.
|
static void |
rethrowException(java.lang.Throwable ex)
Rethrow the given
exception, which is presumably the
target exception of an InvocationTargetException. |
static void |
rethrowRuntimeException(java.lang.Throwable ex)
Rethrow the given
exception, which is presumably the
target exception of an InvocationTargetException. |
static void |
setField(java.lang.reflect.Field field,
java.lang.Object target,
java.lang.Object value)
Set the field represented by the supplied
field object on
the specified target object to the specified
value. |
public static ReflectionUtils.FieldFilter COPYABLE_FIELDS
public static java.lang.reflect.Field findField(java.lang.Class clazz,
java.lang.String name)
field on the supplied Class with
the supplied name. Searches all superclasses up to Object.clazz - the class to introspectname - the name of the fieldnull if not foundpublic static void setField(java.lang.reflect.Field field,
java.lang.Object target,
java.lang.Object value)
field object on
the specified target object to the specified
value. In accordance with
Field.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 be nullpublic static java.lang.Object getField(java.lang.reflect.Field field,
java.lang.Object target)
field object on
the specified target object. In accordance with
Field.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 fieldpublic static java.lang.reflect.Method findMethod(java.lang.Class clazz,
java.lang.String name)
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.
clazz - the class to introspectname - the name of the methodnull if none foundpublic static java.lang.reflect.Method findMethod(java.lang.Class clazz,
java.lang.String name,
java.lang.Class... paramTypes)
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.
clazz - the class to introspectname - the name of the methodparamTypes - the parameter types of the method
(may be null to indicate any signature)null if none foundpublic static java.lang.Object invokeMethod(java.lang.reflect.Method method,
java.lang.Object target)
Method against the supplied target object
with no arguments. The target object can be null when
invoking a static Method.
Thrown exceptions are handled via a call to handleReflectionException(java.lang.Exception).
method - the method to invoketarget - the target object to invoke the method oninvokeMethod(Method, Object, Object[])public static java.lang.Object invokeMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
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(java.lang.Exception).
method - the method to invoketarget - the target object to invoke the method onargs - the invocation arguments (may be null)public static java.lang.Object invokeJdbcMethod(java.lang.reflect.Method method,
java.lang.Object target)
throws java.sql.SQLException
Method against the supplied
target object with no arguments.method - the method to invoketarget - the target object to invoke the method onjava.sql.SQLException - the JDBC API SQLException to rethrow (if any)invokeJdbcMethod(Method, Object, Object[])public static java.lang.Object invokeJdbcMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
throws java.sql.SQLException
Method against 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 be null)java.sql.SQLException - the JDBC API SQLException to rethrow (if any)invokeMethod(Method, Object, Object[])public static void handleReflectionException(java.lang.Exception ex)
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 handlepublic static void handleInvocationTargetException(java.lang.reflect.InvocationTargetException ex)
Throws the underlying RuntimeException or Error in case of such a root cause. Throws an IllegalStateException else.
ex - the invocation target exception to handlepublic static void rethrowRuntimeException(java.lang.Throwable ex)
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 RuntimeException
or Error if appropriate; otherwise, throws an
IllegalStateException.
ex - the exception to rethrowjava.lang.RuntimeException - the rethrown exceptionpublic static void rethrowException(java.lang.Throwable ex)
throws java.lang.Exception
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.
ex - the exception to rethrowjava.lang.Exception - the rethrown exception (in case of a checked exception)public static boolean declaresException(java.lang.reflect.Method method,
java.lang.Class exceptionType)
method - the declaring methodexceptionType - the exception to throwtrue if the exception can be thrown as-is;
false if it needs to be wrappedpublic static boolean isPublicStaticFinal(java.lang.reflect.Field field)
field - the field to checkpublic static boolean isEqualsMethod(java.lang.reflect.Method method)
Object.equals(java.lang.Object)public static boolean isHashCodeMethod(java.lang.reflect.Method method)
Object.hashCode()public static boolean isToStringMethod(java.lang.reflect.Method method)
Object.toString()public static void makeAccessible(java.lang.reflect.Field field)
setAccessible(true) method is only called when actually necessary,
to avoid unnecessary conflicts with a JVM SecurityManager (if active).field - the field to make accessibleAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)public static void makeAccessible(java.lang.reflect.Method method)
setAccessible(true) method is only called when actually necessary,
to avoid unnecessary conflicts with a JVM SecurityManager (if active).method - the method to make accessibleAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)public static boolean isAccessible(java.lang.reflect.Method method)
public static void makeAccessible(java.lang.reflect.Constructor ctor)
setAccessible(true) method is only called when actually necessary,
to avoid unnecessary conflicts with a JVM SecurityManager (if active).ctor - the constructor to make accessibleAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)public static void doWithMethods(java.lang.Class targetClass,
ReflectionUtils.MethodCallback mc)
throws java.lang.IllegalArgumentException
The same named method occurring on subclass and superclass will
appear twice, unless excluded by a ReflectionUtils.MethodFilter.
targetClass - class to start looking atmc - the callback to invoke for each methodjava.lang.IllegalArgumentExceptiondoWithMethods(Class, MethodCallback, MethodFilter)public static void doWithMethods(java.lang.Class targetClass,
ReflectionUtils.MethodCallback mc,
ReflectionUtils.MethodFilter mf)
throws java.lang.IllegalArgumentException
The same named method occurring on subclass and superclass will
appear twice, unless excluded by the specified ReflectionUtils.MethodFilter.
targetClass - class to start looking atmc - the callback to invoke for each methodmf - the filter that determines the methods to apply the callback tojava.lang.IllegalArgumentExceptionpublic static java.lang.reflect.Method[] getAllDeclaredMethods(java.lang.Class leafClass)
throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException