Type-check reference to property with invisible setter to KProperty
#KT-12337 Fixed
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
import kotlin.reflect.KMutableProperty
|
||||
|
||||
class Bar(name: String) {
|
||||
var foo: String = name
|
||||
private set
|
||||
|
||||
fun test() {
|
||||
val p = Bar::foo
|
||||
if (p !is KMutableProperty<*>) throw AssertionError("Fail: p is not a KMutableProperty")
|
||||
p.set(this, "OK")
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val bar = Bar("Fail")
|
||||
bar.test()
|
||||
return bar.foo
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// See KT-12337 Reference to property with invisible setter should not be a KMutableProperty
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.KMutableProperty
|
||||
|
||||
open class Bar(name: String) {
|
||||
var foo: String = name
|
||||
private set
|
||||
}
|
||||
|
||||
class Baz : Bar("") {
|
||||
fun ref() = Bar::foo
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val p1: KProperty1<Bar, String> = Bar::foo
|
||||
if (p1 is KMutableProperty<*>) return "Fail: p1 is a KMutableProperty"
|
||||
|
||||
val p2 = Baz().ref()
|
||||
if (p2 is KMutableProperty<*>) return "Fail: p2 is a KMutableProperty"
|
||||
|
||||
val p3 = Bar("")::foo
|
||||
if (p3 is KMutableProperty<*>) return "Fail: p3 is a KMutableProperty"
|
||||
|
||||
return p1.get(Bar("OK"))
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.*
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(thiz: My, property: KProperty<*>): String {
|
||||
if (property !is KMutableProperty<*>) return "Fail: property is not a KMutableProperty"
|
||||
property as KMutableProperty1<My, String>
|
||||
|
||||
try {
|
||||
property.set(thiz, "")
|
||||
return "Fail: property.set should cause IllegalCallableAccessException"
|
||||
}
|
||||
catch (e: IllegalCallableAccessException) {
|
||||
// OK
|
||||
}
|
||||
|
||||
property.accessible = true
|
||||
property.set(thiz, "")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
operator fun setValue(thiz: My, property: KProperty<*>, value: String) {
|
||||
}
|
||||
}
|
||||
|
||||
class My {
|
||||
var delegate: String by Delegate
|
||||
private set
|
||||
}
|
||||
|
||||
fun box() = My().delegate
|
||||
Reference in New Issue
Block a user