Class FieldUtil

java.lang.Object
cloud.opencode.base.reflect.FieldUtil

public final class FieldUtil extends Object
Field Utility Class 字段工具类

Provides low-level field operation utilities with caching.

提供带缓存的底层字段操作工具。

Features | 主要功能:

  • Field discovery with caching - 带缓存的字段发现
  • Inherited field resolution - 继承字段解析
  • Field filtering by type, modifier, annotation - 按类型、修饰符、注解过滤字段

Usage Examples | 使用示例:

List<Field> fields = FieldUtil.getAllFields(User.class);
Field field = FieldUtil.getField(User.class, "name");

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) for cached lookups; O(f) for first access where f is the number of fields (including inherited) - 时间复杂度: 缓存命中时 O(1);首次访问为 O(f),f为字段数量(含继承)
  • Space complexity: O(f) for the cached fields per class - 空间复杂度: O(f),每类缓存字段
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getDeclaredFields

      public static Field[] getDeclaredFields(Class<?> clazz)
      Gets all declared fields (no inheritance) 获取所有声明的字段(不含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      array of fields | 字段数组
    • getAllFields

      public static List<Field> getAllFields(Class<?> clazz)
      Gets all fields including inherited (cached) 获取所有字段包含继承(缓存)
      Parameters:
      clazz - the class | 类
      Returns:
      list of fields | 字段列表
    • getField

      public static Field getField(Class<?> clazz, String fieldName)
      Gets field by name (cached) 按名称获取字段(缓存)
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the field or null | 字段或null
    • getFieldOrThrow

      public static Field getFieldOrThrow(Class<?> clazz, String fieldName)
      Gets field or throws exception 获取字段或抛出异常
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the field | 字段
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getFields

      public static List<Field> getFields(Class<?> clazz, Predicate<Field> predicate)
      Gets fields matching predicate 获取匹配条件的字段
      Parameters:
      clazz - the class | 类
      predicate - the predicate | 谓词
      Returns:
      list of matching fields | 匹配的字段列表
    • getFieldsWithAnnotation

      public static List<Field> getFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass)
      Gets fields with annotation 获取带注解的字段
      Parameters:
      clazz - the class | 类
      annotationClass - the annotation class | 注解类
      Returns:
      list of fields | 字段列表
    • getFieldsOfType

      public static List<Field> getFieldsOfType(Class<?> clazz, Class<?> fieldType)
      Gets fields of specific type 获取特定类型的字段
      Parameters:
      clazz - the class | 类
      fieldType - the field type | 字段类型
      Returns:
      list of fields | 字段列表
    • getInstanceFields

      public static List<Field> getInstanceFields(Class<?> clazz)
      Gets non-static fields 获取非静态字段
      Parameters:
      clazz - the class | 类
      Returns:
      list of fields | 字段列表
    • getStaticFields

      public static List<Field> getStaticFields(Class<?> clazz)
      Gets static fields 获取静态字段
      Parameters:
      clazz - the class | 类
      Returns:
      list of fields | 字段列表
    • getFieldValue

      public static Object getFieldValue(Object object, String fieldName)
      Gets field value by name, with unified Record/Class support. 按名称获取字段值,统一支持 Record 和普通类。

      For records, invokes the accessor method. For classes, uses field reflection.

      对于 Record,调用访问器方法。对于普通类,使用字段反射。

      Parameters:
      object - the target object | 目标对象
      fieldName - the field name | 字段名
      Returns:
      the field value | 字段值
      Throws:
      OpenReflectException - if field not found or access failed | 如果字段未找到或访问失败
    • getValue

      public static Object getValue(Field field, Object target)
      Gets field value 获取字段值
      Parameters:
      field - the field | 字段
      target - the target object | 目标对象
      Returns:
      the value | 值
    • getValue

      public static <T> T getValue(Field field, Object target, Class<T> type)
      Gets field value with type cast 获取字段值并转型
      Type Parameters:
      T - the type | 类型
      Parameters:
      field - the field | 字段
      target - the target object | 目标对象
      type - the expected type | 期望类型
      Returns:
      the value | 值
    • setValue

      public static void setValue(Field field, Object target, Object value)
      Sets field value 设置字段值
      Parameters:
      field - the field | 字段
      target - the target object | 目标对象
      value - the value | 值
    • getStaticValue

      public static Object getStaticValue(Field field)
      Gets static field value 获取静态字段值
      Parameters:
      field - the field | 字段
      Returns:
      the value | 值
    • setStaticValue

      public static void setStaticValue(Field field, Object value)
      Sets static field value 设置静态字段值
      Parameters:
      field - the field | 字段
      value - the value | 值
    • getType

      public static Class<?> getType(Field field)
      Gets field type 获取字段类型
      Parameters:
      field - the field | 字段
      Returns:
      the type | 类型
    • getGenericType

      public static Type getGenericType(Field field)
      Gets field generic type 获取字段泛型类型
      Parameters:
      field - the field | 字段
      Returns:
      the generic type | 泛型类型
    • isStatic

      public static boolean isStatic(Field field)
      Checks if field is static 检查字段是否为静态
      Parameters:
      field - the field | 字段
      Returns:
      true if static | 如果是静态返回true
    • isFinal

      public static boolean isFinal(Field field)
      Checks if field is final 检查字段是否为final
      Parameters:
      field - the field | 字段
      Returns:
      true if final | 如果是final返回true
    • isTransient

      public static boolean isTransient(Field field)
      Checks if field is transient 检查字段是否为transient
      Parameters:
      field - the field | 字段
      Returns:
      true if transient | 如果是transient返回true
    • isVolatile

      public static boolean isVolatile(Field field)
      Checks if field is volatile 检查字段是否为volatile
      Parameters:
      field - the field | 字段
      Returns:
      true if volatile | 如果是volatile返回true
    • clearCache

      public static void clearCache()
      Clears field cache 清除字段缓存
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears cache for specific class 清除特定类的缓存
      Parameters:
      clazz - the class | 类