Class AbstractCloner

java.lang.Object
cloud.opencode.base.deepclone.cloner.AbstractCloner
All Implemented Interfaces:
Cloner
Direct Known Subclasses:
ReflectiveCloner, SerializingCloner, UnsafeCloner

public abstract sealed class AbstractCloner extends Object implements Cloner permits ReflectiveCloner, SerializingCloner, UnsafeCloner
Abstract base class for cloners (sealed hierarchy) 克隆器的抽象基类(密封层次结构)

Provides common functionality for all cloner implementations including immutable type detection, type handlers, and circular reference handling.

为所有克隆器实现提供通用功能,包括不可变类型检测、类型处理器和循环引用处理。

Features | 主要功能:

  • Immutable type registry - 不可变类型注册
  • Type handler management - 类型处理器管理
  • Circular reference detection - 循环引用检测
  • DeepCloneable interface support - DeepCloneable接口支持

Usage Examples | 使用示例:

// Extend to implement custom cloning logic
public final class MyCloner extends AbstractCloner {
    @Override
    protected Object cloneInternal(Object obj, CloneContext ctx) {
        // custom clone logic
    }
}

Security | 安全性:

  • Thread-safe: Yes (immutable type registry, ConcurrentHashMap cache) - 线程安全: 是(不可变类型注册,ConcurrentHashMap缓存)
Since:
JDK 25, opencode-base-deepclone V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • customImmutableTypes

      protected final Set<Class<?>> customImmutableTypes
      Custom immutable types 自定义不可变类型
    • typeHandlers

      protected final Map<Class<?>, TypeHandler<?>> typeHandlers
      Type handlers 类型处理器
    • arrayHandler

      protected final ArrayHandler arrayHandler
      Default handlers 默认处理器
    • collectionHandler

      protected final CollectionHandler collectionHandler
    • mapHandler

      protected final MapHandler mapHandler
    • recordHandler

      protected final RecordHandler recordHandler
    • enumHandler

      protected final EnumHandler enumHandler
    • optionalHandler

      protected final OptionalHandler optionalHandler
    • maxDepth

      protected volatile int maxDepth
      Max clone depth 最大克隆深度
    • cloneTransient

      protected volatile boolean cloneTransient
      Whether to clone transient fields 是否克隆transient字段
    • policy

      protected volatile ClonePolicy policy
      Clone policy 克隆策略
    • fieldFilter

      protected volatile FieldFilter fieldFilter
      Field filter 字段过滤器
    • listener

      protected volatile CloneListener listener
      Clone listener 克隆监听器
  • Constructor Details

    • AbstractCloner

      public AbstractCloner()
  • Method Details

    • clone

      public <T> T clone(T original)
      Description copied from interface: Cloner
      Deep clones an object 深度克隆对象
      Specified by:
      clone in interface Cloner
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      original - the original object | 原始对象
      Returns:
      the cloned object | 克隆的对象
    • clone

      public <T> T clone(T original, CloneContext context)
      Description copied from interface: Cloner
      Deep clones an object using a specific context 使用特定上下文深度克隆对象
      Specified by:
      clone in interface Cloner
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      original - the original object | 原始对象
      context - the clone context | 克隆上下文
      Returns:
      the cloned object | 克隆的对象
    • doClone

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

      protected Set<Class<?>> getImmutableTypes()
      Gets registered immutable types 获取注册的不可变类型
      Returns:
      the immutable types | 不可变类型集合
    • isImmutable

      protected boolean isImmutable(Class<?> type)
      Checks if a type is immutable 检查类型是否不可变
      Parameters:
      type - the type | 类型
      Returns:
      true if immutable | 如果不可变返回true
    • isBuiltinImmutable

      protected boolean isBuiltinImmutable(Class<?> type)
      Checks if a type is a built-in immutable type 检查类型是否为内置不可变类型
      Parameters:
      type - the type | 类型
      Returns:
      true if built-in immutable | 如果是内置不可变返回true
    • registerImmutable

      public void registerImmutable(Class<?>... types)
      Registers custom immutable types 注册自定义不可变类型
      Parameters:
      types - the types to register | 要注册的类型
    • getHandler

      protected <T> TypeHandler<T> getHandler(Class<T> type)
      Gets the type handler for a type 获取类型的处理器
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      Returns:
      the handler, or null if none | 处理器,如果没有则为null
    • registerHandler

      public <T> void registerHandler(Class<T> type, TypeHandler<T> handler)
      Registers a type handler 注册类型处理器
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      type - the type | 类型
      handler - the handler | 处理器
    • cloneArray

      protected Object cloneArray(Object array, CloneContext context)
      Clones an array 克隆数组
      Parameters:
      array - the array | 数组
      context - the context | 上下文
      Returns:
      the cloned array | 克隆的数组
    • cloneCollection

      protected <T> Collection<T> cloneCollection(Collection<T> collection, CloneContext context)
      Clones a collection 克隆集合
      Type Parameters:
      T - the element type | 元素类型
      Parameters:
      collection - the collection | 集合
      context - the context | 上下文
      Returns:
      the cloned collection | 克隆的集合
    • cloneMap

      protected <K,V> Map<K,V> cloneMap(Map<K,V> map, CloneContext context)
      Clones a map 克隆Map
      Type Parameters:
      K - the key type | 键类型
      V - the value type | 值类型
      Parameters:
      map - the map | Map
      context - the context | 上下文
      Returns:
      the cloned map | 克隆的Map
    • setMaxDepth

      public void setMaxDepth(int maxDepth)
      Sets the max clone depth 设置最大克隆深度
      Parameters:
      maxDepth - the max depth | 最大深度
    • setCloneTransient

      public void setCloneTransient(boolean cloneTransient)
      Sets whether to clone transient fields 设置是否克隆transient字段
      Parameters:
      cloneTransient - whether to clone | 是否克隆
    • setPolicy

      public void setPolicy(ClonePolicy policy)
      Sets the clone policy 设置克隆策略
      Parameters:
      policy - the policy | 策略
    • setFieldFilter

      public void setFieldFilter(FieldFilter fieldFilter)
      Sets the field filter 设置字段过滤器
      Parameters:
      fieldFilter - the filter | 过滤器
    • getFieldFilter

      public FieldFilter getFieldFilter()
      Gets the field filter 获取字段过滤器
      Returns:
      the field filter, may be null | 字段过滤器,可能为null
    • setListener

      public void setListener(CloneListener listener)
      Sets the clone listener 设置克隆监听器
      Parameters:
      listener - the listener | 监听器