Module io.github.sugarcubes.cloner
Package io.github.sugarcubes.cloner
package io.github.sugarcubes.cloner
Java deep cloning library.
Clone with the default cloner:
Order clone = Cloners.clone(order);
Configure a cloner once and reuse it, cloners are thread safe:
Cloner cloner = Cloners.builder()
// share instances of a type and its subtypes
.typeAction(Predicates.subclass(Connection.class), CopyAction.ORIGINAL)
// clones get no value in a field
.fieldAction(Order.class, "cachedTotal", CopyAction.CLEAR)
// clones get a new list in a field
.fieldValue(Order.class, "lines", ArrayList::new)
// clear fields with an annotation
.fieldAction(Predicates.annotatedWith(Transient.class), CopyAction.CLEAR)
// a custom copier of a type
.copier(Money.class, new MoneyCopier())
// copy with the threads of a pool
.executor(pool)
.build();
Order clone = cloner.clone(order);
Objects which cannot be copied meaningfully (threads, open files, sockets, executors) make cloning fail with
UncopyableObjectException unless the configuration decides what the clone gets.
The low-level mechanisms (allocation, field access, module opening) are chosen by
Toolsets.defaults() for the running JVM, see
Toolset.
The guide: sugar-cubes.github.io/sugar-cubes-cloner.
- See Also:
-
ClassDescriptionMechanisms creating instances without calling constructors, see
ToolsetBuilder.allocation(Allocation...).Cloner interface.Generic cloner exception.Factory for creating differentCloners.Cloner work mode.Copyable<T>An interface for classes which know how to clone themselves.Actions which can be applied to objects, types and fields: what the clone gets instead of the original value.Context of the single cloning process.Receives a clone.CopyPolicy<I>Copy policy interface.Mechanisms reading and writing fields, seeToolsetBuilder.fieldAccess(FieldAccess...).Annotation for applyingCopyActionto fields.Mechanisms opening packages of other modules to the cloner for reflection, JDK 9+; on JDK 8 there is nothing to open.NoopCopier<T>Copier which always returns original object.NullCopier<T>Copier which always returnsnull.ObjectCopier<T>Copier for the specific object type.Predicates factory, for the predicate variants ofReflectionClonerBuilder.typeAction(Predicate, CopyAction)andReflectionClonerBuilder.fieldAction(Predicate, CopyAction).Builder for reflection cloners.Low-level tools of the reflection cloner: reflection, instance allocators and field adapters.Builder ofToolset.Profiles ofToolset: ready combinations of the low-level mechanisms.Object graph traversal algorithm.Marker interface for trivial copiers.Two-phase copier.Annotation for applyingObjectCopierto type.Annotation for applyingCopyActionto types.Thrown when the cloner meets a live object of the JVM or the operating system (a thread, a reference, an open file, a socket, a process, an executor) and the developer has not chosen what the clone gets: a clone cannot get its own one, and sharing the original must be chosen explicitly byCopyAction.ORIGINAL(orCopyAction.CLEAR) for the type, the field or the object, or by a copier of the type.