Implement typeOf intrinsic on JVM

#KT-29915 Fixed
This commit is contained in:
Alexander Udalov
2019-02-21 19:47:45 +01:00
parent 5d297c40fd
commit d1e33534db
28 changed files with 950 additions and 4 deletions
@@ -0,0 +1,41 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// IGNORE_BACKEND: JS, JS_IR, NATIVE
// WITH_REFLECT
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
class C
fun check(expected: String, actual: KType) {
assertEquals(expected, actual.toString())
}
fun box(): String {
check("kotlin.Any", typeOf<Any>())
check("kotlin.String", typeOf<String>())
check("kotlin.String?", typeOf<String?>())
check("kotlin.Unit", typeOf<Unit>())
check("test.C", typeOf<C>())
check("test.C?", typeOf<C?>())
check("kotlin.collections.List<kotlin.String>", typeOf<List<String>>())
check("kotlin.collections.Map<in kotlin.Number, kotlin.Any?>?", typeOf<Map<in Number, *>?>())
check("kotlin.Enum<out kotlin.Enum<*>>", typeOf<Enum<*>>())
check("kotlin.Enum<kotlin.annotation.AnnotationRetention>", typeOf<Enum<AnnotationRetention>>())
check("kotlin.Array<kotlin.Any>", typeOf<Array<Any>>())
check("kotlin.Array<out kotlin.Any?>", typeOf<Array<*>>())
check("kotlin.Array<kotlin.IntArray>", typeOf<Array<IntArray>>())
check("kotlin.Array<in kotlin.Array<test.C>?>", typeOf<Array<in Array<C>?>>())
check("kotlin.Int", typeOf<Int>())
check("kotlin.Int?", typeOf<Int?>())
check("kotlin.Boolean", typeOf<Boolean>())
return "OK"
}
@@ -0,0 +1,32 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// IGNORE_BACKEND: JVM_IR, JS, JS_IR, NATIVE
// WITH_REFLECT
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
inline class Z(val value: String)
fun check(expected: String, actual: KType) {
assertEquals(expected, actual.toString())
}
fun box(): String {
check("test.Z", typeOf<Z>())
check("test.Z?", typeOf<Z?>())
check("kotlin.Array<test.Z>", typeOf<Array<Z>>())
check("kotlin.Array<test.Z?>", typeOf<Array<Z?>>())
check("kotlin.UInt", typeOf<UInt>())
check("kotlin.UInt?", typeOf<UInt?>())
check("kotlin.ULong?", typeOf<ULong?>())
check("kotlin.UShortArray", typeOf<UShortArray>())
check("kotlin.UShortArray?", typeOf<UShortArray?>())
check("kotlin.Array<kotlin.UByteArray>", typeOf<Array<UByteArray>>())
check("kotlin.Array<kotlin.UByteArray?>?", typeOf<Array<UByteArray?>?>())
return "OK"
}
@@ -0,0 +1,31 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// IGNORE_BACKEND: JS, JS_IR, NATIVE
// WITH_REFLECT
package test
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
interface I1
interface I2
interface I3
interface I4
interface I5
interface I6
interface I7
class C<T1, T2, T3, T4, T5, T6, T7>
fun box(): String {
assertEquals(
"test.C<test.I1, test.I2, test.I3?, in test.I4, out test.I5, test.I6, test.I7?>",
typeOf<C<I1, I2, I3?, in I4, out I5, I6, I7?>>().toString()
)
assertEquals(
"test.C<out test.I1, test.I2?, test.I3, test.I4, test.I5, test.I6?, in test.I7>?",
typeOf<C<out I1, I2?, I3, I4, I5, I6?, in I7>?>().toString()
)
return "OK"
}
@@ -0,0 +1,22 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// IGNORE_BACKEND: JS, JS_IR, NATIVE
// WITH_REFLECT
package test
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
interface C
inline fun <reified T> get() = typeOf<T>()
inline fun <reified U> get1() = get<U?>()
inline fun <reified V> get2() = get1<Map<in V?, Array<V>>>()
fun box(): String {
assertEquals("kotlin.collections.Map<in test.C?, kotlin.Array<test.C>>?", get2<C>().toString())
assertEquals("kotlin.collections.Map<in kotlin.collections.List<test.C>?, kotlin.Array<kotlin.collections.List<test.C>>>?", get2<List<C>>().toString())
return "OK"
}
@@ -0,0 +1,41 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// WITH_RUNTIME
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
class C
fun check(expected: String, actual: KType) {
assertEquals(expected + " (Kotlin reflection is not available)", actual.toString())
}
fun box(): String {
check("java.lang.Object", typeOf<Any>())
check("java.lang.String", typeOf<String>())
check("java.lang.String?", typeOf<String?>())
check("kotlin.Unit", typeOf<Unit>())
check("test.C", typeOf<C>())
check("test.C?", typeOf<C?>())
check("java.util.List<java.lang.String>", typeOf<List<String>>())
check("java.util.Map<in java.lang.Number, java.lang.Object?>?", typeOf<Map<in Number, *>?>())
check("java.lang.Enum<out java.lang.Enum<*>>", typeOf<Enum<*>>())
check("java.lang.Enum<kotlin.annotation.AnnotationRetention>", typeOf<Enum<AnnotationRetention>>())
check("kotlin.Array<java.lang.Object>", typeOf<Array<Any>>())
check("kotlin.Array<out java.lang.Object?>", typeOf<Array<*>>())
check("kotlin.Array<kotlin.IntArray>", typeOf<Array<IntArray>>())
check("kotlin.Array<in kotlin.Array<test.C>?>", typeOf<Array<in Array<C>?>>())
check("int", typeOf<Int>())
check("java.lang.Integer?", typeOf<Int?>())
check("boolean", typeOf<Boolean>())
return "OK"
}
@@ -0,0 +1,33 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.test.assertEquals
inline class Z(val value: String)
fun check(expected: String, actual: KType) {
assertEquals(expected + " (Kotlin reflection is not available)", actual.toString())
}
fun box(): String {
check("test.Z", typeOf<Z>())
check("test.Z?", typeOf<Z?>())
check("kotlin.Array<test.Z>", typeOf<Array<Z>>())
check("kotlin.Array<test.Z?>", typeOf<Array<Z?>>())
check("kotlin.UInt", typeOf<UInt>())
check("kotlin.UInt?", typeOf<UInt?>())
check("kotlin.ULong?", typeOf<ULong?>())
check("kotlin.UShortArray", typeOf<UShortArray>())
check("kotlin.UShortArray?", typeOf<UShortArray?>())
check("kotlin.Array<kotlin.UByteArray>", typeOf<Array<UByteArray>>())
check("kotlin.Array<kotlin.UByteArray?>?", typeOf<Array<UByteArray?>?>())
return "OK"
}
@@ -0,0 +1,54 @@
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
// TARGET_BACKEND: JVM
// WITH_RUNTIME
package test
import kotlin.reflect.KType
import kotlin.reflect.typeOf
class C
fun assertEqual(a: KType, b: KType) {
if (a != b || b != a) throw AssertionError("Fail equals: $a != $b")
if (a.hashCode() != b.hashCode()) throw AssertionError("Fail hashCode: $a != $b")
}
fun assertNotEqual(a: KType, b: KType) {
if (a == b || b == a) throw AssertionError("Fail equals: $a == $b")
}
inline fun <reified A, reified B> equal() {
assertEqual(typeOf<A>(), typeOf<B>())
}
inline fun <reified A, reified B> notEqual() {
assertNotEqual(typeOf<A>(), typeOf<B>())
}
fun box(): String {
equal<Any, Any>()
equal<Any?, Any?>()
equal<String, String>()
equal<C, C>()
equal<C?, C?>()
equal<List<String>, List<String>>()
equal<Enum<AnnotationRetention>, Enum<AnnotationRetention>>()
equal<Array<Any>, Array<Any>>()
equal<Array<IntArray>, Array<IntArray>>()
equal<Array<*>, Array<out Any?>>() // This is subject to change if we retain star projections in typeOf
equal<Int, Int>()
equal<Int?, Int?>()
notEqual<Any, Any?>()
notEqual<Any, String>()
notEqual<List<Any>, List<Any?>>()
notEqual<Map<in Number, BooleanArray>, Map<out Number, BooleanArray>>()
notEqual<Array<IntArray>, Array<Array<Int>>>()
return "OK"
}