Generate separate anonymous class for each property reference

Each property reference obtained by the '::' operator now causes back-end to
generate an anonymous subclass of the corresponding KProperty class, with the
customized behavior. This fixes a number of issues:

- get/set/name of property references now works without kotlin-reflect.jar in
  the classpath
- get/set/name methods are now overridden with statically-generated property
  access instead of the default KPropertyImpl's behavior of using Java
  reflection, which should be a lot faster
- references to private/protected properties now work without the need to set
  'accessible' flag, because corresponding synthetic accessors are generated at
  compile-time near the target property

 #KT-6870 Fixed
 #KT-6873 Fixed
 #KT-7033 Fixed
This commit is contained in:
Alexander Udalov
2015-06-30 16:07:51 +03:00
parent 30794060a9
commit 048a9b686e
40 changed files with 833 additions and 304 deletions
@@ -0,0 +1,22 @@
class Test {
private var iv = 1
public fun exec() {
val t = object : Thread() {
override fun run() {
::iv.get(this@Test)
::iv.set(this@Test, 2)
}
}
t.start()
t.join(1000)
}
fun result() = if (iv == 2) "OK" else "Fail $iv"
}
fun box(): String {
val t = Test()
t.exec()
return t.result()
}
@@ -1,29 +0,0 @@
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.accessible
class Result {
private val value = "OK"
fun ref(): KProperty1<Result, String> = ::value
}
fun box(): String {
val p = Result().ref()
try {
p.get(Result())
return "Fail: private property is accessible by default"
} catch(e: IllegalPropertyAccessException) { }
p.accessible = true
val r = p.get(Result())
p.accessible = false
try {
p.get(Result())
return "Fail: setAccessible(false) had no effect"
} catch(e: IllegalPropertyAccessException) { }
return r
}
@@ -1,31 +0,0 @@
import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.jvm.accessible
class A {
private var value = 0
fun ref(): KMutableProperty1<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"
}
@@ -1,29 +0,0 @@
import kotlin.reflect.IllegalPropertyAccessException
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: IllegalPropertyAccessException) { }
try {
f.set(a, ":D")
return "Fail: protected property setter is accessible by default"
} catch (e: IllegalPropertyAccessException) { }
f.accessible = true
f.set(a, ":)")
return if (f[a] != ":)") "Fail: ${f[a]}" else "OK"
}