Inspectopedia Help

Kotlin non-const property used as Java constant

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).


Kotlin code in foo.kt file:

annotation class Ann(val s: String) @JvmField val importantString = "important"

Java code:

public class JavaUser { // This is dangerous @Ann(s = FooKt.importantString) public void foo() {} }

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

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3


Kotlin, @snapshot@

Last modified: 13 July 2023