简介
官网地址:https://projectlombok.org
提供的注解:https://projectlombok.org/features/index.html
下载链接:https://projectlombok.org/download.html
安装方式:
使用 lombok 是需要安装的,如果不安装,IDE 则无法解析 lombok 注解
java -jar lombok-1.16.6.jar 目前最新的版本是:1.16.6
然后按照提示进行安装,如果不能检测到安装的Eclipse,手工指定Eclipse的安装目录即可。
安装后,会在Eclipse安装目录中增加lombok.jar, 并在eclipse.ini中增加如下一行:
-javaagent:lombok.jar
安装截图:
如何在maven项目中引入
注意:代码compile后,会根据lombok的注解,增加指定的代码
比如使用@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() + ")"; }
}