Class FieldUtil

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

public final class FieldUtil extends Object
Field Utility Class - Field reflection operations 字段工具类 - 字段反射操作

Provides utilities for field discovery, filtering, and access operations.

提供字段发现、过滤和访问操作工具。

Features | 主要功能:

  • Get all/declared fields (with inheritance) - 获取全部/声明字段(含继承)
  • Filter by annotation, type, modifier - 按注解、类型、修饰符过滤
  • Field value get/set operations - 字段值读写操作
  • Modifier checks (static, final, transient) - 修饰符检查

Usage Examples | 使用示例:

// Get all fields - 获取所有字段
List<Field> fields = FieldUtil.getAllFields(User.class);

// Get by annotation - 按注解获取
List<Field> annotated = FieldUtil.getFieldsWithAnnotation(User.class, Column.class);

// Get/Set value - 读写值
Object value = FieldUtil.getValue(user, field);
FieldUtil.setValue(user, field, newValue);

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: No - 空值安全: 否

Performance | 性能特性:

  • Time complexity: O(n) where n = declared fields - O(n), n为声明的字段数
  • Space complexity: O(1) per access - 每次访问 O(1)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getAllFields

      public static List<Field> getAllFields(Class<?> clazz)
      Gets all fields (including superclass) 获取所有字段(包括父类)
    • getDeclaredFields

      public static List<Field> getDeclaredFields(Class<?> clazz)
      Gets declared fields (excluding superclass) 获取声明的字段(不包括父类)
    • getFieldByName

      public static Optional<Field> getFieldByName(Class<?> clazz, String name)
      Gets a field by name 按名称获取字段
    • getFieldsWithAnnotation

      public static List<Field> getFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
      Gets fields with the specified annotation 获取带有指定注解的字段
    • getFieldsByType

      public static List<Field> getFieldsByType(Class<?> clazz, Class<?> fieldType)
      Gets fields of the specified type 获取指定类型的字段
    • getStaticFields

      public static List<Field> getStaticFields(Class<?> clazz)
      Gets static fields 获取静态字段
    • getInstanceFields

      public static List<Field> getInstanceFields(Class<?> clazz)
      Gets instance fields (non-static) 获取实例字段(非静态)
    • getPublicFields

      public static List<Field> getPublicFields(Class<?> clazz)
      Gets public fields 获取公共字段
    • getGenericType

      public static Type getGenericType(Field field)
      Gets the generic type of a field 获取字段的泛型类型
    • getValue

      public static <T> T getValue(Object obj, Field field)
      Gets the field value 获取字段值
    • setValue

      public static void setValue(Object obj, Field field, Object value)
      Sets the field value 设置字段值
    • isFinal

      public static boolean isFinal(Field field)
      Checks if the field is final 检查字段是否为 final
    • isStatic

      public static boolean isStatic(Field field)
      Checks if the field is static 检查字段是否为 static
    • isTransient

      public static boolean isTransient(Field field)
      Checks if the field is transient 检查字段是否为 transient
    • isVolatile

      public static boolean isVolatile(Field field)
      Checks if the field is volatile 检查字段是否为 volatile
    • getFieldNames

      public static List<String> getFieldNames(Class<?> clazz)
      Gets the list of field names 获取字段名列表
    • getFieldMap

      public static Map<String,Field> getFieldMap(Class<?> clazz)
      Creates a field name to field mapping 创建字段名到字段的映射