当前位置:网站首页>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 .
边栏推荐
- 机器人队伍学习方法,实现8.8倍的人力回报
- 如何从0到1构建32Core树莓派集群
- Why am I warned that the 'CMAKE_ TOOLCHAIN_ FILE' variable is not used by the project?
- 1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
- Alibaba cloud middleware open source past
- Introduction to the internal structure of the data directory of PostgreSQL
- FLIR blackfly s industrial camera: auto exposure configuration and code
- 阿里云易立:云原生如何破解企业降本提效难题?
- GEE升级,可以实现一件run tasks
- B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
猜你喜欢

The boss is quarantined

低代码平台中的数据连接方式(上)

FLIR blackfly s industrial camera: auto exposure configuration and code

Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem

SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!

企业中台建设新路径——低代码平台

This week's hot open source project!

Summer Challenge database Xueba notes (Part 2)~

Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption

Word wrap when flex exceeds width
随机推荐
红外相机:巨哥红外MAG32产品介绍
豆瓣平均 9.x,分布式领域的 5 本神书!
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
Processus général de requête pour PostgreSQL
C#/VB. Net to delete watermarks in word documents
Integrated navigation: product description and interface description of zhonghaida inav2
Web3的先锋兵:虚拟人
leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
[xlua notes] array of lua to array of C #
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
A new path for enterprise mid Platform Construction -- low code platform
FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
Recent applet development records
Word wrap when flex exceeds width
Introduction to RC oscillator and crystal oscillator
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Blackfly s usb3 industrial camera: buffer processing
传感器:土壤湿度传感器(XH-M214)介绍及stm32驱动代码
What to do when encountering slow SQL? (next)