Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Lombok 提供编辑器的插件及编译时的 Library,帮助我们来改善 Java 的开发。
Never write another getter or equals method again. Early access to future java features such as val, and much more. 你可以不需要再手动编写 get/set/equals 方法。
Lombok 支持的 features
参见 https://projectlombok.org/features/all
The Lombok javadoc is available, but we advise these pages.
-
- Finally! Hassle-free final local variables. 不可变变量
-
- Mutably! Hassle-free local variables. 可变变量
-
- How I learned to stop worrying and love the NullPointerException.
-
- Automatic resource management: Call your
close()
methods safely with no hassle.
- Automatic resource management: Call your
-
- Never write
public int getFoo() {return foo;}
again.
- Never write
-
- No need to start a debugger to see your fields: Just let lombok generate a
toString
for you! 创建一个 toString 方法,包含所有的字段
- No need to start a debugger to see your fields: Just let lombok generate a
-
- Equality made easy: Generates
hashCode
andequals
implementations from the fields of your object..
- Equality made easy: Generates
-
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
- Constructors made to order: Generates constructors that take no arguments, one argument per final / non-nullfield, or one argument for every field.
-
- All together now: A shortcut for
@ToString
,@EqualsAndHashCode
,@Getter
on all fields, and@Setter
on all non-final fields, and@RequiredArgsConstructor
!
- All together now: A shortcut for
-
- Immutable classes made very easy.
... and Bob's your uncle: No-hassle fancy-pants APIs for object creation!
-
- To boldly throw checked exceptions where no one has thrown them before!
-
-
synchronized
done right: Don't expose your locks.
-
-
- Laziness is a virtue! 延时加载
-
- Captain's Log, stardate 24435.7: "What was that line again?"
Lombok 的示例 - 如何自动构造 get/set/constructor/toString 方法
编写一个实体类:
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@AllArgsConstructor
@ToString
public class Student {
@Getter @Setter
private String name;
@Getter @Setter
private int age;
}
调用他的 get/set/constructor/toString
方法:
public class StudentMain {
public static void main(String args[]) {
Student s = new Student("Tom", 19);
s.setAge(20);
System.out.println(s.toString());
}
}
安装一个 IntelliJ IDEA 的 Lombok 如下插件:
如果不安装这个插件,IntelliJ IDEA 编辑器会出现红色下滑线,例如 s.setAge(20);
显示找不到 setAge
方法,这个插件使得编辑器能够理解 Student
类中的 @...
注释。
利用 Lombok 进行编译:
自从 Java 6起,javac
就支持“JSR 269 Pluggable Annotation Processing API”规范,只要程序实现了该API,就能在 javac
运行的时候得到调用。
Lombok 本质上就是这样的一个实现了"JSR 269 API"的程序。具体流程如下:
-
javac
对源代码进行分析,生成一棵抽象语法树(AST)
-
- 运行过程中调用实现了"JSR 269 API"的 Lombok 程序
- Lombok 对第一步骤得到的抽象语法树(AST)进行处理,依据是代码中定义的各种
@...
注释
- Lombok 对第一步骤得到的抽象语法树(AST)进行处理,依据是代码中定义的各种
-
javac
使用修改后的抽象语法树(AST)生成字节码文件
-
将编译产生的字节码文件 Student.class
反编译后,可以看出源代码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.demo;
import java.beans.ConstructorProperties;
public class Student {
private String name;
private int age;
@ConstructorProperties({"name", "age"})
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Student(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
运行输出如下:
Student(name=Tom, age=20)
引用:
Project Lombok