Files
kotlin/compiler/testData/codegen/boxWithStdlib/reflection/properties/getExtensionPropertiesMutableVsReadonly.kt
Alexander Udalov 30794060a9 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
2015-07-10 20:10:09 +03:00

28 lines
802 B
Kotlin
Vendored

import kotlin.reflect.jvm.kotlin
import kotlin.reflect.KMutableProperty2
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.extensionProperties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableProperty2<A, *, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableProperty2<A, *, *>) { "Fail 2: $mutable" }
val a = A()
mutable as KMutableProperty2<A, String, String>
assert(mutable[a, ""] == "before") { "Fail 3: ${mutable.get(a, "")}" }
mutable[a, ""] = "OK"
return mutable.get(a, "")
}