Class ReflectKit

java.lang.Object
org.aoju.bus.core.toolkit.ReflectKit

public class ReflectKit extends Object
反射工具类. 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • ReflectKit

      public ReflectKit()
  • Method Details

    • invokeGetter

      public static Object invokeGetter(Object object, String name)
      调用Getter方法. 支持多级,如:对象名.对象名.方法
      Parameters:
      object - 对象
      name - 属性名
      Returns:
      the object
    • invokeSetter

      public static void invokeSetter(Object object, String name, Object value)
      调用Setter方法, 仅匹配方法名 支持多级,如:对象名.对象名.方法
      Parameters:
      object - 对象
      name - 属性名
      value - 值
    • convert

      public static Object convert(Object object, Class<?> type)
    • invokeMethod

      public static Object invokeMethod(Method method, Object target)
    • invokeMethod

      public static Object invokeMethod(Method method, Object target, Object... args)
    • invokeMethod

      public static Object invokeMethod(Object object, String name, Class<?>[] types, Object[] args)
      直接调用对象方法, 无视private/protected修饰符. 用于一次性调用的情况,否则应使用getAccessibleMethod()函数获得Method后反复调用. 同时匹配方法名+参数类型,
      Parameters:
      object - 对象
      name - 方法名
      types - 参数类型
      args - 参数
      Returns:
      the object
    • invokeMethodByName

      public static Object invokeMethodByName(Object object, String name, Object[] args)
      直接调用对象方法, 无视private/protected修饰符, 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用. 只匹配函数名,如果有多个同名函数调用第一个
      Parameters:
      object - 对象
      name - 方法
      args - 参数
      Returns:
      the object
    • getAccessibleField

      public static Field getAccessibleField(Object object, String name)
      循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.

      如向上转型到Object仍无法找到, 返回null.

      Parameters:
      object - 对象
      name - 列名
      Returns:
      the object
    • getAccessibleMethod

      public static Method getAccessibleMethod(Object object, String name, Class<?>... types)
      循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 匹配函数名+参数类型

      用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object object, Object... args)

      Parameters:
      object - 对象
      name - 方法名
      types - 参数类型
      Returns:
      the object
    • getAccessibleMethodByName

      public static Method getAccessibleMethodByName(Object object, String name)
      循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问. 如向上转型到Object仍无法找到, 返回null. 只匹配函数名

      用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object object, Object... args)

      Parameters:
      object - 对象
      name - 方法名
      Returns:
      the object
    • makeAccessible

      public static void makeAccessible(Method method)
      改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨
      Parameters:
      method - 方法
    • makeAccessible

      public static void makeAccessible(Field field)
      改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨
      Parameters:
      field - 对象
    • getClassGenricType

      public static <T> Class<T> getClassGenricType(Class clazz)
      通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处 如无法找到, 返回Object.class.
      Type Parameters:
      T - 对象
      Parameters:
      clazz - 对象
      Returns:
      the object
    • getClassGenricType

      public static Class getClassGenricType(Class clazz, int index)
      通过反射, 获得Class定义中声明的父类的泛型参数的类型. 如无法找到, 返回Object.class.
      Parameters:
      clazz - clazz The class to introspect
      index - the Index of the generic ddeclaration,start from 0.
      Returns:
      the index generic declaration, or Object.class if cannot be determined
    • getUserClass

      public static Class<?> getUserClass(Object instance)
    • convertReflectionExceptionToUnchecked

      public static RuntimeException convertReflectionExceptionToUnchecked(Exception e)
      将反射时的checked exception转换为unchecked exception.
      Parameters:
      e - 异常
      Returns:
      the ex
    • hasField

      public static boolean hasField(Object object, String fieldName)
      判断某个对象是否拥有某个属性
      Parameters:
      object - 对象
      fieldName - 属性名
      Returns:
      有属性返回true 无属性返回false
    • isOuterClassField

      public static boolean isOuterClassField(Field field)
      是否为父类引用字段 当字段所在类是对象子类时(对象中定义的非static的class),会自动生成一个以"this$0"为名称的字段,指向父类对象
      Parameters:
      field - 字段
      Returns:
      是否为父类引用字段
    • getConstructor

      public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... parameterTypes)
      查找类中的指定参数的构造方法
      Type Parameters:
      T - 对象类型
      Parameters:
      clazz - 类
      parameterTypes - 参数类型,只要任何一个参数是指定参数的父类或接口或相等即可
      Returns:
      构造方法, 如果未找到返回null
    • getConstructors

      public static <T> Constructor<T>[] getConstructors(Class<T> beanClass) throws SecurityException
      获得一个类中所有构造列表
      Type Parameters:
      T - 构造的对象类型
      Parameters:
      beanClass - 类
      Returns:
      字段列表
      Throws:
      SecurityException - 安全检查异常
    • getConstructorsDirectly

      public static Constructor<?>[] getConstructorsDirectly(Class<?> beanClass) throws SecurityException
      获得一个类中所有构造列表,直接反射获取,无缓存
      Parameters:
      beanClass - 类
      Returns:
      字段列表
      Throws:
      SecurityException - 安全检查异常
    • getField

      public static Field getField(Class<?> beanClass, String name) throws SecurityException
      查找指定类中的所有字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回null
      Parameters:
      beanClass - 被查找字段的类,不能为null
      name - 字段名
      Returns:
      field字段
      Throws:
      SecurityException - 安全异常
    • getFields

      public static Field[] getFields(Class<?> beanClass) throws SecurityException
      获得一个类中所有字段列表,包括其父类中的字段
      Parameters:
      beanClass - 类
      Returns:
      字段列表
      Throws:
      SecurityException - 安全检查异常
    • getFields

      public static Field[] getFields(Class<?> beanClass, Filter<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

      public static String getFieldName(Field field)
      获取字段名,如果存在Alias注解,读取注解的值作为名称
      Parameters:
      field - 字段信息
      Returns:
      字段名
    • getFieldValue

      public static Object getFieldValue(Object object, String fieldName) throws InstrumentException
      获取字段值
      Parameters:
      object - 对象
      fieldName - 字段名
      Returns:
      字段值
      Throws:
      InstrumentException - 包装IllegalAccessException异常
    • getFieldValue

      public static Object getFieldValue(Object object, Field field) throws InstrumentException
      获取字段值
      Parameters:
      object - 对象
      field - 字段
      Returns:
      字段值
      Throws:
      InstrumentException - 包装IllegalAccessException异常
    • getFieldsValue

      public static Object[] getFieldsValue(Object object)
      获取所有字段的值
      Parameters:
      object - bean对象,如果是static字段,此处为类class
      Returns:
      字段值数组
    • setFieldValue

      public static void setFieldValue(Object object, String fieldName, Object value) throws InstrumentException
      设置字段值
      Parameters:
      object - 对象,static字段则此处传Class
      fieldName - 字段名
      value - 值,值类型必须与字段类型匹配,不会自动转换对象类型
      Throws:
      InstrumentException - 包装IllegalAccessException异常
    • setFieldValue

      public static void setFieldValue(Object object, Field field, Object value) throws InstrumentException
      设置字段值
      Parameters:
      object - 对象,如果是static字段,此参数为null
      field - 字段
      value - 值,值类型必须与字段类型匹配,不会自动转换对象类型
      Throws:
      InstrumentException - UtilException 包装IllegalAccessException异常
    • getMethodOfObject

      public static Method getMethodOfObject(Object object, String methodName, Object... args) throws SecurityException
      查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法
      Parameters:
      object - 被查找的对象,如果为null返回null
      methodName - 方法名,如果为空字符串返回null
      args - 参数
      Returns:
      方法
      Throws:
      SecurityException - 无访问权限抛出异常
    • getMethodIgnoreCase

      public static Method getMethodIgnoreCase(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException
      忽略大小写查找指定方法,如果找不到对应的方法则返回null
      Parameters:
      clazz - 类,如果为null返回null
      methodName - 方法名,如果为空字符串返回null
      paramTypes - 参数类型,指定参数类型如果是方法的子类也算
      Returns:
      方法
      Throws:
      SecurityException - 无权访问抛出异常
    • getMethod

      public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException
      查找指定方法 如果找不到对应的方法则返回null
      Parameters:
      clazz - 类,如果为null返回null
      methodName - 方法名,如果为空字符串返回null
      paramTypes - 参数类型,指定参数类型如果是方法的子类也算
      Returns:
      方法
      Throws:
      SecurityException - 无权访问抛出异常
    • getMethod

      public static Method getMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException
      查找指定方法 如果找不到对应的方法则返回null
      Parameters:
      clazz - 类,如果为null返回null
      ignoreCase - 是否忽略大小写
      methodName - 方法名,如果为空字符串返回null
      paramTypes - 参数类型,指定参数类型如果是方法的子类也算
      Returns:
      方法
      Throws:
      SecurityException - 无权访问抛出异常
    • getMethodNames

      public static Set<String> getMethodNames(Class<?> clazz) throws SecurityException
      获得指定类中的Public方法名 去重重载的方法
      Parameters:
      clazz - 类
      Returns:
      方法名Set
      Throws:
      SecurityException - 安全异常
    • getMethods

      public static Method[] getMethods(Class<?> clazz, Filter<Method> filter) throws SecurityException
      获得指定类过滤后的Public方法列表
      Parameters:
      clazz - 查找方法的类
      filter - 过滤器
      Returns:
      过滤后的方法列表
      Throws:
      SecurityException - 安全异常
    • getMethods

      public static Method[] getMethods(Class<?> beanClass) throws SecurityException
      获得一个类中所有方法列表,包括其父类中的方法
      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

      public static boolean isEqualsMethod(Method method)
      是否为equals方法
      Parameters:
      method - 方法
      Returns:
      是否为equals方法
    • isHashCodeMethod

      public static boolean isHashCodeMethod(Method method)
      是否为hashCode方法
      Parameters:
      method - 方法
      Returns:
      是否为hashCode方法
    • isToStringMethod

      public static boolean isToStringMethod(Method method)
      是否为toString方法
      Parameters:
      method - 方法
      Returns:
      是否为toString方法
    • isEmptyParam

      public static boolean isEmptyParam(Method method)
      是否为无参数方法
      Parameters:
      method - 方法
      Returns:
      是否为无参数方法
    • isGetterOrSetterIgnoreCase

      public static boolean isGetterOrSetterIgnoreCase(Method method)
      检查给定方法是否为Getter或者Setter方法,规则为:
      • 方法参数必须为0个或1个
      • 如果是无参方法,则判断是否以“get”或“is”开头
      • 如果方法参数1个,则判断是否以“set”开头
      Parameters:
      method - 方法
      Returns:
      是否为Getter或者Setter方法
    • isGetterOrSetter

      public static boolean isGetterOrSetter(Method method, boolean ignoreCase)
      检查给定方法是否为Getter或者Setter方法,规则为:
      • 方法参数必须为0个或1个
      • 方法名称不能是getClass
      • 如果是无参方法,则判断是否以“get”或“is”开头
      • 如果方法参数1个,则判断是否以“set”开头
      Parameters:
      method - 方法
      ignoreCase - 是否忽略方法名的大小写
      Returns:
      是否为Getter或者Setter方法
    • newInstance

      public static <T> T newInstance(String clazz) throws InstrumentException
      实例化对象
      Type Parameters:
      T - 对象类型
      Parameters:
      clazz - 类名
      Returns:
      对象
      Throws:
      InstrumentException - 包装各类异常
    • newInstance

      public static <T> T newInstance(Class<T> clazz, Object... params) throws InstrumentException
      实例化对象
      Type Parameters:
      T - 对象类型
      Parameters:
      clazz - 类
      params - 构造函数参数
      Returns:
      对象
      Throws:
      InstrumentException - 包装各类异常
    • newInstanceIfPossible

      public static <T> T newInstanceIfPossible(Class<T> type)
      尝试遍历并调用此类的所有构造方法,直到构造成功并返回
      Type Parameters:
      T - 对象类型
      Parameters:
      type - 被构造的类
      Returns:
      构造后的对象, 构造失败返回null
    • invokeStatic

      public static <T> T invokeStatic(Method method, Object... args) throws InstrumentException
      执行静态方法
      Type Parameters:
      T - 对象类型
      Parameters:
      method - 方法(对象方法或static方法都可)
      args - 参数对象
      Returns:
      结果
      Throws:
      InstrumentException - 多种异常包装
    • invokeWithCheck

      public static <T> T invokeWithCheck(Object object, Method method, Object... args) throws InstrumentException
      执行方法 执行前要检查给定参数:
       1. 参数个数是否与方法参数个数一致
       2. 如果某个参数为null但是方法这个位置的参数为原始类型,则赋予原始类型默认值
       
      Type Parameters:
      T - 返回对象类型
      Parameters:
      object - 对象,如果执行静态方法,此值为null
      method - 方法(对象方法或static方法都可)
      args - 参数对象
      Returns:
      结果
      Throws:
      InstrumentException - 一些列异常的包装
    • invoke

      public static <T> T invoke(Object object, Method method, Object... args)
      执行方法
      Type Parameters:
      T - 返回对象类型
      Parameters:
      object - 对象,如果执行静态方法,此值为null
      method - 方法(对象方法或static方法都可)
      args - 参数对象
      Returns:
      结果
    • invoke

      public static <T> T invoke(Object object, String methodName, Object... args)
      执行对象中指定方法
      Type Parameters:
      T - 返回对象类型
      Parameters:
      object - 方法所在对象
      methodName - 方法名
      args - 参数列表
      Returns:
      执行结果
    • getGetMethodName

      public static String getGetMethodName(String fieldName)
      获取字段的get函数
      Parameters:
      fieldName - fieldName
      Returns:
      返回结果
    • getSetMethodName

      public static String getSetMethodName(String fieldName)
      根据字段名获取set方法
      Parameters:
      fieldName - 字段名
      Returns:
      返回结果
    • setAccessible

      public static <T extends AccessibleObject> T setAccessible(T accessibleObject)
      设置方法为可访问(私有方法可以被外部调用)
      Type Parameters:
      T - AccessibleObject的子类,比如Class、Method、Field等
      Parameters:
      accessibleObject - 可设置访问权限的对象,比如Class、Method、Field等
      Returns:
      被设置可访问的对象