Class ReflectUtil

java.lang.Object
org.huiche.core.support.ReflectUtil

public final class ReflectUtil extends Object
反射工具类
Author:
Maning
  • Method Details

    • getEntityFields

      public static Field[] getEntityFields(Class<?> clazz)
      获取实体类的所有字段,顺序从顶层父类到当前类,每个类内按声明顺序。 排除 final 和 static 和 transient 字段

      final 实例字段无法写入,因此不参与映射(静默跳过);不可变实体请使用 record, 其 final 组件通过 getRecordFields(Class) 保留并由规范构造器填充。

      Parameters:
      clazz - 目标类
      Returns:
      字段数组
    • getRecordFields

      public static Field[] getRecordFields(Class<?> clazz)
      获取 record 的实例字段(即 record 组件),顺序为声明顺序。 record 组件本身是 final,因此保留 final,仅排除 static、transient 和 synthetic 字段。
      Parameters:
      clazz - record 类型
      Returns:
      字段数组
    • parameterizedType

      public static ParameterizedType parameterizedType(Class<?> rawType, Type... typeArgs)
      合成一个ParameterizedType
      Parameters:
      rawType - 原始类型
      typeArgs - 泛型
      Returns:
      带泛型的类型
    • resolveTypeArgument

      public static Type resolveTypeArgument(Class<?> clazz, Class<?> targetType, int index)
      解析类继承或实现关系中目标类型的实际泛型参数
      Parameters:
      clazz - 起始类
      targetType - 目标父类或接口
      index - 泛型参数下标
      Returns:
      实际类型,目标类型不在继承链中返回null;无法完全解析时返回值中保留类型变量
    • hasTypeVariable

      public static boolean hasTypeVariable(Type type)
      判断类型中是否包含未解析的类型变量
      Parameters:
      type - 类型
      Returns:
      是否包含类型变量
    • unreflectGetter

      public static Getter unreflectGetter(Field field)
      创建Field的getter方法的MethodHandle(只是为了处理异常)
      Parameters:
      field - 字段
      Returns:
      MethodHandle
    • unreflectInstantiator

      public static Supplier<Object> unreflectInstantiator(Class<?> cls)
      创建实例化提供者
      Parameters:
      cls - 实体类类型
      Returns:
      MethodHandle
    • unreflectInstantiator

      public static Function<Object[],Object> unreflectInstantiator(Class<?> cls, Field[] fields)
      创建实例化方法(传入参数,返回实例)
      Parameters:
      cls - 实体类类型
      fields - 字段
      Returns:
      MethodHandle
    • unreflectSetter

      public static Setter unreflectSetter(Field field)
      创建Field的setter方法的MethodHandle(只是为了处理异常)

      值为 null 时不做写入,即保留字段当前值(含字段初始值); 行映射依赖该约定:结果集中的 NULL 列不会覆盖实体/VO 的字段初始值。

      Parameters:
      field - 字段
      Returns:
      MethodHandle