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

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

2022-07-06 04:12:00 Hollis Chuang

source :https://juejin.cn/post/7103011031672176677


The problem background

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 :

799c8da916b154a33e66619ebb6342f7.jpeg


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 .

29bfab8914410721eefb31e896652a6a.jpeg

The compiled class by : You can see that it not only helps us generate get and set , There is also a default parameterless constructor

6b233157c008fa6695752506b0db58e3.jpeg

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

30867a55d79a416d7e02e155c6f716f4.jpeg

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 .

7b7d96ffb1f7ce319f4ae0c3cf81a890.jpeg

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 .

fd263a4cde058ca1f83f04b9ed9c9094.jpeg

The compiled class:

fac1046184f80c0ffb0140248fc93918.jpeg

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

36feb04fc71ed757f6fbeac79b2a9644.jpeg

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 .

0dca0674a3c2543d58b9f1e76861f2f1.jpeg

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 :

484cc355b2b7b0252ed932e3d2cc0613.jpeg

The effect after compiling :

7f512e04d355d3a4f7193f6c6ab12a98.jpeg

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 :

abe865620db6d05d6043b11d84a13301.jpeg

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

85be5b5d0a17bf9402a31663d90e50a7.jpegad99af6a06e0fe708ea57be5b70dc637.jpeg684c5a1fa8835a0eb83bccfacbcbbb95.jpeg

The test class is as follows :

295bf00de8a2c5e50da2c35b0c1284f8.jpeg

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 )

2a8fae00823a80af8cdb6c527e7f95aa.jpeg

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 :

7c0cb2d2450b9d65716b31876269611d.jpeg

348fdf918ea897cf1c6b3df05d946bec.jpeg

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 .

End

Previous recommendation

35fe9580ad48427fc3410fabdeb79014.jpeg

How to build a technical team that will bring down the company ?


1712ce63e39b34a546341a1f7765d8c7.jpeg

Performance of major mainstream programming languages PK, The results were unexpected


9a63447dfec6f2d0d2fbfc73707cfca3.jpeg

For amount calculation BigDecimal It's safe ? Look at these five pits ~~


There is Tao without skill , It can be done with skill ; No way with skill , Stop at surgery

Welcome to pay attention Java Road official account

1f049a9e84d6392f99a8e9263ea29a00.jpeg

Good article , I was watching ️

原网站

版权声明
本文为[Hollis Chuang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060410037356.html

随机推荐