Lombok 项目是一个 Java 库,它会自动插入编辑器和构建工具中,Lombok 提供了一组有用的注解,用来消除 Java 类中的大量样板代码。
@[TOC]
官方介绍
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 库,它会自动插入您的编辑器和构建工具中,简化您的 Java 。不需要再写另一个 getter、setter、toString 或 equals 方法,带有一个注释的您的类有一个功能全面的生成器,可以自动化您的日志记录变量,以及更多其他功能
官网链接
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
注意: 在这里 scope
要设置为 provided
, 防止依赖传递给其他项目
在开发过程中,一般还需要配合插件使用,在 IDEA 中需要安装 Lombok
插件即可
为什么要安装插件?
首先在不安装插件的情况下,代码是可以正常的编译和运行的。如果不安装插件,IDEA 不会自动提示 Lombok 在编译时才会生成的一些样板方法,同样 IDEA 在校验语法正确性的时候也会提示有问题,会有大面积报红的代码
下面举两个栗子,看看使用 lombok 和不使用的区别
创建一个用户类
不使用 Lombok
public class User {
private Integer id;
private Integer age;
private String realName;
public User() {
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
if (!Objects.equals(id, user.id)) {
return false;
}
if (!Objects.equals(age, user.age)) {
return false;
}
return Objects.equals(realName, user.realName);
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (age != null ? age.hashCode() : 0);
result = 31 * result + (realName != null ? realName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", realName='" + realName + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
}
使用Lombok
@Data
public class User {
private Integer id;
private String username;
private Integer age;
}
使用 @Data
注解会在编译的时候自动生成以下模板代码:
关于什么是必要参数下面会举例说明
上面已经简单看了一下 @Data
注解,下面看下所有的可以用的注解
@Setter
不起作用关于所有的注解可以查看 https://projectlombok.org/features/all
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter // 生成 getter
@AllArgsConstructor // 生成所有的参数
@RequiredArgsConstructor // 生成必要参数的构造器
@ToString // 生成 toString
@EqualsAndHashCode // 生成 equals 和 hashCode
@Builder // 生成一个 builder
public class UserLombok {
// 创建 setter 并且校验 id 不能为空
@Setter
@NonNull
private Integer id;
// 创建 setter 且生成方法的访问级别为 PROTECTED
@Setter(AccessLevel.PROTECTED)
private Integer age;
// 创建 setter 不校验是否为空
@Setter
private String realName;
// 构造器,校验 id 不能为空
public UserLombok(@NonNull Integer id, Integer age) {
this.id = id;
this.age = age;
}
/**
* 自定义 realName 的 setter 方法,这个优先高于 Lombok
* @param realName 真实姓名
*/
public void setRealName(String realName) {
this.realName = "realName:" + realName;
}
}
具体生成的类为
import lombok.NonNull;
public class UserLombok {
@NonNull
private Integer id;
private Integer age;
private String realName;
public UserLombok(@NonNull Integer id, Integer age) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
this.age = age;
}
}
public void setRealName(String realName) {
this.realName = "realName:" + realName;
}
public static UserLombok.UserLombokBuilder builder() {
return new UserLombok.UserLombokBuilder();
}
@NonNull
public Integer getId() {
return this.id;
}
public Integer getAge() {
return this.age;
}
public String getRealName() {
return this.realName;
}
public UserLombok(@NonNull Integer id, Integer age, String realName) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
this.age = age;
this.realName = realName;
}
}
public UserLombok(@NonNull Integer id) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
}
}
public String toString() {
return "UserLombok(id=" + this.getId() + ", age=" + this.getAge() + ", realName=" + this.getRealName() + ")";
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof UserLombok)) {
return false;
} else {
UserLombok other = (UserLombok)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label47;
}
} else if (this$id.equals(other$id)) {
break label47;
}
return false;
}
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
Object this$realName = this.getRealName();
Object other$realName = other.getRealName();
if (this$realName == null) {
if (other$realName != null) {
return false;
}
} else if (!this$realName.equals(other$realName)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof UserLombok;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $realName = this.getRealName();
result = result * 59 + ($realName == null ? 43 : $realName.hashCode());
return result;
}
public void setId(@NonNull Integer id) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
}
}
protected void setAge(Integer age) {
this.age = age;
}
public static class UserLombokBuilder {
private Integer id;
private Integer age;
private String realName;
UserLombokBuilder() {
}
public UserLombok.UserLombokBuilder id(@NonNull Integer id) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
return this;
}
}
public UserLombok.UserLombokBuilder age(Integer age) {
this.age = age;
return this;
}
public UserLombok.UserLombokBuilder realName(String realName) {
this.realName = realName;
return this;
}
public UserLombok build() {
return new UserLombok(this.id, this.age, this.realName);
}
public String toString() {
return "UserLombok.UserLombokBuilder(id=" + this.id + ", age=" + this.age + ", realName=" + this.realName + ")";
}
}
}
@Value
public class UserLombok {
@NonNull
private Integer id;
// 这里的 setter 不会生成,所有没用,这里反面示例
@Setter(AccessLevel.PROTECTED)
private Integer age;
private String realName;
}
@Value
是 ToString、EqualsAndHashCode、AllArgsConstructor、Getter
的组合注解
生成的代码
import lombok.NonNull;
public final class UserLombok {
@NonNull
private final Integer id;
private final Integer age;
private final String realName;
public UserLombok(@NonNull Integer id, Integer age, String realName) {
if (id == null) {
throw new NullPointerException("id is marked non-null but is null");
} else {
this.id = id;
this.age = age;
this.realName = realName;
}
}
@NonNull
public Integer getId() {
return this.id;
}
public Integer getAge() {
return this.age;
}
public String getRealName() {
return this.realName;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof UserLombok)) {
return false;
} else {
UserLombok other;
label44: {
other = (UserLombok)o;
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label44;
}
} else if (this$id.equals(other$id)) {
break label44;
}
return false;
}
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
Object this$realName = this.getRealName();
Object other$realName = other.getRealName();
if (this$realName == null) {
if (other$realName != null) {
return false;
}
} else if (!this$realName.equals(other$realName)) {
return false;
}
return true;
}
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $realName = this.getRealName();
result = result * 59 + ($realName == null ? 43 : $realName.hashCode());
return result;
}
public String toString() {
return "UserLombok(id=" + this.getId() + ", age=" + this.getAge() + ", realName=" + this.getRealName() + ")";
}
}
日志使用
import lombok.extern.java.Log;
@Log
public class LogLombok {
public void log() {
log.info("打个日志");
}
}
生成后代码
import java.util.logging.Logger;
public class LogLombok {
private static final Logger log = Logger.getLogger(LogLombok.class.getName());
public LogLombok() {
}
public void log() {
log.info("打个日志");
}
}
通过上面的示例,我们可以看出 Lombok 可以大大简化我们的代码
优点:
record
, 也在帮我们从原生方面解决这种模板代码缺点:
编译时出错,可能是没有启用注解处理器。Build, Execution, Deployment > Annotation Processors > Enable annotation processing
。设置完成之后程序正常运行。
@Data
注解, 这个注解太全了,不利于维护,除非你知道你在干什么@AllArgsConstructor
注解时,记得加上 @NoArgsConstructor
@AllArgsConstructor
注解@Setter
、@Getter
注解如果需要可以缩小使用范围@ToString
注解默认不会生成父类的信息,如果需要生成需要 @ToString(callSuper = true)
@RequiredArgsConstructor
和 @NoArgsConstructor
尽量不要一起使用,无参构造器无法处理 @NonNull
,但在序列化/反序列化的还是需要提供无参的Refactor > Delombok
中再次注意- @AllArgsConstructor
尽量不要使用
工作原理来自网上资料
在Lombok使用的过程中,只需要添加相应的注解,无需再为此写任何代码。自动生成的代码到底是如何产生的呢?
核心之处就是对于注解的解析上。JDK5引入了注解的同时,也提供了两种解析方式。
运行时能够解析的注解,必须将@Retention设置为RUNTIME,这样就可以通过反射拿到该注解。java.lang.reflect反射包中提供了一个接口AnnotatedElement,该接口定义了获取注解信息的几个方法,Class、Constructor、Field、Method、Package等都实现了该接口,对反射熟悉的朋友应该都会很熟悉这种解析方式。
编译时解析有两种机制,分别简单描述下:
1)Annotation Processing Tool
apt自JDK5产生,JDK7已标记为过期,不推荐使用,JDK8中已彻底删除,自JDK6开始,可以使用Pluggable Annotation Processing API来替换它,apt被替换主要有2点原因:
2)Pluggable Annotation Processing API
JSR 269自JDK6加入,作为apt的替代方案,它解决了apt的两个问题,javac在执行的时候会调用实现了该API的程序,这样我们就可以对编译器做一些增强,javac执行的过程如下:
Lombok本质上就是一个实现了“JSR 269 API”的程序。在使用javac的过程中,它产生作用的具体流程如下:
通过读Lombok源码,发现对应注解的实现都在HandleXXX中,比如@Getter注解的实现在HandleGetter.handle()。还有一些其它类库使用这种方式实现,比如Google Auto、Dagger等等。