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)
      Allocates an instance without calling constructor (static utility) 不调用构造函数分配实例(静态工具方法)
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      Returns:
      the new instance | 新实例
    • 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