Skip to content
Edit this page

Java versions

The cloner runs on JDK 8 and newer. Every few releases the JDK restricts a mechanism which deep cloning relies on: reflection into the JDK, sun.misc.Unsafe, sun.reflect.ReflectionFactory, the mutation of final fields. The default toolset follows the restrictions by itself; this page tells what changes in which version, what you see and which JVM options matter.

Restrictions by version

JDK Change of the JDK The cloner
8 no modules generated copiers are anonymous classes of Unsafe
9 modules (JEP 261), deep reflection into the JDK permitted by default opens the packages it needs, see module opening
15 hidden classes (JEP 371) generated copiers are hidden classes in the nest of the copied class
16 the JDK strongly encapsulated by default (JEP 396); records (JEP 395) clones common graphs without --add-opens; records are created by their constructors
17 --illegal-access removed (JEP 403), Unsafe.defineAnonymousClass removed nothing new
23 memory access of sun.misc.Unsafe deprecated for removal (JEP 471) nothing new
24 warnings about Unsafe memory access (JEP 498) and JNI (JEP 472); ClassFile API the JVM warns once about Unsafe; generated copiers are written by the ClassFile API
26 warnings about the mutation of final fields by deep reflection (JEP 500) writes final fields by Unsafe in the default toolset, so no warning
27 nothing new for the cloner
28 ReflectionFactory refuses classes which are not serializable (JDK-8393262) creates their instances by Unsafe.allocateInstance(), which the JDK keeps

28 is the early access build, its release is planned for March 2027.

Warnings

On JDK 24+ with the default toolset the JVM warns once:

WARNING: sun.misc.Unsafe::objectFieldOffset has been called by io.github.sugarcubes.cloner.unsafe.SunMiscUnsafeBridge

It is harmless. --sun-misc-unsafe-memory-access=allow silences it; the cloner then works as before.

On JDK 26+ a toolset which writes fields by reflection (Toolsets.conservative(), Toolsets.reflection(), or the default toolset when Unsafe is denied) makes the JVM warn once about the first final field:

WARNING: Final field total in class com.acme.Order has been mutated reflectively by class
io.github.sugarcubes.cloner.fields.ReflectionFieldAdapterImpl in unnamed module @6d06d69c

--enable-final-field-mutation=ALL-UNNAMED (or io.github.sugarcubes.cloner if the cloner is a named module) allows it and silences the warning. Classes with copy constructors from the build tools write their final fields in the constructor, the JVM does not check that.

On JDK 24+ Narcissus warns that it loads a native library unless the JVM is started with --enable-native-access=ALL-UNNAMED.

JVM options

The options which deny a mechanism now are the defaults of future JDKs. The cloner is tested with them.

Option JDK The cloner
--sun-misc-unsafe-memory-access=deny 24+ the default toolset accesses fields by reflection and opens packages by the agent or Narcissus; Toolsets.unsafe() cannot be built
--illegal-final-field-mutation=deny 26+ the default toolset writes final fields by Unsafe; toolsets with reflection fail on a final field and tell the remedies
--enable-final-field-mutation=... 26+ reflection writes final fields again
--illegal-native-access=deny 24+ Narcissus cannot be loaded; the default toolset opens packages by the agent only
-Djdk.reflect.allowNonSerializableConstruction=true 28 ReflectionFactory creates all classes again, until a later JDK ignores the property

Future JDKs

The JDK plans to deny the memory access of Unsafe and the mutation of final fields by default, and later to remove the memory access methods of Unsafe. Then reflection is the only standard way to write a field, and a final field only if the JVM allows it to the cloner. To be ready:

  • add copy constructors to your classes with the build tools or the agent: the cloner needs neither Unsafe nor reflection for them;
  • or start the JVM with --enable-final-field-mutation=ALL-UNNAMED;
  • for private fields of JDK classes start the JVM with the cloner agent (-javaagent:/path/to/sugar-cubes-cloner.jar) or open their packages by --add-opens.

Tested versions

The build runs the tests of every module on the JDK of the module (8, 9, 16, 24) and on JDK 17, the tests without --add-opens on JDK 17, the tests of denied mechanisms on JDK 25 (Unsafe and native access) and JDK 27 (the mutation of final fields, alone and with Unsafe denied). A separate job of the CI runs the tests on the early access build of the next JDK, now 28. -PtestJdk=21 runs the tests on another JDK, see CONTRIBUTING.