Files
kotlin/compiler/testData/codegen/box/reflection/multifileClasses/javaFieldForVarAndConstVal.kt
2016-03-28 21:11:14 +03:00

53 lines
1.1 KiB
Kotlin
Vendored

// WITH_REFLECT
// FULL_JDK
// FILE: 1.kt
@file:kotlin.jvm.JvmName("Test")
@file:kotlin.jvm.JvmMultifileClass
package test
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
fun testX() {
val field = ::x.javaField ?: throw AssertionError("No java field for ${::x.name}")
try {
field.get(null)
throw AssertionError("Fail: field.get should fail because the field is private")
}
catch (e: IllegalAccessException) {
// OK
}
field.setAccessible(true)
assertEquals("I am x", field.get(null))
field.set(null, "OK")
}
fun testY() {
val field = ::y.javaField ?: throw AssertionError("No java field for ${::y.name}")
assertEquals("I am const y", field.get(null))
// Accessible = false should have no effect because the field is public
field.setAccessible(false)
assertEquals("I am const y", field.get(null))
}
fun box(): String {
testX()
testY()
return x
}
// FILE: 2.kt
@file:kotlin.jvm.JvmName("Test")
@file:kotlin.jvm.JvmMultifileClass
package test
var x = "I am x"
const val y = "I am const y"