类 Reflections
java.lang.Object
com.jeeplus.devtools.util.Reflections
反射工具类.
提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
- 版本:
- 2021-01-15
- 作者:
- calvin
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static RuntimeException将反射时的checked exception转换为unchecked exception.static FieldgetAccessibleField(Object obj, String fieldName) 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.static MethodgetAccessibleMethod(Object obj, String methodName, Class<?>... parameterTypes) 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.static MethodgetAccessibleMethodByName(Object obj, String methodName) 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.static <T> Class<T>getClassGenricType(Class clazz) 通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 如无法找到, 返回Object.class.static ClassgetClassGenricType(Class clazz, int index) 通过反射, 获得Class定义中声明的父类的泛型参数的类型.static ObjectgetFieldValue(Object obj, String fieldName) 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.static Class<?>getUserClass(Object instance) static ObjectinvokeGetter(Object obj, String propertyName) 调用Getter方法.static ObjectinvokeMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args) 直接调用对象方法, 无视private/protected修饰符.static ObjectinvokeMethodByName(Object obj, String methodName, Object[] args) 直接调用对象方法, 无视private/protected修饰符, 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用.static voidinvokeSetter(Object obj, String propertyName, Object value) 调用Setter方法, 仅匹配方法名。static voidmakeAccessible(Field field) 改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。static voidmakeAccessible(Method method) 改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。static voidsetFieldValue(Object obj, String fieldName, Object value) 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
-
构造器详细资料
-
Reflections
public Reflections()
-
-
方法详细资料
-
invokeGetter
调用Getter方法. 支持多级,如:对象名.对象名.方法 -
invokeSetter
调用Setter方法, 仅匹配方法名。 支持多级,如:对象名.对象名.方法 -
getFieldValue
直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数. -
setFieldValue
直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数. -
invokeMethod
public static Object invokeMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args) 直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况,否则应使用getAccessibleMethod()函数获得Method后反复调用. 同时匹配方法名+参数类型, -
invokeMethodByName
直接调用对象方法, 无视private/protected修饰符, 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用. 只匹配函数名,如果有多个同名函数调用第一个。 -
getAccessibleField
循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.如向上转型到Object仍无法找到, 返回null.
-
getAccessibleMethod
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 匹配函数名+参数类型。用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
-
getAccessibleMethodByName
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 只匹配函数名。用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
-
makeAccessible
改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。 -
makeAccessible
改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。 -
getClassGenricType
通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 如无法找到, 返回Object.class. eg. public UserDao extends HibernateDao- 参数:
clazz- The class to introspect- 返回:
- the first generic declaration, or Object.class if cannot be determined
-
getClassGenricType
通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class.如public UserDao extends HibernateDao<User,Long>
- 参数:
clazz- clazz The class to introspectindex- the Index of the generic ddeclaration,start from 0.- 返回:
- the index generic declaration, or Object.class if cannot be determined
-
getUserClass
-
convertReflectionExceptionToUnchecked
将反射时的checked exception转换为unchecked exception.
-