In this post, I will show you how to generate getter methods, setter methods, and constructors automatically in Java projects using Project Lombok annotations - @Getter, @Setter, @NoArgsConstructor, and @AllArgsConstructor.
Project Lombok is a very useful tool for Java projects to reduce boiler-plate code so in this example I will demonstrate how to reduce boilerplate with an example.
Project Lombok Maven
- Create a simple maven project using - How to Create a Simple Maven Project in Eclipse article.
- Add the below dependency in your maven project pom.xml file:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
Adding the Lombok Plugin in IDE (Eclipse)
In order to see Here are the installation steps for Windows:
- Downloaded jar from https://projectlombok.org/download or use the jar which is downloaded from your maven build.
- Execute command in terminal: java -jar lombok.jar
- This command will open window as shown in the picture below, install and quit the installer and restart eclipse.
Without Project Lombok
Let's first look at a sample of code which has a boilerplate code. Let's create a User class with getter methods, setter methods, and constructors:
package net.javaguides.lombok;
/**
*
* @author Ramesh Fadatare
*
*/
public class User {
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
public User() {}
public User(long id, String firstName, String lastName, int age, String gender) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
With Project Lombok
With Project Lombok, we can remove getter methods, setter methods, default constructor and parameterized constructor in User class by adding by @Getter, @Setter, @NoArgsConstructor, and @AllArgsConstructor:
package net.javaguides.lombok.constructor;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class UserLombokModel {
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
}
Testing
As we installed Lombok plugin in eclipse so we can test Project Lombok annotations. Let's create a main() method and test Lombok annotations.
package net.javaguides.lombok.constructor;
public class LombokTest {
public static void main(String[] args) {
// test constructor
UserLombokModel lombokModel = new UserLombokModel(100, "Ramesh", "Fadatare", 28, "Male");
System.out.println(lombokModel.toString());
UserLombokModel lombokMdl = new UserLombokModel();
// test getter and setter methods
lombokMdl.setId(100 L);
lombokMdl.setFirstName("Ramesh");
lombokMdl.setLastName("Fadatare");
lombokMdl.setAge(28);
lombokMdl.setGender("Male");
System.out.println(lombokMdl.getId());
System.out.println(lombokMdl.getFirstName());
System.out.println(lombokMdl.getLastName());
System.out.println(lombokMdl.getGender());
System.out.println(lombokMdl.getAge());
}
}
Note that we have tested getter methods, setter methods, default constructor and parameterized constructor in the above example.
Output
We can declare getter and setter methods on only for some properties by using @Getter, @Setter annotation for those properties. For example, If we only need getter, setter method for property firstName, lastName in User class, we can declare as below:
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class UserLombokModel {
private long id;
@Getter
@Setter
private String firstName;
@Getter
@Setter
private String lastName;
private int age;
private String gender;
}
Reference
GitHub Repository
You can view the source code of this article on my GitHub repository at https://github.com/RameshMF/project-lombok-tutorial
Comments
Post a Comment
Leave Comment