一款好用的Java外掛 - Lombok

語言: CN / TW / HK

作者:燒雞太子爺

來源:恆生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主要功能是通過註解的方式,用到的核心的功能我整理畫了一個圖

1.png

Lombok的使用

官網說明

官網有介紹很多引入的方式,可以參考官網install介紹

2.png

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,點選進去就可以看到相關的說明和使用樣例

3.png

參考

官網https://projectlombok.org/

文件https://projectlombok.org/features/all

github專案https://github.com/projectlombok/lombok