官网地址: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() + ")"; } }