Lombok會自動插入編輯器和構建工具中,在以前的Java項目中,充斥着太多不友好的代碼:getter/setter/toString;異常處理;I/O流的關閉操作等等,這些樣板代碼既沒有技術含量,又影響着代碼的美觀,Lombok提供了一組有用的註釋,用來消除Java類中的大量樣板代碼。
Lombok使用方法
Lombok 能通過註解的方式,在編譯時自動爲屬性生成構造器、getter/setter、equals、hashcode、toString 方法。出現的神奇就是在源碼中沒有 getter 和 setter 方法,但是在編譯生成的字節碼文件中有 getter 和 setter 方法。這樣就省去了手動重建這些代碼的麻煩,使代碼看起來更簡潔些。
Lombok 的使用跟引用 jar 包一樣,可以在官網(https://projectlombok.org/download)下載 jar 包,也可以使用 maven 添加依賴:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
注意:
IDEA第一次使用 Lombok 插件需要做如下幾步配置
- 將 Lombok 插件安裝到 IDEA(File -> Setting -> plugins,搜索 Lombok,點擊 Install)
- 選擇默認的編譯方式爲 javac,因爲 eclipse 是不支持 Lombok 的編譯方式的,javac 支持 Lombok 的編譯方式。
- 打開註解生成器 Enable annotation processing。
IntelliJ IDEA 2019.2 默認是不支持 Lombok 插件的,需要去
https://plugins.jetbrains.com/plugin/6317-lombok/versions
下載對應版本的插件,然後手動引入,在 IDEA 中選擇 File -> Setting -> plugins 找到 Install Plugin from Disk…(注意版本不同位置可能有所差異)
異常處理:
Lombok Requires Annotation Processing Annotation processing seems to be disabled for the project ...
根據提示將這裏勾選上即可
Lombok 中註解的具體用法:
@Data
@Data 註解在類上,會爲類的所有屬性自動生成 setter/getter、equals、canEqual、hashCode、toString 方法,如爲 final 屬性,則不會爲該屬性生成 setter 方法。
@Data
public class Student {
private String name;
private Integer age;
private Integer id;
private String major;
}
這樣就可以調用 set/get 方法了。
@Getter/@Setter
如果覺得@Data 太過殘暴(因爲@Data 集合了@ToString、@EqualsAndHashCode、@Getter/@Setter、@RequiredArgsConstructor 的所有特性)不夠精細,可以使用@Getter/@Setter 註解,此註解在屬性上,可以爲相應的屬性自動生成 set/get 方法。
public class Student {
@Setter private String name;
private Integer age;
private Integer id;
private String major;
public static void main(String[] args) {
Student stu = new Student();
stu.setName("Mr.ml");
}
}
@NonNull
該註解用在屬性或構造器上,Lombok 會生成一個非空的聲明,可用於校驗參數,能幫助避免空指針。
public class Student {
@Setter private String name;
private Integer age;
private Integer id;
private String major;
public Student(@NonNull String name) {
this.name = name;
}
}
@Cleanup
該註解能幫助我們自動調用 close() 方法,很大的簡化了代碼。
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);
}
}
}
@EqualsAndHashCode
默認情況下,會使用所有非靜態(non-static)和非瞬態(non-transient)屬性來生成 equals 和 hashCode,也能通過 exclude 註解來排除一些屬性。
@EqualsAndHashCode(exclude={"id", "shape"})
public class EqualsAndHashCodeExample {
private transient int transientVar = 10;
private String name;
private double score;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.name;
}
@EqualsAndHashCode(callSuper=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}
@ToString
類使用@ToString 註解,Lombok 會生成一個 toString() 方法,默認情況下,會輸出類名、所有屬性(會按照屬性定義順序),用逗號來分割。
通過將 includeFieldNames 參數設爲 true,就能明確的輸出 toString() 屬性。這一點是不是有點繞口,通過代碼來看會更清晰些。
@ToString(exclude="id")
public class ToStringExample {
private static final int STATIC_VAR = 10;
private String name;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.getName();
}
@ToString(callSuper=true, includeFieldNames=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
無參構造器、部分參數構造器、全參構造器。Lombok沒法實現多種參數構造器的重載。
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
@NoArgsConstructor
public static class NoArgsExample {
@NonNull private String field;
}
}