Class RecordUtil
java.lang.Object
cloud.opencode.base.reflect.record.RecordUtil
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 Summary
Modifier and TypeMethodDescriptionstatic voidClears all caches 清除所有缓存static voidclearCache(Class<?> clazz) Clears cache for specific class 清除特定类的缓存static <T extends Record>
Tcopy(T record) Creates a copy of a record 创建record的副本static <T extends Record>
TCreates record from map 从map创建recordstatic MethodgetAccessor(Class<?> recordClass, String componentName) Gets accessor method for a component (cached) 获取组件的访问器方法(缓存)getAccessors(Class<?> recordClass) Gets all accessor methods 获取所有访问器方法static <T extends Record>
Constructor<T> getCanonicalConstructor(Class<T> recordClass) Gets canonical constructor (cached) 获取规范构造器(缓存)static RecordComponentgetComponent(Class<?> recordClass, int index) Gets component by index 按索引获取组件static RecordComponentgetComponent(Class<?> recordClass, String componentName) Gets component by name 按名称获取组件static intgetComponentCount(Class<?> recordClass) Gets component count 获取组件数量getComponentNames(Class<?> recordClass) Gets component names 获取组件名称static Class<?>[]getComponentTypes(Class<?> recordClass) Gets component types 获取组件类型static ObjectgetComponentValue(Record record, String componentName) Gets component value 获取组件值static <T> TgetComponentValue(Record record, String componentName, Class<T> type) Gets component value with type 获取组件值(带类型)static Object[]getComponentValues(Record record) Gets all component values 获取所有组件值static RecordComponent[]getRecordComponents(Class<?> recordClass) Gets record components (cached) 获取record组件(缓存)static booleanChecks if a class is a record 检查类是否为recordstatic booleanisRecordInstance(Object obj) Checks if an object is a record instance 检查对象是否为record实例static <T extends Record>
TnewInstance(Class<T> recordClass, Object... values) Creates a new record instance 创建新的record实例static voidrequireRecord(Class<?> clazz) Requires class to be a record 要求类为recordConverts record to map 将record转换为map
-
Method Details
-
isRecord
Checks if a class is a record 检查类是否为record- Parameters:
clazz- the class | 类- Returns:
- true if record | 如果是record返回true
-
isRecordInstance
Checks if an object is a record instance 检查对象是否为record实例- Parameters:
obj- the object | 对象- Returns:
- true if record instance | 如果是record实例返回true
-
requireRecord
Requires class to be a record 要求类为record- Parameters:
clazz- the class | 类- Throws:
IllegalArgumentException- if not a record | 如果不是record
-
getRecordComponents
Gets record components (cached) 获取record组件(缓存)- Parameters:
recordClass- the record class | record类- Returns:
- array of record components | record组件数组
-
getComponentCount
Gets component count 获取组件数量- Parameters:
recordClass- the record class | record类- Returns:
- the count | 数量
-
getComponentNames
-
getComponentTypes
-
getComponent
Gets component by name 按名称获取组件- Parameters:
recordClass- the record class | record类componentName- the component name | 组件名- Returns:
- the component or null | 组件或null
-
getComponent
Gets component by index 按索引获取组件- Parameters:
recordClass- the record class | record类index- the index | 索引- Returns:
- the component | 组件
-
getAccessor
-
getAccessors
-
getComponentValue
-
getComponentValue
Gets component value with type 获取组件值(带类型)- Type Parameters:
T- the value type | 值类型- Parameters:
record- the record | recordcomponentName- the component name | 组件名type- the expected type | 期望类型- Returns:
- the value | 值
-
getComponentValues
-
getCanonicalConstructor
Gets canonical constructor (cached) 获取规范构造器(缓存)- Type Parameters:
T- the record type | record类型- Parameters:
recordClass- the record class | record类- Returns:
- the canonical constructor | 规范构造器
-
newInstance
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
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
-
fromMap
-
clearCache
public static void clearCache()Clears all caches 清除所有缓存 -
clearCache
Clears cache for specific class 清除特定类的缓存- Parameters:
clazz- the class | 类
-