Lombok特性

  • val

val可以用于定义局部变量,不需要提前指定它的类型,能根据变量的初始化值来推断出变量的类型,且用final进行修饰。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
import lombok.val;
import java.util.ArrayList;

public class ValExample {
public static void main(String[] args){
val array = new ArrayList<String>();
array.add("val..");
for(val obj: array){
System.out.println(obj);
}
}
}
  • var

var和val类似,区别在于不再有final进行修饰.

  • @NonNull

该注解可以用于参数的空指针检查,避免出现讨厌的空指针问题。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import lombok.NonNull;

public class NonNullExample {

private Person person;

public NonNullExample(@NonNull Person person){
this.person = person;
}

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

如下是编译后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import lombok.NonNull;

public class NonNullExample {
private NonNullExample.Person person;

public NonNullExample(@NonNull NonNullExample.Person person) {
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
} else {
this.person = person;
}
}
class Person {
private String name;
private Integer age;
Person(String name, Integer age) {
this.name = name;
this.age = age;
}
}
}
  • @Cleanup

该注解可以进行资源自动管理,在作用域结束时进行资源回收。当一个局部变量被@Cleanup进行修饰时,整个变量的作用域范围会被try/finally 代码块进行包裹,在finally中调用资源回收的close方法(默认为close())。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   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);
}
}
}

编译后的代码显示如下:

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
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();
}
}
}
}

当然,如果某个变量类型没有close方法,而有其他的无参(只有无参的才能用户@Cleanup进行修饰,有参的不能)的资源回收的方法,可以用以下的方式进行申明变量(官网案例):

1
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
  • @Getter/@Setter

这两个注解可以用于修饰成员变量,也可以用于修饰类,会自动生成标准javabean的getter和setter方法。生成的getter和setter方法的public域,这个是可以控制的。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class GeterSeterExample {

@Getter@Setter private String name;

@Getter(value = AccessLevel.PROTECTED)
@Setter(value = AccessLevel.PROTECTED)
private String size;
}
  • @ToString

修饰类,控制重载toString方法。

  • @EqualsAndHashCode

修饰类,生成equals和hashCode函数。

  • @NoArgsConstructor, @AllArgsConstructor

生成无参构造函数和全参构造函数。示例如下:

1
2
3
4
5
6
7
8
9
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
public class ConstructExample {
private String name;
private String dec;
}

编译后的代码显示如下:

1
2
3
4
5
6
7
8
9
10
11
12
public class ConstructExample {
private String name;
private String dec;

public ConstructExample() {
}

public ConstructExample(String name, String dec) {
this.name = name;
this.dec = dec;
}
}
  • @Data

包含如下功能,是一种简写方式。@ToString,@EqualsAndHashCode,@Getter,@Setter,@RequiredArgsConstructor

  • @Builder

生成创建对象的builder方法api,示例如下:

1
2
3
4
5
6
7
import lombok.Builder;

@Builder
public class BuilderExample {
private String name;
private String dec;
}

编译后的代码如下:

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 BuilderExample {
private String name;
private String dec;

BuilderExample(String name, String dec) {
this.name = name;
this.dec = dec;
}

public static BuilderExample.BuilderExampleBuilder builder() {
return new BuilderExample.BuilderExampleBuilder();
}

public static class BuilderExampleBuilder {
private String name;
private String dec;

BuilderExampleBuilder() {
}

public BuilderExample.BuilderExampleBuilder name(String name) {
this.name = name;
return this;
}

public BuilderExample.BuilderExampleBuilder dec(String dec) {
this.dec = dec;
return this;
}

public BuilderExample build() {
return new BuilderExample(this.name, this.dec);
}

public String toString() {
return "BuilderExample.BuilderExampleBuilder(name=" + this.name + ", dec=" + this.dec + ")";
}
}
}
  • @Synchronized

安全的加锁,控制同步。引用官网的例子,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import lombok.Synchronized;

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");
}
}
}
  • @Log

log注解是用于使用log日志输出的,其包含@Log、@Log4j、@Log4j2、@Slf4j等等。这里以@Slf4j为例,如下:

1
2
3
4
5
6
7
8
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LogExample {
public static void main(String... args) {
log.info("log info");
}
}

@Slf4j注解会自动帮我们生成如下代码:

1
private static final Logger log =LoggerFactory.getLogger(LogExample.class);

这篇文档并没有介绍全部的特性,如果想了解更多或者对以上的特性进行更深入的了解,请浏览官方文档