当前位置:网站首页>Lombok makes the pit of ⽤ @data and @builder at the same time

Lombok makes the pit of ⽤ @data and @builder at the same time

2022-07-07 02:27:00 51CTO

      
      
The problem background
  • 1.

Lombok send ⽤ At the same time make ⽤@Data and @Builder , An error is reported when constructing a parameterless constructor ! Compile not pass . Here's the picture :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors

Lombok @Data and @Builder Analyze usage separately

Lombok send ⽤@Data Sure ⽣ become ⽆ Participate in constructing classes ⾥⾯ All attributes of getter/setter⽅ Law . It can simplify the development of our code .( Need to install Lombok Plug ins and leads ⼊Lombok rely on ).

For example, next ⾯ Of ⼀ Entity classes , lead ⼊Lombok after , Sure ⾃ dynamic ⽣ become GET/SET⽅ Law and ⽆ Parameter constructor .

Lombok At the same time make ⽤@Data and @Builder The pit of _java_02 The compiled class by : You can see that it not only helps us generate get and set , There is also a default parameterless constructor

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _03

So how to automatically generate a parameter constructor ? Use @Builder annotation , Will help us ⽣ Complete the construction of attributes ⽅ Law .

Lombok At the same time make ⽤@Data and @Builder The pit of _ Test class _04

The compiled class by : You can see It has helped us build a full attribute constructor , But if the value only references @Builder Annotations cannot be generated get and set Of .

Lombok At the same time make ⽤@Data and @Builder The pit of _java_05

But if at the same time ⽤@Data and @Builder Words , It can be seen that although ⽣ a GET/SET⽅ Law , however ⽆ Reference structure ⽅ There is no law , This is obviously unacceptable , Because many frameworks can be adjusted ⽤⽆ Reference structure to create objects .

Lombok At the same time make ⽤@Data and @Builder The pit of _ Test class _06

The compiled class:Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _07

We try to be in Tet1 class ,⼿ Dynamic add ⽆ Reference structure ⽅ Law . The compiler finds that an error is reported and fails :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Test class _08

resolvent

Method 1

Lombok At the same time make ⽤@Data and @Builder When , If you want to ⽣ become ⽆ Reference structure , Need to be in code ⾥⾯⼿ Dynamic leading ⼊ annotation @Tolerate, Give Way Lombok stay ⽣ When you are in a class , Not aware of the specified constructor .

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _09

Method 2

Use the parameterless constructor directly + There is a way of involving constructors ,@RequiredArgsConstructor To build a reference ,@NoArgsConstructor To build a parameterless constructor , As shown in the figure :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _10 The effect after compiling :

Lombok At the same time make ⽤@Data and @Builder The pit of _java_11

Lombok principle

Java The compilation of is divided into the following ⼏ Stages :

Parsing and filling symbol tables -> Annotation Processing -> Analysis and bytecode ⽣ become ->⽣ become ⼆ Base number class⽂ Pieces of .

  • Lombok send ⽤ Yes. JDK 6 Realized JSR 269: Pluggable Annotation Processing API ( Compile time annotation processor ), It is in compile time to put Lombok The annotation code for , Convert to regular Java ⽅ Law ⽽ Implementation note ⼊.
  • At compile time , When Java Source code is abstract idiom tree (AST) after ,Lombok Will be based on ⾃⼰ The annotation processor dynamically modifies AST, Add new code ( node ), Here ⼀ Cut hold ⾏ after , And then through the analysis ⽣ It becomes the final bytecode (.class) ⽂ Pieces of , This is it. Lombok The hold of ⾏ principle .

It can be implemented with the help of annotation processor ⼀ A simple one. Setter, Our implementation steps are :

  • ⾃ Definition ⼀ An annotation tag is attached to ⼝, And implement ⼀ individual ⾃ Defined annotation handler ;
  • benefit ⽤ tools.jar Of javac api Handle AST ( Abstract syntax tree )3. send ⽤⾃ The defined annotation handler compiles the code .

1. Definition ⾃ Define annotations and annotation handlers

⾸ First create ⼀ individual MySetter.java ⾃ Definition ⼀ A note , The code is as follows :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _12

Re realization ⼀ individual ⾃ Defined annotation handler , The code is as follows :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _13Lombok At the same time make ⽤@Data and @Builder The pit of _ Test class _14Lombok At the same time make ⽤@Data and @Builder The pit of _ Test class _15

The test class is as follows :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Constructors _16

2. To the annotation processor ⾏ compile , Subsequently make ⽤ The annotation processor advances the class ⾏ compile

⾸ The annotation processor needs to be modified first ⾏ compile (javac -cp ⽤ Yu Yin ⼊ Third ⽅jar Baojin ⾏ compile )

Lombok At the same time make ⽤@Data and @Builder The pit of _java_17

Then make ⽤ Annotation processor for this Person Test class into ⾏ compile :

Let's look at it later ⽣ Yes Person.class, You can find Setter⽅ The law has ⽣ a :

Lombok At the same time make ⽤@Data and @Builder The pit of _ Test class _18

Lombok At the same time make ⽤@Data and @Builder The pit of _java_19

summary

Of course, although the test class has ⽣ become Setter⽅ Law , But because it is at compile time ⽣ Yes , Therefore, we can't directly adjust when we are developing ⽤Setter⽅ French , therefore Lombok Provides plug-in mechanism ,⽅ So we can directly adjust it during development ⽤Lombok Characteristics of .

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061833045013.html