当前位置:网站首页>Lombok原理和同时使⽤@Data和@Builder 的坑
Lombok原理和同时使⽤@Data和@Builder 的坑
2022-07-06 04:10:00 【Hollis Chuang】
来源:https://juejin.cn/post/7103011031672176677
问题背景
Lombok使⽤ 同时使⽤@Data和@Builder ,构建无参构造器报错!编译不通过。如下图:

Lombok @Data和@Builder分别单独分析用法
Lombok使⽤@Data可以⽣成⽆参构造和类⾥⾯所有属性的getter/setter⽅法。可以简化我们代码的开发。(需要安装Lombok插件和引⼊Lombok依赖)。
例如下⾯的⼀个实体类,引⼊Lombok后,可以⾃动⽣成GET/SET⽅法和⽆参构造函数。

编译后的class为:可以看到不仅帮我们生成了get和set ,同时也有默认的无参构造器

那么怎么自动生成有参构造器呢?使用@Builder注解,将会帮助我们⽣成全属性的构造⽅法。

编译后的class为:可以看到 已经帮我们构建好了全属性的构造方法,但是如果值只引用@Builder注解是无法生成get和set的。

但是如果同时使⽤@Data和@Builder的话,可以看出尽管⽣成了GET/SET⽅法,但是⽆参构造⽅法没有了,这显然是不能接受的,因为很多框架都会调⽤⽆参构造去创建对象。

编译后的class:

我们尝试在Tet1类,⼿动添加⽆参构造⽅法。编译发现报错不通过:

解决方法
方法一
Lombok同时使⽤@Data和@Builder的时候,如果要⽣成⽆参构造,需要在代码⾥⾯⼿动引⼊注解@Tolerate,让Lombok在⽣成类的时候,对指定的构造函数不感知。

方法二
直接使用无参构造器+有参构造器的方式,@RequiredArgsConstructor 来构建有参,@NoArgsConstructor来构建无参构造器,如图所示:

编译后效果:

Lombok原理
Java的编译分为以下⼏个阶段:
解析与填充符号表->注解处理->分析与字节码⽣成->⽣成⼆进制class⽂件。
Lombok 使⽤的是 JDK 6 实现的 JSR 269: Pluggable Annotation Processing API (编译期的注解处理器),它是在编译期时把 Lombok 的注解代码,转换为常规的 Java ⽅法⽽实现注⼊。
在编译期阶段,当 Java 源码被抽象成语法树 (AST) 之后,Lombok 会根据⾃⼰的注解处理器动态的修改AST,增加新的代码 (节点),在这⼀切执⾏之后,再通过分析⽣成了最终的字节码 (.class) ⽂件,这就是Lombok 的执⾏原理。
可以借助注解处理器实现⼀个简单的 Setter,我们的实现步骤是:
⾃定义⼀个注解标签接⼝,并实现⼀个⾃定义的注解处理器;
利⽤ tools.jar 的 javac api 处理 AST (抽象语法树)3. 使⽤⾃定义的注解处理器编译代码。
1.定义⾃定义注解和注解处理器
⾸先创建⼀个 MySetter.java ⾃定义⼀个注解,代码如下:

再实现⼀个⾃定义的注解处理器,代码如下:



测试类如下:

2.对注解处理器进⾏编译,随后使⽤注解处理器对类进⾏编译
⾸先需要先对注解处理器进⾏编译(javac -cp ⽤于引⼊第三⽅jar包进⾏编译)

然后使⽤注解处理器对这个Person测试类进⾏编译:
这时候再看⽣成的Person.class,可以发现Setter⽅法已经⽣成了:


总结
当然尽管测试类已经⽣成Setter⽅法,但是因为是在编译时期⽣成的,因此我们在开发的时候是没法直接调⽤Setter⽅法的,因此Lombok提供了插件机制,⽅便我们在开发的时候可以直接去调⽤Lombok的特性。
完
往期推荐
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
有道无术,术可成;有术无道,止于术
欢迎大家关注Java之道公众号

好文章,我在看️
边栏推荐
- WPF effect Article 191 box selection listbox
- 登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
- MySql數據庫root賬戶無法遠程登陸解决辦法
- The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
- Benefits of automated testing
- Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
- 【leetcode】22. bracket-generating
- Record the pit of NETCORE's memory surge
- Lora gateway Ethernet transmission
- Unity中几个重要类
猜你喜欢

KS003基于JSP和Servlet实现的商城系统

Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did

Stable Huawei micro certification, stable Huawei cloud database service practice

C (thirty) C combobox listview TreeView

No qualifying bean of type ‘......‘ available

Record the pit of NETCORE's memory surge

Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts

【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用

R note prophet

10 exemples les plus courants de gestion du trafic istio, que savez - vous?
随机推荐
Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
Chinese brand hybrid technology: there is no best technical route, only better products
R note prophet
pd. to_ numeric
In Net 6 CS more concise method
No qualifying bean of type ‘......‘ available
Security xxE vulnerability recurrence (XXe Lab)
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
Custom event of C (31)
Viewing and verifying backup sets using dmrman
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
Thread sleep, thread sleep application scenarios
[Key shake elimination] development of key shake elimination module based on FPGA
深入浅出node模板解析错误escape is not a function
asp. Core is compatible with both JWT authentication and cookies authentication
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
SSTI template injection explanation and real problem practice
Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
POI add border


