Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin doesn't see Java Lombok accessors?

Using Kotlin 1.0.0 release (compiling in IntelliJ 15).

println(myPojoInstance.foo) 

When it tries to compile code (in IntelliJ or Gradle) that references Lombok based POJOs it gives the error "Cannot access 'foo': it is 'private' in "MyPojo". Which is true, they're all private and my object has @Value @Builder for lombok annotations.

I've tried specifically calling getFoo(), but it says "unresolved reference for getFoo". There's perhaps some trick to make Kotlin aware of how to handle the lombok annotations?

like image 457
Chris Kessel Avatar asked Feb 20 '16 00:02

Chris Kessel


People also ask

Does Lombok work with Kotlin?

The Kotlin Lombok compiler plugin allows the generation and use of Java's Lombok declarations by Kotlin code in the same mixed Java/Kotlin module. If you call such declarations from another module, then you don't need to use this plugin for the compilation of that module.

Which Lombok is compatible with Java 11?

Latest version of Lombok and/or IntelliJ plugin perfectly supports Java 11.

What is Delombok?

Delombok's standard mode of operation is that it copies an entire directory into another directory, recursively, skipping class files, and applying lombok transformations to any java source files it encounters. Delombok's output format can be configured with command line options (use --format-help for a complete list).

How does Lombok work in Java?

Lombok acts as an annotation processor that “adds” code to your classes at compile time. Annotation processing is a feature added to the Java compiler at version 5. The idea is that users can put annotation processors (written by oneself, or via third-party dependencies, like Lombok) into the build classpath.


1 Answers

Generally, no, it doesn't. The reason of that behavior is that Lombok is an annotation processor for javac but when the kotlin compiler runs it uses javac as well but with no annotation processing so this is why kotlin don't see declarations that wasn't yet generated.

The only workaround for now is to define strict compilation order: Java first and after that kotlin. Unfortunately this approach has great disadvantage: you can't use Kotlin code from Java in this case. To workaround it you may need multimodule project that may cause a lot of pain

like image 56
Sergey Mashkov Avatar answered Sep 19 '22 21:09

Sergey Mashkov