当前位置:网站首页>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】
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 .
边栏推荐
- Apifox,你的API接口文档卷成这样了吗?
- TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
- 1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
- C#/VB.NET 删除Word文檔中的水印
- 张平安:加快云上数字创新,共建产业智慧生态
- Real project, realized by wechat applet opening code (end)
- Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
- postgresql之整體查詢大致過程
- Processing image files uploaded by streamlit Library
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
猜你喜欢
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
3D laser slam: time synchronization of livox lidar hardware
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
FLIR blackfly s usb3 industrial camera: how to use counters and timers
[paper reading | deep reading] dngr:deep neural networks for learning graph representations
#夏日挑战赛#数据库学霸笔记(下)~
Application analysis of face recognition
阿里云中间件开源往事
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
随机推荐
argo workflows源码解析
FLIR blackfly s usb3 industrial camera: how to use counters and timers
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!
Application analysis of face recognition
人脸识别应用解析
Lidar: introduction and usage of ouster OS
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
处理streamlit库上传的图片文件
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
[C # notes] use file stream to copy files
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
Web3对法律的需求
Draco - glTF模型压缩利器
Seconds understand the delay and timing function of wechat applet
Alibaba cloud middleware open source past
C # / vb. Net supprime le filigrane d'un document word
Web3的先锋兵:虚拟人
freeswitch拨打分机号源代码跟踪