Introduce KProperty{0,1,2}.getDelegate

#KT-8384 In Progress
This commit is contained in:
Alexander Udalov
2016-12-22 18:30:38 +03:00
parent f1cd2ee6fd
commit 78f2515e95
47 changed files with 1398 additions and 0 deletions
@@ -0,0 +1,21 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
operator fun getValue(instance: Any?, property: KProperty<*>) = true
}
class Foo {
val isOK: Boolean by Delegate
}
fun box(): String {
val foo = Foo()
assertEquals(Delegate, Foo::isOK.apply { isAccessible = true }.getDelegate(foo))
assertEquals(Delegate, foo::isOK.apply { isAccessible = true }.getDelegate())
return if (foo.isOK) "OK" else "Fail"
}
@@ -0,0 +1,26 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
var storage = ""
operator fun getValue(instance: Any?, property: KProperty<*>) = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
class Foo
var Foo.result: String by Delegate
fun box(): String {
val foo = Foo()
foo.result = "Fail"
val d = (foo::result).apply { isAccessible = true }.getDelegate() as Delegate
foo.result = "OK"
assertEquals(d, (foo::result).apply { isAccessible = true }.getDelegate())
assertEquals(d, (Foo()::result).apply { isAccessible = true }.getDelegate())
return d.getValue(foo, Foo::result)
}
@@ -0,0 +1,26 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
var storage = ""
operator fun getValue(instance: Any?, property: KProperty<*>) = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
class Foo {
var result: String by Delegate
}
fun box(): String {
val foo = Foo()
foo.result = "Fail"
val d = (foo::result).apply { isAccessible = true }.getDelegate() as Delegate
foo.result = "OK"
assertEquals(d, (foo::result).apply { isAccessible = true }.getDelegate())
assertEquals(d, (Foo()::result).apply { isAccessible = true }.getDelegate())
return d.getValue(foo, Foo::result)
}
@@ -0,0 +1,25 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
var storage = ""
operator fun getValue(instance: Any?, property: KProperty<*>) = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
class Foo
var Foo.result: String by Delegate
fun box(): String {
val foo = Foo()
foo.result = "Fail"
val d = Foo::result.apply { isAccessible = true }.getDelegate(foo) as Delegate
foo.result = "OK"
assertEquals(d, Foo::result.apply { isAccessible = true }.getDelegate(foo))
return d.getValue(foo, Foo::result)
}
@@ -0,0 +1,25 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
operator fun getValue(instance: Any?, property: KProperty<*>) = "OK"
}
open class Base {
val x: String by Delegate
}
class Derived : Base()
fun box(): String {
val d = Derived()
assertEquals(
(Base::x).apply { isAccessible = true }.getDelegate(d),
(Derived::x).apply { isAccessible = true }.getDelegate(d)
)
return d.x
}
@@ -0,0 +1,32 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
var ref: KProperty<*>? = null
class Delegate {
var storage = ""
operator fun provideDelegate(instance: Any?, property: KProperty<*>): Delegate {
ref = property
return this
}
operator fun getValue(instance: Any?, property: KProperty<*>): String = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
var result: String by Delegate()
fun box(): String {
result
val prop = ref as KProperty0<*>
result = "Fail"
val d = prop.apply { isAccessible = true }.getDelegate() as Delegate
result = "OK"
assertEquals(d, prop.apply { isAccessible = true }.getDelegate())
return result
}
@@ -0,0 +1,29 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
var storage = ""
operator fun getValue(instance: Any?, property: KProperty<*>) = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
class Bar
class Foo {
var Bar.result: String by Delegate
}
fun box(): String {
val foo = Foo()
val bar = Bar()
with(foo) { bar.result = "Fail" }
val prop = Foo::class.members.single { it.name == "result" } as KMutableProperty2<Foo, Bar, String>
val d = prop.apply { isAccessible = true }.getDelegate(foo, bar) as Delegate
with(foo) { bar.result = "OK" }
assertEquals(d, prop.apply { isAccessible = true }.getDelegate(foo, bar))
return d.getValue(foo, prop)
}
@@ -0,0 +1,25 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
var storage = ""
operator fun getValue(instance: Any?, property: KProperty<*>) = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
class Foo {
var result: String by Delegate
}
fun box(): String {
val foo = Foo()
foo.result = "Fail"
val d = (Foo::result).apply { isAccessible = true }.getDelegate(foo) as Delegate
foo.result = "OK"
assertEquals(d, (Foo::result).apply { isAccessible = true }.getDelegate(foo))
return d.getValue(foo, Foo::result)
}
@@ -0,0 +1,26 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
class Delegate(val value: String) {
operator fun getValue(instance: Any?, property: KProperty<*>) = value
}
class Foo {
val x: String by Delegate("class")
companion object {
val x: String by Delegate("companion")
}
}
fun box(): String {
val foo = Foo()
assertEquals("class", ((foo::x).apply { isAccessible = true }.getDelegate() as Delegate).value)
assertEquals("class", ((Foo::x).apply { isAccessible = true }.getDelegate(foo) as Delegate).value)
assertEquals("companion", ((Foo.Companion::x).apply { isAccessible = true }.getDelegate() as Delegate).value)
return "OK"
}
@@ -0,0 +1,42 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.full.extensionReceiverParameter
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
class Delegate(val value: String) {
operator fun getValue(instance: Any?, property: KProperty<*>) = value
}
class Foo
val Foo.bar: String by Delegate("Foo")
val String.bar: String by Delegate("String")
val Unit.bar: String by Delegate("Unit")
class MemberExtensions {
val Foo?.bar: String by Delegate("Foo")
val String?.bar: String by Delegate("String")
val Unit?.bar: String by Delegate("Unit")
}
fun box(): String {
val foo = Foo()
assertEquals("Foo", ((foo::bar).apply { isAccessible = true }.getDelegate() as Delegate).value)
assertEquals("Foo", ((Foo::bar).apply { isAccessible = true }.getDelegate(foo) as Delegate).value)
assertEquals("String", ((""::bar).apply { isAccessible = true }.getDelegate() as Delegate).value)
assertEquals("String", ((String::bar).apply { isAccessible = true }.getDelegate("") as Delegate).value)
assertEquals("Unit", ((Unit::bar).apply { isAccessible = true }.getDelegate() as Delegate).value)
val me = MemberExtensions::class.members.filter { it.name == "bar" } as List<KProperty2<MemberExtensions, Any?, String>>
assertEquals(listOf("Foo", "String", "Unit"), me.sortedBy {
it.extensionReceiverParameter!!.type.toString()
}.map {
(it.apply { isAccessible = true }.getDelegate(MemberExtensions(), null) as Delegate).value
})
return "OK"
}
@@ -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"
}
@@ -0,0 +1,29 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty2
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
val topLevel: Boolean = true
val String.extension: Boolean get() = true
class Foo {
val member: Boolean = true
val String.memberExtension: Boolean get() = true
}
fun box(): String {
assertNull(::topLevel.apply { isAccessible = true }.getDelegate())
assertNull(String::extension.apply { isAccessible = true }.getDelegate(""))
assertNull(""::extension.apply { isAccessible = true }.getDelegate())
assertNull(Foo::member.apply { isAccessible = true }.getDelegate(Foo()))
assertNull(Foo()::member.apply { isAccessible = true }.getDelegate())
val me = Foo::class.members.single { it.name == "memberExtension" } as KProperty2<Foo, String, Boolean>
assertNull(me.apply { isAccessible = true }.getDelegate(Foo(), ""))
return "OK"
}
@@ -0,0 +1,37 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
class Delegate(val value: String) {
operator fun getValue(instance: Any?, property: KProperty<*>) = value
}
open class Base {
open val x: String by Delegate("Base")
}
class Derived : Base() {
override val x: String by Delegate("Derived")
}
fun check(expected: String, delegate: Any?) {
if (delegate == null) throw AssertionError("getDelegate returned null")
assertEquals(expected, (delegate as Delegate).value)
}
fun box(): String {
val base = Base()
val derived = Derived()
check("Base", (Base::x).apply { isAccessible = true }.getDelegate(base))
check("Base", (base::x).apply { isAccessible = true }.getDelegate())
check("Derived", (Derived::x).apply { isAccessible = true }.getDelegate(derived))
check("Derived", (derived::x).apply { isAccessible = true }.getDelegate())
check("Base", (Base::x).apply { isAccessible = true }.getDelegate(derived))
return "OK"
}
@@ -0,0 +1,23 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
object Delegate {
var storage = ""
operator fun getValue(instance: Any?, property: KProperty<*>) = storage
operator fun setValue(instance: Any?, property: KProperty<*>, value: String) { storage = value }
}
var result: String by Delegate
fun box(): String {
result = "Fail"
val p = (::result).apply { isAccessible = true }
val d = p.getDelegate() as Delegate
result = "OK"
assertEquals(d, (::result).apply { isAccessible = true }.getDelegate())
return d.getValue(null, p)
}