Use manual type mapping in reflection for members from built-ins

There are cases when members deserialized from JVM classes have no JVM
signature in the proto. For example, if a member is inherited from a
built-in class (such as Map.getOrDefault in some Map implementations),
or if a member is synthesized in the compiler front-end and back-end
separately (such as enum values/valueOf). In these cases, we'll use the
naive type mapping to try to recover the signature.

 #KT-16616 Fixed
 #KT-17542 Fixed
This commit is contained in:
Alexander Udalov
2018-01-05 15:23:49 +01:00
parent a7c80f2df1
commit 4266e50be8
15 changed files with 347 additions and 161 deletions
@@ -1,16 +1,11 @@
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND: JS_IR
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS, NATIVE
// IGNORE_BACKEND: JVM_IR, JS_IR, JS, NATIVE
// WITH_REFLECT
// FULL_JDK
// See KT-11258 Incorrect resolution sequence for Java field
// TODO: enable this test on JVM, see KT-16616
// IGNORE_BACKEND: JVM
// See KT-11258, KT-16616
import java.util.*
import kotlin.test.assertEquals
fun box(): String {
listOf(
@@ -26,5 +21,8 @@ fun box(): String {
).map {
it.members.map(Any::toString)
}
assertEquals(1, Collection<Any>::size.getter(listOf(1)))
return "OK"
}
@@ -0,0 +1,13 @@
// IGNORE_BACKEND: JS_IR, JS, NATIVE
// WITH_REFLECT
import kotlin.test.assertEquals
enum class E { X, Y, Z }
fun box(): String {
assertEquals(11, E::class.members.size)
assertEquals("Y", E::name.call(E.Y))
assertEquals(2, E::ordinal.call(E.Z))
return "OK"
}
@@ -0,0 +1,10 @@
// IGNORE_BACKEND: JS_IR, JS, NATIVE
// WITH_REFLECT
import kotlin.test.assertEquals
fun box(): String {
String::class.members
assertEquals(2, String::length.call("OK"))
return "OK"
}
@@ -0,0 +1,15 @@
// IGNORE_BACKEND: JS_IR, JS, NATIVE
// WITH_REFLECT
import kotlin.test.assertEquals
enum class E { X, Y, Z }
fun box(): String {
assertEquals("fun values(): kotlin.Array<E>", E::values.toString())
assertEquals(listOf(E.X, E.Y, E.Z), E::values.call().toList())
assertEquals("fun valueOf(kotlin.String): E", E::valueOf.toString())
assertEquals(E.Y, E::valueOf.call("Y"))
return "OK"
}
@@ -0,0 +1,25 @@
// IGNORE_BACKEND: JVM_IR, JS_IR, JS, NATIVE
// FULL_JDK
// WITH_REFLECT
import kotlin.test.assertEquals
import java.util.*
fun assertToString(s: String, x: Any) {
assertEquals(s, x.toString())
}
fun box(): String {
assertToString("fun kotlin.collections.Map<K, V>.getOrDefault(K, V): V", Map<*, *>::getOrDefault)
assertToString("fun java.util.HashMap<K, V>.getOrDefault(K, V): V", HashMap<*, *>::getOrDefault)
// TODO: uncomment once KT-11754 is fixed
// assertToString("", MutableList<*>::removeAt)
assertToString("fun kotlin.collections.Collection<E>.contains(E): kotlin.Boolean", Collection<*>::contains)
assertToString("fun kotlin.collections.Set<E>.contains(E): kotlin.Boolean", Set<*>::contains)
assertToString("fun kotlin.collections.Collection<E>.isEmpty(): kotlin.Boolean", Collection<*>::isEmpty)
return "OK"
}