当前位置:网站首页>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 .
边栏推荐
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- 豆瓣平均 9.x,分布式领域的 5 本神书!
- 企业中台建设新路径——低代码平台
- #yyds干货盘点# 解决名企真题:最大差值
- 解密函数计算异步任务能力之「任务的状态及生命周期管理」
- Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
- This week's hot open source project!
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 处理streamlit库上传的图片文件
- [server data recovery] data recovery case of a Dell server crash caused by raid damage
猜你喜欢

Processus général de requête pour PostgreSQL

【森城市】GIS数据漫谈(二)

3D激光SLAM:Livox激光雷达硬件时间同步
![[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity](/img/21/59c4d61b22e7d021b7a72a64a16b48.png)
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity

Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale

argo workflows源码解析

处理streamlit库上传的图片文件

老板被隔离了

大咖云集|NextArch基金会云开发Meetup来啦!

Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
随机推荐
The cities research center of New York University recruits master of science and postdoctoral students
[C # notes] reading and writing of the contents of text files
【LeetCode】Day97-移除链表元素
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
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!
FLIR blackfly s industrial camera: auto exposure configuration and code
Apifox,你的API接口文档卷成这样了吗?
企业中台建设新路径——低代码平台
RC振荡器和晶体振荡器简介
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
张平安:加快云上数字创新,共建产业智慧生态
3D激光SLAM:Livox激光雷达硬件时间同步
本周 火火火火 的开源项目!
阿里云中间件开源往事
Draco - gltf model compression tool
Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
云原生混部最后一道防线:节点水位线设计
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Lumion 11.0 software installation package download and installation tutorial
freeswitch拨打分机号源代码跟踪