Java Interop: apply @JvmName to getters of properties in an interface or abstract class

Usually we can write the following code in kotlin:

val hasValue : Boolean
    @JvmName("hasValue") get() = true

This will lead to the creation of a method hasValue()instead getHasValue()for Java interop .

However, in the interface this gives me a compilation error:

val hasValue : Boolean
   @JvmName("hasValue") get

The same applies to the following declaration in an abstract class:

abstract val hasValue : Boolean
    @JvmName("hasValue") get

So here is my question: how can I tell the kotlin compiler to use properties in the kotlin interfaces hasValue()instead getHasValue()for getters (and setters)?

+4
source share
1 answer

, Kotlin @JvmName open/override property/function. @JvmName on open/override jvmName / .

getter jvmName (hasValueImpl), (hasValue) :

'@JvmName'

interface Abstract {

    @get:JvmName("hasValue")        //Compile error
    val hasValue: Boolean
        get() = false
}

open class Impl : Abstract {

    @get:JvmName("hasValueImpl")    //Compile error
    final override val hasValue: Boolean
        get() = false

    @get:JvmName("hasValue2")       //Compile error if hasValue2 is open
    val hasValue2: Boolean
        get() = false
}
+1

Source: https://habr.com/ru/post/1689877/


All Articles