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.
Related
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 am trying to simplify my Java code by the use of groovy. At the moment I am struggling with the use of the #Builder annotation. I have a groovy like the following one….
import groovy.transform.builder.Builder
#Builder
class TheFancyOne {
String message
}
From Java I want to use the builder like…
TheFancyOne.builder().message("Hello World!").build();
The problem is, that only the getter and setter of the “TheFancyOne” is visible but no builder.
As a side note I am unsing groovy 2.4.5 and Eclipse (Neo with Groovy Eclipse)
--- edit ---
The builder is visible within groovy and it is also in the resulting jar (checked by decompiling). So it seems to be an eclipse plugin issue
Any idea?
Regards,
Thomas
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 have included Project Lombok in my Gradle dependency like this:
provided 'org.projectlombok:lombok:1.16.0'
I have annotated all my getters and setters with
#Getter #Setter
But now I cannot build my project at all. I am getting errors like this:
Error:C:\Users\Igor\Workspace\Geofencing\libraries\my-sdk\src\main\java\com\sdk\MyMonitor.java:93: The method getLatitude() is undefined for the type Geofence
But I know for a fact that getLatitude is defined by Lombok in my Geofence.java class!
I also have missing getters and setters in a Spring Data project. I fixed the problem by downgrading Lombok to version 1.14.8, see http://mvnrepository.com/artifact/org.projectlombok/lombok
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.