在Lombok的加持下,“小狗”.Equals(“老狗”) = True
一、禁止使用Lombok
我有一個同學(xué),在一家小型互聯(lián)網(wǎng)公司做Java后端開發(fā),最近他們公司新來了一個技術(shù)總監(jiān),這位技術(shù)總監(jiān)對技術(shù)細(xì)節(jié)很看重,一來公司之后就推出了很多"政策",比如定義了很多開發(fā)規(guī)范、日志規(guī)范、甚至是要求大家統(tǒng)一使用Idea進(jìn)行開發(fā)。
其中有一項(xiàng),就是禁止使用Lombok,但是他并沒有明確的說明原因,我的同學(xué)是一個“懶加載的CRUD程序員”,不用Lombok,豈不是要費(fèi)很多事兒?
他特意百度了一下,是這樣說的。
看似很有道理,條條是道,但我認(rèn)為有些以偏概全,自以為是了。
1、jdk版本問題
目前國內(nèi)大多數(shù)Java項(xiàng)目,采用的都是JDK1.8,因?yàn)樗鼔蚍€(wěn)定,功能也完全夠用,但是,如果哪一天,你的客戶心血來潮,就是想用JDK11,甚至JDK17,此時,你會發(fā)現(xiàn)Lombok不好用了,于是,不得不將好幾百個實(shí)體類,通過idea生成get/set、equals、toString等方法,很煩躁。
2、被迫營業(yè)
當(dāng)你的客戶獲取到你的源代碼之后,他也想看看,運(yùn)行一下,此時發(fā)現(xiàn)沒安裝Lombok,報錯了,安裝Lombok,不了解這是什么,還要去百度學(xué)習(xí)一下,被迫營業(yè),很煩躁。
3、可讀性差
- Lombok隱藏了JavaBean的封裝細(xì)節(jié)。
- toString()不知道會打印什么。
- @AllArgsConstructor提供一個全量構(gòu)造器, 讓外界在初始化時,可以隨意修改其屬性,極其不安全。如果屬性過多,這個全量構(gòu)造器,看起來,很雞肋。
- 參數(shù)的順序我們也無法控制,都是按照Lombok的心情來的。
- 使用Lombok,寫代碼的時候很爽,但它
- 污染了你的代碼。
- 玷污了Java語言的純粹。
- 破壞了Java代碼的完整性、可讀性、安全性。
- 增加了代碼的耦合度。
- 增加了代碼的調(diào)試難度。
這是一種弊大于利、得不償失的操作。
二、Lombok中的真實(shí)小坑
我在開發(fā)過程中,遇到這樣一個問題,就是Lombok造成的。
@Data
public class Animals {
private Integer id;
private String name;
}
@Data
public class Dog extends Animals{
private String cry;
}
我去,哪吒,你在侮辱我嗎?一個小狗和一個老狗怎么可能會相等?這什么JB玩意,我去打游戲了,你自己玩吧~
三、看看編譯后的代碼,到底怎么回事
1、編譯class
很多小伙伴,看到這,直接蒙了,根本不知道為什么?
這個時候,可以看看編譯后的class。
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Dog)) {
return false;
} else {
Dog other = (Dog)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$cry = this.getCry();
Object other$cry = other.getCry();
if (this$cry == null) {
if (other$cry != null) {
return false;
}
} else if (!this$cry.equals(other$cry)) {
return false;
}
return true;
}
}
}
為什么equals只比較一個屬性cry,而Animals比較了兩個屬性?
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Animals)) {
return false;
} else {
Animals other = (Animals)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
return true;
}
}
}
2、添加一個注解@EqualsAndHashCode(callSuper = true)
(1)callSuper = true
根據(jù)子類自身的字段值和從父類繼承的字段值 來生成hashcode,當(dāng)兩個子類對象比較時,只有子類對象的本身的字段值和繼承父類的字段值都相同,equals方法的返回值是true。
(2)callSuper = false
根據(jù)子類自身的字段值 來生成hashcode, 當(dāng)兩個子類對象比較時,只有子類對象的本身的字段值相同,父類字段值可以不同,equals方法的返回值是true。
(3)代碼實(shí)例
@Data
@EqualsAndHashCode(callSuper = true)
public class Dog extends Animals{
private String cry;
}