Lombok 一個充滿爭議性的插件,不能否認它的神奇,大量的讓我們偷懶了,不過在減少代碼量的同時,當某一個模塊使用 Lombok 后,其余依賴此模塊的其他代碼都需要引入 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.
Lombok 是一種 Java 實用工具,可用來幫助開發人員消除 Java 的冗長,尤其是對于簡單的 Java 物件(POJO),它通過注釋實作這一目的,通過在開發環境中實作 Lombok,開發人員可以節省構建諸如 hashCode() 和 equals() 這樣的方法以及以往用來分類各種 accessor 和 mutator 的大量時間,
安裝
IDEA 安裝 Lombok 插件
1、Setting → Plugin,搜索 Lombok 安裝即可

IDEA 2020.3 版本開始已經內置 Lombok 插件,

2、匯入 Lombok 依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
Eclipse 安裝 Lombok 插件
1、官網下載 jar 包:https://projectlombok.org/download
2、雙擊 lombak.jar 包
3、點擊 Install / Update

4、點擊Quit Installer,完成安裝

5、安裝完成之后,確認 eclipse 安裝路徑下是否多了一個 lombok.jar 包,并且組態檔 eclipse.ini 中是否添加了如下內容:
-javaagent:D:\eclipse-jee-2021-03-R-win32-x86_64\eclipse\lombok.jar
Lombok 常用注解說明
Lombok注解檔案
@NonNull
@Cleanup
@Getter and @Setter
@AllArgsConstructor
@RequiredArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
@Data
@NonNull
用在成員方法或者構造方法的引數前面,會自動產生一個關于此引數的非空檢查,如果引數為空,則拋出一個空指標例外
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
this.name = person.getName();
}
}
相當于
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
}
@Cleanup
用在變數前面,可以保證此變數代表的資源會被自動關閉,默認是呼叫資源的close()方法
import lombok.Cleanup;
import java.io.*;
public class CleanupExample {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}
相當于
import java.io.*;
public class CleanupExample {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(args[0]);
try {
OutputStream out = new FileOutputStream(args[1]);
try {
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
} finally {
if (out != null) {
out.close();
}
}
} finally {
if (in != null) {
in.close();
}
}
}
}
@Getter and @Setter
在成員變數前面,相當于為成員變數生成對應的 get 和 set 方法,還可以為生成的方法指定訪問修飾符,默認為public
在類上,可以為此類里的所有非靜態成員變數生成對應的 get 和 set 方法,
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
public class GetterSetterExample {
@Getter
@Setter
private int age;
@Setter(AccessLevel.PROTECTED)
private String name;
}
相當于
public class GetterSetterExample {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
protected void setName(String name) {
this.name = name;
}
}
@AllArgsConstructor / @NoArgsConstructor
在類上使用,為該類產生全參構造方法和無參構造方法
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String password;
}
相當于
public class User {
private int id;
private String name;
private String password;
public User() {
}
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
@ToString
在類上使用,生成 toString() 方法
- @ToString.Exclude 排除指定欄位
- @ToString.Include 包含指定欄位
import lombok.ToString;
@ToString
public class User {
@ToString.Exclude
private int id;
private String name;
}
相當于
public class User {
private int id;
private String name;
public User() {
}
public String toString() {
return "User(name=" + this.name + ")";
}
}
@EqualsAndHashCode
生成 equals() 和 hashCode() 方法,以及 canEqual() 方法用來判斷某個物件是否是當前類的實體,生成方法時只會使用類中的 非靜態 和 非transient 成員變數
- @EqualsAndHashCode.Exclude 排除指定欄位
- @EqualsAndHashCode.Include 包含指定欄位
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public class User {
private int id;
private String name;
}
相當于
public class User {
private int id;
private String name;
public User() {
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else if (this.id != other.id) {
return false;
} else {
Object this$name = this.name;
Object other$name = other.name;
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.id;
Object $name = this.name;
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
}
@Data
包含了 @Getter and @Setter 、@ToString、@EqualsAndHashCode、@NoArgsConstructor
import lombok.Data;
@Data
public class User {
private int id;
private String name;
}
相當于
public class User {
private int id;
private String name;
public User() {
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
return false;
} else {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getId();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336234.html
標籤:java
