Class RecordUtil

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

public final class RecordUtil extends Object
Record Utility Class Record工具类

Provides low-level record operation utilities with caching.

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

Features | 主要功能:

  • Record component discovery with caching - 带缓存的record组件发现
  • Canonical constructor resolution - 规范构造器解析
  • Accessor method caching - 访问器方法缓存

Usage Examples | 使用示例:

RecordComponent[] components = RecordUtil.getComponents(User.class);
Constructor<?> ctor = RecordUtil.getCanonicalConstructor(User.class);

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(c) for first access where c is the number of record components - 时间复杂度: 缓存命中时 O(1);首次访问为 O(c),c为 record 组件数量
  • Space complexity: O(c) for the cached component and accessor maps - 空间复杂度: O(c),缓存组件和访问器映射
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 void requireRecord(Class<?> clazz)
      Requires class to be a record 要求类为record
      Parameters:
      clazz - the class | 类
      Throws:
      IllegalArgumentException - if not a record | 如果不是record
    • getRecordComponents

      public static RecordComponent[] getRecordComponents(Class<?> recordClass)
      Gets record components (cached) 获取record组件(缓存)
      Parameters:
      recordClass - the record class | record类
      Returns:
      array of record components | record组件数组
    • getComponentCount

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

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

      public static Class<?>[] getComponentTypes(Class<?> recordClass)
      Gets component types 获取组件类型
      Parameters:
      recordClass - the record class | record类
      Returns:
      array of types | 类型数组
    • 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 | 组件
    • getAccessor

      public static Method getAccessor(Class<?> recordClass, String componentName)
      Gets accessor method for a component (cached) 获取组件的访问器方法(缓存)
      Parameters:
      recordClass - the record class | record类
      componentName - the component name | 组件名
      Returns:
      the accessor method | 访问器方法
    • getAccessors

      public static List<Method> getAccessors(Class<?> recordClass)
      Gets all accessor methods 获取所有访问器方法
      Parameters:
      recordClass - the record class | record类
      Returns:
      list of accessor methods | 访问器方法列表
    • getComponentValue

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

      public static <T> T getComponentValue(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 | 值
    • getComponentValues

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

      public static <T extends Record> Constructor<T> getCanonicalConstructor(Class<T> recordClass)
      Gets canonical constructor (cached) 获取规范构造器(缓存)
      Type Parameters:
      T - the record type | record类型
      Parameters:
      recordClass - the record class | record类
      Returns:
      the canonical constructor | 规范构造器
    • newInstance

      public static <T extends Record> T newInstance(Class<T> recordClass, Object... values)
      Creates a new record instance 创建新的record实例
      Type Parameters:
      T - the record type | record类型
      Parameters:
      recordClass - the record class | record类
      values - the component values | 组件值
      Returns:
      the record instance | 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 | 副本
    • toMap

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

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

      public static void clearCache()
      Clears all caches 清除所有缓存
    • clearCache

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