Previous Java projects were full of unfriendly code: getters/setters/toStrings for POjos; Exception handling; The shutdown of I/O streams and so on, boilerplate code that was both untechnical and ugly, Lombok was born.

Any technology appears to solve a certain kind of problem, if on this basis to build a clever technology, it is better to go back to Java itself, should maintain reasonable use and not abuse.

Lombok is very simple to use:

1) Introduce maven packages

< the dependency > < groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.16.18 < / version > <scope>provided</scope> </dependency>Copy the code

Lombok’s scope=provided means that it only works at compile time and does not need to be typed into the package. In fact, Lombok correctly compiles lombok-annotated Java files into full Class files at compile time.

2) Add IDE tool support for Lombok

Add Lombok support to IDEA as follows: Click on Intellij --> Preferences... Setup interface to install Lombok plugin:Copy the code

Click On Intellij –> Preferences… Setup screen to enable AnnocationProcessors:

This is turned on to allow Lombok annotations to come into play at compile time.

Installation of the Lombok plug-in for Eclipse is self-explanatory and relatively simple, notably because the built-in Eclipse Compiler is not Oracle Javac, but Eclipse's own Implementation of the Eclipse Compiler for Java (ECJ). To enable ECJ to support Lombok, you need to add the following two items to the eclipse.ini configuration file: -xbootclasspath /a:[LombokCopy the code

3) Lombok implementation principle

Since Java 6, Javac has supported the “JSR 269 Pluggable Annotation Processing API” specification, which can be called whenever a program implements the API while Javac is running.

Lombok is a program that implements the “JSR 269 API”. When using JavAC, it works as follows:

  1. Javac analyzes the source code to generate an abstract syntax tree (AST)

  2. The Lombok program that implements JSR 269 is invoked during javAC compilation

  3. Lombok then processes the AST obtained in the first step, finds the syntax tree (AST) corresponding to Lombok’s annotation class, and then modifies the AST to add the corresponding tree nodes defined by Lombok annotations

  4. Javac uses a modified abstract syntax tree (AST) to generate bytecode files

4) Use of Lombok annotations

POJO class common comments:

@getter / @setter: generates Getter/ Setter methods for all member variables on a class. Function on a member variable to generate getter/setter methods for that member variable. You can set the access permission and whether lazy loading.

package com.kaplan.pojo;

import lombok.*;

import lombok.extern.log4j.Log4j;

@Getter

@Setter

public class TestDemo {

private String name;

private int age ; private String email;

private String address; private String password;

@Getter @Setter private boolean funny;

}
Copy the code

ToString: Overrides the default ToString () method of a class, qualifying certain fields with the of attribute and excluding certain fields with the exclude attribute.

@equalsandHashCode: Applied to the class, overriding the default equals and hashCode

@nonNULL: applies to member variables and parameters. The identifier cannot be null. Otherwise, a null pointer exception is thrown.

NoArgsConstructor, @requiredargsconstructor, @allargsconstructor: Applied to the class to generate the constructor. The attributes include staticName and access.

Once the staticName attribute is set, the instance will be generated using a static method, and the Access attribute can restrict access.

@noargsconstructor: Generates a no-parameter constructor;

RequiredArgsConstructor: generates a constructor for member variables containing final and @nonnull annotations;

AllArgsConstructor: Generates a full-parameter constructor

@data: Applies to a class and is a collection of annotations: @toString @equalSandhashCode @getter @setter @requiredargsconstructor

Builder: Works on a class to convert it to Builder mode

@log: applied to a class to generate a Log variable. There are different annotations for different logging implementation products:

Other important notes:

Cleanup: Automatically closes resources for objects that implement the java.io.Closeable interface, such as typical IO stream objects

The result is as follows:

@sneakythrows: Can catch checked exceptions and throw, can rewrite the above main method as follows:

Synchronized: used at the method level to replace the synchronize keyword or lock, which is not very useful.

@article author