Class OpenRecord

java.lang.Object
cloud.opencode.base.reflect.record.OpenRecord

public final class OpenRecord extends Object
Record Facade Entry Class Record门面入口类

Provides common record operations API.

提供常用record操作API。

Features | 主要功能:

  • Record detection - Record检测
  • Component access - 组件访问
  • Record creation - Record创建
  • Record copying - Record复制

Usage Examples | 使用示例:

// Check if record
boolean isRecord = OpenRecord.isRecord(User.class);

// Get component values
Map<String, Object> values = OpenRecord.toMap(userRecord);

// Copy with modifications
User modified = OpenRecord.copyWith(user, Map.of("name", "Alice"));

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

    • isRecord

      public static boolean isRecord(Class<?> clazz)
      Checks if a class is a record 检查类是否为record
      Parameters:
      clazz - the class | 类
      Returns:
      true if record | 如果是record返回true
    • isRecordInstance

      public static boolean isRecordInstance(Object obj)
      Checks if an object is a record instance 检查对象是否为record实例
      Parameters:
      obj - the object | 对象
      Returns:
      true if record instance | 如果是record实例返回true
    • requireRecord

      public static <T> Class<T> requireRecord(Class<T> clazz)
      Requires class to be a record 要求类为record
      Type Parameters:
      T - the type | 类型
      Parameters:
      clazz - the class | 类
      Returns:
      the class | 类
      Throws:
      IllegalArgumentException - if not a record | 如果不是record
    • getComponents

      public static List<RecordComponent> getComponents(Class<?> recordClass)
      Gets record components 获取record组件
      Parameters:
      recordClass - the record class | record类
      Returns:
      list of components | 组件列表
    • getComponent

      public static RecordComponent getComponent(Class<?> recordClass, String componentName)
      Gets component by name 按名称获取组件
      Parameters:
      recordClass - the record class | record类
      componentName - the component name | 组件名
      Returns:
      the component or null | 组件或null
    • getComponent

      public static RecordComponent getComponent(Class<?> recordClass, int index)
      Gets component by index 按索引获取组件
      Parameters:
      recordClass - the record class | record类
      index - the index | 索引
      Returns:
      the component | 组件
    • getComponentNames

      public static List<String> getComponentNames(Class<?> recordClass)
      Gets component names 获取组件名称
      Parameters:
      recordClass - the record class | record类
      Returns:
      list of names | 名称列表
    • getComponentTypes

      public static List<Class<?>> getComponentTypes(Class<?> recordClass)
      Gets component types 获取组件类型
      Parameters:
      recordClass - the record class | record类
      Returns:
      list of types | 类型列表
    • getComponentCount

      public static int getComponentCount(Class<?> recordClass)
      Gets number of components 获取组件数量
      Parameters:
      recordClass - the record class | record类
      Returns:
      the count | 数量
    • getValue

      public static Object getValue(Record record, String componentName)
      Gets component value from a record 从record获取组件值
      Parameters:
      record - the record | record
      componentName - the component name | 组件名
      Returns:
      the value | 值
    • getValue

      public static <T> T getValue(Record record, String componentName, Class<T> type)
      Gets component value with type 获取组件值(带类型)
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      record - the record | record
      componentName - the component name | 组件名
      type - the expected type | 期望类型
      Returns:
      the value | 值
    • getValues

      public static Object[] getValues(Record record)
      Gets all component values 获取所有组件值
      Parameters:
      record - the record | record
      Returns:
      array of values | 值数组
    • toMap

      public static Map<String,Object> toMap(Record record)
      Converts record to map 将record转换为map
      Parameters:
      record - the record | record
      Returns:
      the map | 映射
    • builder

      public static <T extends Record> RecordBuilder<T> builder(Class<T> recordClass)
      Creates a record builder 创建record构建器
      Type Parameters:
      T - the record type | record类型
      Parameters:
      recordClass - the record class | record类
      Returns:
      the builder | 构建器
    • create

      public static <T extends Record> T create(Class<T> recordClass, Object... values)
      Creates a record from values 从值创建record
      Type Parameters:
      T - the record type | record类型
      Parameters:
      recordClass - the record class | record类
      values - the component values | 组件值
      Returns:
      the record | record
    • fromMap

      public static <T extends Record> T fromMap(Class<T> recordClass, Map<String,?> values)
      Creates a record from a map 从map创建record
      Type Parameters:
      T - the record type | record类型
      Parameters:
      recordClass - the record class | record类
      values - the values map | 值映射
      Returns:
      the record | record
    • copy

      public static <T extends Record> T copy(T record)
      Creates a copy of a record 创建record的副本
      Type Parameters:
      T - the record type | record类型
      Parameters:
      record - the record to copy | 要复制的record
      Returns:
      the copy | 副本
    • copyWith

      public static <T extends Record> T copyWith(T record, Map<String,?> modifications)
      Creates a copy with modified values 创建带修改值的副本
      Type Parameters:
      T - the record type | record类型
      Parameters:
      record - the record to copy | 要复制的record
      modifications - the modifications | 修改
      Returns:
      the modified copy | 修改后的副本
    • copyWith

      public static <T extends Record> T copyWith(T record, String componentName, Object newValue)
      Creates a copy with a single modified value 创建带单个修改值的副本
      Type Parameters:
      T - the record type | record类型
      Parameters:
      record - the record to copy | 要复制的record
      componentName - the component to modify | 要修改的组件
      newValue - the new value | 新值
      Returns:
      the modified copy | 修改后的副本
    • diff

      public static Map<String,Object[]> diff(Record record1, Record record2)
      Compares two records component by component 逐组件比较两个record
      Parameters:
      record1 - the first record | 第一个record
      record2 - the second record | 第二个record
      Returns:
      map of differing component names to their values | 不同组件名到其值的映射
    • valuesEqual

      public static boolean valuesEqual(Record record1, Record record2)
      Checks if two records have the same values 检查两个record是否有相同的值
      Parameters:
      record1 - the first record | 第一个record
      record2 - the second record | 第二个record
      Returns:
      true if equal | 如果相等返回true