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
+3 -1
View File
@@ -16,11 +16,13 @@
package kotlin
public class Array<reified T>(public val size: Int, init: (Int) -> T) {
public class Array<reified T>(public val size: Int, init: (Int) -> T) : Cloneable {
public fun get(index: Int): T
public fun set(index: Int, value: T): Unit
public fun iterator(): Iterator<T>
public val indices: IntRange
public override fun clone(): Array<T>
}
+24 -8
View File
@@ -18,75 +18,91 @@
package kotlin
public class ByteArray(public val size: Int) {
public class ByteArray(public val size: Int) : Cloneable {
public fun get(index: Int): Byte
public fun set(index: Int, value: Byte): Unit
public fun iterator(): ByteIterator
public val indices: IntRange
public override fun clone(): ByteArray
}
public class CharArray(public val size: Int) {
public class CharArray(public val size: Int) : Cloneable {
public fun get(index: Int): Char
public fun set(index: Int, value: Char): Unit
public fun iterator(): CharIterator
public val indices: IntRange
public override fun clone(): CharArray
}
public class ShortArray(public val size: Int) {
public class ShortArray(public val size: Int) : Cloneable {
public fun get(index: Int): Short
public fun set(index: Int, value: Short): Unit
public fun iterator(): ShortIterator
public val indices: IntRange
public override fun clone(): ShortArray
}
public class IntArray(public val size: Int) {
public class IntArray(public val size: Int) : Cloneable {
public fun get(index: Int): Int
public fun set(index: Int, value: Int): Unit
public fun iterator(): IntIterator
public val indices: IntRange
public override fun clone(): IntArray
}
public class LongArray(public val size: Int) {
public class LongArray(public val size: Int) : Cloneable {
public fun get(index: Int): Long
public fun set(index: Int, value: Long): Unit
public fun iterator(): LongIterator
public val indices: IntRange
public override fun clone(): LongArray
}
public class FloatArray(public val size: Int) {
public class FloatArray(public val size: Int) : Cloneable {
public fun get(index: Int): Float
public fun set(index: Int, value: Float): Unit
public fun iterator(): FloatIterator
public val indices: IntRange
public override fun clone(): FloatArray
}
public class DoubleArray(public val size: Int) {
public class DoubleArray(public val size: Int) : Cloneable {
public fun get(index: Int): Double
public fun set(index: Int, value: Double): Unit
public fun iterator(): DoubleIterator
public val indices: IntRange
public override fun clone(): DoubleArray
}
public class BooleanArray(public val size: Int) {
public class BooleanArray(public val size: Int) : Cloneable {
public fun get(index: Int): Boolean
public fun set(index: Int, value: Boolean): Unit
public fun iterator(): BooleanIterator
public val indices: IntRange
public override fun clone(): BooleanArray
}
+21
View File
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin
public trait Cloneable {
protected fun clone(): Any { /* intrinsic */ }
}
@@ -38,6 +38,7 @@ public abstract class JavaToKotlinClassMapBuilder {
register(String.class, kotlinBuiltIns.getString());
register(CharSequence.class, kotlinBuiltIns.getCharSequence());
register(Throwable.class, kotlinBuiltIns.getThrowable());
register(Cloneable.class, kotlinBuiltIns.getCloneable());
register(Number.class, kotlinBuiltIns.getNumber());
register(Comparable.class, kotlinBuiltIns.getComparable());
register(Enum.class, kotlinBuiltIns.getEnum());
@@ -138,6 +138,7 @@ public class KotlinBuiltIns {
private static class FqNames {
public final FqNameUnsafe any = fqName("Any");
public final FqNameUnsafe nothing = fqName("Nothing");
public final FqNameUnsafe cloneable = fqName("Cloneable");
public final FqNameUnsafe suppress = fqName("suppress");
public final ImmutableSet<FqNameUnsafe> functionClasses = computeIndexedFqNames("Function", FUNCTION_TRAIT_COUNT);
@@ -300,6 +301,11 @@ public class KotlinBuiltIns {
return getBuiltInClassByName("Throwable");
}
@NotNull
public ClassDescriptor getCloneable() {
return getBuiltInClassByName("Cloneable");
}
@NotNull
public ClassDescriptor getDataClassAnnotation() {
return getBuiltInClassByName("data");
@@ -841,6 +847,10 @@ public class KotlinBuiltIns {
return !(type instanceof PackageType) && getStringType().equals(type);
}
public boolean isCloneable(@NotNull ClassDescriptor descriptor) {
return fqNames.cloneable.equals(DescriptorUtils.getFqName(descriptor));
}
public boolean isData(@NotNull ClassDescriptor classDescriptor) {
return containsAnnotation(classDescriptor, getDataClassAnnotation());
}