简体   繁体   中英

How to actually make lombok annotations work for getters and setter

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

添加 Lombok jar 和插件后,请确保重新启动 Eclipse/Intellij 或您正在使用的任何 IDE。

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 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM