Files
kotlin-fork/compiler/testData/codegen/box/toArray/toArrayFromJava.kt
T
Juan Chen d163853c97 [FIR] add support for implementation by delgation
This commit handles "subclass: super-interface by delegate-expression".

During Psi2Fir, for each delegate, we add to the subclass a synthetic
field (which has type super-interface), and an assignment of the
delegate-expression to the synthetic field in the primary constructor,
so that the delegate-expression can be resolved and transformed along
the way.

During Fir2Ir, we look up delegatable members from the super-interface
and generate corresponding functions/properties for the subclass.

TODO: support for generic delegatable members and generic
super-interface.
2020-07-08 09:42:24 +03:00

51 lines
1.7 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM
// The old backend thinks `toArray(): Array<Int?>` is the same as `toArray(): Array<Any?>`
// IGNORE_BACKEND: JVM
// WITH_RUNTIME
// FILE: MyListWithCustomToArray.java
public abstract class MyListWithCustomToArray<E> extends java.util.AbstractList<E> {
public Object[] toArray() {
return new Object[]{null};
}
public <T> T[] toArray(T[] a) {
return a;
}
}
// FILE: a.kt
class MyList<T>(val list: List<T>): java.util.AbstractList<T>() {
override fun get(index: Int): T = list[index]
override val size: Int
get() = list.size
}
class MyListSubclass<T>(val list: List<T>): MyListWithCustomToArray<T>() {
override fun get(index: Int): T = list[index]
override val size: Int
get() = list.size
}
class MyCollection<T>(val list: List<T>) : Collection<T> by list {
fun toArray(): Array<Int?> =
arrayOfNulls<Int>(0)
}
fun box(): String {
val list1 = MyList(listOf(2, 3, 9)) as java.util.Collection<*>
list1.toArray().contentToString().let { if (it != "[2, 3, 9]") return "fail 1: $it" }
list1.toArray(arrayOfNulls<Int>(0)).contentToString().let { if (it != "[2, 3, 9]") return "fail 2: $it" }
val list2 = MyListSubclass(listOf(2, 3, 9)) as java.util.Collection<*>
list2.toArray().contentToString().let { if (it != "[null]") return "fail 3: $it" }
list2.toArray(arrayOfNulls<Int>(1)).contentToString().let { if (it != "[null]") return "fail 4: $it" }
val list3 = MyCollection(listOf(2, 3, 9)) as java.util.Collection<*>
list3.toArray().contentToString().let { if (it != "[2, 3, 9]") return "fail 5: $it" }
list3.toArray(arrayOfNulls<Int>(0)).contentToString().let { if (it != "[2, 3, 9]") return "fail 6: $it" }
return "OK"
}