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

Example:

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.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

FakeJvmFieldConstant
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Kotlin | Java interop issues

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

Kotlin, 242.22892-IJ

Last modified: 11 September 2024