当前位置:网站首页>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 :

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 .

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

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

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 .

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 .

The compiled class:

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

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 .

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 :

The effect after compiling :

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 :

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



The test class is as follows :

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 )

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 :


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
How to build a technical team that will bring down the company ?
Performance of major mainstream programming languages PK, The results were unexpected
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

Good article , I was watching ️
边栏推荐
- Record the pit of NETCORE's memory surge
- TCP/IP协议里面的网关地址和ip地址有什么区别?
- Stable Huawei micro certification, stable Huawei cloud database service practice
- [adjustable delay network] development of FPGA based adjustable delay network system Verilog
- 1291_Xshell日志中增加时间戳的功能
- MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
- 2/12 didn't learn anything
- Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
- Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
- C form application of C (27)
猜你喜欢

【FPGA教程案例11】基于vivado核的除法器设计与实现

Slow SQL fetching and analysis of MySQL database

【按鍵消抖】基於FPGA的按鍵消抖模塊開發

Record the pit of NETCORE's memory surge
![[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos](/img/41/27ce3741ef29e87c0f3b954fdef87a.png)
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
![[adjustable delay network] development of FPGA based adjustable delay network system Verilog](/img/82/7ff7f99f5164f91fab7713978cf720.png)
[adjustable delay network] development of FPGA based adjustable delay network system Verilog

C mouse event and keyboard event of C (XXVIII)

Proof of Stirling formula

Basic use of MySQL (it is recommended to read and recite the content)

解决“C2001:常量中有换行符“编译问题
随机推荐
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
图应用详解
C mouse event and keyboard event of C (XXVIII)
MySql数据库root账户无法远程登陆解决办法
E. Best Pair
【FPGA教程案例11】基于vivado核的除法器设计与实现
Query the number and size of records in each table in MySQL database
Simple blog system
pd. to_ numeric
Hashcode and equals
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
Maxay paper latex template description
2/12 didn't learn anything
Recommendation system (IX) PNN model (product based neural networks)
Scalpel like analysis of JVM -- this article takes you to peek into the secrets of JVM
VPP性能测试
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
How does technology have the ability to solve problems perfectly
Path of class file generated by idea compiling JSP page
P2022 有趣的数(二分&数位dp)


