Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 270
Scorpion Avatar asked Jun 15 '13 03:06

Scorpion


People also ask

How do you use Lombok to create getters and setters?

Overview. You can annotate any field with @Getter and/or @Setter , to let lombok generate the default getter/setter automatically. A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean ).

How do you use Lombok annotations?

The @Data annotation can be used to apply usefulness behind all the annotations. It means that annotate a class with the @Data annotation, Lombok produces getters and setters for every one of the non-static class fields and a class constructor. It is the same as toString(), equivalents(), and hashCode() strategies.

How do you bypass Lombok setter?

To override the access level, annotate the field or class with an explicit @Setter or @Getter annotation.

What does Lombok @data annotation do?

It generates the getter methods for all the fields. It generates the setter methods for all the non-final fields. It generates the toString() method implementation. It generates appropriate equals() and hashCode() implementations, involving the fields of class.


3 Answers

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 enter image description here - 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 enter image description here
like image 94
Gim Avatar answered Oct 20 '22 05:10

Gim


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

like image 38
Roel Spilker Avatar answered Oct 20 '22 04:10

Roel Spilker


Make sure you Restart Eclipse/Intellij or Any IDE that you are working in after adding Lombok jars and plugins.

like image 37
Aditi Pal Avatar answered Oct 20 '22 03:10

Aditi Pal