当前位置:网站首页>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 .
边栏推荐
- argo workflows源码解析
- The empirical asset pricing package (EAP) can be installed through pypi
- 张平安:加快云上数字创新,共建产业智慧生态
- leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
- FLIR blackfly s industrial camera: auto exposure configuration and code
- 3D激光SLAM:Livox激光雷达硬件时间同步
- STM32项目 -- 选题分享(部分)
- The cities research center of New York University recruits master of science and postdoctoral students
- FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
- 4--新唐nuc980 挂载initramfs nfs文件系统
猜你喜欢
UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Lidar: introduction and usage of ouster OS
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity
[paper reading | deep reading] dngr:deep neural networks for learning graph representations
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Blackfly s usb3 industrial camera: buffer processing
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
#夏日挑战赛#数据库学霸笔记(下)~
随机推荐
纽约大学 CITIES 研究中心招聘理学硕士和博士后
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
【LeetCode】Day97-移除链表元素
Integerset of PostgreSQL
MFC Windows 程序设计[147]之ODBC数据库连接(附源码)
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Apifox,你的API接口文档卷成这样了吗?
How can reinforcement learning be used in medical imaging? A review of Emory University's latest "reinforcement learning medical image analysis", which expounds the latest RL medical image analysis co
Douban average 9 x. Five God books in the distributed field!
#夏日挑战赛#数据库学霸笔记(下)~
Time synchronization of livox lidar hardware -- PPS method
Pgadmin4 of PostgreSQL graphical interface tool
Dall-E Mini的Mega版本模型发布,已开放下载
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
#yyds干货盘点# 解决名企真题:最大差值
3D激光SLAM:Livox激光雷达硬件时间同步
String or binary data will be truncated
postgresql 之 数据目录内部结构 简介
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
【森城市】GIS数据漫谈(二)