写点什么

一款好用的 Java 插件 - Lombok

  • 2021 年 12 月 14 日
  • 本文字数:2759 字

    阅读完需:约 9 分钟

作者:烧鸡太子爷

来源:恒生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 主要功能是通过注解的方式,用到的核心的功能我整理画了一个图


4067

Lombok 的使用

官网说明

官网有介绍很多引入的方式,可以参考官网 install 介绍


4068

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,点击进去就可以看到相关的说明和使用样例


4069

参考

官网https://projectlombok.org/


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


github 项目https://github.com/projectlombok/lombok

发布于: 3 小时前阅读数: 8
用户头像

还未添加个人签名 2018.11.07 加入

还未添加个人简介

评论

发布
暂无评论
一款好用的Java插件 - Lombok