Implement KClass.getProperties()

#KT-6570 In Progress
This commit is contained in:
Alexander Udalov
2015-02-20 19:40:47 +03:00
parent fdfd808d80
commit da209e673c
7 changed files with 125 additions and 1 deletions
@@ -0,0 +1,19 @@
import kotlin.reflect.*
import kotlin.reflect.jvm.*
class K(private val value: String)
fun box(): String {
val p = javaClass<K>().kotlin.getProperties().single() as KMemberProperty<K, String>
try {
return p.get(K("Fail: private property should not be accessible by default"))
}
catch (e: IllegalPropertyAccessException) {
// OK
}
p.accessible = true
return p.get(K("OK"))
}
@@ -0,0 +1,12 @@
import kotlin.reflect.jvm.*
import kotlin.test.*
open class Super(val r: String)
class Sub(r: String) : Super(r)
fun box(): String {
val props = javaClass<Sub>().kotlin.getProperties()
assertEquals(listOf("r"), props.map { it.name })
return props.single().get(Sub("OK")).toString()
}
@@ -0,0 +1,20 @@
import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableMemberProperty
class A(val readonly: String) {
var mutable: String = "before"
}
fun box(): String {
val props = javaClass<A>().kotlin.getProperties()
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableMemberProperty<A, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableMemberProperty<A, *>) { "Fail 2: $mutable" }
val a = A("")
mutable as KMutableMemberProperty<A, String>
assert(mutable[a] == "before") { "Fail 3: ${mutable.get(a)}" }
mutable[a] = "OK"
return mutable.get(a)
}
@@ -0,0 +1,23 @@
import kotlin.reflect.jvm.kotlin
class A(param: String) {
val int: Int get() = 42
val string: String = param
var anyVar: Any? = null
val List<IntRange>.extensionToList: Unit get() {}
fun notAProperty() {}
}
fun box(): String {
val klass = javaClass<A>().kotlin
val props = klass.getProperties()
val names = props.map { it.name }.toSortedList()
assert(names == listOf("anyVar", "int", "string")) { "Fail names: $props" }
val stringProp = props.firstOrNull { it.name == "string" } ?: return "Fail, string not found: $props"
return stringProp.get(A("OK")) as String
}