Lombok - solution for getter and setter boilerplate code

Lombok - solution for getter and setter boilerplate code Have you wondered why some programming languages die and some other appear? Probably there are many explanations but the one that is noticeable from the first glance is simplicity. The least effort is required to build an application the better. Java is quite good in this area, but experienced software developers complain about writing getter and setter methods that are undoubtedly straightforward.

getterSetterHave you wondered why some programming languages die and some other appear? Probably there are many explanations but the one that is noticeable from the first glance is simplicity. The least effort is required to build an application the better. Java is quite good in this area, but experienced software developers complain about writing getter and setter methods that are undoubtedly straightforward. Project Lombok is an answer for those complains.

Lombok introduces a few annotations that inject boilerplate code that can be used at development time instead writing it on our own.

Add Lombok to the project

To get started, add Lombok library to the project. If you use Gradle, add the lombok dependency in build.gradle:

dependencies {
...
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
...
}

and refresh the Gradle project in your IDE.

 

Install Lombok plugin

Your IDE has to know that getters and setters code will be generated to not report them as missing at development time. To make sure the IDE is aware of Lombok, a plugin has to be installed. If you use IntelliJ IDEA, follow the below steps:

  1. Go to File -> Settings -> Plugins.
  2. Click Browse repositories.
  3. Find Lombok Plugin and choose Install.
  4. Restart the IDE.

 

Use @Getter, @Setter

After all above steps, you do not need obvious getter and setter methods. It is enough to add proper annotations on the class level like in the following example:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ServiceDTO {

private String name;
private int duration;

public static ServiceDTO fromService(Service service) {
ServiceDTO dto = new ServiceDTO();
dto.name = service.getName();
dto.duration = service.getDuration();
return dto;
}

}

If @Getter/@Setter annotations are set at the class level as above, public getter/setter methods are added for all fields. In this case the following methods are automatically generated:

  • public String getName()
  • public void getName(String name)
  • public int getDuration()
  • public void setDuration(int duration)

 

This is not the only option. Those annotations can be put on the field level also. Additionally, access level can be specified if other then public is required.

public class ServiceDTO {

@Getter
private String name;

@Setter(AccessLevel.PROTECTED)
private int duration;

public static ServiceDTO fromService(Service service) {
ServiceDTO dto = new ServiceDTO();
dto.name = service.getName();
dto.duration = service.getDuration();
return dto;
}

}

The above code has the following methods generated:

  • public String getName()
  • protected void setDuration(int duration)

 

More information about Lombok Project and @Getter/@Setter annotations can be found on the project website:

This functionality is not revolutionary but it undoubtedly speeds up development.

 

If you like what I do, consider buying me a coffee :)

Buy me a coffeeBuy me a coffee