Project Lombok

Table of Contents

Intro

Project Lombok is a Java library that provides a set of annotations and utility classes that help to reduce boilerplate code in Java projects. It offers features such as automatic generation of getters, setters, equals, hashCode, toString, and constructors, as well as support for logging, fluent API creation, and more.

By using Lombok annotations, developers can focus on writing the core business logic of their code and let Lombok generate the repetitive, boilerplate code for them. This reduces code verbosity and can lead to improved code readability and maintainability.

Lombok is widely used in the Java community and can be integrated into popular IDEs such as Eclipse, IntelliJ, and NetBeans. It is also compatible with popular build tools such as Maven and Gradle.

Overall, Lombok is a powerful tool for Java developers who want to increase productivity and reduce the amount of repetitive code in their projects.

How does it work?

  • Hooks in via the Annotation processor API

  • The AST (raw source code) is passed to Lombok for code generation before java continues.
  • Thus, produces properly compiled Java code in conjunction with the Java compiler
  • NOTE: Code is generated and complied. No run-time performance penalty.
  • If you write an implantation of a method Project Lombok would generate, your code is used
  • Make it easy to override Lombok generated code

Features

  • val - declares final local variable
  • var - declares mutable local variable
  • @Getter
    • Creates getter methods for all properties
  • @Setter
    • Creates setter for all non-final properties
  • @ToString
    • Generates String of classname, and each field separated by commas
    • Optional parameter to include field names
    • Optional parameter to include call to the super toString method
  • @EqualsAndHashCode
    • Generates implementations of ‘equals(Object other) and hashCode()
    • By default will use all non-static, non-transient properties
    • Can optionally exclude specific properties
  • @NoArgsConstructor
    • Generates no args constructor
    • Will cause compiler error if there are final fields
    • Can optionally force, which will initialize final fields with 0 / false / null
  • @RequiredArgsContructor
    • Generates a constructor for all fields that are final or marked @NonNull
    • Constructor will throw a NullPointerException if any @NonNull fields are null
  • @Data
    • Generates typical boilerplate code for POJOs
    • Combines - @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor
    • No constructor is generated if constructors have been explicitly declared
  • @Value
    • The immutable variant of @Data
    • All fields are made private and final by default
  • @NonNull
    • Set on parameter of method or constructor and a NullPointerException will be thrown if parameter is null
  • @Builder
    • Implements the ‘builder’ pattern for object creation
    • Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();
  • @SneakyThrows
    • Throw checked exceptions without declaring in calling method’s throws clause
  • @Syncronized
    • A safer implementation of Java’s synchronized
  • @Log
    • Creates a Java util logger • Java util loggers are awful
  • @Slf4j
    • Creates a SLF4J logger.
    • Recommended - SLF4J is a generic logging facade
    • Spring Boot’s default logger is LogBack