public final class ReflectHelper extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
ReflectHelper.FieldCallback
Callback interface invoked on each field in the hierarchy.
|
static interface |
ReflectHelper.FieldFilter
Callback optionally used to filter fields to be operated on by a field callback.
|
static interface |
ReflectHelper.MethodCallback
Action to take on each method.
|
static interface |
ReflectHelper.MethodFilter
Callback optionally used to filter methods to be operated on by a method callback.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static ReflectHelper.FieldFilter |
COPYABLE_FIELDS
Pre-built FieldFilter that matches all non-static, non-final fields.
|
static Class[] |
NO_PARAM_SIGNATURE |
static Object[] |
NO_PARAMS |
static ReflectHelper.MethodFilter |
NON_BRIDGED_METHODS
已过时。
as of 5.0.11, in favor of a custom
ReflectHelper.MethodFilter |
static Class[] |
SINGLE_OBJECT_PARAM_SIGNATURE |
static ReflectHelper.MethodFilter |
USER_DECLARED_METHODS
Pre-built MethodFilter that matches all non-bridge non-synthetic methods
which are not declared on
java.lang.Object. |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> Constructor<T> |
accessibleConstructor(Class<T> clazz,
Class<?>... parameterTypes)
Obtain an accessible constructor for the given class and parameters.
|
static Class |
classForName(String name)
已过时。
|
static Class |
classForName(String name,
Class caller)
Perform resolution of a class name.
|
static void |
clearCache()
Clear the internal method/field cache.
|
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.
|
static void |
doWithFields(Class<?> clazz,
ReflectHelper.FieldCallback fc)
Invoke the given callback on all fields in the target class, going up the
class hierarchy to get all declared fields.
|
static void |
doWithFields(Class<?> clazz,
ReflectHelper.FieldCallback fc,
ReflectHelper.FieldFilter ff)
Invoke the given callback on all fields in the target class, going up the
class hierarchy to get all declared fields.
|
static void |
doWithLocalFields(Class<?> clazz,
ReflectHelper.FieldCallback fc)
Invoke the given callback on all locally declared fields in the given class.
|
static void |
doWithLocalMethods(Class<?> clazz,
ReflectHelper.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 void |
doWithMethods(Class<?> clazz,
ReflectHelper.MethodCallback mc)
Perform the given callback operation on all matching methods of the given
class and superclasses.
|
static void |
doWithMethods(Class<?> clazz,
ReflectHelper.MethodCallback mc,
ReflectHelper.MethodFilter mf)
Perform the given callback operation on all matching methods of the given
class and superclasses (or given interface and super-interfaces).
|
static void |
ensureAccessibility(AccessibleObject accessibleObject) |
static Method |
extractEqualsMethod(Class clazz)
Encapsulation of getting hold of a class's
equals method. |
static Method |
extractHashCodeMethod(Class clazz)
Encapsulation of getting hold of a class's
hashCode method. |
static Field |
findField(Class<?> clazz,
String name)
|
static Field |
findField(Class<?> clazz,
String name,
Class<?> type)
|
static Method |
findGetterMethod(Class containerClass,
String propertyName) |
static Method |
findMethod(Class<?> clazz,
String name)
Attempt to find a
Method on the supplied class with the supplied name
and no parameters. |
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. |
static Method |
findSetterMethod(Class containerClass,
String propertyName,
Class propertyType) |
static Method[] |
getAllDeclaredMethods(Class<?> leafClass)
Get all declared methods on the leaf class and all superclasses.
|
static <T> Constructor<T> |
getDefaultConstructor(Class<T> clazz)
Retrieve the default (no arg) constructor from the given class.
|
static Object |
getField(Field field,
Object target)
Get the field represented by the supplied
field object on the
specified target object. |
static Method |
getMethod(Class clazz,
Method method) |
static Method[] |
getUniqueDeclaredMethods(Class<?> leafClass)
Get the unique set of 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 boolean |
implementsInterface(Class clazz,
Class intf)
Determine if the given class implements the given interface.
|
static Object |
invokeJdbcMethod(Method method,
Object target)
已过时。
as of 5.0.11, in favor of custom SQLException handling
|
static Object |
invokeJdbcMethod(Method method,
Object target,
Object... args)
已过时。
as of 5.0.11, in favor of custom SQLException handling
|
static Object |
invokeMethod(Method method,
Object target)
Invoke the specified
Method against the supplied target object with no arguments. |
static Object |
invokeMethod(Method method,
Object target,
Object... args)
Invoke the specified
Method against the supplied target object with the
supplied arguments. |
static boolean |
isAbstractClass(Class clazz)
Determine if the given class is declared abstract.
|
static boolean |
isCglibRenamedMethod(Method renamedMethod)
Determine whether the given method is a CGLIB 'renamed' method,
following the pattern "CGLIB$methodName$0".
|
static boolean |
isEqualsMethod(Method method)
Determine whether the given method is an "equals" method.
|
static boolean |
isFinalClass(Class clazz)
Determine is the given class is declared final.
|
static boolean |
isHashCodeMethod(Method method)
Determine whether the given method is a "hashCode" method.
|
static boolean |
isObjectMethod(Method method)
Determine whether the given method is originally declared by
Object. |
static boolean |
isPublic(Class clazz,
Member member)
Is this member publicly accessible.
|
static boolean |
isPublicStaticFinal(Field field)
Determine whether the given field is a "public static final" constant.
|
static boolean |
isToStringMethod(Method method)
Determine whether the given method is a "toString" method.
|
static void |
makeAccessible(Constructor<?> ctor)
Make the given constructor accessible, explicitly setting it accessible
if necessary.
|
static void |
makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if
necessary.
|
static void |
makeAccessible(Method method)
Make the given method accessible, explicitly setting it accessible if
necessary.
|
static boolean |
overridesEquals(Class clazz)
Determine if the given class defines an
Object.equals(java.lang.Object) override. |
static boolean |
overridesHashCode(Class clazz)
Determine if the given class defines a
Object.hashCode() override. |
static void |
rethrowException(Throwable ex)
Rethrow the given
exception, which is presumably the
target exception of an InvocationTargetException. |
static void |
rethrowRuntimeException(Throwable ex)
Rethrow the given
exception, which is presumably the
target exception of an InvocationTargetException. |
static void |
setField(Field field,
Object target,
Object value)
Set the field represented by the supplied
field object on the
specified target object to the specified value. |
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.
|
public static final Class[] NO_PARAM_SIGNATURE
public static final Object[] NO_PARAMS
public static final Class[] SINGLE_OBJECT_PARAM_SIGNATURE
@Deprecated public static final ReflectHelper.MethodFilter NON_BRIDGED_METHODS
ReflectHelper.MethodFilterpublic static final ReflectHelper.MethodFilter USER_DECLARED_METHODS
java.lang.Object.public static final ReflectHelper.FieldFilter COPYABLE_FIELDS
public static void ensureAccessibility(AccessibleObject accessibleObject)
public static Method extractEqualsMethod(Class clazz) throws NoSuchMethodException
equals method.clazz - The class from which to extract the equals method.NoSuchMethodException - Should indicate an attempt to extract equals method from interface.public static Method extractHashCodeMethod(Class clazz) throws NoSuchMethodException
hashCode method.clazz - The class from which to extract the hashCode method.NoSuchMethodException - Should indicate an attempt to extract hashCode method from interface.public static boolean overridesEquals(Class clazz)
Object.equals(java.lang.Object) override.clazz - The class to checkpublic static boolean overridesHashCode(Class clazz)
Object.hashCode() override.clazz - The class to checkpublic static boolean implementsInterface(Class clazz, Class intf)
clazz - The class to checkintf - The interface to check it against.public static Class classForName(String name, Class caller) throws ClassNotFoundException
Class.forName(String, boolean, ClassLoader) using the caller's classloadername - The class namecaller - The class from which this call originated (in order to access that class's loader).ClassNotFoundException - From Class.forName(String, boolean, ClassLoader).@Deprecated public static Class classForName(String name) throws ClassNotFoundException
classForName(String, Class) except that here we delegate to
Class.forName(String) if the context classloader lookup is unsuccessful.name - The class nameClassNotFoundException - From Class.forName(String).public static boolean isPublic(Class clazz, Member member)
clazz - The class which defines the membermember - The memeber.public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws PropertyNotFoundException
clazz - The class for which to retrieve the default ctor.PropertyNotFoundException - Indicates there was not publicly accessible, no-arg constructor (todo : why PropertyNotFoundException???)public static boolean isAbstractClass(Class clazz)
clazz - The class to check.public static boolean isFinalClass(Class clazz)
clazz - The class to check.public static Method findSetterMethod(Class containerClass, String propertyName, Class propertyType)
public static Field findField(Class<?> clazz, 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 Field findField(Class<?> clazz, String name, Class<?> type)
field on the supplied Class with the
supplied name and/or type. Searches all superclasses
up to Object.clazz - the class to introspectname - the name of the field (may be null if type is specified)type - the type of the field (may be null if name is specified)null if not foundpublic static void setField(Field field, Object target, 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 null)public static Object getField(Field field, 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 Method findMethod(Class<?> clazz, 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 Method findMethod(Class<?> clazz, String name, 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 Object invokeMethod(Method method, 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(java.lang.reflect.Method, Object, Object[])public static Object invokeMethod(Method method, Object target, 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)@Deprecated public static Object invokeJdbcMethod(Method method, Object target) throws SQLException
Method against the supplied target
object with no arguments.method - the method to invoketarget - the target object to invoke the method onSQLException - the JDBC API SQLException to rethrow (if any)invokeJdbcMethod(java.lang.reflect.Method, Object, Object[])@Deprecated public static Object invokeJdbcMethod(Method method, Object target, Object... args) throws 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)SQLException - the JDBC API SQLException to rethrow (if any)invokeMethod(java.lang.reflect.Method, Object, Object[])public static void handleReflectionException(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 or UndeclaredThrowableException otherwise.
ex - the reflection exception to handlepublic static void handleInvocationTargetException(InvocationTargetException ex)
Throws the underlying RuntimeException or Error in case of such a root cause. Throws an UndeclaredThrowableException otherwise.
ex - the invocation target exception to handlepublic static void rethrowRuntimeException(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 a RuntimeException or
Error if appropriate; otherwise, throws an
UndeclaredThrowableException.
ex - the exception to rethrowRuntimeException - the rethrown exceptionpublic static void rethrowException(Throwable ex) throws 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
UndeclaredThrowableException.
ex - the exception to rethrowException - the rethrown exception (in case of a checked exception)public static boolean declaresException(Method method, 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(Field field)
field - the field to checkpublic static boolean isEqualsMethod(Method method)
Object.equals(Object)public static boolean isHashCodeMethod(Method method)
Object.hashCode()public static boolean isToStringMethod(Method method)
Object.toString()public static boolean isObjectMethod(Method method)
Object.public static boolean isCglibRenamedMethod(Method renamedMethod)
renamedMethod - the method to checkpublic static void makeAccessible(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(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 void makeAccessible(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 <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) throws NoSuchMethodException
clazz - the clazz to checkparameterTypes - the parameter types of the desired constructorNoSuchMethodException - if no such constructor existspublic static void doWithLocalMethods(Class<?> clazz, ReflectHelper.MethodCallback mc)
clazz - the class to introspectmc - the callback to invoke for each methodIllegalStateException - if introspection failsdoWithMethods(java.lang.Class<?>, cn.sexycode.util.core.object.ReflectHelper.MethodCallback)public static void doWithMethods(Class<?> clazz, ReflectHelper.MethodCallback mc)
The same named method occurring on subclass and superclass will appear
twice, unless excluded by a ReflectHelper.MethodFilter.
clazz - the class to introspectmc - the callback to invoke for each methodIllegalStateException - if introspection failsdoWithMethods(Class, MethodCallback, MethodFilter)public static void doWithMethods(Class<?> clazz, ReflectHelper.MethodCallback mc, ReflectHelper.MethodFilter mf)
The same named method occurring on subclass and superclass will appear
twice, unless excluded by the specified ReflectHelper.MethodFilter.
clazz - the class to introspectmc - the callback to invoke for each methodmf - the filter that determines the methods to apply the callback toIllegalStateException - if introspection failspublic static Method[] getAllDeclaredMethods(Class<?> leafClass)
leafClass - the class to introspectIllegalStateException - if introspection failspublic static Method[] getUniqueDeclaredMethods(Class<?> leafClass)
leafClass - the class to introspectIllegalStateException - if introspection failspublic static void doWithLocalFields(Class<?> clazz, ReflectHelper.FieldCallback fc)
clazz - the target class to analyzefc - the callback to invoke for each fieldIllegalStateException - if introspection failsdoWithFields(java.lang.Class<?>, cn.sexycode.util.core.object.ReflectHelper.FieldCallback)public static void doWithFields(Class<?> clazz, ReflectHelper.FieldCallback fc)
clazz - the target class to analyzefc - the callback to invoke for each fieldIllegalStateException - if introspection failspublic static void doWithFields(Class<?> clazz, ReflectHelper.FieldCallback fc, ReflectHelper.FieldFilter ff)
clazz - the target class to analyzefc - the callback to invoke for each fieldff - the filter that determines the fields to apply the callback toIllegalStateException - if introspection failspublic static void shallowCopyFieldState(Object src, Object dest)
IllegalStateException - if introspection failspublic static void clearCache()
Copyright © 2020. All rights reserved.