官網地址:https://projectlombok.org html
提供的註解:https://projectlombok.org/features/index.html java
下載連接:https://projectlombok.org/download.html eclipse
使用 lombok 是須要安裝的,若是不安裝,IDE 則沒法解析 lombok 註解 maven
java -jar lombok-1.16.6.jar 目前最新的版本是:1.16.6 ide
而後按照提示進行安裝,若是不能檢測到安裝的Eclipse,手工指定Eclipse的安裝目錄便可。
this
安裝後,會在Eclipse安裝目錄中增長lombok.jar, 並在eclipse.ini中增長以下一行:
spa
-javaagent:lombok.jar 翻譯
安裝截圖:
code
注意:代碼compile後,會根據lombok的註解,增長指定的代碼 xml
好比使用@Data註解,則編譯後的字節碼中會爲全部屬性字段增長getter setter方法
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.6</version> <scope>provided</scope> </dependency>
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import lombok.AllArgsConstructor; import lombok.Cleanup; import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.java.Log; @Log @NoArgsConstructor @AllArgsConstructor @Data public class People { private String id; private String name; private String identity; public void writerObj(String inFile,String outFile) throws IOException { @Cleanup InputStream in = new FileInputStream(inFile); @Cleanup OutputStream out = new FileOutputStream(outFile); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } }
編譯後生成的class文件,反翻譯後的代碼
import java.beans.ConstructorProperties; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Collections; import java.util.List; import java.util.logging.Logger; public class People { private static final Logger log = Logger.getLogger(People.class.getName()); private String id; private String name; private String identity; public void writerObj(String inFile, String outFile) throws IOException { InputStream in = new FileInputStream(inFile); try { OutputStream out = new FileOutputStream(outFile); try { byte[] b = new byte[10000]; int r = in.read(b); if (r != -1) { out.write(b, 0, r); } } finally { if (Collections.singletonList(out).get(0) != null) out.close(); } } finally { if (Collections.singletonList(in).get(0) != null) in.close(); } } public People() { } @ConstructorProperties({"id", "name", "identity"}) public People(String id, String name, String identity) { this.id = id; this.name = name; this.identity = identity; } public String getId() { return this.id; } public String getName() { return this.name; } public String getIdentity() { return this.identity; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setIdentity(String identity) { this.identity = identity; } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof People)) return false; People other = (People)o; if (!other.canEqual(this)) return false; Object this$id = getId(); Object other$id = other.getId(); if (this$id == null ? other$id != null : !this$id.equals(other$id)) return false; Object this$name = getName(); Object other$name = other.getName(); if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false; Object this$identity = getIdentity(); Object other$identity = other.getIdentity(); return this$identity == null ? other$identity == null : this$identity.equals(other$identity); } protected boolean canEqual(Object other) { return other instanceof People; } public int hashCode() { int PRIME = 59; int result = 1; Object $id = getId(); result = result * 59 + ($id == null ? 43 : $id.hashCode()); Object $name = getName(); result = result * 59 + ($name == null ? 43 : $name.hashCode()); Object $identity = getIdentity(); result = result * 59 + ($identity == null ? 43 : $identity.hashCode()); return result; } public String toString() { return "People(id=" + getId() + ", name=" + getName() + ", identity=" + getIdentity() + ")"; } }