public class ReflectionUtil extends Object
| Constructor and Description |
|---|
ReflectionUtil() |
| Modifier and Type | Method and Description |
|---|---|
static RuntimeException |
convertReflectionExceptionToUnchecked(Exception e)
将反射时的checked exception转换为unchecked exception.
|
static Method |
getAccessibleMethodByName(Class clazz,
String methodName)
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.
|
static Field |
getField(Class clazz,
String fieldName)
循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.
|
static <T> T |
getFieldValue(Object obj,
Field field)
使用已获取的Field, 直接读取对象属性值, 不经过getter函数.
|
static <T> T |
getFieldValue(Object obj,
String fieldName)
直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
|
static Method |
getGetterMethod(Class<?> clazz,
String propertyName)
循环遍历,按属性名获取前缀为get或is的函数,并设为可访问
|
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
循环向上转型, 获取对象的DeclaredMethod, 并强制设置为可访问.
|
static <T> T |
getProperty(Object obj,
String propertyName)
先尝试用Getter函数读取, 如果不存在则直接读取变量.
|
static Method |
getSetterMethod(Class<?> clazz,
String propertyName,
Class<?> parameterType)
循环遍历,按属性名获取前缀为set的函数,并设为可访问
|
static <T> T |
invokeConstructor(Class<T> cls,
Object... args)
调用构造函数.
|
static <T> T |
invokeGetter(Object obj,
String propertyName)
调用Getter方法, 无视private/protected修饰符.
|
static <T> T |
invokeMethod(Object obj,
Method method,
Object... args)
调用预先获取的Method,用于反复调用的场景
|
static <T> T |
invokeMethod(Object obj,
String methodName,
Object... args)
反射调用对象方法, 无视private/protected修饰符.
|
static <T> T |
invokeMethod(Object obj,
String methodName,
Object[] args,
Class<?>[] parameterTypes)
反射调用对象方法, 无视private/protected修饰符.
|
static <T> T |
invokeMethodByName(Object obj,
String methodName,
Object[] args)
反射调用对象方法, 无视private/protected修饰符
只匹配函数名,如果有多个同名函数调用第一个.
|
static void |
invokeSetter(Object obj,
String propertyName,
Object value)
调用Setter方法, 无视private/protected修饰符, 按传入value的类型匹配函数.
|
static void |
makeAccessible(Field field)
改变private/protected的成员变量为可访问,尽量不进行改变,避免JDK的SecurityManager抱怨。
|
static void |
makeAccessible(Method method)
改变private/protected的方法为可访问,尽量不进行改变,避免JDK的SecurityManager抱怨。
|
static void |
setField(Object obj,
Field field,
Object value)
使用预先获取的Field, 直接读取对象属性值, 不经过setter函数.
|
static void |
setFieldValue(Object obj,
String fieldName,
Object value)
直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
|
static void |
setProperty(Object obj,
String propertyName,
Object value)
先尝试用Setter函数写入, 如果不存在则直接写入变量, 按传入value的类型匹配函数.
|
public static Method getSetterMethod(Class<?> clazz, String propertyName, Class<?> parameterType)
public static Method getGetterMethod(Class<?> clazz, String propertyName)
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
public static Method getAccessibleMethodByName(Class clazz, String methodName)
public static Field getField(Class clazz, String fieldName)
public static <T> T invokeGetter(Object obj, String propertyName)
public static void invokeSetter(Object obj, String propertyName, Object value)
public static <T> T getFieldValue(Object obj, String fieldName)
public static <T> T getFieldValue(Object obj, Field field)
public static void setFieldValue(Object obj, String fieldName, Object value)
public static void setField(Object obj, Field field, Object value)
public static <T> T getProperty(Object obj, String propertyName)
public static void setProperty(Object obj, String propertyName, Object value)
public static <T> T invokeMethod(Object obj, String methodName, Object... args)
public static <T> T invokeMethod(Object obj, String methodName, Object[] args, Class<?>[] parameterTypes)
public static <T> T invokeMethodByName(Object obj, String methodName, Object[] args)
public static <T> T invokeMethod(Object obj, Method method, Object... args)
public static void makeAccessible(Method method)
public static void makeAccessible(Field field)
public static RuntimeException convertReflectionExceptionToUnchecked(Exception e)
Copyright © 2018. All rights reserved.