Support @JvmField on interface properties

#KT-15807 Fixed
This commit is contained in:
Mikhael Bogdanov
2018-03-13 15:18:01 +01:00
parent 719c1882e0
commit 1d283d243e
36 changed files with 718 additions and 34 deletions
@@ -0,0 +1,20 @@
// !LANGUAGE: +JvmFieldInInterface
// TARGET_BACKEND: JVM
// WITH_RUNTIME
import kotlin.reflect.full.memberProperties
class Bar(val value: String)
interface Foo {
companion object {
@JvmField
val z = Bar("OK")
}
}
fun box(): String {
val field = Foo.Companion::z
return field.get().value
}
@@ -0,0 +1,21 @@
// !LANGUAGE: +JvmFieldInInterface +NestedClassesInAnnotations
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: Foo.kt
public class Bar(public val value: String)
annotation class Foo {
companion object {
@JvmField
val FOO = Bar("OK")
}
}
// FILE: bar.kt
fun box(): String {
return Foo.FOO.value
}
@@ -0,0 +1,29 @@
// !LANGUAGE: +JvmFieldInInterface +NestedClassesInAnnotations
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: Test.java
public class Test {
public static String publicField() {
return Foo.z.getS();
}
}
// FILE: simple.kt
public class Bar(public val s: String)
annotation class Foo {
companion object {
@JvmField
val z = Bar("OK")
}
}
fun box(): String {
return Test.publicField()
}
@@ -8,6 +8,7 @@
package zzz
import java.lang.reflect.Field
import kotlin.reflect.KProperty1
import kotlin.reflect.KProperty0
import kotlin.test.assertEquals
class A(val s1: String, val s2: String) {
@@ -21,27 +22,28 @@ class A(val s1: String, val s2: String) {
}
/*
// TODO: uncomment when callable references to object members are supported
class AWithCompanion {
companion object {
@JvmField public val publicField = "1";
@JvmField internal val internalField = "2";
fun testAccessors() {
checkAccessor(AWithCompanion.Companion::publicField, "1", AWithCompanion.Companion)
checkAccessor(AWithCompanion.Companion::internalField, "2", AWithCompanion.Companion)
checkAccessor(AWithCompanion.Companion::publicField, "1")
checkAccessor(AWithCompanion.Companion::internalField, "2")
}
}
}
*/
fun box(): String {
A("1", "2").testAccessors()
// AWithCompanion.testAccessors()
AWithCompanion.testAccessors()
return "OK"
}
public fun <T, R> checkAccessor(prop: KProperty1<T, R>, value: R, receiver: T) {
assertEquals(prop.get(receiver), value, "Property ${prop} has wrong value")
}
public fun <R> checkAccessor(prop: KProperty0<R>, value: R) {
assertEquals(prop.get(), value, "Property ${prop} has wrong value")
}
@@ -0,0 +1,21 @@
// !LANGUAGE: +JvmFieldInInterface
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: Foo.kt
public class Bar(public val value: String)
interface Foo {
companion object {
@JvmField
val FOO = Bar("OK")
}
}
// FILE: bar.kt
fun box(): String {
return Foo.FOO.value
}
@@ -0,0 +1,32 @@
// !LANGUAGE: +JvmFieldInInterface
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: Test.java
public class Test {
public static String publicField() {
return Foo.o.getS() + Foo.k.getS();
}
}
// FILE: simple.kt
public class Bar(public val s: String)
interface Foo {
companion object {
@JvmField
val o = Bar("O")
@JvmField
val k = Bar("K")
}
}
fun box(): String {
return Test.publicField()
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +JvmFieldInInterface
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.jvm.*
import kotlin.test.assertEquals
interface Foo {
companion object {
@JvmField
val value = "OK"
}
}
fun box(): String {
val field = Foo.Companion::value.javaField!!
return field.get(null) as String
}
@@ -0,0 +1,22 @@
// !LANGUAGE: +JvmFieldInInterface +NestedClassesInAnnotations
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// WITH_REFLECT
import kotlin.reflect.full.declaredMemberProperties
annotation class Ann(val value: String)
public class Bar(public val value: String)
annotation class Foo {
companion object {
@JvmField @Ann("O")
val FOO = Bar("K")
}
}
fun box(): String {
val field = Foo.Companion::class.declaredMemberProperties.single()
return (field.annotations.single() as Ann).value + (field.get(Foo.Companion) as Bar).value
}
@@ -0,0 +1,23 @@
// !LANGUAGE: +JvmFieldInInterface
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// WITH_REFLECT
import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.companionObject
class Bar(val value: String)
interface Foo {
companion object {
@JvmField
val z = Bar("OK")
}
}
fun box(): String {
val field = Foo::class.companionObject!!.memberProperties.single() as KProperty1<Foo.Companion, Bar>
return field.get(Foo.Companion).value
}
@@ -0,0 +1,22 @@
// !LANGUAGE: +JvmFieldInInterface
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// WITH_REFLECT
import kotlin.reflect.full.declaredMemberProperties
annotation class Ann(val value: String)
public class Bar(public val value: String)
interface Foo {
companion object {
@JvmField @Ann("O")
val FOO = Bar("K")
}
}
fun box(): String {
val field = Foo.Companion::class.declaredMemberProperties.single()
return (field.annotations.single() as Ann).value + (field.get(Foo.Companion) as Bar).value
}