Class OpenField

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

public final class OpenField extends Object
Field Facade Entry Class 字段门面入口类

Provides common field operations API, internally delegates to FieldUtil. Similar to Commons Lang FieldUtils.

提供常用字段操作API,内部委托给FieldUtil。 对标Commons Lang FieldUtils。

Features | 主要功能:

  • Field retrieval (with inheritance) - 字段获取(含继承)
  • Field read/write operations - 字段读写操作
  • Batch field operations - 批量字段操作
  • Annotation-based filtering - 基于注解的过滤

Usage Examples | 使用示例:

// Read field value
Object value = OpenField.readField(obj, "name");

// Write field value
OpenField.writeField(obj, "name", "newValue");

// Get all fields with annotation
List<Field> fields = OpenField.getFieldsWithAnnotation(User.class, Column.class);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getField

      public static Field getField(Class<?> clazz, String fieldName)
      Gets a field (including inherited) 获取字段(包含继承)
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the field | 字段
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getField

      public static Field getField(Class<?> clazz, String fieldName, boolean forceAccess)
      Gets a field with optional force access 获取字段(可选强制访问)
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      forceAccess - whether to force access | 是否强制访问
      Returns:
      the field | 字段
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getDeclaredField

      public static Field getDeclaredField(Class<?> clazz, String fieldName)
      Gets a declared field (not inherited) 获取声明的字段(不含继承)
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the field | 字段
      Throws:
      OpenReflectException - if not found | 如果未找到
    • getAllFields

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

      public static List<Field> getDeclaredFields(Class<?> clazz)
      Gets declared fields (not inherited) 获取声明的字段(不含继承)
      Parameters:
      clazz - the class | 类
      Returns:
      list of fields | 字段列表
    • getFieldsWithAnnotation

      public static List<Field> getFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass)
      Gets fields with specific 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 | 字段列表
    • getFieldsWithModifiers

      public static List<Field> getFieldsWithModifiers(Class<?> clazz, int... modifiers)
      Gets fields with specific modifiers 按修饰符获取字段
      Parameters:
      clazz - the class | 类
      modifiers - the modifiers | 修饰符
      Returns:
      list of fields | 字段列表
    • readField

      public static Object readField(Object target, String fieldName)
      Reads field value 读取字段值
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      Returns:
      the value | 值
    • readField

      public static Object readField(Object target, String fieldName, boolean forceAccess)
      Reads field value with optional force access 读取字段值(可选强制访问)
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      forceAccess - whether to force access | 是否强制访问
      Returns:
      the value | 值
    • readField

      public static Object readField(Field field, Object target)
      Reads field value using Field object 读取字段值(Field对象)
      Parameters:
      field - the field | 字段
      target - the target object | 目标对象
      Returns:
      the value | 值
    • readField

      public static <T> T readField(Object target, String fieldName, Class<T> valueType)
      Reads field value with type safety 读取字段值(泛型安全)
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      valueType - the expected type | 期望类型
      Returns:
      the value | 值
    • readStaticField

      public static Object readStaticField(Class<?> clazz, String fieldName)
      Reads static field 读取静态字段
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the value | 值
    • readStaticField

      public static <T> T readStaticField(Class<?> clazz, String fieldName, Class<T> valueType)
      Reads static field with type safety 读取静态字段(泛型安全)
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      valueType - the expected type | 期望类型
      Returns:
      the value | 值
    • writeField

      public static void writeField(Object target, String fieldName, Object value)
      Writes field value 写入字段值
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      value - the value | 值
    • writeField

      public static void writeField(Object target, String fieldName, Object value, boolean forceAccess)
      Writes field value with optional force access 写入字段值(可选强制访问)
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      value - the value | 值
      forceAccess - whether to force access | 是否强制访问
    • writeField

      public static void writeField(Field field, Object target, Object value)
      Writes field value using Field object 写入字段值(Field对象)
      Parameters:
      field - the field | 字段
      target - the target object | 目标对象
      value - the value | 值
    • writeStaticField

      public static void writeStaticField(Class<?> clazz, String fieldName, Object value)
      Writes static field 写入静态字段
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      value - the value | 值
    • removeFinalAndWrite

      public static void removeFinalAndWrite(Object target, String fieldName, Object value)
      Removes final modifier and writes value 移除final修饰符并写入值
      Parameters:
      target - the target object | 目标对象
      fieldName - the field name | 字段名
      value - the value | 值
    • getFieldType

      public static Class<?> getFieldType(Class<?> clazz, String fieldName)
      Gets field type 获取字段类型
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the field type | 字段类型
    • getFieldGenericType

      public static Type getFieldGenericType(Class<?> clazz, String fieldName)
      Gets field generic type 获取字段泛型类型
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the generic type | 泛型类型
    • getFieldTypeToken

      public static TypeToken<?> getFieldTypeToken(Class<?> clazz, String fieldName)
      Gets field TypeToken 获取字段TypeToken
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the TypeToken | TypeToken
    • hasField

      public static boolean hasField(Class<?> clazz, String fieldName)
      Checks if field exists 检查字段是否存在
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      true if exists | 如果存在返回true
    • forEach

      public static void forEach(Class<?> clazz, Consumer<Field> action)
      Iterates all fields (including inherited) 遍历所有字段(含继承)
      Parameters:
      clazz - the class | 类
      action - the action | 操作
    • findFirst

      public static Optional<Field> findFirst(Class<?> clazz, Predicate<Field> predicate)
      Finds first matching field 查找第一个匹配字段
      Parameters:
      clazz - the class | 类
      predicate - the predicate | 谓词
      Returns:
      Optional of field | 字段的Optional
    • stream

      public static Stream<Field> stream(Class<?> clazz)
      Creates a field stream 创建字段流
      Parameters:
      clazz - the class | 类
      Returns:
      stream of fields | 字段流
    • readFieldsToMap

      public static Map<String,Object> readFieldsToMap(Object target)
      Reads all fields to Map 批量读取为Map
      Parameters:
      target - the target object | 目标对象
      Returns:
      map of field names to values | 字段名到值的映射
    • readFields

      public static Map<String,Object> readFields(Object target, String... fieldNames)
      Reads specific fields 批量读取指定字段
      Parameters:
      target - the target object | 目标对象
      fieldNames - the field names | 字段名
      Returns:
      map of field names to values | 字段名到值的映射
    • writeFieldsFromMap

      public static void writeFieldsFromMap(Object target, Map<String,Object> values)
      Writes fields from Map 从Map批量写入
      Parameters:
      target - the target object | 目标对象
      values - the values map | 值映射