Class ReflectUtil

java.lang.Object
cn.yusiwen.commons.mapper.ReflectUtil

public final class ReflectUtil extends Object
反射工具类,提供对类、字段和方法的反射操作功能。

主要功能包括:

  • 获取类的字段(包括父类继承的字段)
  • 获取和设置字段值
  • 调用对象方法
  • 检查方法类型(equals/hashCode/toString等)
Since:
1.0
Author:
Siwen Yu (yusiwen@gmail.com)
  • Method Details

    • getFields

      public static Field[] getFields(Class<?> clazz)
      获取指定类的所有字段,包含父类继承的字段。
      Parameters:
      clazz - 要获取字段的类
      Returns:
      类的所有字段数组
    • getFields

      public static Field[] getFields(Class<?> clazz, Predicate<Field> fieldFilter)
      获取指定类的所有的field,包括父类
      Parameters:
      clazz - 字段所属类型
      fieldFilter - 字段过滤器
      Returns:
      符合过滤器条件的字段数组
    • doWithFields

      public static void doWithFields(Class<?> clazz, Consumer<Field> consumer)
      对指定类的所有字段执行consumer操作
      Parameters:
      clazz - 目标对象
      consumer - 对字段进行操作
    • getField

      public static Field getField(Class<?> clazz, String name)
      获取指定类中指定名称的字段,包括父类中的字段。
      Parameters:
      clazz - 要查找的类
      name - 字段名称
      Returns:
      找到的字段对象,如果未找到返回null
    • getField

      public static Field getField(Class<?> clazz, String name, Class<?> type)
      获取指定类的指定field,包括父类
      Parameters:
      clazz - 字段所属类型
      name - 字段名
      type - field类型
      Returns:
      Field对象
    • getFieldValue

      public static Object getFieldValue(Field field, Object target)
      获取字段值
      Parameters:
      field - 字段
      target - 字段所属实例对象
      Returns:
      字段值
    • getFieldValue

      public static Object getFieldValue(Object obj, String fieldName)
      获取对象中指定field值
      Parameters:
      obj - 对象
      fieldName - 字段名
      Returns:
      字段值
    • getValueByFieldPath

      public static Object getValueByFieldPath(Object obj, String fieldPath)
      获取指定对象中指定字段路径的值(类似js访问对象属性)
      如:Product p = new Product(new User())
      可使用ReflectionUtils.getValueByFieldPath(p, "user.name")获取到用户的name属性
      Parameters:
      obj - 取值对象
      fieldPath - 字段路径(形如 user.name)
      Returns:
      字段value
    • setFieldValue

      public static void setFieldValue(Field field, Object target, Object value)
      设置字段值
      Parameters:
      field - 字段
      target - 字段所属对象实例
      value - 需要设置的值
    • makeAccessible

      public static void makeAccessible(Field field)
      设置字段为可访问,主要用于访问私有字段。
      Parameters:
      field - 要设置的字段对象
    • invokeMethod

      public static Object invokeMethod(Method method, Object target)
      调用对象的无参数方法。
      Parameters:
      method - 要调用的方法对象
      target - 目标对象实例
      Returns:
      方法执行的返回结果
    • invokeMethod

      public static Object invokeMethod(Method method, Object target, Object... args)
      调用指定对象的方法
      Parameters:
      method - 方法对象
      target - 调用对象
      args - 方法参数
      Returns:
      执行结果
    • makeAccessible

      public static void makeAccessible(Method method)
      设置方法可见性
      Parameters:
      method - 方法
    • isEqualsMethod

      public static boolean isEqualsMethod(Method method)
      判断给定的方法是否为Object类的equals方法。
      Parameters:
      method - 要检查的方法
      Returns:
      如果是equals方法返回true,否则返回false
      See Also:
    • isHashCodeMethod

      public static boolean isHashCodeMethod(Method method)
      判断给定的方法是否为Object类的hashCode方法。
      Parameters:
      method - 要检查的方法
      Returns:
      如果是hashCode方法返回true,否则返回false
      See Also:
    • isToStringMethod

      public static boolean isToStringMethod(Method method)
      判断给定的方法是否为Object类的toString方法。
      Parameters:
      method - 要检查的方法
      Returns:
      如果是toString方法返回true,否则返回false
      See Also: