日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Lombok中关于@Data的使用

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lombok中关于@Data的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當你在使用 Lombok 的 @Data 注解時,其實會有一些坑需要關注,今天就讓我們來見識一下。
<!-- more -->

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自動添加類中所有屬性相關的 set 方法
@Getter自動添加類中所有屬性相關的 get 方法
@Builder使得該類可以通過 builder (建造者模式)構建對象
@RequiredArgsConstructor生成一個該類的構造方法,禁止無參構造
@ToString重寫該類的toString()方法
@EqualsAndHashCode重寫該類的equals()和hashCode()方法
@Data等價于上面的@Setter、@Getter、@RequiredArgsConstructor、@ToString、@EqualsAndHashCode

看起來似乎這些注解都很正常,并且對我們的代碼也有一定的優化,那為什么說@Data注解存在坑呢?

@Data注解

內部實現

由上面的表格我們可以知道,@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()方法,當它進行屬性比較時,其實只比較了當前類中的屬性。如果你不信的話,我們再來創建一個類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));}

結果為:

true true 6373 6373 false true

問題總結

對于父類是Object且使用了@EqualsAndHashCode(callSuper = true)注解的類,這個類由 Lombok 生成的equals()方法只有在兩個對象是同一個對象時,才會返回 true ,否則總為 false ,無論它們的屬性是否相同。

這個行為在大部分時間是不符合預期的,equals()失去了其意義。即使我們期望equals()是這樣工作的,那么其余的屬性比較代碼便是累贅,會大幅度降低代碼的分支覆蓋率。

解決方法

  • 用了@Data就不要有繼承關系,類似 Kotlin 的做法。
  • 自己重寫equals(), Lombok 不會對顯式重寫的方法進行生成。
  • 顯式使用@EqualsAndHashCode(callSuper = true), Lombok 會以顯式指定的為準。
  • 總結

    以上是生活随笔為你收集整理的Lombok中关于@Data的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。