当前位置:网站首页>Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
2022-07-07 00:36:00 【Java collection】
The problem background
Lombok Use Use at the same time @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 Use @Data
Sure ⽣ become ⽆ Participate in constructing classes ⾥⾯ All attributes of getter/setter
Method . 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
Methods 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 ⽣ The construction method of completion attribute .
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 . in addition , public Number Java selected , reply java interview , Access to interview information .
But if used at the same time @Data
and @Builder
Words , It can be seen that although ⽣ a GET/SET
Method , however ⽆ The parameter construction method is gone , This is obviously unacceptable , Because many frameworks call ⽆ Reference structure to create objects .
The compiled class:
We try to be in Tet1 class ,⼿ Dynamic add ⽆ Refer to the construction method . The compiler finds that an error is reported and fails :
resolvent
Method 1
Lombok Use at the same time @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 It uses 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 Method ⽽ 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 :
Customize ⼀ An annotation tag is attached to ⼝, And implement ⼀ individual ⾃ Defined annotation handler ;
utilize tools.jar Of javac api Handle AST ( Abstract syntax tree )3. Use ⾃ The defined annotation handler compiles the code .
1. Define custom 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 , Then use the annotation processor to enter the class ⾏ compile
⾸ The annotation processor needs to be modified first ⾏ compile (javac -cp For leading ⼊ Third jar Baojin ⾏ compile )
Then use the annotation processor to process this Person Test class into ⾏ compile :
Let's look at it later ⽣ Yes Person.class
, You can find Setter The method has been ⽣ a :
summary
Of course, although the test class has ⽣ become Setter Method , But because it is at compile time ⽣ Yes , Therefore, we cannot call directly when developing Setter Methodical , therefore Lombok Provides plug-in mechanism , We can call directly when developing Lombok Characteristics of .
author : Shudongjun
https://juejin.cn/post/7103011031672176677
official account “ Big coffee notes ” The published content indicates the source of , All rights reserved ( Those whose copyright cannot be verified or whose source is not indicated all come from the Internet , Reprinted , The purpose of reprinting is to convey more information , The copyright belongs to the original author . If there is any infringement , Please contact the , The author will delete the first time !
Many people have asked recently , Is there any readers Communication group ! The way to join is simple , official account Java selected , reply “ Add group ”, You can join the group !
Java Interview questions ( Wechat applet ):3000+ The road test questions , contain Java Basics 、 Concurrent 、JVM、 Threads 、MQ series 、Redis、Spring series 、Elasticsearch、Docker、K8s、Flink、Spark、 Architecture design, etc , Brush questions online at any time !
------ Special recommendation ------
Special recommendation : Focus on the most cutting-edge information and technology sharing , Official account for preparing for overtaking on curves and various open source projects and efficient software ,「 Big coffee notes 」, Focus on finding good things , It's worth your attention . Click the official account card below to follow .
If the article helps , Click to see , Forward! !
边栏推荐
- Value Function Approximation
- [vector retrieval research series] product introduction
- Basic information of mujoco
- Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
- DAY THREE
- 智能运维应用之道,告别企业数字化转型危机
- 以机房B级建设标准满足等保2.0三级要求 | 混合云基础设施
- Command line kills window process
- Personal digestion of DDD
- Mujoco Jacobi - inverse motion - sensor
猜你喜欢
基于SSM框架的文章管理系统
How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
Interface master v3.9, API low code development tool, build your interface service platform immediately
工程师如何对待开源 --- 一个老工程师的肺腑之言
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
互动滑轨屏演示能为企业展厅带来什么
Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解
随机推荐
Advanced learning of MySQL -- basics -- multi table query -- joint query
从外企离开,我才知道什么叫尊重跟合规…
Advanced learning of MySQL -- Fundamentals -- concurrency of transactions
Compilation of kickstart file
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
智能运维应用之道,告别企业数字化转型危机
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
Clipboard management tool paste Chinese version
2022/2/12 summary
浅谈测试开发怎么入门,如何提升?
GPIO简介
Model-Free Prediction
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Win10 startup error, press F9 to enter how to repair?
Use mujoco to simulate Cassie robot
Introduction au GPIO
【软件逆向-自动化】逆向工具大全
Cross-entrpy Method