public class UReflectUtil extends Object
Constructor and Description |
---|
UReflectUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> Constructor<T> |
getConstructor(Class<T> clazz,
Class<?>... parameterTypes)
查找类中的指定参数的构造方法,如果找到构造方法,会自动设置可访问为true
|
static <T> Constructor<T>[] |
getConstructors(Class<T> beanClass)
获得一个类中所有构造列表
|
static Constructor<?>[] |
getConstructorsDirectly(Class<?> beanClass)
获得一个类中所有字段列表,直接反射获取,无缓存
|
static Field[] |
getFields(Class<?> beanClass)
获得一个类中所有字段列表,包括其父类中的字段
|
static Field[] |
getFieldsDirectly(Class<?> beanClass,
boolean withSuperClassFieds)
获得一个类中所有字段列表,直接反射获取,无缓存
|
static Object |
getFieldValue(Object obj,
Field field)
获取字段值
|
static Method |
getMethod(Class<?> clazz,
boolean ignoreCase,
String methodName,
Class<?>... paramTypes)
查找指定方法 如果找不到对应的方法则返回
null |
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... paramTypes)
查找指定方法 如果找不到对应的方法则返回
null |
static Method |
getMethodOfObj(Object obj,
String methodName,
Object... args)
查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法
|
static Method[] |
getMethods(Class<?> beanClass)
获得一个类中所有方法列表,包括其父类中的方法
|
static Method[] |
getMethodsDirectly(Class<?> beanClass,
boolean withSuperClassMethods)
获得一个类中所有方法列表,直接反射获取,无缓存
|
static <T> T |
invoke(Object obj,
Method method,
Object... args)
执行方法
|
static <T> T |
invoke(Object obj,
String methodName,
Object... args)
执行对象中指定方法
|
static <T> T |
newInstance(Class<T> clazz,
Object... params)
实例化对象
|
static <T> T |
newInstanceIfPossible(Class<T> beanClass)
尝试遍历并调用此类的所有构造方法,直到构造成功并返回
|
static void |
setFieldValue(Object obj,
Field field,
Object value)
设置字段值
|
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes)
T
- 对象类型clazz
- 类parameterTypes
- 参数类型,只要任何一个参数是指定参数的父类或接口或相等即可,此参数可以不传public static <T> Constructor<T>[] getConstructors(Class<T> beanClass) throws SecurityException
T
- 构造的对象类型beanClass
- 类SecurityException
- 安全检查异常public static Constructor<?>[] getConstructorsDirectly(Class<?> beanClass) throws SecurityException
beanClass
- 类SecurityException
- 安全检查异常public static <T> T newInstance(Class<T> clazz, Object... params) throws UtilException
T
- 对象类型clazz
- 类params
- 构造函数参数UtilException
- 包装各类异常public static Method[] getMethodsDirectly(Class<?> beanClass, boolean withSuperClassMethods) throws SecurityException
beanClass
- 类withSuperClassMethods
- 是否包括父类的方法列表SecurityException
- 安全检查异常public static Method[] getMethods(Class<?> beanClass) throws SecurityException
beanClass
- 类SecurityException
- 安全检查异常public static Method getMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException
null
clazz
- 类,如果为null
返回null
ignoreCase
- 是否忽略大小写methodName
- 方法名,如果为空字符串返回null
paramTypes
- 参数类型,指定参数类型如果是方法的子类也算SecurityException
- 无权访问抛出异常public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException
null
clazz
- 类,如果为null
返回null
methodName
- 方法名,如果为空字符串返回null
paramTypes
- 参数类型,指定参数类型如果是方法的子类也算SecurityException
- 无权访问抛出异常public static Method getMethodOfObj(Object obj, String methodName, Object... args) throws SecurityException
obj
- 被查找的对象,如果为null
返回null
methodName
- 方法名,如果为空字符串返回null
args
- 参数SecurityException
- 无访问权限抛出异常public static <T> T invoke(Object obj, Method method, Object... args) throws UtilException
T
- 返回对象类型obj
- 对象,如果执行静态方法,此值为null
method
- 方法(对象方法或static方法都可)args
- 参数对象UtilException
- 一些列异常的包装public static <T> T invoke(Object obj, String methodName, Object... args) throws UtilException
T
- 返回对象类型obj
- 方法所在对象methodName
- 方法名args
- 参数列表UtilException
- IllegalAccessException包装public static Object getFieldValue(Object obj, Field field) throws UtilException
obj
- 对象field
- 字段UtilException
- 包装IllegalAccessException异常public static void setFieldValue(Object obj, Field field, Object value) throws UtilException
obj
- 对象field
- 字段value
- 值,值类型必须与字段类型匹配,不会自动转换对象类型UtilException
- UtilException 包装IllegalAccessException异常public static Field[] getFields(Class<?> beanClass) throws SecurityException
beanClass
- 类SecurityException
- 安全检查异常public static Field[] getFieldsDirectly(Class<?> beanClass, boolean withSuperClassFieds) throws SecurityException
beanClass
- 类withSuperClassFieds
- 是否包括父类的字段列表SecurityException
- 安全检查异常public static <T> T newInstanceIfPossible(Class<T> beanClass)
T
- 对象类型beanClass
- 被构造的类Copyright © 2020. All rights reserved.