Lombok中關於@Data的使用

  • 2019 年 11 月 3 日
  • 筆記

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

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 提供了一系列的註解幫助我們簡化程式碼,比如:

自動添加類中所有屬性相關的 set 方法

看起來似乎這些註解都很正常,並且對我們的程式碼也有一定的優化,那為什麼說@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()是這樣工作的,那麼其餘的屬性比較程式碼便是累贅,會大幅度降低程式碼的分支覆蓋率。

解決方法

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

總結

以上便是我在使用@Data時碰到的問題以及自己的一些思考,在現在的項目,我乾脆不再使用該註解。如果你有什麼想法,歡迎在下方留言。