mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-09 08:31:29 +00:00
The former name clashes with java.lang.IllegalAccessException and proved to be inconvenient because it should always be qualified in the source. Also use java.lang exception's message as kotlin.reflect exception's message
32 lines
673 B
Kotlin
32 lines
673 B
Kotlin
import kotlin.reflect.IllegalPropertyAccessException
|
|
import kotlin.reflect.KMutableMemberProperty
|
|
import kotlin.reflect.jvm.accessible
|
|
|
|
class A {
|
|
private var value = 0
|
|
|
|
fun ref(): KMutableMemberProperty<A, Int> = ::value
|
|
}
|
|
|
|
fun box(): String {
|
|
val a = A()
|
|
val p = a.ref()
|
|
try {
|
|
p.set(a, 1)
|
|
return "Fail: private property is accessible by default"
|
|
} catch(e: IllegalPropertyAccessException) { }
|
|
|
|
p.accessible = true
|
|
|
|
p.set(a, 2)
|
|
p.get(a)
|
|
|
|
p.accessible = false
|
|
try {
|
|
p.set(a, 3)
|
|
return "Fail: setAccessible(false) had no effect"
|
|
} catch(e: IllegalPropertyAccessException) { }
|
|
|
|
return "OK"
|
|
}
|