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:
Alexander Udalov
2014-07-21 22:10:53 +04:00
parent f3309d1fa7
commit fb958897a9
33 changed files with 392 additions and 38 deletions
+22 -9
View File
@@ -13,12 +13,13 @@ public open class Any {
/*primary*/ public constructor Any()
}
public final class Array</*0*/ reified T> {
public final class Array</*0*/ reified T> : kotlin.Cloneable {
/*primary*/ public constructor Array</*0*/ reified T>(/*0*/ size: kotlin.Int, /*1*/ init: kotlin.Function1<kotlin.Int, T>)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.Array<T>
public final fun get(/*0*/ index: kotlin.Int): T
public final fun iterator(): kotlin.Iterator<T>
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: T): kotlin.Unit
@@ -33,12 +34,13 @@ public final class Boolean : kotlin.Comparable<kotlin.Boolean> {
public final fun xor(/*0*/ other: kotlin.Boolean): kotlin.Boolean
}
public final class BooleanArray {
public final class BooleanArray : kotlin.Cloneable {
/*primary*/ public constructor BooleanArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.BooleanArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Boolean
public final fun iterator(): kotlin.BooleanIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Boolean): kotlin.Unit
@@ -115,12 +117,13 @@ public final class Byte : kotlin.Number, kotlin.Comparable<kotlin.Byte> {
public open override /*1*/ fun toShort(): kotlin.Short
}
public final class ByteArray {
public final class ByteArray : kotlin.Cloneable {
/*primary*/ public constructor ByteArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.ByteArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Byte
public final fun iterator(): kotlin.ByteIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Byte): kotlin.Unit
@@ -235,12 +238,13 @@ public final class Char : kotlin.Comparable<kotlin.Char> {
public final fun toShort(): kotlin.Short
}
public final class CharArray {
public final class CharArray : kotlin.Cloneable {
/*primary*/ public constructor CharArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.CharArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Char
public final fun iterator(): kotlin.CharIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Char): kotlin.Unit
@@ -307,6 +311,10 @@ public trait CharSequence {
public abstract fun get(/*0*/ index: kotlin.Int): kotlin.Char
}
public trait Cloneable {
protected open fun clone(): kotlin.Any
}
public trait Collection</*0*/ out E> : kotlin.Iterable<E> {
public abstract fun contains(/*0*/ o: kotlin.Any?): kotlin.Boolean
public abstract fun containsAll(/*0*/ c: kotlin.Collection<kotlin.Any?>): kotlin.Boolean
@@ -382,12 +390,13 @@ public final class Double : kotlin.Number, kotlin.Comparable<kotlin.Double> {
public open override /*1*/ fun toShort(): kotlin.Short
}
public final class DoubleArray {
public final class DoubleArray : kotlin.Cloneable {
/*primary*/ public constructor DoubleArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.DoubleArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Double
public final fun iterator(): kotlin.DoubleIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Double): kotlin.Unit
@@ -607,12 +616,13 @@ public final class Float : kotlin.Number, kotlin.Comparable<kotlin.Float> {
public open override /*1*/ fun toShort(): kotlin.Short
}
public final class FloatArray {
public final class FloatArray : kotlin.Cloneable {
/*primary*/ public constructor FloatArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.FloatArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Float
public final fun iterator(): kotlin.FloatIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Float): kotlin.Unit
@@ -905,12 +915,13 @@ public final class Int : kotlin.Number, kotlin.Comparable<kotlin.Int> {
public final fun xor(/*0*/ other: kotlin.Int): kotlin.Int
}
public final class IntArray {
public final class IntArray : kotlin.Cloneable {
/*primary*/ public constructor IntArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.IntArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Int
public final fun iterator(): kotlin.IntIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Int): kotlin.Unit
@@ -1074,12 +1085,13 @@ public final class Long : kotlin.Number, kotlin.Comparable<kotlin.Long> {
public final fun xor(/*0*/ other: kotlin.Long): kotlin.Long
}
public final class LongArray {
public final class LongArray : kotlin.Cloneable {
/*primary*/ public constructor LongArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.LongArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Long
public final fun iterator(): kotlin.LongIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Long): kotlin.Unit
@@ -1378,12 +1390,13 @@ public final class Short : kotlin.Number, kotlin.Comparable<kotlin.Short> {
public open override /*1*/ fun toShort(): kotlin.Short
}
public final class ShortArray {
public final class ShortArray : kotlin.Cloneable {
/*primary*/ public constructor ShortArray(/*0*/ size: kotlin.Int)
public final val indices: kotlin.IntRange
public final fun <get-indices>(): kotlin.IntRange
public final val size: kotlin.Int
public final fun <get-size>(): kotlin.Int
public open override /*1*/ fun clone(): kotlin.ShortArray
public final fun get(/*0*/ index: kotlin.Int): kotlin.Short
public final fun iterator(): kotlin.ShortIterator
public final fun set(/*0*/ index: kotlin.Int, /*1*/ value: kotlin.Short): kotlin.Unit
@@ -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"
}
@@ -1,7 +1,7 @@
package test
public /*synthesized*/ fun </*0*/ A : kotlin.Comparable<A>?, /*1*/ B : kotlin.List<A>?> GenericInterfaceParametersWithBounds(/*0*/ function: (kotlin.Array<out A>?, B?) -> kotlin.Unit): test.GenericInterfaceParametersWithBounds<A, B> where A : java.lang.Cloneable?
public /*synthesized*/ fun </*0*/ A : kotlin.Comparable<A>?, /*1*/ B : kotlin.List<A>?> GenericInterfaceParametersWithBounds(/*0*/ function: (kotlin.Array<out A>?, B?) -> kotlin.Unit): test.GenericInterfaceParametersWithBounds<A, B> where A : kotlin.Cloneable?
public trait GenericInterfaceParametersWithBounds</*0*/ A : kotlin.Comparable<A>?, /*1*/ B : kotlin.List<A>?> where A : java.lang.Cloneable? {
public trait GenericInterfaceParametersWithBounds</*0*/ A : kotlin.Comparable<A>?, /*1*/ B : kotlin.List<A>?> where A : kotlin.Cloneable? {
public abstract fun method(/*0*/ p0: kotlin.Array<out A>?, /*1*/ p1: B?): kotlin.Unit
}
@@ -2,5 +2,5 @@ package test
public open class MethodTypePOneUpperBound {
public constructor MethodTypePOneUpperBound()
public open fun </*0*/ T : java.lang.Cloneable?> bar(): kotlin.Unit
public open fun </*0*/ T : kotlin.Cloneable?> bar(): kotlin.Unit
}
@@ -2,5 +2,5 @@ package test
public open class MethodTypePTwoUpperBounds {
public constructor MethodTypePTwoUpperBounds()
public open fun </*0*/ T : java.lang.Cloneable?> foo(): kotlin.Unit where T : java.lang.Runnable?
public open fun </*0*/ T : kotlin.Cloneable?> foo(): kotlin.Unit where T : java.lang.Runnable?
}
@@ -2,5 +2,5 @@ package test
public open class MethodWithTypeParameters {
public constructor MethodWithTypeParameters()
public open fun </*0*/ A, /*1*/ B : java.lang.Runnable> foo(/*0*/ a: A, /*1*/ b: kotlin.List<B>, /*2*/ c: kotlin.MutableList<in kotlin.String?>): kotlin.Unit where B : kotlin.List<java.lang.Cloneable>
public open fun </*0*/ A, /*1*/ B : java.lang.Runnable> foo(/*0*/ a: A, /*1*/ b: kotlin.List<B>, /*2*/ c: kotlin.MutableList<in kotlin.String?>): kotlin.Unit where B : kotlin.List<kotlin.Cloneable>
}
@@ -2,5 +2,5 @@ package test
public open class MissingUpperBound {
public constructor MissingUpperBound()
public open fun </*0*/ A : java.lang.Runnable?> foo(): kotlin.String? where A : java.lang.Cloneable?
public open fun </*0*/ A : java.lang.Runnable?> foo(): kotlin.String? where A : kotlin.Cloneable?
}
@@ -2,5 +2,5 @@ package test
public open class WrongTypeParameterBoundStructure1 {
public constructor WrongTypeParameterBoundStructure1()
public open fun </*0*/ A, /*1*/ B : java.lang.Runnable?> foo(/*0*/ p0: A?, /*1*/ p1: kotlin.List<B>?): kotlin.Unit where B : kotlin.List<java.lang.Cloneable>?
public open fun </*0*/ A, /*1*/ B : java.lang.Runnable?> foo(/*0*/ p0: A?, /*1*/ p1: kotlin.List<B>?): kotlin.Unit where B : kotlin.List<kotlin.Cloneable>?
}
@@ -6,7 +6,7 @@ import jet.runtime.typeinfo.KotlinSignature;
import org.jetbrains.jet.jvm.compiler.annotation.ExpectLoadError;
public class WrongTypeParameterBoundStructure2 {
@ExpectLoadError("'kotlin.List<java.lang.Cloneable>?' type in method signature has 1 type arguments, while 'List' in alternative signature has 0 of them")
@ExpectLoadError("'kotlin.List<kotlin.Cloneable>?' type in method signature has 1 type arguments, while 'List' in alternative signature has 0 of them")
@KotlinSignature("fun <A, B : Runnable> foo(a : A, b : List<B>) where B : List")
public <A, B extends Runnable & List<Cloneable>> void foo(A a, List<? extends B> b) {
}
@@ -2,5 +2,5 @@ package test
public open class WrongTypeParameterBoundStructure2 {
public constructor WrongTypeParameterBoundStructure2()
public open fun </*0*/ A, /*1*/ B : java.lang.Runnable?> foo(/*0*/ p0: A?, /*1*/ p1: kotlin.List<B>?): kotlin.Unit where B : kotlin.List<java.lang.Cloneable>?
public open fun </*0*/ A, /*1*/ B : java.lang.Runnable?> foo(/*0*/ p0: A?, /*1*/ p1: kotlin.List<B>?): kotlin.Unit where B : kotlin.List<kotlin.Cloneable>?
}
@@ -3,10 +3,10 @@ package test
public trait TwoBounds {
public trait Sub : test.TwoBounds.Super {
public abstract override /*1*/ fun </*0*/ B : kotlin.CharSequence> foo(/*0*/ a: B): kotlin.Unit where B : java.lang.Cloneable
public abstract override /*1*/ fun </*0*/ B : kotlin.CharSequence> foo(/*0*/ a: B): kotlin.Unit where B : kotlin.Cloneable
}
public trait Super {
public abstract fun </*0*/ A : kotlin.CharSequence> foo(/*0*/ a: A): kotlin.Unit where A : java.lang.Cloneable
public abstract fun </*0*/ A : kotlin.CharSequence> foo(/*0*/ a: A): kotlin.Unit where A : kotlin.Cloneable
}
}
@@ -3,10 +3,10 @@ package test
public trait TwoTypeParameters {
public trait Sub : test.TwoTypeParameters.Super {
public abstract override /*1*/ fun </*0*/ B : kotlin.CharSequence, /*1*/ A : java.lang.Cloneable> foo(/*0*/ a: B, /*1*/ b: A): kotlin.Unit
public abstract override /*1*/ fun </*0*/ B : kotlin.CharSequence, /*1*/ A : kotlin.Cloneable> foo(/*0*/ a: B, /*1*/ b: A): kotlin.Unit
}
public trait Super {
public abstract fun </*0*/ A : kotlin.CharSequence, /*1*/ B : java.lang.Cloneable> foo(/*0*/ a: A, /*1*/ b: B): kotlin.Unit
public abstract fun </*0*/ A : kotlin.CharSequence, /*1*/ B : kotlin.Cloneable> foo(/*0*/ a: A, /*1*/ b: B): kotlin.Unit
}
}