Class UnsafeCloner

java.lang.Object
cloud.opencode.base.deepclone.cloner.AbstractCloner
cloud.opencode.base.deepclone.cloner.UnsafeCloner
All Implemented Interfaces:
Cloner

public final class UnsafeCloner extends AbstractCloner
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 synchronizedMap) - 线程安全: 是(无状态,字段缓存使用synchronizedMap)
Since:
JDK 25, opencode-base-deepclone V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static UnsafeCloner 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

      public static <T> T allocateInstanceStatic(Class<T> type)
      SECURITY-SENSITIVE INTERNAL API. Allocates an instance of type WITHOUT running any constructor — bypassing parameter validation, invariant checks, and security-manager hooks. Reserved for use by the opencode-base-deepclone module'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); the OpenClone entry points provide the supported cloning paths and are the recommended interface. 安全敏感的内部 API。 不调任何构造器分配 type 实例 —— 绕过参数校验、 不变式检查、安全管理器钩子。仅供 opencode-base-deepclone 模块自身的克隆机制使用。 外部调用方不应用此方法实例化安全敏感类(java.security.SecureRandomjavax.crypto.Cipher、含不变式的应用单例); OpenClone 入口提供受支持的克隆路径,是推荐接口。

      V1.0.4 sec round-6 P2: kept public for cross-package use within the deepclone module (

      invalid reference
      cloud.opencode.base.deepclone.OpenClone#newInstance(Class)
      ). Package privacy was considered but blocked by the cross-package internal dependency; closing the surface fully would require either moving OpenClone into 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

      protected <T> T doClone(T original, CloneContext context)
      Description copied from class: AbstractCloner
      Performs the actual cloning logic 执行实际的克隆逻辑
      Specified by:
      doClone in class AbstractCloner
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      original - the original object | 原始对象
      context - the clone context | 克隆上下文
      Returns:
      the cloned object | 克隆的对象
    • allocateInstance

      protected <T> T allocateInstance(Class<T> type)
      Allocates an instance without calling constructor 不调用构造函数分配实例
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      Returns:
      the new instance | 新实例
    • copyField

      protected void copyField(Object source, Object target, Field field, CloneContext context)
      Copies a field value from source to target 从源对象复制字段值到目标对象
      Parameters:
      source - the source object | 源对象
      target - the target object | 目标对象
      field - the field | 字段
      context - the context | 上下文
    • getStrategyName

      public String getStrategyName()
      Description copied from interface: Cloner
      Gets the name of the cloning strategy 获取克隆策略名称
      Returns:
      the strategy name | 策略名称
    • supports

      public boolean supports(Class<?> type)
      Description copied from interface: Cloner
      Checks if this cloner supports the given type 检查此克隆器是否支持给定类型
      Parameters:
      type - the type to check | 要检查的类型
      Returns:
      true if supported | 如果支持返回true