public class Reflections extends Object
| 构造器和说明 |
|---|
Reflections() |
| 限定符和类型 | 方法和说明 |
|---|---|
static RuntimeException |
convertReflectionExceptionToUnchecked(Exception e)
将反射时的checked exception转换为unchecked throwable.
|
static Field |
getAccessibleField(Object obj,
String fieldName)
循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.
|
static Method |
getAccessibleMethod(Object obj,
String methodName,
Class<?>... parameterTypes)
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.
|
static Method |
getAccessibleMethodByName(Object obj,
String methodName)
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.
|
static <T> Class<T> |
getClassGenericType(Class clazz)
通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 如无法找到, 返回Object.class. eg.
|
static Class |
getClassGenericType(Class clazz,
int index)
通过反射, 获得Class定义中声明的父类的泛型参数的类型.
|
static Object |
getFieldValue(Object obj,
String fieldName)
直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
|
static Object |
invokeGetter(Object obj,
String propertyName)
调用Getter方法.
|
static Object |
invokeMethod(Object obj,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
直接调用对象方法, 无视private/protected修饰符.
|
static Object |
invokeMethodByName(Object obj,
String methodName,
Object[] args)
直接调用对象方法, 无视private/protected修饰符,
用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用.
|
static void |
invokeSetter(Object obj,
String propertyName,
Object value)
调用Setter方法, 仅匹配方法名。
|
static void |
makeAccessible(Field field)
改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
|
static void |
makeAccessible(Method method)
改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
|
static void |
setFieldValue(Object obj,
String fieldName,
Object value)
直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
|
public static Object invokeGetter(Object obj, String propertyName)
obj - 对象propertyName - 属性名public static void invokeSetter(Object obj, String propertyName, Object value)
obj - 对象propertyName - 属性名value - 值public static Object getFieldValue(Object obj, String fieldName)
obj - 对象fieldName - 属性名public static void setFieldValue(Object obj, String fieldName, Object value)
obj - 对象fieldName - 属性名value - 值public static Object invokeMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args)
obj - 对象methodName - 方法名parameterTypes - 参数类型args - 参数public static Object invokeMethodByName(Object obj, String methodName, Object[] args)
obj - 对象methodName - 方法名args - 参数public static Field getAccessibleField(Object obj, String fieldName)
如向上转型到Object仍无法找到, 返回null.
obj - 对象fieldName - 属性名public static Method getAccessibleMethod(Object obj, String methodName, Class<?>... parameterTypes)
用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
obj - 对象methodName - 方法名parameterTypes - 参数类型public static Method getAccessibleMethodByName(Object obj, String methodName)
用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
obj - 对象methodName - 方法名public static void makeAccessible(Method method)
method - 方法public static void makeAccessible(Field field)
field - 字段public static <T> Class<T> getClassGenericType(Class clazz)
T - 泛型clazz - The class to introspectpublic static Class getClassGenericType(Class clazz, int index)
clazz - clazz The class to introspectindex - the Index of the generic declaration,start from 0.public static RuntimeException convertReflectionExceptionToUnchecked(Exception e)
e - 异常Copyright © 2023 Pivotal Software, Inc.. All rights reserved.