Alternatives¶
How else a Java object graph can be copied, and what each way costs.
Comparison¶
| Way | Problem |
|---|---|
Object.clone() |
every class needs Cloneable and a hand-written deep copy; clone() cannot assign a final field, so final fields stay shared |
| Copy constructors written by hand | code for every class; a field added later is silently left out; cycles and shared references need extra bookkeeping |
| Java serialization | every class of the graph must be Serializable; slow; runs readObject() and readResolve() |
| JSON (Jackson, Gson) there and back | by default loses the runtime types of polymorphic fields, fails on cycles, duplicates shared objects, drops state without accessors |
| Mappers (MapStruct, ModelMapper) | made for converting between types: a mapper per type, cycles and shared references need extra code |
Builders, toBuilder(), @With |
shallow: the copy shares the mutable objects of the original |
Kryo copy() |
a serialization framework: by default every class must be registered and is created by its constructor without arguments, so it runs code of the class; fields are made accessible by reflection, with the same JDK restrictions as below |
| Cloners copying fields by reflection | need --add-opens for JDK classes on JDK 16+, warn about final fields on JDK 26+ (JEP 500) and fail on them when the JVM denies their mutation |
The cloner copies any graph without code in the classes: no Cloneable, no Serializable, no constructors, no
accessors. Shared references stay shared, cycles stay cycles, records are created by their constructors. It follows the
restrictions of every JDK by itself (see Java versions) and asks what to do with objects which
cannot be copied, instead of copying a thread or a socket silently.
Serialization is still the right choice when the copy must go through readObject() and readResolve(), e.g. to get
the canonical instances of a class: use SerializationUtils.clone() of Apache Commons Lang.
When you need no cloner¶
- Immutable data. Records and classes with immutable fields (strings,
java.time,List.of()) can be shared as they are: a copy changes nothing. - Structural sharing. For large states changed in small steps, persistent collections (e.g. Vavr, PCollections) share the unchanged parts between versions and are cheaper than a copy of the whole graph.
- One class, a few fields. A copy constructor or a
copy()method written by hand is simpler than a library.
Care needed¶
- Entities of JPA and Hibernate. A managed entity may hold lazy proxies and collections bound to an open session.
Clone detached entities with initialized associations, or share the types of the persistence context by
CopyAction.ORIGINAL; give the clone a new id by values of fields. - Objects with identity. Singletons, registries and objects compared by
==must be shared, see Configuration. The cloner already shares enums, Kotlin and Scala objects and identities of the JVM, see JDK types. - Live resources. Threads, executors, files and sockets cannot be copied; the cloner fails until you decide, see Troubleshooting.