Class UnsafeCloner
java.lang.Object
cloud.opencode.base.deepclone.cloner.AbstractCloner
cloud.opencode.base.deepclone.cloner.UnsafeCloner
- All Implemented Interfaces:
Cloner
Unsafe-based high-performance deep cloner
基于Unsafe的高性能深度克隆器
Uses sun.misc.Unsafe for direct memory operations. Creates instances without calling constructors and copies fields directly.
使用sun.misc.Unsafe进行直接内存操作。创建实例时不调用构造函数,直接复制字段。
Features | 主要功能:
- Highest performance - 最高性能
- No constructor invocation - 不调用构造函数
- Direct memory field copy - 直接内存字段复制
Limitations | 限制:
- Uses internal API (sun.misc.Unsafe) - 使用内部API
- May not work in all JVM environments - 可能不在所有JVM环境工作
- Bypasses constructor logic - 绕过构造函数逻辑
Usage Examples | 使用示例:
if (UnsafeCloner.isAvailable()) {
UnsafeCloner cloner = UnsafeCloner.create();
User cloned = cloner.clone(originalUser);
}
Security | 安全性:
- Thread-safe: Yes (stateless, field cache uses ConcurrentHashMap) - 线程安全: 是(无状态,字段缓存使用ConcurrentHashMap)
- Since:
- JDK 25, opencode-base-deepclone V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields inherited from class AbstractCloner
arrayHandler, cloneTransient, collectionHandler, customImmutableTypes, mapHandler, maxDepth, recordHandler, typeHandlers -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> TallocateInstance(Class<T> type) Allocates an instance without calling constructor 不调用构造函数分配实例static <T> TallocateInstanceStatic(Class<T> type) Allocates an instance without calling constructor (static utility) 不调用构造函数分配实例(静态工具方法)protected voidcopyField(Object source, Object target, Field field, CloneContext context) Copies a field value from source to target 从源对象复制字段值到目标对象static UnsafeClonercreate()Creates an UnsafeCloner 创建UnsafeClonerprotected <T> TdoClone(T original, CloneContext context) Performs the actual cloning logic 执行实际的克隆逻辑Gets the name of the cloning strategy 获取克隆策略名称protected sun.misc.UnsafeGets the Unsafe instance 获取Unsafe实例static booleanChecks if Unsafe is available 检查Unsafe是否可用booleanChecks if this cloner supports the given type 检查此克隆器是否支持给定类型Methods inherited from class AbstractCloner
clone, clone, cloneArray, cloneCollection, cloneMap, getHandler, getImmutableTypes, isBuiltinImmutable, isImmutable, registerHandler, registerImmutable, setCloneTransient, setMaxDepth
-
Method Details
-
create
Creates an UnsafeCloner 创建UnsafeCloner- Returns:
- the cloner | 克隆器
- Throws:
OpenDeepCloneException- if Unsafe is not available | 如果Unsafe不可用
-
isAvailable
public static boolean isAvailable()Checks if Unsafe is available 检查Unsafe是否可用- Returns:
- true if available | 如果可用返回true
-
allocateInstanceStatic
Allocates an instance without calling constructor (static utility) 不调用构造函数分配实例(静态工具方法)- Type Parameters:
T- the type parameter | 类型参数- Parameters:
type- the type | 类型- Returns:
- the new instance | 新实例
-
doClone
Description copied from class:AbstractClonerPerforms the actual cloning logic 执行实际的克隆逻辑- Specified by:
doClonein classAbstractCloner- Type Parameters:
T- the object type | 对象类型- Parameters:
original- the original object | 原始对象context- the clone context | 克隆上下文- Returns:
- the cloned object | 克隆的对象
-
getUnsafe
protected sun.misc.Unsafe getUnsafe()Gets the Unsafe instance 获取Unsafe实例- Returns:
- the Unsafe instance | Unsafe实例
-
allocateInstance
Allocates an instance without calling constructor 不调用构造函数分配实例- Type Parameters:
T- the type parameter | 类型参数- Parameters:
type- the type | 类型- Returns:
- the new instance | 新实例
-
copyField
Copies a field value from source to target 从源对象复制字段值到目标对象- Parameters:
source- the source object | 源对象target- the target object | 目标对象field- the field | 字段context- the context | 上下文
-
getStrategyName
-
supports
-