I'm trying to lombok for the first time. I tried to follow directions as best as possible, but when I look at my compiled classes (using a decompiler) they do not have any of the generated getters or setters.
My installation steps:
Downloaded lombok 1.14.8 and ran java -jar lombok.jar. It added lombok to eclipse. Restarted Eclipse (-clean the workspace too). If I check my About Eclipse page, I see:
"Lombok v1.14.8 "Branching Cobra" is installed. http://projectlombok.org/"
Added lombok to my pom.xml:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>1.14.8</version>
</dependency>
Maven->Update Project. Project->Clean
My Lombok'ed java class:
import lombok.Getter;
import lombok.Setter;
public class User extends BaseCouchDbDocument {
public User() {
// TODO Auto-generated constructor stub
}
#Getter #Setter
private String name;
}
When using the code completion in Eclipse, I see User.getName() and User.setName() appear. However, if I try to use the getters or setters, I get a compile time error that no such method exists. When I look at the generated .class file, I only see the following:
public class User extends BaseCouchDbDocument
{
private String name;
}
Similarly, if I run mvn compile from the command line, I get the same class output.
What I find odd is that the #Getter and #Setter annotations are removed, implying that there is some processing occurring on my files. But the getters/setters aren't being generated.
Am I doing something wrong? I'm using Java 7 on a Mac.
After posting this, I ran across a bug report that indicated it was a problem with AspectJ.
Indeed, I am using AspectJ with my project, and it is causing conflicts with Lombok. Removing AspectJ now shows properly generated setters/getters.
This obviously does not "resolve" the issue, but at least points me in the right direction. I created another issue here to track this specific problem.
Hopefully this can help someone else in the future as well.
Related
I have 2 classes in a Eclipse project.
package com.example;
import lombok.Getter;
import lombok.experimental.Accessors;
#Accessors(fluent = true)
#Getter
public class MyBean {
private String value = "aaa";
}
package com.example;
public class MyClass {
public static void main() {
System.out.println(new MyBean().value());
}
}
After building, Eclips reports "the value of the field value is not used" for MyBean class and "The method value() is undefined for the type MyClass" even though Eclipse's content assist shows MyBean#value() method, there are no warnings before building, and compilation, execution both are finished successfully.
So I guess Lombok works properly and the problem is Eclipse doesn't recognize Lombok.
Here are what I tried and found.
attaching lombok.jar to Eclipse and checking eclipse.ini
cleaning and rebuild the project
removing Lombok dependency from maven local repository then reinstall
the problems only occur in this project. Eclipse handles other projects with lombok properly (and all projects belong in the same workspace).
I cannot reproduce these problems in other projects.
Any ideas?
Finally, the problems are solved by just creating new project and moving all sources and resources to it.
It seems that Eclipse's project configurations were broken I don't know why.
I install lombok on eclipse(java -jar lombokxxxx.jar) and the problem disapear.
Lombok API Configuration
Note: Lombok version may change. Present we are using 1.18.24
Goto -> C:\Users\Udaykiran.Pulipati.m2\repository\org\projectlombok\lombok\1.18.24
A Lombok configuration window will open > Click on Specify location button
Select eclipse.exe root folder where it is located(installed)
Selecting eclipse.exe root folder > D:\eclipse\jee-2021-06\eclipse
Click on Install / Update button in Lombok configuration window
Click on Quite Installer
Restart Eclipse IDE
I am not able to use any of the lombok annotations in Intellij, it works fine in Eclipse.
So far, I have done the following things:
Added lombok dependency in eclipse
Installed the lombok plugin
Enabled annotation processing
However, I cannot use any of the lombok annotations, eg: using #Builder gives error because import lombok.Builder does not exists.
I am using IDEA 2018.2.1 CE
Any ideas, what am I doing wrong?
MVN dependency:
Can see the dependency resolved:
Can see the lombok plugin:
Annotation processing enabled:
EDIT:
Following code gives an error, basically i cannot use import lombok because somehow I lombok is not available:
import lombok.Builder //Error, Cannot resolve Builder
#Builder //Gives error, cannot resolve symbol Builder
public class Employee{
private int id;
private String name;
}
From your images, it seems like you have submodule temp, it can be the problem
The pom.xml is for project buildertest, not temp
If you want to have submodule, you should also set it as Maven project and have another pom.xml
I'm trying to run sample Spring boot application and I'm facing problem with entities that are marked as #RequiredArgsConstructor on my IDE. I'm using latest intelliJ IDEA (14.1) over java 1.8. There's an error marked on IDE when I tried to initialize the entity with constructor arguments.
E.g. It would be showing "cannot resolve symbol" for following line.
itemRepository.save(new Item("MacBook Pro"));
My entity would be as follows.
#Entity
#Data
#RequiredArgsConstructor
public class Item {
private #Id #GeneratedValue Long id;
private final String description;
Item() {
this.description = null;
}
}
Apart from IDE error project builds and runs properly.
The sample project you are running uses Lombok, a library which can generate a lot of boilerplate code for you (such as getters and setters) based on annotations (e.g. #RequiredArgsConstructor). This is useful, but because the code is generated during compilation, the IDE doesn't see it and therefore shows errors.
You must install Lombok plugin to make IntelliJ aware that the constructor actually does exist, but is generated during compilation. Then the errors will go away.
You can also take a look at this post for more details about how Lombok works under the hood.
I'm using Lombok to add logging to my Java applications. I've been using this for some time, but since I upgraded my IDE from IntelliJ 13 Community edition to 14 Ultimate, I get the following compile errors (using maven):
error: log has private access in SesameServer
This error originates in a subclass of SesameServer:
#Slf4j
public class AnnotatorServices extends SesameServer {
#Override
protected void initialiseRDFStoreManager(Series<Parameter> parameters) throws RepositoryConfigException, RepositoryException {
log.info("<< ------ Annotator Services ------- >>");
}
}
Of course SesameServer also uses the #Slf4j annotation to add logging. The #Slf4j annotation adds the line:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SesameServer.class);
to the class in which it is used (both SesameServer and AnnotatorServices where SesameServer.class is of course replaced by AnnotatorServices.class).
Apparently the compiler thinks I want to use the log variable from SesameServer instead of AnnotatorServices (both classes have variables with the same name).
How can I prevent this problem, i.e. that the SesameServer.log is used instead of AnnotatorServices.log?
NOTE: This still comes up for anyone else googling this error message; so adding my fix hope this helps.
I had a similar issue after reopening a project from pom.xml.
Since my parent class SesameServer was already built in a different module and used a dependency; when I compiled my AnnotatorServices I also saw the error: log has private access in SesameServer
To fix it I just had to turn on Annotation Processing for the module containing AnnotatorServices.
In IntelliJ Community 2017.1.6 the check box was found under:
File -> Settings
"Build, Execution Deployment -> Compiler -> Annotation Processors"
Tick the "Enable annotation processing"
Following steps worked for me
Install Lombok Plugin in Settings=>plugins under Marketplace search for Lombok and install.
Then in IntelliJ Community 2017.1.6 go to File->Invalidate Caches/ Restart and click on both.
Did the trick for me. All the best.
Make sure your subclass is also annotated properly with #Slf4j.
Update your lombok plugin. Sometimes idea does not display the new updates so goto settings => plugins and search for "lombok"
Click "update" and restart idea
I am trying to use lombok getters and setters annotations.
As far as I know the annotated code is generated at runtime and not compile time so how can take the help of autogenerated getters and setters for writing code?
for example I have a class like this
#lombok.Getters
#lombok.Setters
public class MyLombokTesting {
private String userName;
}
but what is the use of such annotations if these are not generated while writing code...
Now I want to do something like this
MyLombokTesting myLombokTesting = new MyLombokTesting();
String username = myLombokTesting.getUserName();
or myLombokTesting.setUserName("some username");
but I can't do any of these since there are no setters and getters generated for me in eclipse while writing code..
Obviously I can have a one argument constructor to set the username but what about getter?
First of all, Lombok does run compile time to change the generated class file on the fly.
Possibly, lombok is not correctly installed in your Eclipse. See this answer on lombok installation problems in Eclipse.
Furthermore, the runtime processing of annotations is not the only use for them. Java 5 already shipped with apt, the Annotation Processing Tool and since java 6 annotations can be processed by the standard compiler (javac). Annotations can generate class files, source files or other resource files.
Disclosure: I am one of the Project Lombok developers
Make sure you Restart Eclipse/Intellij or Any IDE that you are working in after adding Lombok jars and plugins.
I use Eclipse IDE. I have to install lombok in my computer before adding lombok library in pom.xml
Step 1:
- Download lombok at https://projectlombok.org/download
- Run command java -jar
- Restart your IDE
- Finish
Step 2:
- Add lombok to pom.xml file
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
call getter / setter in your code