Agent¶
The jar of the cloner is also a Java agent. Started with the JVM, it adds copy constructors to the listed classes when they are loaded, without a change of the build, and opens the packages of the JDK to the cloner:
The arguments of the agent are not used yet.
Copy constructors at load time¶
The classes get the same copy constructors as from the build tools: the cloner creates their clones
by new and writes final fields directly, without Unsafe and without reflection, which future JDKs restrict, see
Java versions.
- Which classes. The masks are read from the resources
META-INF/sugar-cubes-cloner/copy-constructors, in the format of the build tools, those which the class loader defining the class sees: a project lists its classes in its resources, a library in its jar. Classes enhanced at build time are left as they are. - Bytecode engine. The class files are changed by the ClassFile API of the JVM on JDK 24+, by ASM on older JDKs:
org.ow2.asm:asmmust be on the classpath of the cloner or of the class loader of the class. Without an engine the classes stay as they are, and the agent warns once. - Started with the JVM only. Only the classes loaded after the agent started get copy constructors: the JVM does not let an agent add constructors to loaded classes, so the agent loaded into a running JVM by the Attach API adds none.
- Class loaders. The class loader of a class must see the cloner, which the constructor calls. The superclass of a class is looked up by parent-first delegation: with a class loader which loads its own classes first, list the classes of a hierarchy in the resources of one loader.
- Kotlin, Scala, Groovy. Their classes get copy constructors too, except the classes which the cloner never copies, see Kotlin, Scala, Groovy.
Problems are logged by java.util.logging, the logger io.github.sugarcubes.cloner.agent.CopyConstructorTransformer,
see Troubleshooting.
Module opening¶
The agent gives the cloner java.lang.instrument.Instrumentation, which opens the packages of the JDK whose private
fields the cloner copies, without --add-opens. The default toolset prefers it to the other ways, see
Toolsets. For module opening alone the agent may also be loaded into a running
JVM by the Attach API.