Lombok中關(guān)于@Data的使用解析
當你在使用 Lombok 的 @Data 注解時,其實會有一些坑需要關(guān)注,今天就讓我們來見識一下。
Lombok
先來簡單介紹一下 Lombok ,其官方介紹如下:
Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code.
大致意思是 Lombok 通過增加一些"處理程序",可以讓 Java 代碼變得簡潔、快速。
Lombok 提供了一系列的注解幫助我們簡化代碼,比如:
| 注解名稱 | 功能 |
|---|---|
| @Setter | 自動添加類中所有屬性相關(guān)的 set 方法 |
| @Getter | 自動添加類中所有屬性相關(guān)的 get 方法 |
| @Builder | 使得該類可以通過 builder (建造者模式)構(gòu)建對象 |
| @RequiredArgsConstructor | 生成一個該類的構(gòu)造方法,禁止無參構(gòu)造 |
| @ToString | 重寫該類的toString()方法 |
| @EqualsAndHashCode | 重寫該類的equals()和hashCode()方法 |
| @Data | 等價于上面的@Setter、@Getter、@RequiredArgsConstructor、@ToString、@EqualsAndHashCode |
看起來似乎這些注解都很正常,并且對我們的代碼也有一定的優(yōu)化,那為什么說@Data注解存在坑呢?
@Data注解
內(nèi)部實現(xiàn)
由上面的表格我們可以知道,@Data是包含了@EqualsAndHashCode的功能,那么它究竟是如何重寫equals()和hashCode()方法的呢?
我們定義一個類TestA:
@Data
public class TestA {
String oldName;
}
我們將其編譯后的 class 文件進行反編譯:
public class TestA {
String oldName;
public TestA() {
}
public String getOldName() {
return this.oldName;
}
public void setOldName(String oldName) {
this.oldName = oldName;
}
public boolean equals(Object o) {
// 判斷是否是同一個對象
if (o == this) {
return true;
}
// 判斷是否是同一個類
else if (!(o instanceof TestA)) {
return false;
} else {
TestA other = (TestA) o;
if (!other.canEqual(this)) {
return false;
} else {
// 比較類中的屬性(注意這里,只比較了當前類中的屬性)
Object this$oldName = this.getOldName();
Object other$oldName = other.getOldName();
if (this$oldName == null) {
if (other$oldName != null) {
return false;
}
} else if (!this$oldName.equals(other$oldName)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TestA;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $oldName = this.getOldName();
int result = result * 59 ($oldName == null ? 43 : $oldName.hashCode());
return result;
}
public String toString() {
return "TestA(oldName=" this.getOldName() ")";
}
}
針對其equals()方法,當它進行屬性比較時,其實只比較了當前類中的屬性。如果你不信的話,我們再來創(chuàng)建一個類TestB,它是TestA的子類:
@Data
public class TestB extends TestA {
private String name;
private int age;
}
我們將其編譯后的 class 文件進行反編譯:
public class TestB extends TestA {
private String name;
private int age;
public TestB() {
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TestB)) {
return false;
} else {
TestB other = (TestB)o;
if (!other.canEqual(this)) {
return false;
} else {
// 注意這里,真的是只比較了當前類中的屬性,并沒有比較父類中的屬性
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
return this.getAge() == other.getAge();
}
} else if (this$name.equals(other$name)) {
return this.getAge() == other.getAge();
}
return false;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof TestB;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 ($name == null ? 43 : $name.hashCode());
result = result * 59 this.getAge();
return result;
}
public String toString() {
return "TestB(name=" this.getName() ", age=" this.getAge() ")";
}
}
按照代碼的理解,如果兩個子類對象,其子類中的屬性相同、父類中的屬性不同時,利用equals()方法時,依舊會認為這兩個對象相同,測試一下:
public static void main(String[] args) {
TestB t1 = new TestB();
TestB t2 = new TestB();
t1.setOldName("123");
t2.setOldName("12345");
String name = "1";
t1.name = name;
t2.name = name;
int age = 1;
t1.age = age;
t2.age = age;
System.out.println(t1.equals(t2));
System.out.println(t2.equals(t1));
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println(t1 == t2);
System.out.println(Objects.equals(t1, t2));
}
結(jié)果為:
true
true
6373
6373
false
true
問題總結(jié)
對于父類是Object且使用了@EqualsAndHashCode(callSuper = true)注解的類,這個類由 Lombok 生成的equals()方法只有在兩個對象是同一個對象時,才會返回 true ,否則總為 false ,無論它們的屬性是否相同。
這個行為在大部分時間是不符合預期的,equals()失去了其意義。即使我們期望equals()是這樣工作的,那么其余的屬性比較代碼便是累贅,會大幅度降低代碼的分支覆蓋率。
解決方法
用了@Data就不要有繼承關(guān)系,類似 Kotlin 的做法。
自己重寫equals(), Lombok 不會對顯式重寫的方法進行生成。
顯式使用@EqualsAndHashCode(callSuper = true), Lombok 會以顯式指定的為準。
Lombok的@Data踩坑記錄
面試問你@Data注解的作用,一般人回答就是生成get/set/toString
真是這樣嗎?
其實不然,其實@Data注解作用是
get/set無參數(shù)構(gòu)造器toStringhashcodeequals
@Data會自動生成hashcode和equals方法,一般人會把這點忘了
證明
idea使用alt+6查看類的具體屬性和方法

小結(jié)一下
***@Data會自動生成以下方法***
get/set無參數(shù)構(gòu)造器toStringhashcodeequals
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Jmeter?BlazeMeter實現(xiàn)web錄制過程
BlazeMeter是一款與Apache JMeter兼容的chrome插件,采用BlazeMeter可以方便的進行流量錄制和腳本生成,作為接口測試腳本編寫的一個基礎,這篇文章主要介紹了Jmeter?BlazeMeter實現(xiàn)web錄制,需要的朋友可以參考下2021-12-12
springboot利用@Aspect實現(xiàn)日志工具類的詳細代碼
這篇文章主要介紹了springboot利用@Aspect實現(xiàn)日志工具類,通過實例代碼介紹了導包及在啟動類上進行注解自動掃描的方法,需要的朋友可以參考下2022-03-03
SpringBoot源碼分析之bootstrap.properties文件加載的原理
本文通過訪問看到bootstrap.properties中的信息獲取到了,同時age也被application.properties中的屬性覆蓋掉了。加載順序到底是什么?為什么會覆蓋呢?我們接下來分析下吧2021-12-12

