Lombok: Remove boilerplate code from Java source files

Photo by Clément H on Unsplash

This article is originally published at https://www.bugdbug.com/post/lombok-remove-boilerplate-code-from-java-source-files

Project Lombok is the boilerplate code generator library for Java. It autogenerates Java bytecode in .class files for different annotations used, during the build process.

It is a very popular framework among open source community and is a part of many open source projects. We can customize different annotations at the project level and define it’s behaviour so that individual developer does not have to worry about it. This also brings consistency to our code throughout the project.

Now, let's dive into its awesomeness.

First, we would need to add the following dependency in our maven project

Let’s see few widely used annotations and their vanilla Java code snippets.

1. NonNull

NonNull can be used to mark parameters that can not be null. The validation code for that is added by Lombok during compilation.

Vanilla Java

Lombok

2. Cleanup

Most of the time when we code, we forget to close resources like Connection, Inputstream, Outstream, etc. Every developer handles it differently. Sometimes, closing a resource on an exception path is missed. To avoid, all these issues Lombok’s Cleanup annotation is very useful.

Close() is considered a cleanup method by default. If cleanup is handled in some other method it can be specified as a parameter.

Vanilla Java

Lombok

3. Logger

A code to get a logger instance for the specified logging framework can be generated using a log annotation. Different annotations are provided to get an instance of different logging frameworks like log4j, log4j2, slf4j, etc.

We can also configure custom logger using CustomLog annotation.

Vanilla Java

Lombok

4.Builder

This annotation generates a builder for Java bean.

Vanilla Java

Lombok

5. Data

This annotation was introduced to create all the essentials POJO constructs, getters, setters, toString(), hashcode(), equals(), all-arguments-constructor.

Similarly, there are other useful annotations like Synchronized, Value( Immutable class).

Intellij Integration

Install the Lombok plugin to get the APIs that would be generated through the build phase.

For more features please refer official website https://projectlombok.org/features/all

Resources

  1. https://projectlombok.org/
  2. https://www.bugdbug.com/post/lombok-remove-boilerplate-code-from-java-source-files

Thanks for reading. If you find this article helpful, share it with a friend.

--

--

Technology Enthusiast | Big Data Developer | Amateur Cricketer | Technical Lead Engineer @ eQ Technologic | https://www.bugdbug.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amar Gajbhiye

Technology Enthusiast | Big Data Developer | Amateur Cricketer | Technical Lead Engineer @ eQ Technologic | https://www.bugdbug.com