Support 'accessible' for reflected properties on JVM

Calls Java reflection's isAccessible/setAccessible
This commit is contained in:
Alexander Udalov
2014-06-05 19:15:09 +04:00
parent 1275c84f92
commit e7f19c531a
7 changed files with 150 additions and 4 deletions
@@ -1,3 +1,5 @@
import kotlin.reflect.jvm.accessible
class Result {
private val value = "OK"
@@ -11,5 +13,15 @@ fun box(): String {
return "Fail: private property is accessible by default"
} catch(e: IllegalAccessException) { }
return "OK"
p.accessible = true
val r = p.get(Result())
p.accessible = false
try {
p.get(Result())
return "Fail: setAccessible(false) had no effect"
} catch(e: IllegalAccessException) { }
return r
}
@@ -0,0 +1,29 @@
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: IllegalAccessException) { }
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: IllegalAccessException) { }
return "OK"
}
@@ -0,0 +1,28 @@
import kotlin.reflect.jvm.accessible
class A(param: String) {
protected var v: String = param
fun ref() = ::v
}
fun box(): String {
val a = A(":(")
val f = a.ref()
try {
f.get(a)
return "Fail: protected property getter is accessible by default"
} catch (e: IllegalAccessException) { }
try {
f.set(a, ":D")
return "Fail: protected property setter is accessible by default"
} catch (e: IllegalAccessException) { }
f.accessible = true
f.set(a, ":)")
return if (f[a] != ":)") "Fail: ${f[a]}" else "OK"
}
@@ -0,0 +1,12 @@
import kotlin.reflect.jvm.accessible
class Result {
public val value: String = "OK"
}
fun box(): String {
val p = Result::value
p.accessible = false
// setAccessible(false) should have no effect on the accessibility of a public reflection object
return p.get(Result())
}