Lombok (@Data, NoArgsConstructor, @NonNull, etc.)

1. Reference

-Lombok home > https://projectlombok.org

-Lombok features > https://projectlombok.org/features/all

-Lombok setup > https://projectlombok.org/setup/

-Lombok Eclipse > https://projectlombok.org/setup/eclipse

    Min. v1.18.24 Double click lombok.jar or execute java -jar lombok.jar

2. Introduction

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

Never write another getter or equals method again. Early access to future java features such as val, and much more. 

3a. Lombok config

"lombok.config" file at the Maven project root (same level than pom.xml):

config.stopBubbling = true

lombok.addNullAnnotations = spring

lombok.addLombokGeneratedAnnotation = true

#lombok.extern.findbugs.addSuppressFBWarnings = true

lombok.nonNull.exceptionType = IllegalArgumentException


Note: lombok.addLombokGeneratedAnnotation only to be used if the spotbugs (former findbugs) is uded

3b. Maven config

Spring Boot manages the Lombok dependency version, but it can be overriden in pom.xml properties section:

<lombok.version>1.18.28</lombok.version>

In the dependencies section:

<dependencies>

  <dependency>

   <groupId>org.projectlombok</groupId>

   <artifactId>lombok</artifactId>

   <scope>provided</scope>

  </dependency>

</dependencies>

And the compiler plug-in configurartion:

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<annotationProcessorPaths>

<path>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>${lombok.version}</version>

</path>

</annotationProcessorPaths>

<encoding>${project.build.sourceEncoding}</encoding>

             <parameters>true</parameters>

</configuration>

    <dependencies>

        <!-- Java bytecode manipulation framework -->

                    <dependency>

                        <groupId>org.ow2.asm</groupId>

                        <artifactId>asm</artifactId>

                        <version>9.1</version>

                    </dependency>

                 </dependencies>

</plugin>

Optional, if using  the queryDSL processor for JPA annotations:

<build><plugins>

<plugin>

<!-- http://www.querydsl.com/static/querydsl/latest/reference/html_single/ -->

<!-- mvnw eclipse:eclipse -->

<groupId>com.mysema.maven</groupId>

<artifactId>apt-maven-plugin</artifactId>

<version>1.1.3</version>

<executions>

<execution>

<!-- phase>generate-sources</phase -->

<goals>

<goal>process</goal>

</goals>

<configuration>

<outputDirectory>${project.build.directory}/generated-sources/querydsl</outputDirectory>

<!-- Added 'lombok' for making it work together with the apt-maven-plugin 

and queryDSL processor for JPA annotations -->

<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor,lombok.launch.AnnotationProcessorHider$AnnotationProcessor</processor>

</configuration>

</execution>

</executions>

</plugin>

</plugins></build>

4. Example

package edu.cou.doc.model;


import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.NonNull;


@Data

@NoArgsConstructor

public class CalendarDetail {


 private String code;

 private String nameShort;

 private String nameLong;


 public CalendarDetail(@NonNull final String code, @NonNull final String nameShort, @NonNull final String nameLong) {

  this.code = code;

  this.nameShort = nameShort;

  this.nameLong = nameLong;

 }


}


5. Common annotations

@Builder

@Cleanup InputStream in = new FileInputStream(args[0]);

@Data for @ToString, @EqualsAndHashCode, @Getter, @Setter and @RequiredArgsConstructor!

@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)

@EqualsAndHashCode.Include

@Getter(AccessLevel.NONE)

@Log

@NoArgsConstructor

@NonNull

@Setter(AccessLevel.NONE)

@StandardException //adds up to 4 exception constructors