Why IntelliJ needs Lombok plugin?
Asked Answered
T

3

8

As far as I understand, Lombok uses Java's Annotation Processors to generate additional methods.

With Maven 3.5 it works perfectly without adding any additional configuration, just add dependecy to Lombok and put some annotations like @Getter, @Setter.

However, if I open this project in IntelliJ IDEA 2018.2, all usages of generated getters/setters are highlighted as errors. I have Annotation Processing turned on, I tried to built project in IntelliJ or build in Maven and then use in IntelliJ, but it still requires Lombok Plugin to avoid false errors.

Is it some kind of bug? Error in workflow? Or maybe Lombok is using not only Annotation Processors, but some other stuff I didn't know and that's why IntelliJ + javac cannot figure out how to deal with it? It would be strange as javac itself compiles those files without errors

I know there are many questions "I have errors while using Lombok" and answers like "use the plugin". I'm not asking if I should use plugin, but why I should use it, why IntelliJ cannot handle it without plugin while javac does

Taber answered 30/8, 2018 at 9:3 Comment(1)
Lombok does do dirty evil magic that can't always be processed the same way as other annotation processors.Beetner
T
15

IntelliJ's code analysis engine does not use javac or run annotation processors. Instead, IntelliJ uses its own Java parser and reference resolution logic, and builds its own code model. The Lombok plugin extends the code model to provide information about declarations generated by the Lombok annotation processor.

Tulley answered 30/8, 2018 at 9:9 Comment(3)
That makes sense. But IntelliJ has an option "Annotation Processors" -> "Enable annotation processing". It seems like this option does nothing expected, it does not run annotation processors when building with internal compiler. Am I right? I'm asking for clarification, maybe it's a good idea to open ticket for JetBrains TeamMidsummer
There is no "internal compiler". IntelliJ does not generate any bytecode when performing its code analysis. The option enables running annotation processors when you actually run a build from within IntelliJ (which uses the regular javac compiler).Tulley
Ok, got it. Thank you! :)Midsummer
J
3

It's because IDEA syntax highlighter uses internal Java parser. If IDEA used just javac, then it wouldn't be able to highlight syntax errors as you type. It also gives much better hints about wrong code, so each Java construct, feature or annotation must be implemented by JetBrains team or there's plugin for it like in this case.

Annotation processing option is just for building project which is done via javac, but it's not for syntax highlighting.

Jamieson answered 30/8, 2018 at 9:10 Comment(0)
G
2

Ale IDEs use lombok plugin be it intelij-idea or eclipse. javac works fine with it - but remember that it works when for example you build you project with mvn clean package. Then when you have your IDE - it works differntly - the code is not processed like in build task.

The plugin make it know to IDE what is this annotation and what code it generates underhood without need of javac.

Gaylordgaylussac answered 30/8, 2018 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.