Initial support of JvmField annotation
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
open class A {
|
||||
@JvmField public val publicField = "1";
|
||||
@JvmField internal val internalField = "2";
|
||||
@JvmField protected val protectedField = "34";
|
||||
|
||||
fun test(): String {
|
||||
return {
|
||||
publicField + internalField + protectedField
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return if (A().test() == "1234") return "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
@JvmField public val publicField = "1";
|
||||
@JvmField internal val internalField = "23";
|
||||
|
||||
fun test(): String {
|
||||
return {
|
||||
publicField + internalField
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return if (test() == "123") return "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import kotlin.test.assertFalse
|
||||
|
||||
@JvmField public val field = "OK";
|
||||
|
||||
class A {
|
||||
@JvmField public val field = "OK";
|
||||
|
||||
companion object {
|
||||
@JvmField public val cfield = "OK";
|
||||
}
|
||||
}
|
||||
|
||||
object Object {
|
||||
@JvmField public val field = "OK";
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = A().field
|
||||
|
||||
checkNoAccessors(A::class.java)
|
||||
checkNoAccessors(A.Companion::class.java)
|
||||
checkNoAccessors(Object::class.java)
|
||||
checkNoAccessors(Class.forName("CheckNoAccessorsKt"))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
public fun checkNoAccessors(clazz: Class<*>) {
|
||||
clazz.declaredMethods.forEach {
|
||||
assertFalse(it.name.startsWith("get") || it.name.startsWith("set"),
|
||||
"Class ${clazz.name} has accessor '${it.name}'"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package zzz
|
||||
import java.lang.reflect.Field
|
||||
import kotlin.reflect.jvm.javaField
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class A(val s1: String, val s2: String) {
|
||||
@JvmField public val publicField = s1;
|
||||
@JvmField internal val internalField = s2;
|
||||
|
||||
fun testAccessors() {
|
||||
checkAccessor(A::publicField, s1, this)
|
||||
checkAccessor(A::internalField, s2, this)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A("1", "2").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")
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package zzz
|
||||
import java.lang.reflect.Field
|
||||
import kotlin.reflect.jvm.javaField
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
||||
import kotlin.reflect.KMutableProperty1
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class A(val s1: String, val s2: String) {
|
||||
@JvmField public var publicField = s1;
|
||||
@JvmField internal var internalField = s2;
|
||||
|
||||
fun testAccessors() {
|
||||
checkAccessor(A::class.members.firstOrNull { it.name == "publicField" } as KMutableProperty1<A, String>, s1, "3", this)
|
||||
checkAccessor(A::class.members.firstOrNull { it.name == "internalField" } as KMutableProperty1<A, String>, s2, "4", this)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class AWithCompanion {
|
||||
companion object {
|
||||
@JvmField public var publicField = "1";
|
||||
@JvmField internal var internalField = "2";
|
||||
|
||||
fun testAccessors() {
|
||||
checkAccessor(AWithCompanion.Companion::class.members.firstOrNull { it.name == "publicField" } as KMutableProperty1<AWithCompanion.Companion, String>, "1", "3", AWithCompanion.Companion)
|
||||
checkAccessor(AWithCompanion.Companion::class.members.firstOrNull { it.name == "internalField" } as KMutableProperty1<AWithCompanion.Companion, String>, "2", "4", AWithCompanion.Companion)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A("1", "2").testAccessors()
|
||||
AWithCompanion.testAccessors()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
public fun <T, R> checkAccessor(prop: KMutableProperty1<T, R>, value: R, newValue: R, receiver: T) {
|
||||
assertEquals(prop.get(receiver), value, "Property ${prop} has wrong value")
|
||||
prop.set(receiver, newValue)
|
||||
assertEquals(prop.get(receiver), newValue, "Property ${prop} has wrong value")
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
class A {
|
||||
@JvmField public val field = "OK";
|
||||
|
||||
companion object {
|
||||
@JvmField public val cfield = "OK";
|
||||
}
|
||||
}
|
||||
|
||||
object Object {
|
||||
@JvmField public val field = "OK";
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var result = A().field
|
||||
|
||||
if (result != "OK") return "fail 1: $result"
|
||||
if (A.cfield != "OK") return "fail 2: ${A.cfield}"
|
||||
if (Object.field != "OK") return "fail 3: ${Object.field}"
|
||||
|
||||
return "OK"
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
open class A {
|
||||
@JvmField public val publicField = "1";
|
||||
@JvmField internal val internalField = "2";
|
||||
@JvmField protected val protectedfield = "3";
|
||||
}
|
||||
|
||||
|
||||
class B : A() {
|
||||
fun test(): String {
|
||||
return super.publicField + super.internalField + super.protectedfield
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return if (B().test() == "123") return "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
open class A {
|
||||
@JvmField public val publicField = "1";
|
||||
@JvmField internal val internalField = "2";
|
||||
@JvmField protected val protectedfield = "3";
|
||||
}
|
||||
|
||||
open class B : A() {
|
||||
|
||||
}
|
||||
|
||||
open class C : B() {
|
||||
fun test(): String {
|
||||
return super.publicField + super.internalField + super.protectedfield
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return if (C().test() == "123") return "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package zzz
|
||||
import java.lang.reflect.Field
|
||||
import kotlin.reflect.jvm.javaField
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.reflect.KProperty0
|
||||
|
||||
@JvmField public val publicField = "1";
|
||||
@JvmField internal val internalField = "2";
|
||||
|
||||
fun testAccessors() {
|
||||
val kProperty: KProperty0<String> = ::publicField
|
||||
checkAccessor(kProperty, "1")
|
||||
checkAccessor(::internalField, "2")
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
testAccessors()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
public fun <T, R> checkAccessor(prop: KProperty0<T>, value: R) {
|
||||
assertEquals(prop.get(), value, "Property ${prop} has wrong value")
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package test
|
||||
|
||||
import kotlin.reflect.KMutableProperty0
|
||||
import kotlin.reflect.jvm.kotlinProperty
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
public var publicField = "1"
|
||||
internal var internalField = "2"
|
||||
|
||||
fun testAccessors() {
|
||||
val packageClass = Class.forName("test.TopLevelFieldReflectionKt")
|
||||
packageClass.getDeclaredField("publicField").kotlinProperty
|
||||
checkAccessor(packageClass.getDeclaredField("publicField").kotlinProperty as KMutableProperty0<String>, "1", "3")
|
||||
checkAccessor(packageClass.getDeclaredField("internalField").kotlinProperty as KMutableProperty0<String>, "2", "4")
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
testAccessors()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
public fun < R> checkAccessor(prop: KMutableProperty0<R>, value: R, newValue: R) {
|
||||
assertEquals(prop.get(), value, "Property ${prop} has wrong value")
|
||||
prop.set(newValue)
|
||||
assertEquals(prop.get(), newValue, "Property ${prop} has wrong value")
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// FULL_JDK
|
||||
|
||||
import java.lang.reflect.Field
|
||||
import kotlin.reflect.jvm.javaField
|
||||
import kotlin.test.assertNotEquals
|
||||
import java.lang.reflect.Modifier
|
||||
|
||||
@JvmField public val publicField = "OK";
|
||||
@JvmField internal val internalField = "OK";
|
||||
|
||||
fun testVisibilities() {
|
||||
checkVisibility(::publicField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(::internalField.javaField!!, Modifier.PUBLIC)
|
||||
}
|
||||
|
||||
class A {
|
||||
@JvmField public val publicField = "OK";
|
||||
@JvmField internal val internalField = "OK";
|
||||
@JvmField protected val protectedfield = "OK";
|
||||
|
||||
fun testVisibilities() {
|
||||
checkVisibility(A::publicField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(A::internalField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(A::protectedfield.javaField!!, Modifier.PROTECTED)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class AWithCompanion {
|
||||
companion object {
|
||||
@JvmField public val publicField = "OK";
|
||||
@JvmField internal val internalField = "OK";
|
||||
@JvmField protected val protectedfield = "OK";
|
||||
|
||||
fun testVisibilities() {
|
||||
checkVisibility(AWithCompanion.Companion::publicField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(AWithCompanion.Companion::internalField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(AWithCompanion.Companion::protectedfield.javaField!!, Modifier.PROTECTED)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object Object {
|
||||
@JvmField public val publicField = "OK";
|
||||
@JvmField internal val internalField = "OK";
|
||||
@JvmField protected val protectedfield = "OK";
|
||||
|
||||
fun testVisibilities() {
|
||||
checkVisibility(Object::publicField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(Object::internalField.javaField!!, Modifier.PUBLIC)
|
||||
checkVisibility(Object::protectedfield.javaField!!, Modifier.PROTECTED)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A().testVisibilities()
|
||||
AWithCompanion.testVisibilities()
|
||||
Object.testVisibilities()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
public fun checkVisibility(field: Field, visibility: Int) {
|
||||
assertNotEquals(field.modifiers and visibility, 0, "Field ${field} has wrong visibility")
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package zzz
|
||||
import kotlin.reflect.KMutableProperty1
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class A(val s1: String, val s2: String) {
|
||||
@JvmField public var publicField = s1;
|
||||
@JvmField internal var internalField = s2;
|
||||
|
||||
fun testAccessors() {
|
||||
val kMutableProperty: KMutableProperty1<A, String> = A::publicField
|
||||
checkAccessor(kMutableProperty, s1, "3", this)
|
||||
checkAccessor(A::internalField, s2, "4", this)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A("1", "2").testAccessors()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
public fun <T, R> checkAccessor(prop: KMutableProperty1<T, R>, value: R, newValue: R, receiver: T) {
|
||||
assertEquals(prop.get(receiver), value, "Property ${prop} has wrong value")
|
||||
prop.set(receiver, newValue)
|
||||
assertEquals(prop.get(receiver), newValue, "Property ${prop} has wrong value")
|
||||
}
|
||||
Reference in New Issue
Block a user