Lombok 详解

简介

lombok是一个编译级别的插件,它可以在项目编译的时候生成一些代码。通俗的说,lombok可以通过注解来标示生成getter settter等代码。

引入

创建gradle项目

1
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.20'

注解

@NonNull

标记字段不可为null

1
2
3
4
5
6
7
@Setter
public class Person {
@NonNull
private String name;
@NonNull
private Integer age;
}

对应的字节码文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Person {
@NonNull
private String name;
@NonNull
private Integer age;

public Person() {
}

public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name");
} else {
this.name = name;
}
}

public void setAge(@NonNull Integer age) {
if (age == null) {
throw new NullPointerException("age");
} else {
this.age = age;
}
}
}

@Getter/@Setter

自动生成getter和setter方法

1
2
3
4
5
6
public class Person {
@Getter
private String name;
@Setter
private Integer age;
}

对应的字节码文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Person {
private String name;
private Integer age;

public Person() {
}

public String getName() {
return this.name;
}

public void setAge(Integer age) {
this.age = age;
}
}

@Cleanup

自动关闭流代码

1
2
@Cleanup
InputStream in = new FileInputStream(args[0]);

对应的字节码文件:

1
2
3
4
InputStream in = new FileInputStream(args[0]);
if (Collections.singletonList(in).get(0) != null) {
in.close();
}

@AllArgsConstructor/@NoArgsConstructor/@RequiredArgsConstructor

自动生成全参构造函数和无参构造函数

1
2
3
4
5
6
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
}

对应的字节码文件

1
2
3
4
5
6
7
8
9
10
11
12
public class Person {
private String name;
private Integer age;

public Person(String name, Integer age) {
this.name = name;
this.age = age;
}

public Person() {
}
}

@Builder

自动生成建造者模式的bean

1
2
3
4
5
@Builder
public class Person {
private String name;
private Integer age;
}

对应的字节码文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Person {
private String name;
private Integer age;

Person(String name, Integer age) {
this.name = name;
this.age = age;
}

public static Person.PersonBuilder builder() {
return new Person.PersonBuilder();
}

public static class PersonBuilder {
private String name;
private Integer age;

PersonBuilder() {
}

public Person.PersonBuilder name(String name) {
this.name = name;
return this;
}

public Person.PersonBuilder age(Integer age) {
this.age = age;
return this;
}

public Person build() {
return new Person(this.name, this.age);
}

public String toString() {
return "Person.PersonBuilder(name=" + this.name + ", age=" + this.age + ")";
}
}
}

@EqualsAndHashCode

自动生成equals和hashcode方法

1
2
3
4
5
@EqualsAndHashCode
public class Person {
private String name;
private Integer age;
}

对应的字节码文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Person {
private String name;
private Integer age;

public Person() {
}

public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$name = this.name;
Object other$name = other.name;
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}

Object this$age = this.age;
Object other$age = other.age;
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}

return true;
}
}
}

protected boolean canEqual(Object other) {
return other instanceof Person;
}

public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.name;
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.age;
result = result * 59 + ($age == null ? 43 : $age.hashCode());
return result;
}
}

@ToString

自动生成toString()方法

1
2
3
4
5
@ToString
public class Person {
private String name;
private Integer age;
}

对应的字节码文件

1
2
3
4
5
6
7
8
9
10
11
public class Person {
private String name;
private Integer age;

public Person() {
}

public String toString() {
return "Person(name=" + this.name + ", age=" + this.age + ")";
}
}

@Value

自动生成全参构造函数、Getter方法、equals方法、hashCode法、toString方法

1
2
3
4
5
@Value
public class Person {
private String name;
private Integer age;
}

注意:@Value不会生成Setter方法

@Synchronized

自动为被标记的方法添加synchronized锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class SynchronizedExample {
private final Object readLock = new Object();

@Synchronized
public static void hello() {
System.out.println("world");
}

@Synchronized
public int answerToLife() {
return 42;
}

@Synchronized("readLock")
public void foo() {
System.out.println("bar");
}
}

对应的字节码文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class SynchronizedExample {
private static final Object $LOCK = new Object[0];
private final Object $lock = new Object[0];
private final Object readLock = new Object();

public static void hello() {
synchronized($LOCK) {
System.out.println("world");
}
}

public int answerToLife() {
synchronized($lock) {
return 42;
}
}

public void foo() {
synchronized(readLock) {
System.out.println("bar");
}
}
}

@Delegate

为标记属性生成委托方法

1
2
3
4
5
6
7
8
9
10
public class DelegateExample {
public void show() {
System.out.println("show...");
}
}
@AllArgsConstructor
public class Demo {
@Delegate
private final DelegateExample delegateExample;
}

对应的字节码文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DelegateExample {
public DelegateExample() {
}

public void show() {
System.out.println("show...");
}
}
public class Demo {
private final DelegateExample delegateExample;

public Demo(DelegateExample delegateExample) {
this.delegateExample = delegateExample;
}

// 委托方法
public void show() {
this.delegateExample.show();
}
}