Kotlin, Scala, Groovy¶
The cloner is a Java library and works with the classes of other JVM languages as they are:
// Kotlin
val clone = Cloners.clone(order)
// a shortcut, if you like
fun <T> T.deepCopy(): T = Cloners.clone(this)
The cloner needs no dependency on a language: it recognizes their singletons and running computations by names and by the fields of their classes. Tested on JDK 8 to 25 and OpenJ9 with:
| Language | Versions |
|---|---|
| Kotlin | compilers 1.9 to 2.4, lambdas compiled to classes (Kotlin 1.x, -Xlambdas=class) and by invokedynamic (Kotlin 2.x) |
| Scala | 2.13, 3.3 and 3.10 (Scala 3.8+ needs JDK 17) |
| Groovy | 3.0, 4.0, 5.1 and 6.0 (Groovy 5 needs JDK 11, Groovy 6 JDK 17) |
Kotlin¶
Kotlin classes¶
- Data classes and ordinary classes are copied by their fields, including
valproperties (final fields),private setandlateinit var. No constructors run,initblocks neither. listOf,mapOf,setOfreturn collections of the JDK, which the cloner rebuilds;Pair, ranges, value classes, sealed hierarchies and enums are handled as any other classes.- Lambdas are handled as Java lambdas, see JDK types: a capturing lambda gets the copies of the
captured values, so
val count = { items.size }of a clone counts the items of the clone. - A
by lazydelegate is copied with its state: an initialized value stays, an uninitialized one is computed by the clone with the copied initializer.
Kotlin objects¶
object, data object and companion object declarations are singletons, and Kotlin code compares them by identity,
so the clone references the same instances, as it does with enum constants. This covers the objects of the standard
library too, e.g. emptyList() or the marker of an uninitialized lazy.
The cloner recognizes a Kotlin class by its kotlin.Metadata annotation and an object by the static field which holds
its instance. An ordinary class with a default instance in its companion, e.g. Config.DEFAULT, is not a singleton,
and its instances are copied. To copy an object anyway, set a custom copier for its
class.
Coroutines¶
A job of kotlinx.coroutines (a scope, a launched coroutine, a deferred value) is a running computation: its copy
would be a coroutine which never resumes and never completes. A graph which reaches a job fails with
UncopyableObjectException until you choose what the clone gets:
val cloner = Cloners.builder()
.typeAction(Predicates.subclass(CoroutineScope::class.java), CopyAction.ORIGINAL)
.build()
Dispatchers are Kotlin objects and are shared.
Scala¶
Scala classes¶
- Case classes and ordinary classes are copied by their fields, including
val(final fields). No constructors run. - Immutable collections (
List,Vector,Map,Set,TreeMap, ...) and mutable ones are copied with their elements; the copies work as the originals do. - Functions are lambdas of the JVM and are handled as Java lambdas, see JDK types: a function which captures values gets their copies.
- A
lazy valis copied with its state: an evaluated value stays, an unevaluated one is computed by the clone. - Values of an
Enumerationare copied and equal the originals, somatchworks with them.
Scala objects¶
object and case object declarations, among them companion objects, are singletons, and Scala code compares them by
identity: List ends with Nil, a match checks None. So the clone references the same instances, as it does with
enum constants. The cases of Scala 3 enums without parameters (case Red, Green) are shared too; cases with parameters
are copied as case classes.
The cloner recognizes an object by the field MODULE$ which holds its instance, and a case of a Scala 3 enum by the
interface scala.runtime.EnumValue. To copy an object anyway, set a custom copier
for its class.
Futures¶
A Future (and a Promise, which is a future too) belongs to a running computation: a copy of a pending future would
never complete. A graph which reaches a future fails with UncopyableObjectException until you choose what the clone
gets:
val cloner = Cloners.builder()
.typeAction(Predicates.subclass(classOf[Future[_]]), CopyAction.ORIGINAL)
.build()
Execution contexts hold threads and fail the same way, see JDK types.
Groovy¶
Groovy classes¶
- Dynamic and
@CompileStaticclasses,@Immutableclasses and classes with traits are copied by their fields. No constructors run. GString, ranges, lists and maps (collections of the JDK) andExpandoare copied as any other objects.- A closure is copied with its owner, delegate and captured variables: a closure held by a copied object works with the copy of the object.
Meta classes¶
Every Groovy object has the field metaClass, set by the first dynamic call. A meta class describes the class and holds
caches of the Groovy runtime, so the clone references the same meta class as the original. A meta class changed for
one object (order.metaClass.total = { -> 0 }) is shared by its clone too. The cloner recognizes a meta class by the
interface groovy.lang.MetaClass.
Build tools¶
The Maven and Gradle plugins add copy constructors to the classes of these languages too, the agent when they are loaded. The annotation processor works with javac only and does not see them.
| Language | Maven | Gradle: the output of |
|---|---|---|
| Kotlin | the Kotlin Maven plugin compiles to target/classes |
compileKotlin, compileTestKotlin, ... |
| Scala | the Scala Maven plugin compiles to target/classes |
compileScala, compileTestScala, ... |
| Groovy | GMavenPlus compiles to target/classes |
compileGroovy, compileTestGroovy, ... |
The goal enhance processes target/classes; the Gradle plugin enhances these outputs as it does with compileJava.
The Kotlin Gradle plugins 1.9.25 and 2.4.21 are tested in Gradle
6.8.3 and 9.9.3.
- Classes which the cloner never copies get no copy constructors: Kotlin and Scala singletons, Kotlin file facades
(
ModelKt), coroutine jobs, Scala futures, Groovy meta classes. - Kotlin is compiled before Java, so a Kotlin class whose superclass is a Java class of the same source set gets no copy constructor and is cloned by generated copiers. Java is compiled before Scala and Groovy, so their classes may extend Java classes of the same source set.
- The Groovy compiler reads the copy constructor of a Java superclass, whose parameters are classes of the cloner: the
cloner must be on the compile classpath (
implementation, notruntimeOnly).