Class ToolsetBuilder

java.lang.Object
io.github.sugarcubes.cloner.ToolsetBuilder

public final class ToolsetBuilder extends Object
Builder of Toolset. Mechanisms are chosen by variants (Allocation, FieldAccess, ModuleOpening); the builder creates their implementations for the running JDK and connects them, a variant which is not available fails the build with an explanation. Custom implementations of the spi interfaces may be set instead.

Not set mechanisms are those of Toolsets.conservative(), they do not depend on the JVM as Toolsets.defaults() does: no module opening, Allocation.REFLECTION_FACTORY as instance allocator, Unsafe for classes which it refuses (JDK 28+), constructors as fallback; reflection field access with Unsafe for fields which reflection cannot access.

Instance allocators create objects without calling constructors, as Object.clone() does. Constructors may have side effects (registration, counters, validation) which a clone should not repeat, so allocators calling constructors are accepted only as an explicit fallback for types which no allocator supports.

  • Method Details

    • moduleOpening

      public ToolsetBuilder moduleOpening(ModuleOpening moduleOpening)
      Sets the mechanism opening packages to the cloner for reflection.
      Parameters:
      moduleOpening - module opening
      Returns:
      same builder instance
    • reflection

      public ToolsetBuilder reflection(Reflection reflection)
      Sets custom reflection instead of the default one, which opens packages by moduleOpening.
      Parameters:
      reflection - reflection
      Returns:
      same builder instance
    • allocation

      public ToolsetBuilder allocation(Allocation... allocation)
      Adds mechanisms creating instances without constructors. Mechanisms and custom allocators are tried in the order of adding; for every type the first one supporting it is used.
      Parameters:
      allocation - mechanisms in order of preference
      Returns:
      same builder instance
    • allocator

      public ToolsetBuilder allocator(InstanceAllocatorFactory allocator)
      Adds a custom instance allocator factory, which must not call constructors.
      Parameters:
      allocator - instance allocator factory
      Returns:
      same builder instance
    • allocators

      public ToolsetBuilder allocators(InstanceAllocatorFactory... allocators)
      Adds custom instance allocator factories, which must not call constructors. Mechanisms and custom allocators are tried in the order of adding; for every type the first one supporting it is used.
      Parameters:
      allocators - instance allocator factories in order of preference
      Returns:
      same builder instance
    • constructorFallback

      public ToolsetBuilder constructorFallback(InstanceAllocatorFactory constructorFallback)
      Sets instance allocator factory for types which no allocator supports. It may call constructors.
      Parameters:
      constructorFallback - instance allocator factory
      Returns:
      same builder instance
    • noConstructorFallback

      public ToolsetBuilder noConstructorFallback()
      Disables constructor fallback: types which no allocator supports cannot be cloned.
      Returns:
      same builder instance
    • fieldAccess

      public ToolsetBuilder fieldAccess(FieldAccess... fieldAccess)
      Adds mechanisms reading and writing fields. Mechanisms and custom adapters are tried in the order of adding; for every field the first one able to access it is used.
      Parameters:
      fieldAccess - mechanisms in order of preference
      Returns:
      same builder instance
    • fieldAdapter

      public ToolsetBuilder fieldAdapter(FieldAdapterFactory fieldAdapter)
      Adds a custom field adapter factory.
      Parameters:
      fieldAdapter - field adapter factory
      Returns:
      same builder instance
    • fieldAdapters

      public ToolsetBuilder fieldAdapters(FieldAdapterFactory... fieldAdapters)
      Adds custom field adapter factories. Mechanisms and custom adapters are tried in the order of adding; for every field the first one able to access it is used.
      Parameters:
      fieldAdapters - field adapter factories in order of preference
      Returns:
      same builder instance
    • build

      public Toolset build()
      Creates the toolset, fails with ClonerException if a chosen mechanism is not available.
      Returns:
      toolset