7

I am testing a new kotlin-allopen and kotlin-spring plugins under Kotlin 1.0.6.

In one of my @Transactional-annotated classes I have a field:

@JvmField val foo = null

When I try to build the project, I get:

Error:(45, 5) Kotlin: JvmField can only be applied to final property

Is there any proper way of dealing with this? My real-life code needed @JvmField because of the JUnit's @Rule. Managed to "solve" the problem by removing a @JvmField and annotating a getter instead. Not sure if a bug or a feature.

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • I see you submitted a bug in tracker, so it should be here https://youtrack.jetbrains.com/issue/KT-15541 – Ruslan Jan 04 '17 at 09:14

1 Answers1

12

I got the official solution.

In such case, finality provided by val is not enough. It turns out you need explicitly add final keyword there and this is not considered a bug.

@JvmField final val foo = null
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • How is this not a bug? If we put the compiler aside, it's a but in the IDE at least because the message is not clear enough. I had no IDEA (pun intended) what to do with it. For normal JUnit tests it works fine, but for Spring (for example) it throws an error... – milosmns Apr 08 '18 at 22:50
  • I meant to say "*bug* in the IDE", lol. Anyway, I'm following the issue now. Thanks! – milosmns Apr 13 '18 at 15:56