In my company, we're starting to mix Kotlin with Java and found a curious scenario. When I place a Java annotation on a property parameter of a constructor in a Kotlin class, and use a non-existent reference as one of the annotation's parameters, IntelliJ visually indicates an error, but both its build (Ctrl+F9) and maven's build compile normally, without errors.
We're using Java 8 and Kotlin 1.4.20.
Here's the annotation, declared as a Java file:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Required {
String scope() default "ABC";
}
And here's the Kotlin class using the annotation (class Abc
does not exist):
data class Test(
// Compiles normally
@Required(scope = Abc.X)
val text: String
) {
// Compilation error
@Required(scope = Abc.X)
fun x() {
}
}
As mentioned in the code comments, the same annotation placed in a Kotlin function behaves as expected (that is, the code does not compile). An equivalent annotation declared as a Kotlin file also behaves as expected.
When the code is run, the scope
variable assumes its default value, so there are no runtime errors.
I've already tried to:
- Invalidate IntelliJ cache and restart.
- Switch the annotation declaration from
@Required(scope = Abc.X)
to@field:Required(scope = Abc.X)
I also tried to replicate the behaviour in a brand new project without inheriting from the company's base maven project, but to no avail.
Honestly, I think there's a huge possibility that it is something related to the company's project. I know I haven't specified what my company uses and all the configuration (in fact, the question would get way too big if I did that), but I hope that even with just the basic problem someone may be able to help.
@Target(FIELD)
, but on closer inspection I needed to add it to cause the bug. I'm gonna update the question to consider this. Also, I changed thescope
property to a non-existent one, as mentioned in the bug, and it does compile. If you want to post your answer, I'll gadly accept it! – Flumen