Introduce KProperty{0,1,2}.getDelegate
#KT-8384 In Progress
This commit is contained in:
+44
@@ -0,0 +1,44 @@
|
||||
// IGNORE_BACKEND: JS
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty2
|
||||
import kotlin.reflect.full.IllegalPropertyDelegateAccessException
|
||||
import kotlin.test.*
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(instance: Any?, property: KProperty<*>) = true
|
||||
}
|
||||
|
||||
val topLevel: Boolean by Delegate
|
||||
val String.extension: Boolean by Delegate
|
||||
|
||||
class Foo {
|
||||
val member: Boolean by Delegate
|
||||
val String.memberExtension: Boolean by Delegate
|
||||
}
|
||||
|
||||
inline fun check(block: () -> Unit) {
|
||||
try {
|
||||
block()
|
||||
throw AssertionError("No IllegalPropertyDelegateAccessException has been thrown")
|
||||
} catch (e: IllegalPropertyDelegateAccessException) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
check { ::topLevel.getDelegate() }
|
||||
|
||||
check { String::extension.getDelegate("") }
|
||||
check { ""::extension.getDelegate() }
|
||||
|
||||
val foo = Foo()
|
||||
check { Foo::member.getDelegate(foo) }
|
||||
check { foo::member.getDelegate() }
|
||||
|
||||
val me = Foo::class.members.single { it.name == "memberExtension" } as KProperty2<Foo, String, Boolean>
|
||||
check { me.getDelegate(foo, "") }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user