Lombok with Java for everyday use — Part 1 !!!

Athul RAVINDRAN
3 min readJan 20, 2020

--

Lombok is a poweful open source java library which could save a lot of time for a developer by taking away the joy of writing tons of boilerplate code.

I have been using Lombok for about 5 years now and I cannot imagine writing such boilerplate code without lombok although some of it could be handled by the IDE.

Lombok works entirely based on annotations, applied at class level, method level, constructor level and even at the variable level. One annotation could do so much magic !!

Lets start looking at some examples…

See below picture. Even though IDEs can do most of it for you, Lombok makes it even more easy. I like how a Lombok enabled class looks, clean, neat and easy for eyes.

Left: Person Class with no Lombok, Right: Same Person class with Lombok
Left: Person Class with no Lombok implementation. Right: Same Person Class with Lombok
  1. @Getter and @Setter — The most basic and popular annotations I would say. Lombok generates getters / setters and handles it in the background for all variables marked with @Getter and @Setter. You can also use this annotation at the class level and in that case, lombok generates getters and setters for all non-static members of the class.

Things to note :

a) Lombok will not generate getters / setters for a variable, if you have already generated or written one manually, agnostic of the number of parameters.

For example: getFoo(String a) will prevent Lombok from generating getFoo() for member foo. To let lombok ignore your manually written getter method and have Lombok do its getter/ setter job for a variable, add @Tolerate to the already implemented getter or setter.

@Getter
@Setter
@ToString
@Slf4j
public class PersonLombok {

private String firstName;
private String lastName;
private int age;

@Tolerate
public String getLastName(String firstName) {

if(firstName == null)
{
return "FNU" + lastName;
}
else
{
return lastName;
}
}

public void exampleOfLogging()
{
log.info("Lombok - Spice up your java !!");
}
}

b) All Getters and Setters are generated with access level — public. You could override the default behavior by using something like this @Getter(AccessLevel.PROTECTED)

2) @ToString — Using this annotation at class level will override the default toString() method and lombok will generate code to print comma separated value of all the non-static member variables in the order defined.

You can spice up the behavior by adding @ToString(includeFieldNames=true) and lombok will print the toString output by adding the variable name as well to the print statement making to more readable “variableName = variableValue”. This will give you a lot of clarity.

Things to note:

a) You can always exclude any non static member from getting printed by adding @ToString.Exclude to the member variable.

b) Another trick is to let Lombok know to include only fields marked as @ToString.Include and annotate the class with @ToString(onlyExplicitlyIncluded = true)

3) @Slf4j — Annotating a class with slf4j will let lombok generate a final static instance of logger with Slf4j implementation which you can use to print log statements. Lombok also provides other implementations like Log4j2, JBossLog, Log4j, Xslf4j, Flogger and CustomLog.

4) @NonNull — Non Null is used for annotating a method’s parameter to indicate Lombok to perform null check before any statement using that parameter is executed and throw a null pointer exception otherwise.

public class LombokNonNull {
private String name;

public NonNullExample(@NonNull Person person) {
/*
lombok will perform a null-check here and throw a NPE if person
is null
*/
this.name = person.getName();
}
}

5) @Cleanup — I love this one. Another powerful annotation. When you use any resources such as InputStream, Readers or any resource that requires cleaning up of the resource explicitly using try/finally block can be handled elegantly with this annotation. Annotating any resource with @Cleanup will be taken care by Lombok behind the scenes.

Thank You !! Few more important annotations like @AllArgsConstructor, @NoArgsConstructor, @Data, @Builder and @UtilityClass will be addressed in Part 2

--

--