java.lang.Object
io.github.sugarcubes.cloner.ToolsetBuilder
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 Summary
Modifier and TypeMethodDescriptionallocation(Allocation... allocation) Adds mechanisms creating instances without constructors.allocator(InstanceAllocatorFactory allocator) Adds a custom instance allocator factory, which must not call constructors.allocators(InstanceAllocatorFactory... allocators) Adds custom instance allocator factories, which must not call constructors.build()Creates the toolset, fails withClonerExceptionif a chosen mechanism is not available.constructorFallback(InstanceAllocatorFactory constructorFallback) Sets instance allocator factory for types which no allocator supports.fieldAccess(FieldAccess... fieldAccess) Adds mechanisms reading and writing fields.fieldAdapter(FieldAdapterFactory fieldAdapter) Adds a custom field adapter factory.fieldAdapters(FieldAdapterFactory... fieldAdapters) Adds custom field adapter factories.moduleOpening(ModuleOpening moduleOpening) Sets the mechanism opening packages to the cloner for reflection.Disables constructor fallback: types which no allocator supports cannot be cloned.reflection(Reflection reflection) Sets custom reflection instead of the default one, which opens packages bymoduleOpening.
-
Method Details
-
moduleOpening
Sets the mechanism opening packages to the cloner for reflection.- Parameters:
moduleOpening- module opening- Returns:
- same builder instance
-
reflection
Sets custom reflection instead of the default one, which opens packages bymoduleOpening.- Parameters:
reflection- reflection- Returns:
- same builder instance
-
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
Adds a custom instance allocator factory, which must not call constructors.- Parameters:
allocator- instance allocator factory- Returns:
- same builder instance
-
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
Sets instance allocator factory for types which no allocator supports. It may call constructors.- Parameters:
constructorFallback- instance allocator factory- Returns:
- same builder instance
-
noConstructorFallback
Disables constructor fallback: types which no allocator supports cannot be cloned.- Returns:
- same builder instance
-
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
Adds a custom field adapter factory.- Parameters:
fieldAdapter- field adapter factory- Returns:
- same builder instance
-
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
Creates the toolset, fails withClonerExceptionif a chosen mechanism is not available.- Returns:
- toolset
-