is there a way to make a current java class a data...
# getting-started
j
is there a way to make a current java class a data class so I have access to the data class functions? I really only need copy(), so I could probably just make an extension function for it.
d
jordan_k_miles: Another option is to use inheritance:
Copy code
public class Pojo {
    final private String fieldA;
    final private String fieldB;
    public Pojo(String fieldA, String fieldB) {
        this.fieldA = fieldA;
        this.fieldB = fieldB;
    }
}

data class PojoData(val paramA: String, val paramB: String) : Pojo(paramA, paramB)

fun main(args: Array<String>) {
    val pojo1 = PojoData("A", "B")
    val pojo2 = pojo1.copy()
    println(pojo1 == pojo2) // true
    println(pojo1 === pojo2) // false
}
j
yeah, that was my first thought. problem is, I'm using Java's clipboard APIs and a lot of their factory methods return Clipboard class. So I would end up having to overwrite and extend several methods.
didn't know if there was a quick and easy way to add the data functionality.
d
Class delegation can't resolve this for you?
This way you don't need to implement anything
Copy code
class AnotherSet<T>(val hashSet: MutableCollection<T> = HashSet<T>()): MutableCollection<T> by hashSet
j
let me give that a shot.