探测 Lombok 工具
生活随笔
收集整理的這篇文章主要介紹了
探测 Lombok 工具
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Lombok 是一種 Java? 實用工具,它通過注釋來幫助開發(fā)人員消除 Java 的冗長部分,尤其是簡單的 Java 對象(POJO)。Lombok是如何消除Java中的冗長部分,可瀏覽《以簡單的方式消除 Java 的冗長》。在這里,筆者只是介紹通過反編譯由此生成的類文件,從中可看出 Lombok 工具都做了哪些事情。 ? 1,編程時需要寫的源文件(Lure.java)。我們假設(shè) 數(shù)據(jù)對象不想去寫getter/setter等方法;color 屬性是不需要包含在toString()、equals()、hashCode()方法中;而且 size 屬性是只有包可見訪問限制;以及operate()方法要求同步,且內(nèi)部邏輯需要簡化 try/finally語句樣式。正如我們看到的,所需要寫的代碼如下所示,非常簡潔。 @ToString(exclude="color")
@EqualsAndHashCode(exclude="color")
public @Data class Lure {
????private String name;
????private @Getter(AccessLevel.PACKAGE) int size;
????private String color;
????private String style;
????private boolean active;
?
????@Synchronized
????public void operate(String[] args) throws IOException {
??????? @Cleanup InputStream in = new FileInputStream(args[0]);????????? ??????? @Cleanup OutputStream out = new FileOutputStream(args[1]);
??????? //write file code goes here
????}
} 2,通過 javap 獲得編譯后的源文件(Lure.java)。顯然,我們沒有看到 @Synchronized 說起的作用。事實上,Lombok 是通過添加一個$lock對象來實現(xiàn)的,事實上,可以給 @Synchronized 添加參數(shù)的,比如@Synchronized("localock")。而有關(guān)toString()、equals()和hashCode()方法的實際情況,只有通過 JD 反編譯其類文件后才查看得到(詳見 3)。 D:\lombok\src\javac -cp lombok.jar Lure.java D:\lombok\src\javap Lure
Compiled from "Lure.java"
public class Lure extends java.lang.Object{
??? public void operate(java.lang.String[]) throws java.io.IOException;
??? public java.lang.String toString();
??? public boolean equals(java.lang.Object);
??? public int hashCode();
??? public spike.lombok.Lure();
??? public java.lang.String getName();
??? public void setName(java.lang.String);
??? public void setSize(int);
??? public java.lang.String getColor();
??? public void setColor(java.lang.String);
??? public java.lang.String getStyle();
??? public void setStyle(java.lang.String);
??? public boolean isActive();
??? public void setActive(boolean);
??? int getSize();
} 此外,通過 Eclipse? IDE工具,可瀏覽到經(jīng) Lombok 注釋編織代碼后的代碼樹(Outline),見下圖: ? 3,然后,試試通過?Java Decompiler?獲得反編譯后的源文件(Lure.java)內(nèi)部方法的實現(xiàn)情況會如何。正如我們看到的,Lombok 做得非常棒。它把我們編程時應(yīng)該寫的代碼全部編織好了。僅僅是通過少量的特定的注釋類。 public class Lure {
?private String name;
?private int size;
?private String color;
?private String style;
?private boolean active;
?private final Object $lock; //因為@Synchronized注釋
?public Lure() {
??this.$lock = new Object[0];//因為@Synchronized注釋
?}
?public void operate(String[] args) throws IOException {
??synchronized (this.$lock) {//因為@Synchronized注釋
???InputStream in = new FileInputStream(args[0]);
???try {//因為 @Cleanup注釋
????OutputStream out = new FileOutputStream(args[1]);
????out.close();
???} finally {
????in.close();//因為 @Cleanup注釋
???}
??}
?}
?int getSize() { return this.size; }//包可見訪問限制, @Getter(AccessLevel.PACKAGE)注釋
?public String toString() {//因為 @ToString(exclude="color")注釋,見不到處理 color屬性
??return "Lure(name=" + this.name + ", size=" + this.size + ", style="
????+ this.style + ", active=" + this.active + ")";
?}
?/*
? * 因為@EqualsAndHashCode(exclude="color")注釋,見不到處理 color屬性
? * 此方法包含反編譯生成代碼時的錯誤,如break label64,與Lombok編織代碼無關(guān)
? */
?public boolean equals(Object o) {
??if (o == this) return true;
??if (o == null) return false;
??if (o.getClass() != super.getClass()) return false;
??Lure other = (Lure) o;
??if (this.name == null)
???if (other.name == null) break label64;
???else if (this.name.equals(other.name)) break label64;
??return false;
??if (this.size != other.size) label64: return false;
??if (this.style == null)
???if (other.style == null) break label110;
???else if (this.style.equals(other.style)) break label110;
??return false;
??label110: return this.active == other.active;
?}
?/*
? * 因為@EqualsAndHashCode(exclude="color")注釋,見不到處理 color屬性
? * 存在反編譯生成代碼的情況,如 有 PRIME 的表達式,直接是 31, 與Lombok編織代碼無關(guān)
? */
?public int hashCode() {
??int PRIME = 31;
??int result = 1;
??result = result * 31 + ((this.name == null) ? 0 : this.name.hashCode());
??result = result * 31 + this.size;
??result = result * 31 + ((this.style == null) ? 0 : this.style.hashCode());
??return result * 31 + ((this.active) ? 1231 : 1237);
?}
?/*
? * 因為 @Data注釋,生成了隨后的方法
? */
?public String getName() { return this.name; }
?public void setName(String name) { this.name = name; }
?public void setSize(int size) { this.size = size; }
?public String getColor() { return this.color; }
?public void setColor(String color) { this.color = color; }
?public String getStyle() { return this.style; }
?public void setStyle(String style) { this.style = style; }
?public boolean isActive() { return this.active; }
?public void setActive(boolean active) { this.active = active; }
} 小結(jié) 必須要說 Lombok 確是個小而精干的實用工具,但還有優(yōu)化改進的地方,比如 @ToString、@EqualsAndHashCode等注釋類的不包括(exclude)只允許單項。這意味著不需要(exclude)多項屬性(field)時則不被允許。 ?
@EqualsAndHashCode(exclude="color")
public @Data class Lure {
????private String name;
????private @Getter(AccessLevel.PACKAGE) int size;
????private String color;
????private String style;
????private boolean active;
?
????@Synchronized
????public void operate(String[] args) throws IOException {
??????? @Cleanup InputStream in = new FileInputStream(args[0]);????????? ??????? @Cleanup OutputStream out = new FileOutputStream(args[1]);
??????? //write file code goes here
????}
} 2,通過 javap 獲得編譯后的源文件(Lure.java)。顯然,我們沒有看到 @Synchronized 說起的作用。事實上,Lombok 是通過添加一個$lock對象來實現(xiàn)的,事實上,可以給 @Synchronized 添加參數(shù)的,比如@Synchronized("localock")。而有關(guān)toString()、equals()和hashCode()方法的實際情況,只有通過 JD 反編譯其類文件后才查看得到(詳見 3)。 D:\lombok\src\javac -cp lombok.jar Lure.java D:\lombok\src\javap Lure
Compiled from "Lure.java"
public class Lure extends java.lang.Object{
??? public void operate(java.lang.String[]) throws java.io.IOException;
??? public java.lang.String toString();
??? public boolean equals(java.lang.Object);
??? public int hashCode();
??? public spike.lombok.Lure();
??? public java.lang.String getName();
??? public void setName(java.lang.String);
??? public void setSize(int);
??? public java.lang.String getColor();
??? public void setColor(java.lang.String);
??? public java.lang.String getStyle();
??? public void setStyle(java.lang.String);
??? public boolean isActive();
??? public void setActive(boolean);
??? int getSize();
} 此外,通過 Eclipse? IDE工具,可瀏覽到經(jīng) Lombok 注釋編織代碼后的代碼樹(Outline),見下圖: ? 3,然后,試試通過?Java Decompiler?獲得反編譯后的源文件(Lure.java)內(nèi)部方法的實現(xiàn)情況會如何。正如我們看到的,Lombok 做得非常棒。它把我們編程時應(yīng)該寫的代碼全部編織好了。僅僅是通過少量的特定的注釋類。 public class Lure {
?private String name;
?private int size;
?private String color;
?private String style;
?private boolean active;
?private final Object $lock; //因為@Synchronized注釋
?public Lure() {
??this.$lock = new Object[0];//因為@Synchronized注釋
?}
?public void operate(String[] args) throws IOException {
??synchronized (this.$lock) {//因為@Synchronized注釋
???InputStream in = new FileInputStream(args[0]);
???try {//因為 @Cleanup注釋
????OutputStream out = new FileOutputStream(args[1]);
????out.close();
???} finally {
????in.close();//因為 @Cleanup注釋
???}
??}
?}
?int getSize() { return this.size; }//包可見訪問限制, @Getter(AccessLevel.PACKAGE)注釋
?public String toString() {//因為 @ToString(exclude="color")注釋,見不到處理 color屬性
??return "Lure(name=" + this.name + ", size=" + this.size + ", style="
????+ this.style + ", active=" + this.active + ")";
?}
?/*
? * 因為@EqualsAndHashCode(exclude="color")注釋,見不到處理 color屬性
? * 此方法包含反編譯生成代碼時的錯誤,如break label64,與Lombok編織代碼無關(guān)
? */
?public boolean equals(Object o) {
??if (o == this) return true;
??if (o == null) return false;
??if (o.getClass() != super.getClass()) return false;
??Lure other = (Lure) o;
??if (this.name == null)
???if (other.name == null) break label64;
???else if (this.name.equals(other.name)) break label64;
??return false;
??if (this.size != other.size) label64: return false;
??if (this.style == null)
???if (other.style == null) break label110;
???else if (this.style.equals(other.style)) break label110;
??return false;
??label110: return this.active == other.active;
?}
?/*
? * 因為@EqualsAndHashCode(exclude="color")注釋,見不到處理 color屬性
? * 存在反編譯生成代碼的情況,如 有 PRIME 的表達式,直接是 31, 與Lombok編織代碼無關(guān)
? */
?public int hashCode() {
??int PRIME = 31;
??int result = 1;
??result = result * 31 + ((this.name == null) ? 0 : this.name.hashCode());
??result = result * 31 + this.size;
??result = result * 31 + ((this.style == null) ? 0 : this.style.hashCode());
??return result * 31 + ((this.active) ? 1231 : 1237);
?}
?/*
? * 因為 @Data注釋,生成了隨后的方法
? */
?public String getName() { return this.name; }
?public void setName(String name) { this.name = name; }
?public void setSize(int size) { this.size = size; }
?public String getColor() { return this.color; }
?public void setColor(String color) { this.color = color; }
?public String getStyle() { return this.style; }
?public void setStyle(String style) { this.style = style; }
?public boolean isActive() { return this.active; }
?public void setActive(boolean active) { this.active = active; }
} 小結(jié) 必須要說 Lombok 確是個小而精干的實用工具,但還有優(yōu)化改進的地方,比如 @ToString、@EqualsAndHashCode等注釋類的不包括(exclude)只允許單項。這意味著不需要(exclude)多項屬性(field)時則不被允許。 ?
轉(zhuǎn)載于:https://blog.51cto.com/marchtiger/298029
總結(jié)
以上是生活随笔為你收集整理的探测 Lombok 工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AS3中,setTimeOut、setI
- 下一篇: 转PET灌装机的完善