当前位置:网站首页>Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
2022-07-05 11:05:00 【Java notes shrimp】
Click on the official account , utilize Fragment time to learn
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 .
source :juejin.cn/post/7103011031672176677
recommend :
The most comprehensive java Interview question bank
PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !边栏推荐
- DDRx寻址原理
- Characteristics and electrical parameters of DDR4
- 中职组网络安全C模块全漏洞脚本讲解包含4个漏洞的脚本
- DDR4的特性与电气参数
- Sqlserver regularly backup database and regularly kill database deadlock solution
- iframe
- About the use of Vray 5.2 (self research notes)
- Cross page communication
- Some understandings of heterogeneous graphs in DGL and the usage of heterogeneous graph convolution heterographconv
- Web3 Foundation grant program empowers developers to review four successful projects
猜你喜欢

Operation of simulated examination platform of special operation certificate examination question bank for safety production management personnel of hazardous chemical production units in 2022

Basic testing process of CSDN Software Testing Introduction

2022 t elevator repair operation certificate examination questions and answers

购买小间距LED显示屏的三个建议

About the use of Vray 5.2 (self research notes)

小红书自研KV存储架构如何实现万亿量级存储与跨云多活

Lombok 同时使⽤@Data和@Builder 的坑,你中招没?

32:第三章:开发通行证服务:15:浏览器存储介质,简介;(cookie,Session Storage,Local Storage)

Bidirectional RNN and stacked bidirectional RNN

Some understandings of heterogeneous graphs in DGL and the usage of heterogeneous graph convolution heterographconv
随机推荐
Node の MongoDB Driver
关于vray 5.2的使用(自研笔记)
32: Chapter 3: development of pass service: 15: Browser storage media, introduction; (cookie,Session Storage,Local Storage)
修复动漫1K变8K
2022 mobile crane driver examination question bank and simulation examination
【广告系统】增量训练 & 特征准入/特征淘汰
Applet framework taro
第五届 Polkadot Hackathon 创业大赛全程回顾,获胜项目揭秘!
Based on shengteng AI Yisa technology, it launched a full target structured solution for video images, reaching the industry-leading level
LSTM应用于MNIST数据集分类(与CNN做对比)
Scaffold development foundation
磨礪·聚變|知道創宇移動端官網煥新上線,開啟數字安全之旅!
Share Net lightweight ORM
Repair animation 1K to 8K
【广告系统】Parameter Server分布式训练
GBase 8c数据库如何查看登录用户的登录信息,如上一次登录认证通过的日期、时间和IP等信息?
Broyage · fusion | savoir que le site officiel de chuangyu mobile end est en ligne et commencer le voyage de sécurité numérique!
regular expression
[TCP] TCP connection status JSON output on the server
A mining of edu certificate station