Support reflection for local delegated properties
#KT-15222 Fixed
This commit is contained in:
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.test.*
|
||||
|
||||
object Delegate {
|
||||
lateinit var property: KProperty<*>
|
||||
|
||||
operator fun getValue(instance: Any?, kProperty: KProperty<*>): List<String?> {
|
||||
property = kProperty
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
operator fun setValue(instance: Any?, kProperty: KProperty<*>, value: List<String?>) {
|
||||
throw AssertionError()
|
||||
}
|
||||
}
|
||||
|
||||
fun check(expectedName: String, p: KProperty0<*>): String? {
|
||||
assertEquals(expectedName, p.name)
|
||||
assertEquals(emptyList<KParameter>(), p.parameters)
|
||||
assertEquals(emptyList<KTypeParameter>(), p.typeParameters)
|
||||
assertEquals(null, p.visibility) // "local" visibility is not representable with reflection API
|
||||
assertEquals("kotlin.collections.List<kotlin.String?>", p.returnType.toString())
|
||||
assertTrue(p.isFinal)
|
||||
assertFalse(p.isOpen)
|
||||
assertFalse(p.isAbstract)
|
||||
assertFalse(p.isLateinit)
|
||||
assertFalse(p.isConst)
|
||||
|
||||
// TODO: support getDelegate for local delegated properties
|
||||
assertEquals(null, (p as KProperty0<*>).getDelegate())
|
||||
|
||||
assertEquals(emptyList<KParameter>(), p.getter.parameters)
|
||||
assertEquals("kotlin.collections.List<kotlin.String?>", p.getter.returnType.toString())
|
||||
|
||||
// TODO: support annotations
|
||||
assertEquals(emptyList<Annotation>(), p.annotations)
|
||||
|
||||
try {
|
||||
p.call()
|
||||
return "Fail: reflective call of a local delegated property should fail because it's not supported"
|
||||
} catch (e: UnsupportedOperationException) { /* ok */ }
|
||||
|
||||
if (p is KMutableProperty0<*>) {
|
||||
assertEquals(listOf("kotlin.collections.List<kotlin.String?>"), p.setter.parameters.map { it.type.toString() })
|
||||
assertEquals("kotlin.Unit", p.setter.returnType.toString())
|
||||
|
||||
try {
|
||||
p.setter.call()
|
||||
return "Fail: reflective call of a local delegated property setter should fail because it's not supported"
|
||||
} catch (e: UnsupportedOperationException) { /* ok */ }
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
annotation class Anno
|
||||
|
||||
fun box(): String {
|
||||
@Anno
|
||||
val localVal by Delegate
|
||||
localVal
|
||||
|
||||
check("localVal", Delegate.property as KProperty0<*>)?.let { error -> return error }
|
||||
|
||||
@Anno
|
||||
var localVar by Delegate
|
||||
localVar
|
||||
|
||||
check("localVar", Delegate.property as KProperty0<*>)?.let { error -> return error }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// WITH_REFLECT
|
||||
|
||||
// FILE: 1.kt
|
||||
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("Test")
|
||||
|
||||
package test
|
||||
|
||||
import kotlin.reflect.*
|
||||
|
||||
object Delegate {
|
||||
lateinit var property: KProperty<*>
|
||||
|
||||
operator fun getValue(instance: Any?, kProperty: KProperty<*>): List<String?> {
|
||||
property = kProperty
|
||||
return emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: 2.kt
|
||||
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("Test")
|
||||
|
||||
package test
|
||||
|
||||
fun foo() {
|
||||
val x by Delegate
|
||||
x
|
||||
}
|
||||
|
||||
// FILE: test.kt
|
||||
|
||||
import test.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
foo()
|
||||
assertEquals("val x: kotlin.collections.List<kotlin.String?>", Delegate.property.toString())
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
// TARGET_BACKEND: JVM
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.test.*
|
||||
|
||||
class Delegate<out T>(val value: T) {
|
||||
lateinit var property: KProperty<*>
|
||||
|
||||
operator fun getValue(instance: Any?, kProperty: KProperty<*>): T {
|
||||
property = kProperty
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
class A<X> {
|
||||
inner class B<Y> {
|
||||
fun <Z> foo() {
|
||||
val delegate = Delegate<Map<Pair<X, Y>, Z>>(emptyMap())
|
||||
val c: Map<Pair<X, Y>, Z> by delegate
|
||||
c
|
||||
|
||||
assertEquals("kotlin.collections.Map<kotlin.Pair<X, Y>, Z>", delegate.property.returnType.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A<String>().B<Int>().foo<Double>()
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user