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.
- 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
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Kotlin, 243.23126-IJ |
Thanks for your feedback!