public abstract class ReflectUtil extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
ReflectUtil.FieldCallback
Callback interface invoked on each field in the hierarchy.
|
static interface |
ReflectUtil.FieldFilter
Callback optionally used to filter fields to be operated on by a field callback.
|
static interface |
ReflectUtil.MethodCallback
Action to take on each method.
|
static interface |
ReflectUtil.MethodFilter
Callback optionally used to method fields to be operated on by a method callback.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static ReflectUtil.FieldFilter |
COPYABLE_FIELDS
Pre-built FieldFilter that matches all non-static, non-final fields.
|
static ReflectUtil.MethodFilter |
NON_BRIDGED_METHODS
Pre-built MethodFilter that matches all non-bridge methods.
|
static ReflectUtil.MethodFilter |
USER_DECLARED_METHODS
Pre-built MethodFilter that matches all non-bridge methods
which are not declared on
java.lang.Object. |
| 构造器和说明 |
|---|
ReflectUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Map<String,Object> |
beanToMap(Object bean)
bean 转 map
|
static Object[] |
convert(Method method,
Object... params)
转换方法的所有参数类型
|
static Object |
covert(Class<?> _class,
Object param)
根据method形参转换参数,支持常用参数类型
|
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,
ReflectUtil.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,
ReflectUtil.FieldCallback fc,
ReflectUtil.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 |
doWithMethods(Class<?> clazz,
ReflectUtil.MethodCallback mc)
Perform the given callback operation on all matching methods of the given
class and superclasses.
|
static void |
doWithMethods(Class<?> clazz,
ReflectUtil.MethodCallback mc,
ReflectUtil.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 |
doWithMethodsWithNoRecursion(Class<?> clazz,
ReflectUtil.MethodCallback mc,
ReflectUtil.MethodFilter mf)
Perform the given callback operation on all matching methods of the given
class and superclasses (or given interface and super-interfaces).
|
static Field |
findField(Class<?> clazz,
String name)
|
static Field |
findField(Class<?> clazz,
String name,
Class<?> type)
|
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[] |
getAllDeclaredMethods(Class<?> leafClass)
Get all declared methods on the leaf class and all superclasses.
|
static Class<?>[] |
getAllInterfacesForClass(Class<?> clazz)
Return all interfaces that the given class implements as array,
including ones implemented by superclasses.
|
static Class<?>[] |
getAllInterfacesForClass(Class<?> clazz,
ClassLoader classLoader)
Return all interfaces that the given class implements as array,
including ones implemented by superclasses.
|
static Set<Class> |
getAllInterfacesForClassAsSet(Class clazz,
ClassLoader classLoader)
Return all interfaces that the given class implements as Set,
including ones implemented by superclasses.
|
static Method |
getDeclaredMethod(Class<?> _class,
String methodName)
根据方法名获取方法
|
static Method |
getDeclaredMethod(Object object,
String methodName,
Class<?>[] parameterTypes)
持续向上查找,直到找到某个方法
|
static Object |
getField(Field field,
Object target)
Get the field represented by the supplied
field object on the
specified target object. |
static Class<?> |
getSuperClassGenricType(Class<?> clazz,
int index)
查找泛型类的泛型
|
static void |
handleInvocationTargetException(InvocationTargetException ex)
Handle the given invocation target exception.
|
static void |
handleReflectionException(Exception ex)
Handle the given reflection exception.
|
static Object |
invokeJdbcMethod(Method method,
Object target)
Invoke the specified JDBC API
Method against the supplied target
object with no arguments. |
static Object |
invokeJdbcMethod(Method method,
Object target,
Object... args)
Invoke the specified JDBC API
Method against the supplied target
object with the supplied arguments. |
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 Object |
invokeMethod(Object object,
Method method,
Object... params) |
static Object |
invokeMethodByConvert(Object object,
Method method,
Object... params)
method.setAccessible(true); 执行方法 method.invoke(obj, params...)
|
static boolean |
isEqualsMethod(Method method)
Determine whether the given method is an "equals" method.
|
static boolean |
isHashCodeMethod(Method method)
Determine whether the given method is a "hashCode" method.
|
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 boolean |
isVisible(Class<?> clazz,
ClassLoader classLoader)
Check whether the given class is visible in the given ClassLoader.
|
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 <T> T |
mapToBean(Map<String,?> map,
Class<T> _class)
map 转 bean
|
static void |
populate(Object obj,
Properties prop)
将prop 注入到 obj(CoC name --> setName 注入)
|
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.
|
static String |
toGetMethod(String name) |
static String |
toSetMethod(String name) |
static String |
toTitleCase(String name) |
public static ReflectUtil.FieldFilter COPYABLE_FIELDS
public static ReflectUtil.MethodFilter NON_BRIDGED_METHODS
public static ReflectUtil.MethodFilter USER_DECLARED_METHODS
java.lang.Object.public static Object invokeMethodByConvert(Object object, Method method, Object... params)
public static Object[] convert(Method method, Object... params)
method - params - public static Object covert(Class<?> _class, Object param)
_class - param - public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes)
public static Method getDeclaredMethod(Class<?> _class, String methodName)
public static Class<?> getSuperClassGenricType(Class<?> clazz, int index)
public static <T> T mapToBean(Map<String,?> map, Class<T> _class)
T - map - _class - public static boolean isVisible(Class<?> clazz, ClassLoader classLoader)
clazz - the class to check (typically an interface)classLoader - the ClassLoader to check against (may be null,
in which case this method will always return true)public static Class<?>[] getAllInterfacesForClass(Class<?> clazz)
If the class itself is an interface, it gets returned as sole interface.
clazz - the class to analyze for interfacespublic static Class<?>[] getAllInterfacesForClass(Class<?> clazz, ClassLoader classLoader)
If the class itself is an interface, it gets returned as sole interface.
clazz - the class to analyze for interfacesclassLoader - the ClassLoader that the interfaces need to be visible in
(may be null when accepting all declared interfaces)public static Set<Class> getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader)
If the class itself is an interface, it gets returned as sole interface.
clazz - the class to analyze for interfacesclassLoader - the ClassLoader that the interfaces need to be visible in
(may be null when accepting all declared interfaces)public static void populate(Object obj, Properties prop)
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 nullpublic 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)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[])public static Object invokeJdbcMethod(Method method, Object target, Object... args)
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 else.
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 IllegalStateException else.
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 an RuntimeException or
Error if appropriate; otherwise, throws an
IllegalStateException.
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
IllegalStateException.
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)
public static boolean isHashCodeMethod(Method method)
Object.hashCode()public static boolean isToStringMethod(Method method)
Object.toString()public 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 void doWithMethods(Class<?> clazz, ReflectUtil.MethodCallback mc) throws IllegalArgumentException
The same named method occurring on subclass and superclass will appear
twice, unless excluded by a ReflectUtil.MethodFilter.
clazz - class to start looking atmc - the callback to invoke for each methodIllegalArgumentExceptiondoWithMethods(Class, MethodCallback, MethodFilter)public static void doWithMethods(Class<?> clazz, ReflectUtil.MethodCallback mc, ReflectUtil.MethodFilter mf) throws IllegalArgumentException
The same named method occurring on subclass and superclass will appear
twice, unless excluded by the specified ReflectUtil.MethodFilter.
clazz - class to start looking atmc - the callback to invoke for each methodmf - the filter that determines the methods to apply the callback toIllegalArgumentExceptionpublic static void doWithMethodsWithNoRecursion(Class<?> clazz, ReflectUtil.MethodCallback mc, ReflectUtil.MethodFilter mf) throws IllegalArgumentException
The same named method occurring on subclass and superclass will appear
twice, unless excluded by the specified ReflectUtil.MethodFilter.
clazz - class to start looking atmc - the callback to invoke for each methodmf - the filter that determines the methods to apply the callback toIllegalArgumentExceptionpublic static Method[] getAllDeclaredMethods(Class<?> leafClass) throws IllegalArgumentException
public static void doWithFields(Class<?> clazz, ReflectUtil.FieldCallback fc) throws IllegalArgumentException
clazz - the target class to analyzefc - the callback to invoke for each fieldIllegalArgumentExceptionpublic static void doWithFields(Class<?> clazz, ReflectUtil.FieldCallback fc, ReflectUtil.FieldFilter ff) throws IllegalArgumentException
clazz - the target class to analyzefc - the callback to invoke for each fieldff - the filter that determines the fields to apply the callback toIllegalArgumentExceptionpublic static void shallowCopyFieldState(Object src, Object dest) throws IllegalArgumentException
IllegalArgumentException - if the arguments are incompatibleCopyright © 2019. All rights reserved.