Simplify property hierarchy in reflection

Leave only 3*2 = 6 classes: KProperty0, KProperty1, KProperty2 and their
mutable analogs, depending on the number of receivers a property takes
This commit is contained in:
Alexander Udalov
2015-06-26 16:40:39 +03:00
parent c3b97e0668
commit 30794060a9
58 changed files with 374 additions and 653 deletions
@@ -4,7 +4,7 @@ import kotlin.reflect.jvm.*
class K(private val value: String)
fun box(): String {
val p = javaClass<K>().kotlin.properties.single() as KMemberProperty<K, String>
val p = javaClass<K>().kotlin.properties.single() as KProperty1<K, String>
try {
return p.get(K("Fail: private property should not be accessible by default"))
@@ -1,11 +1,10 @@
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class A<T> {
val result = "OK"
}
fun box(): String {
val k = A::class.properties.single()
k : KMemberProperty<A<*>, *>
val k : KProperty1<A<*>, *> = A::class.properties.single()
return k.get(A<String>()) as String
}
@@ -1,5 +1,5 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableMemberExtensionProperty
import kotlin.reflect.KMutableProperty2
var storage = "before"
@@ -15,12 +15,12 @@ class A {
fun box(): String {
val props = javaClass<A>().kotlin.extensionProperties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableMemberExtensionProperty<A, *, *>) { "Fail 1: $readonly" }
assert(readonly !is KMutableProperty2<A, *, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableMemberExtensionProperty<A, *, *>) { "Fail 2: $mutable" }
assert(mutable is KMutableProperty2<A, *, *>) { "Fail 2: $mutable" }
val a = A()
mutable as KMutableMemberExtensionProperty<A, String, String>
mutable as KMutableProperty2<A, String, String>
assert(mutable[a, ""] == "before") { "Fail 3: ${mutable.get(a, "")}" }
mutable[a, ""] = "OK"
return mutable.get(a, "")
@@ -1,5 +1,5 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableMemberProperty
import kotlin.reflect.KMutableProperty1
class A(val readonly: String) {
var mutable: String = "before"
@@ -8,12 +8,12 @@ class A(val readonly: String) {
fun box(): String {
val props = javaClass<A>().kotlin.properties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableMemberProperty<A, *>) { "Fail 1: $readonly" }
assert(readonly !is KMutableProperty1<A, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableMemberProperty<A, *>) { "Fail 2: $mutable" }
assert(mutable is KMutableProperty1<A, *>) { "Fail 2: $mutable" }
val a = A("")
mutable as KMutableMemberProperty<A, String>
mutable as KMutableProperty1<A, String>
assert(mutable[a] == "before") { "Fail 3: ${mutable.get(a)}" }
mutable[a] = "OK"
return mutable.get(a)
@@ -1,6 +1,6 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KMemberExtensionProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.KProperty2
class A {
val foo: String = "member"
@@ -9,15 +9,15 @@ class A {
fun box(): String {
run {
val foo: KMemberProperty<A, *> = javaClass<A>().kotlin.properties.single()
val foo: KProperty1<A, *> = javaClass<A>().kotlin.properties.single()
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
assert(foo.get(A()) == "member") { "Fail value: ${foo[A()]}" }
}
run {
val foo: KMemberExtensionProperty<A, *, *> = javaClass<A>().kotlin.extensionProperties.single()
val foo: KProperty2<A, *, *> = javaClass<A>().kotlin.extensionProperties.single()
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
foo as KMemberExtensionProperty<A, Unit, *>
foo as KProperty2<A, Unit, *>
assert(foo.get(A(), Unit) == "extension") { "Fail value: ${foo[A(), Unit]}" }
}