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:
@@ -10,8 +10,8 @@ fun box(): String {
|
||||
val s = J::s
|
||||
|
||||
// Check that correct reflection objects are created
|
||||
assert(i.javaClass.getSimpleName() == "KMemberPropertyImpl", "Fail i class")
|
||||
assert(s.javaClass.getSimpleName() == "KMutableMemberPropertyImpl", "Fail s class")
|
||||
assert(i.javaClass.getSimpleName() == "KProperty1Impl", "Fail i class")
|
||||
assert(s.javaClass.getSimpleName() == "KMutableProperty1Impl", "Fail s class")
|
||||
|
||||
// Check that no Method objects are created for such properties
|
||||
assert(i.javaGetter == null, "Fail i getter")
|
||||
@@ -33,8 +33,8 @@ fun box(): String {
|
||||
assert(a.s == "def", "Fail js access")
|
||||
|
||||
// Check that valid Kotlin reflection objects are created by those Field objects
|
||||
val ki = ji.kotlin as KMemberProperty<J, Int>
|
||||
val ks = js.kotlin as KMutableMemberProperty<J, String>
|
||||
val ki = ji.kotlin as KProperty1<J, Int>
|
||||
val ks = js.kotlin as KMutableProperty1<J, String>
|
||||
assert(ki.get(a) == 42, "Fail ki get")
|
||||
assert(ks.get(a) == "def", "Fail ks get")
|
||||
ks.set(a, "ghi")
|
||||
|
||||
+4
-4
@@ -11,7 +11,7 @@ fun box(): String {
|
||||
val j = J()
|
||||
|
||||
val prop = J::prop
|
||||
if (prop !is KMutableMemberProperty<*, *>) return "Fail instanceof"
|
||||
if (prop !is KMutableProperty1<*, *>) return "Fail instanceof"
|
||||
if (prop.name != "prop") return "Fail name: ${prop.name}"
|
||||
if (prop.get(j) != ":(") return "Fail get before: ${prop[j]}"
|
||||
prop[j] = ":)"
|
||||
@@ -27,13 +27,13 @@ fun box(): String {
|
||||
|
||||
val prop2 = klass.properties.firstOrNull { it.name == "prop" } ?: "Fail: no 'prop' property in properties"
|
||||
if (prop != prop2) return "Fail: property references from :: and from properties differ: $prop != $prop2"
|
||||
if (prop2 !is KMutableMemberProperty<*, *>) return "Fail instanceof 2"
|
||||
(prop2 as KMutableMemberProperty<J, String>).set(j, "::)")
|
||||
if (prop2 !is KMutableProperty1<*, *>) return "Fail instanceof 2"
|
||||
(prop2 as KMutableProperty1<J, String>).set(j, "::)")
|
||||
if (prop.get(j) != "::)") return "Fail get after 2: ${prop[j]}"
|
||||
|
||||
|
||||
val ext = klass.extensionProperties.firstOrNull { it.name == "ext" } ?: "Fail: no 'ext' property in extensionProperties"
|
||||
ext as KMemberExtensionProperty<J, Int, Int>
|
||||
ext as KProperty2<J, Int, Int>
|
||||
val fortyTwo = ext.get(j, 42)
|
||||
if (fortyTwo != 42) return "Fail ext get: $fortyTwo"
|
||||
|
||||
|
||||
+4
-4
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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"
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
+2
-2
@@ -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"
|
||||
|
||||
+2
-2
@@ -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"
|
||||
|
||||
+1
-1
@@ -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"))
|
||||
|
||||
+2
-3
@@ -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
|
||||
}
|
||||
|
||||
+4
-4
@@ -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, "")
|
||||
|
||||
Vendored
+4
-4
@@ -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)
|
||||
|
||||
+5
-5
@@ -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]}" }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user