Files
kotlin/compiler/testData/codegen/boxWithStdlib/reflection/properties/protectedClassVar.kt
Alexander Udalov 048a9b686e 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
2015-07-10 20:10:09 +03:00

31 lines
750 B
Kotlin
Vendored

import kotlin.reflect.IllegalPropertyAccessException
import kotlin.reflect.jvm.accessible
import kotlin.reflect.KMutableProperty1
class A(param: String) {
protected var v: String = param
fun ref() = A::class.properties.single() as KMutableProperty1<A, String>
}
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"
}