-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCorrectNullChecks.java
More file actions
139 lines (99 loc) · 2.73 KB
/
CorrectNullChecks.java
File metadata and controls
139 lines (99 loc) · 2.73 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package testSuite;
import java.util.ArrayList;
import java.util.Date;
import liquidjava.specification.Refinement;
@SuppressWarnings("unused")
public class CorrectNullChecks {
Integer x;
void test() {
if (x == null) {
mustBeNull(x);
} else {
mustBeNotNull(x);
}
}
void mustBeNull(@Refinement("_ == null") Integer i) {}
void mustBeNotNull(@Refinement("_ != null") Integer i) {}
void testNullInteger() {
Integer i = null;
@Refinement("_ == null")
Integer i1 = i;
i = 123;
@Refinement("_ != null")
Integer i2 = i;
}
void testNullString() {
String s = null;
@Refinement("_ == null")
String s1 = s;
s = "hello";
@Refinement("_ != null")
String s2 = s;
}
void testNulls() {
@Refinement("_ == null")
String s = null;
@Refinement("_ == null")
Integer i = null;
@Refinement("_ == null")
Boolean b = null;
@Refinement("_ == null")
Double d = null;
@Refinement("_ == null")
Long l = null;
@Refinement("_ == null")
Float f = null;
@Refinement("_ == null")
Date dt = null;
@Refinement("_ == null")
ArrayList<String> lst = null;
}
void testNonNulls() {
@Refinement("_ != null")
String s = "hello";
@Refinement("_ != null")
Integer i = 123;
@Refinement("_ != null")
Boolean b = true;
@Refinement("_ != null")
Double d = 1.0;
@Refinement("_ != null")
Long l = 2L;
@Refinement("_ != null")
Float f = 1.0f;
@Refinement("_ != null")
Date dt = new Date();
@Refinement("_ != null")
ArrayList<String> lst = new ArrayList<>();
@Refinement("_ != null")
CorrectNullChecks r = this;
}
void testNullChecksInMethods() {
@Refinement("_ != null")
String x = returnNotNullIf(null);
@Refinement("_ != null")
String y = returnNotNullTernary(null);
@Refinement("_ != null")
String z = returnNotNullParam("not null");
@Refinement("_ == null")
String w = returnNull();
}
@Refinement("_ != null")
String returnNotNullIf(String s) {
if (s == null)
s = "default";
return s;
}
@Refinement("_ != null")
String returnNotNullTernary(String s) {
return s != null ? s : "default";
}
@Refinement("_ != null")
String returnNotNullParam(@Refinement("_ != null") String s) {
return s;
}
@Refinement("_ == null")
String returnNull() {
return null;
}
}