public class Reflections extends Object
提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
限定符和类型 | 字段和说明 |
---|---|
static String |
CGLIB_CLASS_SEPARATOR |
限定符和类型 | 方法和说明 |
---|---|
static RuntimeException |
convertReflectionExceptionToUnchecked(Exception e)
将反射时的checked exception转换为unchecked exception.
|
static Field |
getAccessibleField(Object obj,
String fieldName)
循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.
|
static Method |
getAccessibleMethod(Object obj,
String methodName,
Class<?>... parameterTypes)
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.
|
static Object |
getFieldValue(Object obj,
String fieldName)
直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
|
static <T> Class<T> |
getSuperClassGenricType(Class<?> clazz)
通过反射, 获得Class定义中声明的父类的泛型参数的类型.
|
static Class<?> |
getSuperClassGenricType(Class<?> clazz,
int index)
通过反射, 获得Class定义中声明的父类的泛型参数的类型.
|
static Class<?> |
getUserClass(Class<?> clazz)
对于被cglib AOP过的对象, 取得真实的Class类型.
|
static Object |
invokeGetterMethod(Object obj,
String propertyName)
调用Getter方法.
|
static Object |
invokeMethod(Object obj,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
直接调用对象方法, 无视private/protected修饰符.
|
static void |
invokeSetterMethod(Object obj,
String propertyName,
Object value)
调用Setter方法.使用value的Class来查找Setter方法.
|
static void |
invokeSetterMethod(Object obj,
String propertyName,
Object value,
Class<?> propertyType)
调用Setter方法.
|
static void |
setFieldValue(Object obj,
String fieldName,
Object value)
直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
|
public static Object invokeGetterMethod(Object obj, String propertyName)
public static void invokeSetterMethod(Object obj, String propertyName, Object value)
public static void invokeSetterMethod(Object obj, String propertyName, Object value, Class<?> propertyType)
propertyType
- 用于查找Setter方法,为空时使用value的Class替代.public static Object getFieldValue(Object obj, String fieldName)
public static void setFieldValue(Object obj, String fieldName, Object value)
public static Field getAccessibleField(Object obj, String fieldName)
public static Object invokeMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] args)
public static Method getAccessibleMethod(Object obj, String methodName, Class<?>... parameterTypes)
public static <T> Class<T> getSuperClassGenricType(Class<?> clazz)
clazz
- The class to introspectpublic static Class<?> getSuperClassGenricType(Class<?> clazz, int index)
clazz
- clazz The class to introspectindex
- the Index of the generic ddeclaration,start from 0.public static RuntimeException convertReflectionExceptionToUnchecked(Exception e)
Copyright © 2014 EPIC Team. All rights reserved.