一款好用的Java外掛 - Lombok
作者:燒雞太子爺
來源:恆生LIGHT雲社群
前面介紹了一款java的maven外掛叫maven helper,很多同學還挺感興趣的,今天再接再厲,給大家再推薦一款還用的java外掛叫lombok。先給大家講講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 程式碼的工具,簡單來說,比如我們新建了一個類,然後在其中寫了幾個欄位,然後通常情況下我們需要手動去建立getter和setter方法啊,建構函式啊之類的,lombok的作用就是為了省去我們手動建立這些程式碼的麻煩,它能夠在我們編譯原始碼的時候自動幫我們生成這些方法
Lombok的主要功能
Lombok主要功能是通過註解的方式,用到的核心的功能我整理畫了一個圖
Lombok的使用
官網說明
官網有介紹很多引入的方式,可以參考官網install介紹
maven引入
本文只介紹maven引入,pom檔案中直接引入lombok,目前官網最新的版本是1.18.22
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
使用示例
lombok使用過程中主要是靠註解起作用的,官網上的文件裡面有所有的註解,這裡不一一羅列,只拿@Data做舉例,程式碼也是來自官網。
@Data
@Data註解在類上,會為類的所有屬性自動生成setter/getter、equals、canEqual、hashCode、toString方法,如為final屬性,則不會為該屬性生成setter方法。
import lombok.AccessLevel;
import lombok.Setter;
import lombok.Data;
import lombok.ToString;
@Data public class DataExample {
private final String name;
@Setter(AccessLevel.PACKAGE) private int age;
private double score;
private String[] tags;
@ToString(includeFieldNames=true)
@Data(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private final T value;
}
}
如不使用Lombok,則實現如下:
import java.util.Arrays;
public class DataExample {
private final String name;
private int age;
private double score;
private String[] tags;
public DataExample(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return this.score;
}
public String[] getTags() {
return this.tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override public String toString() {
return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
}
protected boolean canEqual(Object other) {
return other instanceof DataExample;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof DataExample)) return false;
DataExample other = (DataExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (this.getAge() != other.getAge()) return false;
if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + this.getAge();
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
return result;
}
public static class Exercise<T> {
private final String name;
private final T value;
private Exercise(String name, T value) {
this.name = name;
this.value = value;
}
public static <T> Exercise<T> of(String name, T value) {
return new Exercise<T>(name, value);
}
public String getName() {
return this.name;
}
public T getValue() {
return this.value;
}
@Override public String toString() {
return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
}
protected boolean canEqual(Object other) {
return other instanceof Exercise;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Exercise)) return false;
Exercise<?> other = (Exercise<?>) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
return result;
}
}
}
其他的可以參考官網的地址 https://projectlombok.org/features/all,點選進去就可以看到相關的說明和使用樣例
參考
- 淺析大資料框架 Hadoop
- 一款好用的Java外掛 - Lombok
- 深入程式碼理解MVC模式、MVP模式和MVVM模式
- 深入理解大資料數倉工具 Apache Hive 底層原理
- ES2022 有什麼新功能
- 前端快取那些事
- 大資料分散式計算系統 Spark 入門核心之 RDD
- Nginx基本介紹 跨域解決方案
- 「MySQL」資料庫備份和還原
- 雲原生PaaS平臺LIGHT-CORE實踐之道:基於K8s,聚焦服務與價值
- 聊聊 ab 和 jmeter 的併發模型
- 【Pandas學習筆記01】強大的分析結構化資料的工具集
- 白話 Linux 容器資源的隔離限制原理
- Hadoop 入門筆記—核心元件 YARN
- Hadoop 入門筆記—核心元件 YARN
- Hadoop 入門筆記—核心元件 MapRuduce
- Hadoop 入門筆記—核心元件 HDFS
- Python量化資料倉庫搭建3:資料落庫程式碼封裝
- Python量化資料倉庫搭建系列2:Python操作資料庫
- Python量化資料倉庫搭建系列2:Python操作資料庫