Support 'accessible' for reflected properties on JVM
Calls Java reflection's isAccessible/setAccessible
This commit is contained in:
+13
-1
@@ -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"
|
||||
}
|
||||
+28
@@ -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"
|
||||
}
|
||||
+12
@@ -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())
|
||||
}
|
||||
+15
@@ -459,6 +459,21 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/callableReference/property/privateClassVal.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("privateClassVar.kt")
|
||||
public void testPrivateClassVar() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/callableReference/property/privateClassVar.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("protectedClassVar.kt")
|
||||
public void testProtectedClassVar() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/callableReference/property/protectedClassVar.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("publicClassValAccessible.kt")
|
||||
public void testPublicClassValAccessible() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/callableReference/property/publicClassValAccessible.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleExtension.kt")
|
||||
public void testSimpleExtension() throws Exception {
|
||||
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/callableReference/property/simpleExtension.kt");
|
||||
|
||||
@@ -23,14 +23,14 @@ open class KMemberPropertyImpl<T, out R>(
|
||||
name: String,
|
||||
protected val owner: KClassImpl<T>
|
||||
) : KMemberProperty<T, R>, KPropertyImpl<R>(name) {
|
||||
protected val field: Field? =
|
||||
val field: Field? =
|
||||
if (owner.origin == KClassOrigin.FOREIGN) {
|
||||
owner.jClass.getField(name)
|
||||
}
|
||||
else null
|
||||
|
||||
// TODO: extract, make lazy (weak?), use our descriptors knowledge
|
||||
protected val getter: Method? =
|
||||
val getter: Method? =
|
||||
if (owner.origin == KClassOrigin.KOTLIN) {
|
||||
owner.jClass.getMaybeDeclaredMethod(getterName(name))
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class KMutableMemberPropertyImpl<T, R>(
|
||||
name: String,
|
||||
owner: KClassImpl<T>
|
||||
) : KMutableMemberProperty<T, R>, KMemberPropertyImpl<T, R>(name, owner) {
|
||||
private val setter: Method? =
|
||||
val setter: Method? =
|
||||
if (owner.origin == KClassOrigin.KOTLIN) {
|
||||
owner.jClass.getMaybeDeclaredMethod(setterName(name), getter!!.getReturnType()!!)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package kotlin.reflect.jvm
|
||||
|
||||
import kotlin.reflect.jvm.internal.KMemberPropertyImpl
|
||||
import kotlin.reflect.jvm.internal.KMutableMemberPropertyImpl
|
||||
|
||||
public var <R> KProperty<R>.accessible: Boolean
|
||||
get() {
|
||||
return when (this) {
|
||||
is KMutableMemberPropertyImpl<*, R> ->
|
||||
field?.isAccessible() ?: true &&
|
||||
getter?.isAccessible() ?: true &&
|
||||
setter?.isAccessible() ?: true
|
||||
is KMemberPropertyImpl<*, R> ->
|
||||
field?.isAccessible() ?: true &&
|
||||
getter?.isAccessible() ?: true
|
||||
else -> {
|
||||
// Non-member properties always have public visibility on JVM, thus accessible has no effect on them
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
set(value) {
|
||||
when (this) {
|
||||
is KMutableMemberPropertyImpl<*, R> -> {
|
||||
field?.setAccessible(value)
|
||||
getter?.setAccessible(value)
|
||||
setter?.setAccessible(value)
|
||||
}
|
||||
is KMemberPropertyImpl<*, R> -> {
|
||||
field?.setAccessible(value)
|
||||
getter?.setAccessible(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user