当前位置:网站首页>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 .
边栏推荐
- [server data recovery] data recovery case of a Dell server crash caused by raid damage
- Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
- FLIR blackfly s usb3 industrial camera: white balance setting method
- 遇到慢SQL该怎么办?(下)
- Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
- 1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
- 【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
- 纽约大学 CITIES 研究中心招聘理学硕士和博士后
- 【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
- Web3对法律的需求
猜你喜欢

Draco - gltf model compression tool

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

3D laser slam: time synchronization of livox lidar hardware

老板被隔离了

1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
![[paper reading | deep reading] dngr:deep neural networks for learning graph representations](/img/8e/41a80131c6ec7a7cdffe4a77012fed.png)
[paper reading | deep reading] dngr:deep neural networks for learning graph representations

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient

机器人队伍学习方法,实现8.8倍的人力回报

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效

fiddler的使用
随机推荐
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
老板被隔离了
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Several classes and functions that must be clarified when using Ceres to slam
Processing image files uploaded by streamlit Library
4 -- Xintang nuc980 mount initramfs NFS file system
遇到慢SQL该怎么办?(下)
C语言练习题_1
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Lombok同时使⽤@Data和@Builder 的坑
Word wrap when flex exceeds width
Metaforce force meta universe development and construction - fossage 2.0 system development
Douban average 9 x. Five God books in the distributed field!
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Gee upgrade can realize one piece of run tasks
处理streamlit库上传的图片文件
Lumion 11.0 software installation package download and installation tutorial
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
云原生混部最后一道防线:节点水位线设计
投资的再思考