Why Eclipse needs to be configure for Lombok? - java

In my Java project, i use Lombok tool to generate getter and setter.
It works perfectly with Maven, without any configuration.
For what reason i need to configure Eclipse for Lombok ?
I already know how to configure Eclipse : https://projectlombok.org/download.html
Why i have to add the following line in my eclipse.ini ?
-javaagent:lombok.jar

The Lombok site states:
Lombok copies your source files to another directory,
replacing all lombok annotations with their desugared form.
So configuring is done just because of source code modification. Without "javaagent" would be difficult.

Lombok uses Java annotation processor. So, irrespective of which IDE you use, if Lombok's jar is available for javac, your getters and setters get generated and all other functionality of Lombok works as expected.
Maven adds dependent jars (including Lombok's) to -classpath parameter of javac, so you wouldn't need a Java agent to instrument the code for you.

Related

How to see lombok source code in NetBeans?

I have the Lombok plugin setup in NetBeans IDE and my code builds fine. I can see the Lombok generated methods in the structure view. What I want is some way to actually see the source Lombok generates for each of the methods. I realize I can delombok the file, but I'm wondering if there is some way I can flip a switch to show or hide the Lombok generated code (How to delombok in NetBeans?).
Procedure to see the source code of Lombok methods.

How to get lombok generated source to be visible in eclipse/maven?

I am attempting to use lombok (https://projectlombok.org/) to help with "boilerplate" code generation. I have added the maven dependency and have also added the lombok maven plugin to the <build> element in the POM.
One of the things I was also told to do was to place the lombok-annotated code in src/main/lombok instead of src/main/java. I assume that I keep the package directory structure the same as in src/main/java.
This seems a little odd, but I did this and lombok did generate source files with all the getter/setter/equals/hashCode etc methods. They are placed in e.g. target/generated-sources/lombok/x/y/z/MyClass.java. This is kind of what I would expect having used other code generation tools like XJC.
My question is: how do I get this generated source to be "visible" to eclipse and the compile environment? I'm getting compile errors all over the place because it can't "see" the generated code. This occurs in eclipse as well as when I attempt to do a mvn clean install.
UPDATE:
I have already tried installing lombok into eclipse. It generates the getters/setters/etc, but there is a bug introduced somewhere that breaks the auto-complete feature of eclipse. I removed it from eclipse and am just trying to get maven to do the generation (or delombok-ing).
I've just installed Lombok (after a while) in my new Eclipse (STS) installation, and i was a bit "annoyed" by the lack of generated sources.
I found a hint however on this page that the generated structure is visible in the "Outline" view. (Look for the screenshots)
If you don't have Outline view open yet, enable it in the menu: Window / Show view / Outline.
Download Lombok jar (https://projectlombok.org/download) and execute it java -jar lombok.jar and select your eclipse installed folder.Restart eclipse and rebuild project.
To verify Lombok installation, please check Help -> About eclipse. Lombok entry show show at bottom.
You have to enable annotation processing for Eclipse to be able to pick up generated code.
Right-click on the project and select Properties.
Open Java Compiler and then Annotation Processing. Check Enable
annotation processing.
Open Java Compiler -> Annotation Processing -> Factory Path. Check
Enable project specific settings. Add your Lombok JAR file to the list.
Clean and build the project.
My problem was using full namespace annotation: #lombok.Getter, and I resolved it by doing import lombok.Getter and do #Getter

Why IntelliJ needs Lombok plugin?

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
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.
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.
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.

Problems compiling AutoValue library example project

I just discovered google's AutoValue library, which seems great.
I'm trying to compile the example project which I downloaded from here
But the project doesn't compile.
The compiler complains about the AutoValue_ constructor symbol not being recognized.
Can someone explain what I am doing wrong?
I am using auto-value-1.3.jar
Thanks
IntelliJ does not enable annotation processors by default.
You can follow the official guide. You need to go to the Annotation Processors page and click + to create a new profile. Then you have to associate your module with this profile. In the end you only need to enable annotation processing for this profile and everything should work.
The IntelliJ editor might not find the generated class files though and still display the name red. But it should not affect the build.

Custom Annotation Processing, Generating Documentation, and Maven 3

My goal is to "read" the Java classes of a certain package, then to process the Javadoc and annotations (preferably at the same time) on the classes within that package and on the methods within those classes. Solution must be implemented in Java 6 or 7, build tool is Maven 3. We're currently using the maven-compiler-plugin, version 2.5.1, but I can probably upgrade that further if available/necessary.
As I understand it, the purpose of the javax.annotation.processing classes are to do this kind of thing, and I believe Java code along the lines of this other SO answer should work for my purposes, but the practical details of how to get it actually running are still a little fuzzy.
All that being said, here's what I think I need:
Java code to pick out the annotations and Javadoc items that I want, and then convert those items into the data model needed to create my custom documentation.
Java code to then write this data model out to a docs file or directory of files
Maven 3 configuration to:
Run the annotation processor once at a good time
Include the generated docs directory in the outputted war file
The Maven bits trip me up more than the Java code, so if you're only going to answer half, that's the half that'll get my check mark. Also, my preference would be to not put this annotation processor into a Maven repository as a separate plugin since it will be very tightly coupled with some custom annotations we're using.
Here's a brief listing of questions that I found as related from which I could not synthesize my own answer, though:
Writing an annotation processor for maven-processor-plugin
Maven annotation processing with maven-compiler-plugin
How to configure the Annotation Processing API without external Jar using Maven?
Can the Pluggable Annotation Processor API retrieve source code comments?
I'm not sure if you can access the Javadoc from annotation processors. Consider to use the Doclet API:
http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/doclet/overview.html
Edit:
Here is an annotation processor I wrote. Maybe it can serve as a sample:
http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/ae786193023d/softsmithy-lib-core/src/main/java/org/softsmithy/lib/util/impl/ServiceProviderAnnotationProcessor.java
http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/ae786193023d/softsmithy-lib-core/pom.xml
http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/softsmithy-parent/main-golden/file/9397853ba514/pom.xml
For Maven: as far as I can remember you have to pass -proc:none as compiler argument (compiler plugin) in the project that contains the annotation processor. In that project also add the following resource file: META-INF/services/javax.annotation.processing.Processor and add the fully qualified name of your annotation processor in the first line.
I usually package the annotation, the annotation processor and this "service registry file" in the same project A. Then in any other project B where I'm using the annotation and thus have a dependency on this project A, the annotation processor is picked up automatically.
I'm using the Maven Compiler Plugin v2.3.2 and Java SE 7.

Categories

Resources