问题
I found recent interest in Kotlin as a language, because the platform we develop for is Java 6 based and hence lacks any syntactic sugar the recent years brought to Java.
There's but one thing that makes it impossible to use Kotlin over Java in development, that is, the platform we develop for uses some reflection internally and requires members to be public. It won't work otherwise.
So, the bytecode generated from the Kotlin file in fact produces public getters and setters, the fields themselves yet remain private.
Is there a way to overcome this, so I get real public fields?
I am aware of the design failure that requires public fields, but the system is kind of a black box to us, we cannot change the fact that it has to be this way.
回答1:
The annotation @JvmField
should help you. It makes the Kotlin-compiler expose the property as a field on the JVM. See here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-field/
来源:https://stackoverflow.com/questions/41039875/public-fields-for-java-compatibility