Inspectopedia
 
2024.3

Kotlin non-const property used as Java constant

Warning
New
Last modified: 03 December 2024

Reports Kotlin properties that are not const and used as Java annotation arguments.

For example, a property with the @JvmField annotation has an initializer that can be evaluated at compile-time, and it has a primitive or String type.

Such properties have a ConstantValue attribute in bytecode in Kotlin 1.1-1.2. This attribute allows javac to fold usages of the corresponding field and use that field in annotations. This can lead to incorrect behavior in the case of separate or incremental compilation in mixed Java/Kotlin code. This behavior is subject to change in Kotlin 1.3 (no ConstantValue attribute any more).

Example:

Kotlin code in foo.kt file:

Java code:

To fix the problem replace the @JvmField annotation with the const modifier on a relevant Kotlin property or inline it.