Lombok for everyone

Working in different projects gives you the opportunity to know different technologies.
The latest library I found which can help you reduce boilerplate code is Lombok - https://projectlombok.org .
As we can read in the site project:  "Boilerplate" is a term used to describe code that is repeated in many parts of an application with little alteration.  One of the most frequently voiced criticisms of the Java language is the volume of this type of code that is found in most projects. 

Simply adding some annotations to your POJO will save you from writing hundreds - literally - of lines of code.  Here is an example:

public class City {

    private String name;

    private String state;

    private String country;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

When you create a new class - and we create thousands of them - normally you add private attributes to it, and then you add the public getters & setters.
As you can see in this class I only added 3 attributes and already the lines of code are growing.
With Lombok, you can use the @Getter and @Setter annotations in the class and voilĂ 

import lombok.Getter;
import lombok.Setter;

public class City {

    @Getter
    @Setter
    private String name;

    @Getter
    @Setter
    private String state;

    @Getter
    @Setter
    private String country;


}
Do you spot the difference? With these two annotations the getters and setters will generate the methods for each field.

Another Lombok annotation very useful is @ToString : it generates an implementation of the toString() method including all non-static fields in the class. You can also add the exclude parameter if you don't want to add one or more fields to the implementation.

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString(exclude = "country")
public class City {

    @Getter
    @Setter
    private String name;

    @Getter
    @Setter
    private String state;

    @Getter
    @Setter
    private String country;


There is also a class level annotation for @EqualsAndHashCode, with an implementation that uses any non-static or transient fields in the class. It comes with the exclude parameter if you need to exclude any field.

Finally, the most frequently used annotation of them all, @Data, includes the ones I already mentioned: @Getter, @Setter, @ToString and @EqualsAndHashCode .

import lombok.Data;


@Data
public class City {

    private String name;

    private String state;

    private String country;

}


Ain't a beauty? :-)

The only thing you need to enjoy this library is adding the following to your Maven pom file.

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>0.9.2</version> </dependency>

For more information go to https://projectlombok.org  .

Enjoy !


Comments

Popular Posts