Lombok uses @Data and @Builder at the same time, so don’t use them indiscriminately!

1. Lombok usage demonstration

Lombok uses @Data and @Builder at the same time, and an error occurs when building a parameterless constructor! Compilation fails. As shown below:

Insert image description here

Lombok uses @Data to automatically generate parameterless constructs and getter/setter methods for all properties in the class. It can simplify the development of our code. (IDEA needs to install the Lombok plug-in and import Lombok dependencies).

For example, the following entity class can automatically generate GET/SET methods and parameterless constructors after introducing Lombok .

Insert image description here

The compiled class is: You can see that not only get and set are generated for us , but also a default no-argument constructor

Insert image description here

So how to automatically generate a parameterized constructor? Using the @Builder annotation will help us generate a full attribute construction method.

Insert image description here

The compiled class is: You can see that the construction method of all attributes has been built for us, but if the value only refers to the @Builder annotation, get and set cannot be generated .

Insert image description here

But if @Data and @Builder are used at the same time, it can be seen that although the GET/SET method is generated, the parameterless construction method is gone. This is obviously unacceptable, because many frameworks will call the parameterless constructor. to create objects.

Insert image description here

Compiled class:

Insert image description here

We try to manually add a parameterless constructor to the Tet1 class. The compilation found an error and failed:

Insert image description here

When Lombok uses @Data and @Builder at the same time, if you want to generate a parameterless constructor, you need to manually introduce the @Tolerate annotation into the code so that Lombok will not be aware of the specified constructor when generating a class.

Insert image description here

Directly use the parameterless constructor + parameterized constructor, @RequiredArgsConstructor to build the parameterized constructor, and @NoArgsConstructor to build the parameterless constructor, as shown in the figure:

Insert image description here

Effect after compilation:

Insert image description here

2. Lombok principle

Java compilation is divided into the following stages:

Parse and fill symbol table -> Annotation processing -> Analysis and bytecode generation -> Generate binary class file.

Lombok uses JSR 269: Pluggable Annotation Processing API (compilation-time annotation processor) implemented in JDK 6. It converts Lombok's annotation code into regular Java methods during compilation to implement injection.

During the compilation phase, when the Java source code is abstracted into a syntax tree (AST), Lombok will dynamically modify the AST according to its own annotation processor and add new code (nodes). After all this is executed, it will be passed The analysis generates the final bytecode (.class) file, which is the execution principle of Lombok.
A simple Setter can be implemented with the help of an annotation processor . Our implementation steps are:

Customize an annotation tag interface and implement a custom annotation processor; use
AST (Abstract Syntax Tree) processing 3. Use the custom annotation processor to compile the code.tools.jarjavac api

1. Define custom annotations and annotation processors

First create a MySetter.java and customize an annotation. The code is as follows:

Insert image description here

Then implement a custom annotation processor, the code is as follows:

Insert image description here

Insert image description here

Insert image description here

The test classes are as follows:

Insert image description here

2. Compile the annotation processor, and then use the annotation processor to compile the class

First, you need to compile the annotation processor (javac -cp is used to introduce the third-party jar package for compilation)

Insert image description here

Then use the annotation processor to compile this Person test class:

At this time, if you look at the generated Person.class, you can find that the Setter method has been generated:

Insert image description here

Insert image description here

3. Summary

Of course, although the test class has generated the Setter method, because it is generated during compilation, we cannot directly call the Setter method during development. Therefore, Lombok provides a plug-in mechanism to facilitate our development. You can directly call Lombok's features.

Guess you like

Origin blog.csdn.net/qq_35971258/article/details/132391298