一款好用的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