forked from projectlombok/lombok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
99 lines (86 loc) · 3.24 KB
/
README
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
### 主要添加能判断setter的参数为null时,是否需要添加。
#### 修改如下:
```java
@@ -26,6 +26,7 @@ import static lombok.core.handlers.HandlerUtil.*;
import static lombok.javac.handlers.JavacHandlerUtil.*;
import java.util.Collection;
+import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.ConfigurationKeys;
@@ -61,6 +62,7 @@ import com.sun.tools.javac.util.Name;
*/
@ProviderFor(JavacAnnotationHandler.class)
public class HandleSetter extends JavacAnnotationHandler<Setter> {
+ private Boolean hasNodeExistsOnlySetNonNullAnnotation = false;
public void generateSetterForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelSetter, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
if (checkForTypeLevelSetter) {
if (hasAnnotation(Setter.class, typeNode)) {
@@ -68,7 +70,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
return;
}
}
+ this.hasNodeExistsOnlySetNonNullAnnotation = !findAnnotations(typeNode, Pattern.compile("^(?:onlysetnonnull)$", Pattern.CASE_INSENSITIVE)).isEmpty();
JCClassDecl typeDecl = null;
if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
@@ -185,6 +187,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
}
long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
+ access = (access << 1) | (this.hasNodeExistsOnlySetNonNullAnnotation ? 1 : 0);
JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), sourceNode, onMethod, onParam);
Type fieldType = getMirrorForFieldType(fieldNode);
@@ -224,7 +227,23 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> {
JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD);
JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name));
+ JCStatement assignTarget = treeMaker.Exec(assign);
+ Boolean hasNodeExistsOnlySetNonNullAnnotation = (access & 1) == 1;
+ access = access >> 1;
+ if (hasNodeExistsOnlySetNonNullAnnotation || !findAnnotations(field, Pattern.compile("^(?:onlysetnonnull)$", Pattern.CASE_INSENSITIVE)).isEmpty()) {
+ assignTarget = treeMaker.If(
+ treeMaker.Parens(
+ treeMaker.Binary(
+ CTC_NOT_EQUAL,
+ treeMaker.Literal(CTC_BOT, null),
+ treeMaker.Ident(fieldDecl.name)
+ )
+ ),
+ treeMaker.Block(0L, List.of(assignTarget)),
+ null
+ );
+ }
+
ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(field);
```
#### jdk1.8编译
```sh
git co v1.16.0
手动修改变化
ant dist
```
#### 实例
OnlySetNonNull.java:
```java
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OnlySetNonNull {
}
```
Demo.java:
```java
@OnlySetNonNull
@Data
public class Demo {
//@OnlySetNonNull
private String name;
//@OnlySetNonNull
private Double abc;
}
```
运行:
```sh
javac OnlySetNonNull.java
javac -cp ${lombok_project_path}/lombok/dist/lombok.jar:. Demo.java
javap -c Demo.class
```