Skip to content

Lombok

Overview

Project Lombok is a java library that automatically plugs into editors and build tools, spicing up Java.

Maven

After importing Lombok as following in Maven project, it will takes effect when compiling.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${lombok.version}</version>
    <scope>provided</scope>
</dependency>

Support for IDE

Install plug-in Lombok.

Principle

Since Java 6, Java supports regulation of JSR 269 Pluggable Annotation Processing API which is called when javac is running. Its procedures are as follows:

  1. javac analyzes the source code and generates an Abstract Syntax Tree (AST).
  2. javac calls Lombok programs implementing JSR 269 API when compiling.
  3. Lombok deals with the AST above modifies its nodes by associate annotations.
  4. javac generates bytecode files based on the modified AST.

Usage

  • @NonNull
  • @Cleanup calls close() methods, taking effect for objects implementing java.io.Closeable method.
  • @Getter, @Setter
  • @ToString
  • @EqualsAndHashCode generates equals() and hashCode()
  • @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor generate constructors that take no fields, final / @NonNull fields or all fields.
  • @Data is a shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields and @RequiredArgsConstrutor.
  • @Value
  • @Builder builds types by Builder Pattern.
  • @SneakyThrows checks exceptions.
  • @With
  • @Log generates a logger field.

References