Package org.aoju.bus.core.toolkit
Class ReflectKit
java.lang.Object
org.aoju.bus.core.toolkit.ReflectKit
反射工具类. 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Objectstatic RuntimeException将反射时的checked exception转换为unchecked exception.static FieldgetAccessibleField(Object object, String name) 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.static MethodgetAccessibleMethod(Object object, String name, Class<?>... types) 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.static MethodgetAccessibleMethodByName(Object object, String name) 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.static <T> Class<T> getClassGenricType(Class clazz) 通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 如无法找到, 返回Object.class.static ClassgetClassGenricType(Class clazz, int index) 通过反射, 获得Class定义中声明的父类的泛型参数的类型.static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes) 查找类中的指定参数的构造方法static <T> Constructor<T>[]getConstructors(Class<T> beanClass) 获得一个类中所有构造列表static Constructor<?>[]getConstructorsDirectly(Class<?> beanClass) 获得一个类中所有构造列表,直接反射获取,无缓存static StringgetDescriptor(Class<?> clazz) 获取类型描述符,这是编译成class文件后的二进制名称static StringgetDescriptor(Executable executable) 获取jvm定义的Field Descriptors(字段描述)static Field查找指定类中的所有字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回nullstatic StringgetFieldName(Field field) 获取字段名,如果存在Alias注解,读取注解的值作为名称static Field[]获得一个类中所有字段列表,包括其父类中的字段static Field[]获得一个类中所有字段列表,直接反射获取,无缓存static Field[]获得一个类中所有满足条件的字段列表,包括其父类中的字段 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后static Object[]getFieldsValue(Object object) 获取所有字段的值static ObjectgetFieldValue(Object object, Field field) 获取字段值static ObjectgetFieldValue(Object object, String fieldName) 获取字段值static StringgetGetMethodName(String fieldName) 获取字段的get函数static Method查找指定方法 如果找不到对应的方法则返回nullstatic Method查找指定方法 如果找不到对应的方法则返回nullstatic MethodgetMethodIgnoreCase(Class<?> clazz, String methodName, Class<?>... paramTypes) 忽略大小写查找指定方法,如果找不到对应的方法则返回nullgetMethodNames(Class<?> clazz) 获得指定类中的Public方法名 去重重载的方法static MethodgetMethodOfObject(Object object, String methodName, Object... args) 查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法static Method[]getMethods(Class<?> beanClass) 获得一个类中所有方法列表,包括其父类中的方法static Method[]getMethods(Class<?> beanClass, boolean withSupers, boolean withMethodFromObject) 获得一个类中所有方法列表,直接反射获取,无缓存 接口获取方法和默认方法,获取的方法包括: 本类中的所有方法(包括static方法) 父类中的所有方法(包括static方法) Object中(包括static方法)static Method[]getMethods(Class<?> clazz, Predicate<Method> filter) 获得指定类过滤后的Public方法列表static StringgetSetMethodName(String fieldName) 根据字段名获取set方法static Class<?> getUserClass(Object instance) static boolean判断某个对象是否拥有某个属性static <T> T执行方法static <T> T执行对象中指定方法static ObjectinvokeGetter(Object object, String name) 调用Getter方法.static ObjectinvokeMethod(Object object, String name, Class<?>[] types, Object[] args) 直接调用对象方法, 无视private/protected修饰符.static ObjectinvokeMethod(Method method, Object target) static ObjectinvokeMethod(Method method, Object target, Object... args) static ObjectinvokeMethodByName(Object object, String name, Object[] args) 直接调用对象方法, 无视private/protected修饰符, 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用.static voidinvokeSetter(Object object, String name, Object value) 调用Setter方法, 仅匹配方法名 支持多级,如:对象名.对象名.方法static <T> TinvokeStatic(Method method, Object... args) 执行静态方法static <T> TinvokeWithCheck(Object object, Method method, Object... args) 执行方法 执行前要检查给定参数:static booleanisEmptyParam(Method method) 是否为无参数方法static booleanisEqualsMethod(Method method) 是否为equals方法static booleanisGetterOrSetter(Method method, boolean ignoreCase) 检查给定方法是否为Getter或者Setter方法,规则为: 方法参数必须为0个或1个 方法名称不能是getClass 如果是无参方法,则判断是否以“get”或“is”开头 如果方法参数1个,则判断是否以“set”开头static booleanisGetterOrSetterIgnoreCase(Method method) 检查给定方法是否为Getter或者Setter方法,规则为: 方法参数必须为0个或1个 如果是无参方法,则判断是否以“get”或“is”开头 如果方法参数1个,则判断是否以“set”开头static booleanisHashCodeMethod(Method method) 是否为hashCode方法static booleanisOuterClassField(Field field) 是否为父类引用字段 当字段所在类是对象子类时(对象中定义的非static的class),会自动生成一个以"this$0"为名称的字段,指向父类对象static booleanisToStringMethod(Method method) 是否为toString方法static voidmakeAccessible(Field field) 改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨static voidmakeAccessible(Method method) 改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨static <T> TnewInstance(Class<T> clazz, Object... params) 实例化对象static <T> TnewInstance(String clazz) 实例化对象static <T> TnewInstanceIfPossible(Class<T> type) 尝试遍历并调用此类的所有构造方法,直到构造成功并返回static <T extends AccessibleObject>
TsetAccessible(T accessibleObject) 设置方法为可访问(私有方法可以被外部调用)static voidsetFieldValue(Object object, Field field, Object value) 设置字段值 若值类型与字段类型不一致,则会尝试通过Convert进行转换 若字段类型是原始类型而传入的值是 null,则会将字段设置为对应原始类型的默认值(见ClassKit.getDefaultValue(Class))static voidsetFieldValue(Object object, String fieldName, Object value) 设置字段值 若值类型与字段类型不一致,则会尝试通过Convert进行转换 若字段类型是原始类型而传入的值是 null,则会将字段设置为对应原始类型的默认值(见ClassKit.getDefaultValue(Class))static voidsetStaticFieldValue(Field field, Object value) 设置静态(static)字段值
-
Constructor Details
-
ReflectKit
public ReflectKit()
-
-
Method Details
-
invokeGetter
-
invokeSetter
-
convert
-
invokeMethod
-
invokeMethod
-
invokeMethod
-
invokeMethodByName
-
getAccessibleField
-
getAccessibleMethod
循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 匹配函数名+参数类型用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object object, Object... args)
- Parameters:
object- 对象name- 方法名types- 参数类型- Returns:
- the object
-
getAccessibleMethodByName
-
makeAccessible
改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨- Parameters:
method- 方法
-
makeAccessible
改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨- Parameters:
field- 对象
-
getClassGenricType
-
getClassGenricType
通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class.- Parameters:
clazz- clazz The class to introspectindex- the Index of the generic ddeclaration,start from 0.- Returns:
- the index generic declaration, or Object.class if cannot be determined
-
getUserClass
-
convertReflectionExceptionToUnchecked
将反射时的checked exception转换为unchecked exception.- Parameters:
e- 异常- Returns:
- the ex
-
hasField
-
isOuterClassField
是否为父类引用字段 当字段所在类是对象子类时(对象中定义的非static的class),会自动生成一个以"this$0"为名称的字段,指向父类对象- Parameters:
field- 字段- Returns:
- 是否为父类引用字段
-
getConstructor
查找类中的指定参数的构造方法- Type Parameters:
T- 对象类型- Parameters:
clazz- 类parameterTypes- 参数类型,只要任何一个参数是指定参数的父类或接口或相等即可- Returns:
- 构造方法, 如果未找到返回null
-
getConstructors
获得一个类中所有构造列表- Type Parameters:
T- 构造的对象类型- Parameters:
beanClass- 类- Returns:
- 字段列表
- Throws:
SecurityException- 安全检查异常
-
getConstructorsDirectly
获得一个类中所有构造列表,直接反射获取,无缓存- Parameters:
beanClass- 类- Returns:
- 字段列表
- Throws:
SecurityException- 安全检查异常
-
getField
查找指定类中的所有字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回null- Parameters:
beanClass- 被查找字段的类,不能为nullname- 字段名- Returns:
- field字段
- Throws:
SecurityException- 安全异常
-
getFields
获得一个类中所有字段列表,包括其父类中的字段- Parameters:
beanClass- 类- Returns:
- 字段列表
- Throws:
SecurityException- 安全检查异常
-
getFields
public static Field[] getFields(Class<?> beanClass, Predicate<Field> fieldFilter) throws SecurityException 获得一个类中所有满足条件的字段列表,包括其父类中的字段 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后- Parameters:
beanClass- 类fieldFilter- field过滤器,过滤掉不需要的field- Returns:
- 字段列表
- Throws:
SecurityException- 安全检查异常
-
getFields
public static Field[] getFields(Class<?> beanClass, boolean withSuperClassFields) throws SecurityException 获得一个类中所有字段列表,直接反射获取,无缓存- Parameters:
beanClass- 类withSuperClassFields- 是否包括父类的字段列表- Returns:
- 字段列表
- Throws:
SecurityException- 安全检查异常
-
getFieldName
-
getFieldValue
获取字段值- Parameters:
object- 对象fieldName- 字段名- Returns:
- 字段值
- Throws:
InternalException- 包装IllegalAccessException异常
-
getFieldValue
获取字段值- Parameters:
object- 对象field- 字段- Returns:
- 字段值
- Throws:
InternalException- 包装IllegalAccessException异常
-
getFieldsValue
-
setFieldValue
public static void setFieldValue(Object object, String fieldName, Object value) throws InternalException 设置字段值 若值类型与字段类型不一致,则会尝试通过Convert进行转换 若字段类型是原始类型而传入的值是 null,则会将字段设置为对应原始类型的默认值(见ClassKit.getDefaultValue(Class))- Parameters:
object- 对象,static字段则此处传ClassfieldName- 字段名value- 值,当值类型与字段类型不匹配时,会尝试转换- Throws:
InternalException- 包装IllegalAccessException异常
-
setFieldValue
设置字段值 若值类型与字段类型不一致,则会尝试通过Convert进行转换 若字段类型是原始类型而传入的值是 null,则会将字段设置为对应原始类型的默认值(见ClassKit.getDefaultValue(Class))- Parameters:
object- 对象,如果是static字段,此参数为nullfield- 字段value- 值,当值类型与字段类型不匹配时,会尝试转换- Throws:
InternalException- 包装IllegalAccessException异常
-
setStaticFieldValue
-
getMethodOfObject
public static Method getMethodOfObject(Object object, String methodName, Object... args) throws SecurityException 查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法- Parameters:
object- 被查找的对象,如果为null返回nullmethodName- 方法名,如果为空字符串返回nullargs- 参数- Returns:
- 方法
- Throws:
SecurityException- 无访问权限抛出异常
-
getMethodIgnoreCase
public static Method getMethodIgnoreCase(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException 忽略大小写查找指定方法,如果找不到对应的方法则返回null- Parameters:
clazz- 类,如果为null返回nullmethodName- 方法名,如果为空字符串返回nullparamTypes- 参数类型,指定参数类型如果是方法的子类也算- Returns:
- 方法
- Throws:
SecurityException- 无权访问抛出异常
-
getMethod
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException 查找指定方法 如果找不到对应的方法则返回null- Parameters:
clazz- 类,如果为null返回nullmethodName- 方法名,如果为空字符串返回nullparamTypes- 参数类型,指定参数类型如果是方法的子类也算- Returns:
- 方法
- Throws:
SecurityException- 无权访问抛出异常
-
getMethod
public static Method getMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException 查找指定方法 如果找不到对应的方法则返回null- Parameters:
clazz- 类,如果为null返回nullignoreCase- 是否忽略大小写methodName- 方法名,如果为空字符串返回nullparamTypes- 参数类型,指定参数类型如果是方法的子类也算- Returns:
- 方法
- Throws:
SecurityException- 无权访问抛出异常
-
getMethodNames
获得指定类中的Public方法名 去重重载的方法- Parameters:
clazz- 类- Returns:
- 方法名Set
- Throws:
SecurityException- 安全异常
-
getMethods
public static Method[] getMethods(Class<?> clazz, Predicate<Method> filter) throws SecurityException 获得指定类过滤后的Public方法列表- Parameters:
clazz- 查找方法的类filter- 过滤器- Returns:
- 过滤后的方法列表
- Throws:
SecurityException- 安全异常
-
getMethods
获得一个类中所有方法列表,包括其父类中的方法- Parameters:
beanClass- 类- Returns:
- 方法列表
- Throws:
SecurityException- 安全检查异常
-
getMethods
public static Method[] getMethods(Class<?> beanClass, boolean withSupers, boolean withMethodFromObject) throws SecurityException 获得一个类中所有方法列表,直接反射获取,无缓存 接口获取方法和默认方法,获取的方法包括:- 本类中的所有方法(包括static方法)
- 父类中的所有方法(包括static方法)
- Object中(包括static方法)
- Parameters:
beanClass- 类或接口withSupers- 是否包括父类或接口的方法列表withMethodFromObject- 是否包括Object中的方法- Returns:
- 方法列表
- Throws:
SecurityException- 安全检查异常
-
isEqualsMethod
是否为equals方法- Parameters:
method- 方法- Returns:
- 是否为equals方法
-
isHashCodeMethod
是否为hashCode方法- Parameters:
method- 方法- Returns:
- 是否为hashCode方法
-
isToStringMethod
是否为toString方法- Parameters:
method- 方法- Returns:
- 是否为toString方法
-
isEmptyParam
是否为无参数方法- Parameters:
method- 方法- Returns:
- 是否为无参数方法
-
isGetterOrSetterIgnoreCase
检查给定方法是否为Getter或者Setter方法,规则为:- 方法参数必须为0个或1个
- 如果是无参方法,则判断是否以“get”或“is”开头
- 如果方法参数1个,则判断是否以“set”开头
- Parameters:
method- 方法- Returns:
- 是否为Getter或者Setter方法
-
isGetterOrSetter
检查给定方法是否为Getter或者Setter方法,规则为:- 方法参数必须为0个或1个
- 方法名称不能是getClass
- 如果是无参方法,则判断是否以“get”或“is”开头
- 如果方法参数1个,则判断是否以“set”开头
- Parameters:
method- 方法ignoreCase- 是否忽略方法名的大小写- Returns:
- 是否为Getter或者Setter方法
-
newInstance
实例化对象- Type Parameters:
T- 对象类型- Parameters:
clazz- 类名- Returns:
- 对象
- Throws:
InternalException- 包装各类异常
-
newInstance
实例化对象- Type Parameters:
T- 对象类型- Parameters:
clazz- 类params- 构造函数参数- Returns:
- 对象
- Throws:
InternalException- 包装各类异常
-
newInstanceIfPossible
尝试遍历并调用此类的所有构造方法,直到构造成功并返回- Type Parameters:
T- 对象类型- Parameters:
type- 被构造的类- Returns:
- 构造后的对象, 构造失败返回
null
-
invokeStatic
执行静态方法- Type Parameters:
T- 对象类型- Parameters:
method- 方法(对象方法或static方法都可)args- 参数对象- Returns:
- 结果
- Throws:
InternalException- 多种异常包装
-
invokeWithCheck
public static <T> T invokeWithCheck(Object object, Method method, Object... args) throws InternalException 执行方法 执行前要检查给定参数:1. 参数个数是否与方法参数个数一致 2. 如果某个参数为null但是方法这个位置的参数为原始类型,则赋予原始类型默认值
- Type Parameters:
T- 返回对象类型- Parameters:
object- 对象,如果执行静态方法,此值为nullmethod- 方法(对象方法或static方法都可)args- 参数对象- Returns:
- 结果
- Throws:
InternalException- 一些列异常的包装
-
invoke
-
invoke
-
getGetMethodName
-
getSetMethodName
-
setAccessible
设置方法为可访问(私有方法可以被外部调用)- Type Parameters:
T- AccessibleObject的子类,比如Class、Method、Field等- Parameters:
accessibleObject- 可设置访问权限的对象,比如Class、Method、Field等- Returns:
- 被设置可访问的对象
-
getDescriptor
获取jvm定义的Field Descriptors(字段描述)- Parameters:
executable- 可执行的反射对象- Returns:
- 描述符
例:
getDescriptor(Object.class.getMethod("hashCode")) // "()I"getDescriptor(Object.class.getMethod("toString")) // "()Ljava/lang/String;"getDescriptor(Object.class.getMethod("equals", Object.class)) // "(Ljava/lang/Object;)Z"getDescriptor(ReflectKit.class.getDeclaredMethod("appendDescriptor", Class.clas, StringBuilder.class)) // "(Ljava/lang/Class;Ljava/lang/StringBuilder;)V"getDescriptor(ArrayKit.class.getMethod("isEmpty", Object[].class)) // "([Ljava/lang/Object;)Z"
-
getDescriptor
获取类型描述符,这是编译成class文件后的二进制名称- Parameters:
clazz- 类- Returns:
- 描述字符串
例:
getDescriptor(boolean.class) "Z"getDescriptor(Boolean.class) "Ljava/lang/Boolean;"getDescriptor(double[][][].class) "[[[D"getDescriptor(int.class) "I"getDescriptor(Integer.class) "Ljava/lang/Integer;"
-