目录
参考文档
什么是 Project Lombok ?
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
Project Lombok是一个java库,它可以自动插入编辑器和构建工具,增加java的趣味性。
永远不要再写另一个 getter 或 equals 方法,只需一个注释,你的类就有了一个功能齐全的构建器,自动化你的日志变量,等等。
总结:使用注解即可替换 getter setter equals 等方法,在编译期自动生成生成getter setter equals 等方法代码。
下载 Project Lombok
- 下载页面:https://projectlombok.org/download
- 下载地址:https://projectlombok.org/downloads/lombok.jar
- 历史版本:https://projectlombok.org/all-versions
支持的安装方式
安装 Project Lombok
lombok.jar 安装
依赖:必须保证安装JDK并配置环境变量
windos 打开 CMD命令窗口,执行下面命令执行安装:
java -jar lombok.jar
lombok 会自动扫描已安装的 IDE 开发工具:
如果IDE 不是 exe 安装的,则lombok会找不到已安装的IDE:
这时,点击 【Specify location ...】按钮,自行选择 IDE 开发工具的程序(我本地是 eclipse):
继续点击 【Install / Update】按钮进行安装。
安装完成后,:
至此,安装完成。后续步骤:
之后需要 将 lombok.jar 添加到你的项目中。
eclipse 安装 lombok 插件
两种方式:
- 使用 lombok.jar 安装,选择本机的 eclipse
- 使用 eclipse 市场安装插件 https://projectlombok.org/p2
IntelliJ 安装 lombok 插件
步骤:
- 选择 File > Settings > Plugins
- 点击 Browse repositories...
- 查找 Lombok 插件
- 点击 Install plugin
- 重启 IntelliJ IDEA
使用 Project Lombok
- 新建 maven 项目
- 引入 lombok.jar
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
- 编写标注 lombok注解的 Response.java 类
import java.io.Serializable;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Response implements Serializable {
private static final long serialVersionUID = 5001615840097014470L;
private String code;
private boolean success;
private String message;
}
- eclipse 中 使用 “Ctrl + o” 快捷键查看类结构:
注意:@Builder 会把无参构造方法私有化!
Lombok 提供的注解
@GETTER AND @SETTER
@GETTER 和 @SETTER 注解依次生成get,set方法。
例子:
@Getter @Setter private boolean employed = true;
@Setter(AccessLevel.PROTECTED) private String name;
等同于
private boolean employed = true;
private String name;
public boolean isEmployed() {
return employed;
}
public void setEmployed(final boolean employed) {
this.employed = employed;
}
protected void setName(final String name) {
this.name = name;
}
@NONNULL
@NONNULL 表明为类的成员添加空指针校验。
例子:
@Getter @Setter @NonNull
private List<Person> members;
等同于
@NonNull
private List<Person> members;
public Family(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
@NonNull
public List<Person> getMembers() {
return members;
}
public void setMembers(@NonNull final List<Person> members) {
if (members == null) throw new java.lang.NullPointerException("members");
this.members = members;
}
@TOSTRING
@TOSTRING 注解生成 toString
方法的实现。
例子:
@ToString(callSuper=true,exclude="someExcludedField")
public class Foo extends Bar {
private boolean someBoolean = true;
private String someStringField;
private float someExcludedField;
}
等同于
public class Foo extends Bar {
private boolean someBoolean = true;
private String someStringField;
private float someExcludedField;
@java.lang.Override
public java.lang.String toString() {
return "Foo(super=" + super.toString() +
", someBoolean=" + someBoolean +
", someStringField=" + someStringField + ")";
}
}
@EQUALSANDHASHCODE
@EQUALSANDHASHCODE 注解会指示 Lombok 生成 equals
和 hashCode
方法。
例子:
@EqualsAndHashCode(callSuper=true,exclude={"address","city","state","zip"})
public class Person extends SentientBeing {
enum Gender { Male, Female }
@NonNull private String name;
@NonNull private Gender gender;
private String ssn;
private String address;
private String city;
private String state;
private String zip;
}
等同于
public class Person extends SentientBeing {
enum Gender {
/*public static final*/ Male /* = new Gender() */,
/*public static final*/ Female /* = new Gender() */;
}
@NonNull
private String name;
@NonNull
private Gender gender;
private String ssn;
private String address;
private String city;
private String state;
private String zip;
@java.lang.Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != this.getClass()) return false;
if (!super.equals(o)) return false;
final Person other = (Person)o;
if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
if (this.gender == null ? other.gender != null : !this.gender.equals(other.gender)) return false;
if (this.ssn == null ? other.ssn != null : !this.ssn.equals(other.ssn)) return false;
return true;
}
@java.lang.Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + super.hashCode();
result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
result = result * PRIME + (this.gender == null ? 0 : this.gender.hashCode());
result = result * PRIME + (this.ssn == null ? 0 : this.ssn.hashCode());
return result;
}
}
@DATA
@Data 注解相当于 @ToString, @EqualsAndHashCode, @Getter 和 @Setter 注解的集合。
例子:
@Data(staticConstructor="of")
public class Company {
private final Person founder;
private String name;
private List<Person> employees;
}
等同于
public class Company {
private final Person founder;
private String name;
private List<Person> employees;
private Company(final Person founder) {
this.founder = founder;
}
public static Company of(final Person founder) {
return new Company(founder);
}
public Person getFounder() {
return founder;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<Person> getEmployees() {
return employees;
}
public void setEmployees(final List<Person> employees) {
this.employees = employees;
}
@java.lang.Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != this.getClass()) return false;
final Company other = (Company)o;
if (this.founder == null ? other.founder != null : !this.founder.equals(other.founder)) return false;
if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
if (this.employees == null ? other.employees != null : !this.employees.equals(other.employees)) return false;
return true;
}
@java.lang.Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + (this.founder == null ? 0 : this.founder.hashCode());
result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
result = result * PRIME + (this.employees == null ? 0 : this.employees.hashCode());
return result;
}
@java.lang.Override
public java.lang.String toString() {
return "Company(founder=" + founder + ", name=" + name + ", employees=" + employees + ")";
}
}
@CLEANUP
@Cleanup 注解用于确保分配的资源能够释放掉。如果本地变量标记了 @Cleanup 注解,则子代码将会被 try/finally 块包围住。
例子:
public void testCleanUp() {
try {
@Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(new byte[] {'Y','e','s'});
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
等同于
public void testCleanUp() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(new byte[]{'Y', 'e', 's'});
System.out.println(baos.toString());
} finally {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@SYNCHRONIZED
@Synchronized 注解在方法内添加 synchronized 同步代码块。
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");
@Synchronized
public String synchronizedFormat(Date date) {
return format.format(date);
}
等同于
private final java.lang.Object $lock = new java.lang.Object[0];
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");
public String synchronizedFormat(Date date) {
synchronized ($lock) {
return format.format(date);
}
}
@SNEAKYTHROWS
@SneakyThrows 注解是为了解决抛出异常时 IDE 提示“Unhandled exception ...”:
@SneakyThrows 注解实际就是抛出异常包装成 RuntimeException。
@SneakyThrows 注解加在方法上:
@SneakyThrows
public void testSneakyThrows() {
throw new IllegalAccessException();
}
等同于:
public void testSneakyThrows() {
try {
throw new IllegalAccessException();
} catch (java.lang.Throwable $ex) {
throw lombok.Lombok.sneakyThrow($ex);
}
}
sneakyThrow 方法:
public static RuntimeException sneakyThrow(Throwable t) {
if (t == null) throw new NullPointerException("t");
return Lombok.<RuntimeException>sneakyThrow0(t);
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> T sneakyThrow0(Throwable t) throws T {
throw (T)t;
}
标签:return,name,记录,private,Project,Lombok,null,public,String
From: https://www.cnblogs.com/lihw-study/p/16280912.html