Lombok注解笔记
lombok 版本:1.18.2
# 常用注解
# @Getter
作用
- 生成getter方法。写在类上会生成该类下所有字段的getter。写在某个字段上就作用于该字段
参数
- onMethod:把需要添加的注解写在这
- lazy:懒加载,具体请看 【Lombok】@Getter(lazy=true) | Getter 懒加载 (opens new window)
例子
public class Example {
@Getter(onMethod_={@Deprecated})
private int foo;
private final String bar = "";
}
1
2
3
4
5
6
2
3
4
5
6
生成:
public class Example {
private int foo;
private final String bar = "";
public Example() {
}
/** @deprecated */
@Deprecated
public int getFoo() {
return this.foo;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# @Setter
作用
- 生成setter。写在类上会生成该类下所有字段的setter。写在某个字段上就作用于该字段
参数
- onMethod:在方法上添加中注解,见
@Getter#onMethod - onParam:在方法的参数上添加注解,见
@Getter#onMethod - value:访问权限修饰符
- onMethod:在方法上添加中注解,见
# @NonNull
作用
- 空检查
例子
public class Example {
@NonNull
@Getter
@Setter
private Integer foo;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
生成:
public class Example {
@NonNull
private Integer foo;
public Example() {
}
@NonNull
public Integer getFoo() {
return this.foo;
}
public void setFoo(@NonNull Integer foo) {
if (foo == null) {
throw new NullPointerException("foo is marked @NonNull but is null");
} else {
this.foo = foo;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# @EqualsAndHashCode
作用
- 生成hashCode()、equals()
参数
- callSuper:是否调用父类的hashCode(),默认:false
- doNotUseGetters:是否不调用字段的getter,默认如果有getter会调用。设置为true,直接访问字段,不调用getter
- exclude:此处列出的任何字段都不会在生成的equals和hashCode中使用。
- of:与exclude相反,设置of,exclude失效
- onParam:添加注解,参考@Getter#onMethod
# @ToString
- 作用
- 生成toString()方法
# @AllArgsConstructor
作用
- 生成包含所有字段的构造器
参数
- staticName : (可选)不为空的话,生成一个静态方法返回实例,并把构造器设置为 private
- access :(可选)构造器访问权限修饰符,默认 public
例子
@AllArgsConstructor(staticName = "create")
public class Example {
private int foo;
private final String bar;
}
1
2
3
4
5
6
2
3
4
5
6
生成:
public class Example {
private int foo;
private final String bar;
private Example(int foo, String bar) {
this.foo = foo;
this.bar = bar;
}
public static Example create(int foo, String bar) {
return new Example(foo, bar);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# @NoArgsConstructor
作用
- 生成无参数构造器
参数
- access:访问权限修饰符
- force:为true时,强制生成构造器,final字段初始化为null
- onConstructor:添加注解,参考
@Getter#onMethod
# @RequiredArgsConstructor
作用
- 生成必须初始化字段的构造器,比如带final、
@NonNull
- 生成必须初始化字段的构造器,比如带final、
参数
- access:访问权限修饰符
- force:为true时,强制生成构造器,final字段初始化为null
- onConstructor:添加注解,参考@Getter#onMethod
例子
@RequiredArgsConstructor
public class Example {
@NonNull
private Integer foo;
private final String bar;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
生成:
public class Example {
@NonNull
private Integer foo;
private final String bar;
public Example(@NonNull Integer foo, String bar) {
if (foo == null) {
throw new NullPointerException("foo is marked @NonNull but is null");
} else {
this.foo = foo;
this.bar = bar;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
提示
在Spring项目中,推荐使用@RequiredArgsConstructor生成构造器进行构造器注入,来代替@Autowired属性注入。
# @Data
作用
- 生成所有字段的getter、toString()、hashCode()、equals()、所有非final字段的setter、构造器,相当于设置了
@Getter@Setter@RequiredArgsConstructor@ToString@EqualsAndHashCode
- 生成所有字段的getter、toString()、hashCode()、equals()、所有非final字段的setter、构造器,相当于设置了
例子
@Data
public class Example {
private int foo;
private final String bar;
}
1
2
3
4
5
6
2
3
4
5
6
生成:
public class Example {
private int foo;
private final String bar;
public Example(String bar) {
this.bar = bar;
}
public int getFoo() {
return this.foo;
}
public String getBar() {
return this.bar;
}
public void setFoo(int foo) {
this.foo = foo;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Example)) {
return false;
} else {
Example other = (Example)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getFoo() != other.getFoo()) {
return false;
} else {
Object this$bar = this.getBar();
Object other$bar = other.getBar();
if (this$bar == null) {
if (other$bar != null) {
return false;
}
} else if (!this$bar.equals(other$bar)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Example;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getFoo();
Object $bar = this.getBar();
result = result * 59 + ($bar == null ? 43 : $bar.hashCode());
return result;
}
public String toString() {
return "Example(foo=" + this.getFoo() + ", bar=" + this.getBar() + ")";
}
}
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
57
58
59
60
61
62
63
64
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
57
58
59
60
61
62
63
64
# 需要了解的注解
# @Builder、@Singular
作用
@Builder:生成构建者(Builder)模式@Singular:这个注解和@Builder一起使用,为Builder生成字段是集合类型的add方法,字段名不能是单数形式,否则需要指定value值
参数
- builderMethodName : 创建构建器实例的方法名称
- buildMethodName:构建器类中创建构造器实例的方法名称
- builderClassName:构造器类名
- toBuilder:生成toBuilder方法
例子
@Builder
public class Example {
private int foo;
private final String bar;
@Singular
private List<Integer> foos;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
生成:
public class Example {
private int foo;
private final String bar;
private List<Integer> foos;
Example(int foo, String bar, List<Integer> foos) {
this.foo = foo;
this.bar = bar;
this.foos = foos;
}
public static Example.ExampleBuilder builder() {
return new Example.ExampleBuilder();
}
public static class ExampleBuilder {
private int foo;
private String bar;
private ArrayList<Integer> foos;
ExampleBuilder() {
}
public Example.ExampleBuilder foo(int foo) {
this.foo = foo;
return this;
}
public Example.ExampleBuilder bar(String bar) {
this.bar = bar;
return this;
}
// 这方法是@Singular作用生成的
public Example.ExampleBuilder foo(Integer foo) {
if (this.foos == null) {
this.foos = new ArrayList();
}
this.foos.add(foo);
return this;
}
public Example.ExampleBuilder foos(Collection<? extends Integer> foos) {
if (this.foos == null) {
this.foos = new ArrayList();
}
this.foos.addAll(foos);
return this;
}
public Example.ExampleBuilder clearFoos() {
if (this.foos != null) {
this.foos.clear();
}
return this;
}
public Example build() {
List foos;
switch(this.foos == null ? 0 : this.foos.size()) {
case 0:
foos = Collections.emptyList();
break;
case 1:
foos = Collections.singletonList(this.foos.get(0));
break;
default:
foos = Collections.unmodifiableList(new ArrayList(this.foos));
}
return new Example(this.foo, this.bar, foos);
}
public String toString() {
return "Example.ExampleBuilder(foo=" + this.foo + ", bar=" + this.bar + ", foos=" + this.foos + ")";
}
}
}
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# @Cleanup
作用
- 在变量上声明@Cleanup,生成的代码会把变量用try{}包围,并在finallly块中调用close()
参数
- value:被在finally块中调用的方法名,方法体不能带有参数,默认为close
例子
public class Example {
public void copyFile(String in, String out) throws IOException {
@Cleanup FileInputStream inStream = new FileInputStream(in);
@Cleanup FileOutputStream outStream = new FileOutputStream(out);
byte[] b = new byte[65536];
while (true) {
int r = inStream.read(b);
if (r == -1) break;
outStream.write(b, 0, r);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
生成:
public class Example {
public Example() {
}
public void copyFile(String in, String out) throws IOException {
FileInputStream inStream = new FileInputStream(in);
try {
FileOutputStream outStream = new FileOutputStream(out);
try {
byte[] b = new byte[65536];
while(true) {
int r = inStream.read(b);
if (r == -1) {
return;
}
outStream.write(b, 0, r);
}
} finally {
if (Collections.singletonList(outStream).get(0) != null) {
outStream.close();
}
}
} finally {
if (Collections.singletonList(inStream).get(0) != null) {
inStream.close();
}
}
}
}
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
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
# @SneakyThrows
作用
- 用try{}catch{}捕捉异常
例子
public class Example {
@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
return new String(bytes, "UTF-8");
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
生成:
public class Example {
public Example() {
}
public String utf8ToString(byte[] bytes) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException var3) {
throw var3;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# @Synchronized
作用
- 生成synchronized(){}包围代码
例子
public class Example {
@Synchronized
public String utf8ToString(byte[] bytes) {
return new String(bytes, Charset.defaultCharset());
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
生成:
public class Example {
private final Object $lock = new Object[0];
public Example() {
}
public String utf8ToString(byte[] bytes) {
Object var2 = this.$lock;
synchronized(this.$lock) {
return new String(bytes, Charset.defaultCharset());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# @val、@var(没啥用)
作用
- 两个类作用一样,都是变量声明类型推断(官方文档中说区别就是var不加final修饰,但测试的效果是一样的)
例子
public class ValExample {
public String example() {
val example = new ArrayList<String>();
example.add("Hello, World!");
val foo = example.get(0);
return foo.toLowerCase();
}
public void example2() {
val map = new HashMap<Integer, String>();
map.put(0, "zero");
map.put(5, "five");
for (val entry : map.entrySet()) {
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
生成:
public class ValExample {
public ValExample() {
}
public String example() {
ArrayList<String> example = new ArrayList();
example.add("Hello, World!");
String foo = (String)example.get(0);
return foo.toLowerCase();
}
public void example2() {
HashMap<Integer, String> map = new HashMap();
map.put(0, "zero");
map.put(5, "five");
Iterator var2 = map.entrySet().iterator();
while(var2.hasNext()) {
Entry<Integer, String> entry = (Entry)var2.next();
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# @FieldDefaults
作用
- 设置属性的使用范围,如private、public等,也可以设置属性是否被final修饰
参数
- level:访问权限修饰符,必选
- makeFinal:为true时,属性会被final修饰
例子
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Example {
String a;
String b;
}
1
2
3
4
5
2
3
4
5
生成:
public class Example {
private String a;
private String b;
}
1
2
3
4
2
3
4
# @FieldNameConstants
作用
- 为类中说有属性生成一个常量,值为字段名
参数
- level:内部类修饰符
- asEnum:是否生成枚举
- innerTypeName:内部类名称
例子
@FieldNameConstants
public class FieldNameConstantsExample {
private final String iAmAField;
private final int andSoAmI;
@FieldNameConstants.Exclude private final int asAmI;
}
1
2
3
4
5
6
2
3
4
5
6
生成:
public class FieldNameConstantsExample {
private final String iAmAField;
private final int andSoAmI;
private final int asAmI;
public static final class Fields {
public static final String iAmAField = "iAmAField";
public static final String andSoAmI = "andSoAmI";
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# @Wither
作用
- 生成withXXX方法,返回类实例
例子
@RequiredArgsConstructor
public class Example {
private @Wither final int foo;
}
1
2
3
4
2
3
4
生成:
public class Example {
private final int foo;
public Example(int foo) {
this.foo = foo;
}
public Example withFoo(int foo) {
return this.foo == foo ? this : new Example(foo);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# @Delegate
作用
- 代理模式,把字段的方法代理给类,默认代理所有方法
参数
- types:指定代理的方法
- excludes:和types相反
例子
public class Example {
private interface Add {
boolean add(String x);
boolean addAll(Collection<? extends String> x);
}
private @Delegate(types = Add.class) List<String> strings;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
生成:
public class Example {
private List<String> strings;
public Example() {
}
public boolean add(String x) {
return this.strings.add(x);
}
public boolean addAll(Collection<? extends String> x) {
return this.strings.addAll(x);
}
private interface Add {
boolean add(String var1);
boolean addAll(Collection<? extends String> var1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
编辑 (opens new window)
上次更新: 2022/03/20, 11:17:00