Lombok Annotations

Lombok Annotations
Project Lombok

Why Lombok?

Java is an object oriented programming language. When using Java, it is inevitable not to break down your code into classes and interfaces but that comes with the fact that you would need to write down a lot of boilerplate code in order for your code to be compiled correctly to bytecode understandable by the JVM.

To avoid writing this biolerplate code and reduce maintenance overhead, project Lombok provides a number of annotations. These annotations would make your code base:

  • Easily maintainable.
  • More readable.
  • Written with much less repetitive work.

Lombok Annotations

Project Lombok provides several annotations. Some of these annotations are part of the stable features that the project provides and some are experimental annotations.

Let's go through the list of the stable annotations and talk about them later on one by one:

  1. @NoArgsConstructor
  2. @RequiredArgsConstructor
  3. @AllArgsConstructor
  4. @Getter/@Setter
  5. @ToString
  6. @EqualsAndHashCode
  7. @Data
  8. @Value
  9. @NonNull
  10. @Builder
  11. @With
  12. @Getter(lazy=true)
  13. @Log
  14. @SneakyThrows
  15. @Synchronized
  16. @Cleanup

Project setup

In order to explain more about Lombok, I have created a simple maven project in Intellij IDE. I am using the latest Java LTS version at the time of writing this article and that is JDK 17. Lombok recently released a new version that supports Java 17 which is 1.18.22 so make sure to add it to the pom.xml file of your maven project.

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

If you are using a new version of Intellij, then the Lombok plugin and the annotations processor is already configured and no further action is required. That means the compiler will identify the annotations and process them in the correct way.
What we will do in the next sections is to add the annotations into our Java classes and compare the .java files with the compiled bytecode .class files.


Continue reading

In order to keep the scope of this article as narrow as possible , I have divided the article into several pages for each Lombok annotation.

Please find below the links:
https://blog.emsn-software.nl/lombok-noargsconstructor/

https://blog.emsn-software.nl/lombok-requiredargsconstructor/

https://blog.emsn-software.nl/lombok-allargsconstructor/

https://blog.emsn-software.nl/lombok-xargsconstructor-advanced/

https://blog.emsn-software.nl/lombok-getter-setter/

https://blog.emsn-software.nl/lombok-tostring/

https://blog.emsn-software.nl/lombok-equalsandhashcode/

https://blog.emsn-software.nl/lombok-data/

https://blog.emsn-software.nl/lombok-notnull/

https://blog.emsn-software.nl/lombok-cleanup/

https://blog.emsn-software.nl/lombok-value/

Update cookies preferences