Support member extension properties, implement KClass.getExtensionProperties()

#KT-6570 Fixed
This commit is contained in:
Alexander Udalov
2015-02-24 19:04:47 +03:00
parent da209e673c
commit b3cdd818f0
11 changed files with 255 additions and 4 deletions
@@ -0,0 +1,2 @@
public class J extends K {
}
@@ -0,0 +1,40 @@
import kotlin.reflect.*
import kotlin.reflect.jvm.*
public open class K {
var prop: String = ":("
val Int.ext: Int get() = this
}
fun box(): String {
val j = J()
val prop = J::prop
if (prop !is KMutableMemberProperty<*, *>) return "Fail instanceof"
if (prop.name != "prop") return "Fail name: ${prop.name}"
if (prop.get(j) != ":(") return "Fail get before: ${prop[j]}"
prop[j] = ":)"
if (prop.get(j) != ":)") return "Fail get after: ${prop[j]}"
if (prop == K::prop) return "Fail J::prop == K::prop (these are different properties)"
val klass = javaClass<J>().kotlin
val prop2 = klass.getProperties().firstOrNull { it.name == "prop" }
?: "Fail: no 'prop' property in getProperties()"
if (prop != prop2) return "Fail: property references from :: and from getProperties() differ"
if (prop2 !is KMutableMemberProperty<*, *>) return "Fail instanceof 2"
(prop2 as KMutableMemberProperty<J, String>).set(j, "::)")
if (prop.get(j) != "::)") return "Fail get after 2: ${prop[j]}"
val ext = klass.getExtensionProperties().firstOrNull { it.name == "ext" }
?: "Fail: no 'ext' property in getProperties()"
ext as KMemberExtensionProperty<J, Int, Int>
val fortyTwo = ext.get(j, 42)
if (fortyTwo != 42) return "Fail ext get: $fortyTwo"
return "OK"
}
@@ -0,0 +1,12 @@
import kotlin.reflect.jvm.kotlin
class A {
var String.id: String
get() = this
set(value) {}
}
fun box(): String {
val p = javaClass<A>().kotlin.getExtensionProperties().single()
return if ("$p" == "var A.(kotlin.String.)id") "OK" else "Fail $p"
}
@@ -0,0 +1,27 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableMemberExtensionProperty
var storage = "before"
class A {
val String.readonly: String
get() = this
var String.mutable: String
get() = storage
set(value) { storage = value }
}
fun box(): String {
val props = javaClass<A>().kotlin.getExtensionProperties()
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableMemberExtensionProperty<A, *, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableMemberExtensionProperty<A, *, *>) { "Fail 2: $mutable" }
val a = A()
mutable as KMutableMemberExtensionProperty<A, String, String>
assert(mutable[a, ""] == "before") { "Fail 3: ${mutable.get(a, "")}" }
mutable[a, ""] = "OK"
return mutable.get(a, "")
}
@@ -0,0 +1,25 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMemberProperty
import kotlin.reflect.KMemberExtensionProperty
class A {
val foo: String = "member"
val Unit.foo: String get() = "extension"
}
fun box(): String {
run {
val foo: KMemberProperty<A, *> = javaClass<A>().kotlin.getProperties().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.getExtensionProperties().single()
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
foo as KMemberExtensionProperty<A, Unit, *>
assert(foo.get(A(), Unit) == "extension") { "Fail value: ${foo[A(), Unit]}" }
}
return "OK"
}