Skip to content
Edit this page

Build tools

Add copy constructors to your classes at build time: the cloner creates clones by new, writes final fields directly, needs neither ReflectionFactory nor Unsafe for them. Classes without copy constructors are cloned as usual, so the tools are optional. The agent adds the same constructors when classes are loaded, without a change of the build.

Which classes

List them by masks in src/main/resources/META-INF/sugar-cubes-cloner/copy-constructors (and src/test/resources/... for test classes), one mask per line, # starts a comment:

com.acme.model.**
!com.acme.model.cache.**

? is any character but a dot, * any characters within a package, ** any characters including subpackages, ! excludes. Interfaces, enums and annotations are ignored. A class gets a copy constructor only if its superclass is Object, Record or a class with a copy constructor; other listed classes are reported.

Kotlin, Scala and Groovy classes get copy constructors from the Maven and Gradle plugins, see Kotlin, Scala, Groovy. Classes which the cloner never copies (Kotlin and Scala singletons, Kotlin file facades, coroutine jobs, Scala futures, Groovy meta classes) are skipped.

The tools change class files by the ClassFile API of the JVM on JDK 24+, if the cloner is on the classpath of the compilation, as it is in a project with copy constructors; otherwise by ASM, which reads class files up to Java 28. A class of a newer Java version is left as it is with a warning, the cloner copies it as usual; a newer JDK or a newer version of the tools reads it.

Three ways, use one or combine

The jar io.github.sugar-cubes:sugar-cubes-cloner-tools contains an annotation processor, a Maven plugin and a Gradle plugin. Enhancing is idempotent: combined with the processor, a plugin finds the classes already enhanced.

  • Annotation processor: no build plugin, works in IDE builds (javac only, not Eclipse). Reports problems at the sources. Options: -Asugarcubes.cloner.targets=masks, -Asugarcubes.cloner.targetsFile=paths, -Asugarcubes.cloner.enhance=false (only check). Without options it reads the masks from the class output, where Maven copies resources before compilation.
  • Maven plugin: goals enhance (process-classes) and enhance-test (process-test-classes), works with any compiler. Maven 3.0 and newer, tested with Maven 3.0.5, 3.10.2 and 4.0.0-rc-7.
  • Gradle plugin io.github.sugar-cubes.cloner: enhances the classes of every source set after compileJava, compileKotlin, compileScala and compileGroovy and passes the masks to the processor if it is on the processor path. Gradle 4.9 and newer, tested with Gradle 4.9, 6.8.3, 7.0 and 9.9.3.

The tools run on JDK 8 and newer and do not depend on the cloner: the plugins bring only the tools and ASM into the build. The code which adds the constructors is the one of the cloner, the tools contain a copy of it; on JDK 24+ they take the engine on the ClassFile API from the cloner of the project. The compiled classes refer to the cloner, which the project depends on anyway.

Maven

<plugin>
  <groupId>io.github.sugar-cubes</groupId>
  <artifactId>sugar-cubes-cloner-tools</artifactId>
  <version>2.2.2</version>
  <executions>
    <execution>
      <goals>
        <goal>enhance</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Processor:

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <annotationProcessorPaths>
      <path>
        <groupId>io.github.sugar-cubes</groupId>
        <artifactId>sugar-cubes-cloner-tools</artifactId>
        <version>2.2.2</version>
      </path>
    </annotationProcessorPaths>
  </configuration>
</plugin>

Gradle

The plugin is published to Maven Central with its marker, add mavenCentral() to the plugin repositories:

// settings.gradle.kts
pluginManagement {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
}
// build.gradle.kts
plugins {
    java
    id("io.github.sugar-cubes.cloner") version "2.2.2"
}

dependencies {
    implementation("io.github.sugar-cubes:sugar-cubes-cloner:2.2.2")
    // optional: enhancing at compilation, also in IntelliJ IDEA builds
    annotationProcessor("io.github.sugar-cubes:sugar-cubes-cloner-tools:2.2.2")
}

The copy constructor

C(CopyConstructors marker, C original, CopyContext context), synthetic, private in final classes and protected in the others (a serializable class without serialVersionUID gets the value computed for the original class, so the serial form does not change). The cloner uses it when the field policy copies all fields of the class by default; Cloners.builder().copyConstructors(false) disables it.