Skip to content
Edit this page

Sugar Cubes Cloner Sugar Cubes Cloner

Deep cloning of any Java objects: object graphs with shared references and cycles, JDK collections, records, your own classes without any code in them.

Order clone = Cloners.clone(order);
  • Fast. Copiers are generated at runtime for your classes; copy constructors can be added at build time.
  • Configurable. Choose per type, field or object whether a value is cloned, shared or replaced with null; plug your own copiers.
  • Safe by default. Threads, open files, sockets and the like are not copied silently: the cloner asks you what the clone gets.
  • Sequential or parallel. One thread without recursion, or a thread pool for large graphs.
  • Java 8 to 28. One multi-release jar, the module io.github.sugarcubes.cloner, tested on HotSpot and OpenJ9, see Java versions.

What it solves

A deep copy is needed whenever code must change an object graph without touching the original, or must keep the original safe from code it does not control:

  • Snapshots and undo. Keep the state before a change, return to it if the change fails.
  • Editing a copy. A form, a draft or a wizard works with a copy; the original changes only when it is saved.
  • Defensive copies. A cache or a service hands out copies, so callers cannot corrupt the shared state.
  • Isolation between threads. Every thread or task gets its own instance of a mutable graph.
  • A new entity from a template. Copy an order with a new id, version and creation time, see Values of fields.
  • Test data. One reference object, a modified copy in every test.
  • Branching state. Simulations, games and solvers try variants on copies of the state.

The usual ways to copy fail on real graphs: clone() needs code in every class, serialization needs Serializable everywhere, JSON loses types and cycles, copy constructors written by hand forget new fields, and libraries copying fields by reflection hit the restrictions of new JDKs. See Alternatives for the comparison and for the cases when you need no cloner.

Installation

<dependency>
    <groupId>io.github.sugar-cubes</groupId>
    <artifactId>sugar-cubes-cloner</artifactId>
    <version>2.2.2</version>
</dependency>
implementation("io.github.sugar-cubes:sugar-cubes-cloner:2.2.2")

The library has no required dependencies, see Dependencies for the optional ones.

Where next

  • Getting started: the first clone, cloners, errors.
  • Dependencies: optional libraries, JDK modules, jlink and the module path.
  • Alternatives: other ways to copy objects, when no cloner is needed.
  • Configuration: copy actions, policies, annotations, custom copiers.
  • Modes: sequential, recursive and parallel cloning.
  • JDK types: what happens to strings, collections, records, threads.
  • Toolsets: how objects are created and fields accessed, JVM options.
  • Java versions: restrictions of every JDK, warnings, JVM options.
  • Performance: generated copiers and copy constructors.
  • Build tools: copy constructors by an annotation processor, a Maven or a Gradle plugin.
  • Agent: copy constructors when classes are loaded, module opening.
  • Kotlin, Scala, Groovy: objects, lambdas, coroutines, futures, meta classes.
  • Troubleshooting: common exceptions and warnings.
  • API reference: the Javadoc.
  • llms.txt: a summary of the library for AI coding agents.