Public fields for Java compatibility

老子叫甜甜 提交于 2020-12-04 16:00:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!