Class UnsafeCloner
- All Implemented Interfaces:
Cloner
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 synchronizedMap) - 线程安全: 是(无状态,字段缓存使用synchronizedMap)
- 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, enumHandler, fieldFilter, listener, mapHandler, maxDepth, optionalHandler, policy, recordHandler, typeHandlers -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> TallocateInstance(Class<T> type) Allocates an instance without calling constructor 不调用构造函数分配实例static <T> TallocateInstanceStatic(Class<T> type) SECURITY-SENSITIVE INTERNAL API. Allocates an instance oftypeWITHOUT running any constructor — bypassing parameter validation, invariant checks, and security-manager hooks.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 获取克隆策略名称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, getFieldFilter, getHandler, getImmutableTypes, isBuiltinImmutable, isImmutable, registerHandler, registerImmutable, setCloneTransient, setFieldFilter, setListener, setMaxDepth, setPolicy
-
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
SECURITY-SENSITIVE INTERNAL API. Allocates an instance oftypeWITHOUT running any constructor — bypassing parameter validation, invariant checks, and security-manager hooks. Reserved for use by theopencode-base-deepclonemodule's own cloning machinery. External callers should not use this method to instantiate security-sensitive classes (java.security.SecureRandom,javax.crypto.Cipher, application-specific singletons with invariants); theOpenCloneentry points provide the supported cloning paths and are the recommended interface. 安全敏感的内部 API。 不调任何构造器分配type实例 —— 绕过参数校验、 不变式检查、安全管理器钩子。仅供opencode-base-deepclone模块自身的克隆机制使用。 外部调用方不应用此方法实例化安全敏感类(java.security.SecureRandom、javax.crypto.Cipher、含不变式的应用单例);OpenClone入口提供受支持的克隆路径,是推荐接口。V1.0.4 sec round-6 P2: kept
publicfor cross-package use within the deepclone module (). Package privacy was considered but blocked by the cross-package internal dependency; closing the surface fully would require either movinginvalid reference
cloud.opencode.base.deepclone.OpenClone#newInstance(Class)OpenCloneinto the same package or introducing an exported-to-self module abstraction. Tracked for V1.0.5+; current release documents the security caveat prominently.V1.0.4 sec round-6 P2:因 deepclone 模块内跨包内部依赖 (
)保留invalid reference
cloud.opencode.base.deepclone.OpenClone#newInstance(Class)public。 包私有曾被考虑但被该跨包依赖阻塞;完全关闭表面需要把OpenClone搬到同一包或引入 模块内部抽象。延后至 V1.0.5+;本次发布显著文档化安全注意事项。 -
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 | 克隆的对象
-
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
-