Introduce kotlin.Cloneable
- Cloneable is a trait with a single protected member 'clone', which is mapped to java.lang.Cloneable on JVM - 'clone' is non-abstract to be able to call 'super.clone()' in the implementations. Also if you need your class to be Cloneable, most of the time inheriting from Cloneable and calling 'super.clone()' will work - hack 'super.clone()' in JVM intrinsics and TImpl delegation generation - make arrays Cloneable, handle 'clone()' calls in the intrinsic #KT-4890 Fixed
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
public open class Test(): java.util.RandomAccess, Cloneable, java.io.Serializable
|
||||
{
|
||||
public fun clone(): Test = Test() // Override 'clone()' with more precise type 'Test'
|
||||
public override fun clone(): Test = Test() // Override 'clone()' with more precise type 'Test'
|
||||
|
||||
public override fun toString() = "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
data class A(var x: Int) : Cloneable {
|
||||
public override fun clone(): A = A(x)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A(42)
|
||||
val b = a.clone()
|
||||
if (b != a) return "Fail equals"
|
||||
if (b identityEquals a) return "Fail identity"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
data class A(var x: Int) : Cloneable {
|
||||
public override fun clone(): A = super.clone() as A
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A(42)
|
||||
val b = a.clone()
|
||||
if (a != b) return "Fail equals"
|
||||
if (a identityEquals b) return "Fail identity"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
data class A(var x: Int) : Cloneable {
|
||||
public override fun clone(): A {
|
||||
val result = super.clone() as A
|
||||
result.x = 239
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A(42)
|
||||
val b = a.clone()
|
||||
if (a == b) return "Fail: $a == $b"
|
||||
if (a identityEquals b) return "Fail: $a identityEquals $b"
|
||||
if (b.x != 239) return "Fail: b.x = ${b.x}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.util.HashSet
|
||||
|
||||
fun box(): String {
|
||||
val a = HashSet<String>()
|
||||
a.add("live")
|
||||
a.add("long")
|
||||
a.add("prosper")
|
||||
val b = a.clone()
|
||||
if (a != b) return "Fail equals"
|
||||
if (a identityEquals b) return "Fail identity"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
open class A : Cloneable {
|
||||
public override fun clone(): A = super.clone() as A
|
||||
}
|
||||
|
||||
open class B(var s: String) : A() {
|
||||
override fun clone(): B = super.clone() as B
|
||||
}
|
||||
|
||||
open class C(s: String, var l: ArrayList<Any>): B(s) {
|
||||
override fun clone(): C {
|
||||
val result = super.clone() as C
|
||||
result.l = l.clone() as ArrayList<Any>
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val l = ArrayList<Any>()
|
||||
l.add(true)
|
||||
|
||||
val c = C("OK", l)
|
||||
val d = c.clone()
|
||||
|
||||
if (c.s != d.s) return "Fail s: ${d.s}"
|
||||
if (c.l != d.l) return "Fail l: ${d.l}"
|
||||
if (c.l identityEquals d.l) return "Fail list identity"
|
||||
if (c identityEquals d) return "Fail identity"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
data class A(val s: String) : Cloneable {
|
||||
fun externalClone(): A = clone() as A
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A("OK")
|
||||
val b = a.externalClone()
|
||||
if (a != b) return "Fail equals"
|
||||
if (a identityEquals b) return "Fail identity"
|
||||
return b.s
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import java.util.Arrays.equals
|
||||
|
||||
fun box(): String {
|
||||
val s = array("live", "long")
|
||||
val t = s.clone()
|
||||
t : Array<String>
|
||||
if (!equals(s, t)) return "Fail string"
|
||||
if (s identityEquals t) return "Fail string identity"
|
||||
|
||||
val ss = array(s, s)
|
||||
val tt = ss.clone()
|
||||
tt : Array<Array<String>>
|
||||
if (!equals(ss, tt)) return "Fail string[]"
|
||||
if (ss identityEquals tt) return "Fail string[] identity"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import java.util.Arrays.equals
|
||||
|
||||
fun box(): String {
|
||||
val i = intArray(1, 2)
|
||||
if (!equals(i, i.clone())) return "Fail int"
|
||||
if (i.clone() identityEquals i) return "Fail int identity"
|
||||
|
||||
val j = longArray(1L, 2L)
|
||||
if (!equals(j, j.clone())) return "Fail long"
|
||||
if (j.clone() identityEquals j) return "Fail long identity"
|
||||
|
||||
val s = shortArray(1.toShort(), 2.toShort())
|
||||
if (!equals(s, s.clone())) return "Fail short"
|
||||
if (s.clone() identityEquals s) return "Fail short identity"
|
||||
|
||||
val b = byteArray(1.toByte(), 2.toByte())
|
||||
if (!equals(b, b.clone())) return "Fail byte"
|
||||
if (b.clone() identityEquals b) return "Fail byte identity"
|
||||
|
||||
val c = charArray('a', 'b')
|
||||
if (!equals(c, c.clone())) return "Fail char"
|
||||
if (c.clone() identityEquals c) return "Fail char identity"
|
||||
|
||||
val d = doubleArray(1.0, -1.0)
|
||||
if (!equals(d, d.clone())) return "Fail double"
|
||||
if (d.clone() identityEquals d) return "Fail double identity"
|
||||
|
||||
val f = floatArray(1f, -1f)
|
||||
if (!equals(f, f.clone())) return "Fail float"
|
||||
if (f.clone() identityEquals f) return "Fail float identity"
|
||||
|
||||
val z = booleanArray(true, false)
|
||||
if (!equals(z, z.clone())) return "Fail boolean"
|
||||
if (z.clone() identityEquals z) return "Fail boolean identity"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user