文章来源公众号:猿人谷java
官方介绍以下:git
Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplate-free, not-quite-java code.复制代码
大体意思是Lombok经过增长一些“处理程序”,可让java变得简洁、快速。github
Lombok能以简单的注解形式来简化java代码,提升开发人员的开发效率。例如开发中常常须要写的javabean,都须要花时间去添加相应的getter/setter,也许还要去写构造器、equals等方法,并且须要维护,当属性多时会出现大量的getter/setter方法,这些显得很冗长也没有太多技术含量,一旦修改属性,就容易出现忘记修改对应方法的失误。spring
Lombok能经过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。出现的神奇就是在源码中没有getter和setter方法,可是在编译生成的字节码文件中有getter和setter方法。这样就省去了手动重建这些代码的麻烦,使代码看起来更简洁些。编程
Lombok的使用跟引用jar包同样,能够在官网(https://projectlombok.org/download)下载jar包,也可使用maven添加依赖:api
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>复制代码
接下来咱们来分析Lombok中注解的具体用法。markdown
@Data注解在类上,会为类的全部属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。mybatis
官方实例以下:架构
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,则实现以下:eclipse
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;
}
}
}复制代码
若是以为@Data太过残暴(由于@Data集合了@ToString、@EqualsAndHashCode、@Getter/@Setter、@RequiredArgsConstructor的全部特性)不够精细,可使用@Getter/@Setter注解,此注解在属性上,能够为相应的属性自动生成Getter/Setter方法,示例以下:
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
public class GetterSetterExample {
@Getter @Setter private int age = 10;
@Setter(AccessLevel.PROTECTED) private String name;
@Override public String toString() {
return String.format("%s (age: %d)", name, age);
}
}复制代码
若是不使用Lombok:
public class GetterSetterExample {
private int age = 10;
private String name;
@Override public String toString() {
return String.format("%s (age: %d)", name, age);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
protected void setName(String name) {
this.name = name;
}
}复制代码
该注解用在属性或构造器上,Lombok会生成一个非空的声明,可用于校验参数,能帮助避免空指针。
示例以下:
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
this.name = person.getName();
}
}复制代码
不使用Lombok:
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
if (person == null) {
throw new NullPointerException("person");
}
this.name = person.getName();
}
}复制代码
该注解能帮助咱们自动调用close()方法,很大的简化了代码。
示例以下:
import lombok.Cleanup;
import java.io.*;
public class CleanupExample {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}复制代码
如不使用Lombok,则需以下:
import java.io.*;
public class CleanupExample {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(args[0]);
try {
OutputStream out = new FileOutputStream(args[1]);
try {
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
} finally {
if (out != null) {
out.close();
}
}
} finally {
if (in != null) {
in.close();
}
}
}
}复制代码
默认状况下,会使用全部非静态(non-static)和非瞬态(non-transient)属性来生成equals和hasCode,也能经过exclude注解来排除一些属性。
示例以下:
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(exclude={"id", "shape"})
public class EqualsAndHashCodeExample {
private transient int transientVar = 10;
private String name;
private double score;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.name;
}
@EqualsAndHashCode(callSuper=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}复制代码
类使用@ToString注解,Lombok会生成一个toString()方法,默认状况下,会输出类名、全部属性(会按照属性定义顺序),用逗号来分割。
经过将includeFieldNames
参数设为true,就能明确的输出toString()属性。这一点是否是有点绕口,经过代码来看会更清晰些。
使用Lombok的示例:
import lombok.ToString;
@ToString(exclude="id")
public class ToStringExample {
private static final int STATIC_VAR = 10;
private String name;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.getName();
}
@ToString(callSuper=true, includeFieldNames=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}复制代码
不使用Lombok的示例以下:
import java.util.Arrays;
public class ToStringExample {
private static final int STATIC_VAR = 10;
private String name;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.getName();
}
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
@Override public String toString() {
return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
}
}
@Override public String toString() {
return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
}
}复制代码
无参构造器、部分参数构造器、全参构造器。Lombok无法实现多种参数构造器的重载。
Lombok示例代码以下:
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
@NoArgsConstructor
public static class NoArgsExample {
@NonNull private String field;
}
}复制代码
不使用Lombok的示例以下:
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
private ConstructorExample(T description) {
if (description == null) throw new NullPointerException("description");
this.description = description;
}
public static <T> ConstructorExample<T> of(T description) {
return new ConstructorExample<T>(description);
}
@java.beans.ConstructorProperties({"x", "y", "description"})
protected ConstructorExample(int x, int y, T description) {
if (description == null) throw new NullPointerException("description");
this.x = x;
this.y = y;
this.description = description;
}
public static class NoArgsExample {
@NonNull private String field;
public NoArgsExample() {
}
}
}复制代码
会发如今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等等。
优势:
缺点:
Lombok虽然有不少优势,但Lombok更相似于一种IDE插件,项目也须要依赖相应的jar包。Lombok依赖jar包是由于编译时要用它的注解,为何说它又相似插件?由于在使用时,eclipse或IntelliJ IDEA都须要安装相应的插件,在编译器编译时经过操做AST(抽象语法树)改变字节码生成,变向的就是说它在改变java语法。它不像spring的依赖注入或者mybatis的ORM同样是运行时的特性,而是编译时的特性。这里我我的最感受不爽的地方就是对插件的依赖!由于Lombok只是省去了一些人工生成代码的麻烦,但IDE都有快捷键来协助生成getter/setter等方法,也很是方便。
知乎上有位大神发表过对Lombok的一些见解:
这是一种低级趣味的插件,不建议使用。JAVA发展到今天,各类插件层出不穷,如何甄别各类插件的优劣?能从架构上优化你的设计的,能提升应用程序性能的 ,
实现高度封装可扩展的..., 像lombok这种,像这种插件,已经不只仅是插件了,改变了你如何编写源码,事实上,少去了代码你写上去又如何?
若是JAVA家族处处充斥这样的东西,那只不过是一坨披着金属颜色的屎,早晚会被其它的语言取代。复制代码
虽然话糙但理确实不糙,试想一个项目有很是多相似Lombok这样的插件,我的以为真的会极大的下降阅读源代码的温馨度。
虽然很是不建议在属性的getter/setter写一些业务代码,但在多年项目的实战中,有时经过给getter/setter加一点点业务代码,能极大的简化某些业务场景的代码。所谓取舍,也许就是这时的舍弃必定的规范,取得极大的方便。
我如今很是坚信一条理念,任何编程语言或插件,都仅仅只是工具而已,即便工具再强大也在于用的人,就如同小米加步枪照样能赢飞机大炮的道理同样。结合具体业务场景和项目实际状况,无需一味追求高大上的技术,适合的才是王道。
Lombok有它的得天独厚的优势,也有它避之不及的缺点,熟知其优缺点,在实战中灵活运用才是王道。
参考:
https://projectlombok.org/features/
https://github.com/rzwitserloot/lombok?spm=a2c4e.11153940.blogcont59972.5.2aeb6d32hayLHv
https://www.zhihu.com/question/42348457
https://blog.csdn.net/ghsau/article/details/52334762