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
@@ -1,6 +1,6 @@
//For KT-6020
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KMutableMemberProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.KMutableProperty1
class Value<T>(var value: T = null as T, var text: String? = null)
@@ -8,7 +8,7 @@ val <T> Value<T>.additionalText by DVal(Value<T>::text) //works
val <T> Value<T>.additionalValue by DVal(Value<T>::value) //not work
class DVal<T, R, P: KMemberProperty<T, R>>(val kmember: P) {
class DVal<T, R, P: KProperty1<T, R>>(val kmember: P) {
fun get(t: T, p: PropertyMetadata): R {
return kmember.get(t)
}
@@ -17,4 +17,4 @@ class DVal<T, R, P: KMemberProperty<T, R>>(val kmember: P) {
fun box(): String {
val p = Value("O", "K")
return p.additionalValue + p.additionalText
}
}
@@ -1,8 +1,8 @@
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
class A {
companion object {
val ref: KMemberProperty<A, String> = A::foo
val ref: KProperty1<A, String> = A::foo
}
val foo: String = "OK"
@@ -1,11 +1,11 @@
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.accessible
class Result {
private val value = "OK"
fun ref(): KMemberProperty<Result, String> = ::value
fun ref(): KProperty1<Result, String> = ::value
}
fun box(): String {
@@ -1,11 +1,11 @@
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMutableMemberProperty
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.jvm.accessible
class A {
private var value = 0
fun ref(): KMutableMemberProperty<A, Int> = ::value
fun ref(): KMutableProperty1<A, Int> = ::value
}
fun box(): String {
@@ -4,7 +4,7 @@ object A {
val b: String = "OK"
platformStatic val c: String = "OK"
platformStatic var c: String = "Fail"
platformStatic fun test1() : String {
return b
@@ -36,6 +36,8 @@ fun box(): String {
if ((A::test4)(A) != "1OK") return "fail 4"
(A::c).set(A, "OK")
if (((A::c).get(A)) != "OK") return "fail 5"
return "OK"
@@ -1,7 +1,7 @@
import kotlin.reflect.KExtensionProperty
import kotlin.reflect.KProperty1
import kotlin.test.assertEquals
fun check(expected: String, p: KExtensionProperty<*, *>) {
fun check(expected: String, p: KProperty1<*, *>) {
var s = p.toString()
// Strip "val" or "var"
@@ -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]}" }
}