/
NullAwayNativeModels.java
334 lines (311 loc) · 11.5 KB
/
NullAwayNativeModels.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright (c) 2017 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.nullaway.testdata;
import android.webkit.WebView;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import java.io.File;
import java.lang.ref.WeakReference;
import java.net.URLClassLoader;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import javax.lang.model.element.Element;
import javax.lang.model.util.Elements;
public class NullAwayNativeModels {
public static void referenceStuff() {
AtomicReference<Object> ref = new AtomicReference<>(null);
Object x = ref.get();
// BUG: Diagnostic contains: dereferenced expression
x.toString();
// BUG: Diagnostic contains: dereferenced expression
ref.get().toString();
WeakReference<Object> w = new WeakReference<Object>(x);
// BUG: Diagnostic contains: dereferenced expression
w.get().hashCode();
Exception e = new RuntimeException();
// BUG: Diagnostic contains: dereferenced expression
e.getMessage().hashCode();
}
// we will add bug annotations when we have full support for maps
public static void mapStuff(Map<Object, Object> m) {
// BUG: Diagnostic contains: dereferenced expression
m.get(new Object()).toString();
Object value = m.get(new Object());
// BUG: Diagnostic contains: dereferenced expression
value.toString();
HashMap<Object, Object> h = new HashMap<>();
Object value2 = h.get(new Object());
// BUG: Diagnostic contains: dereferenced expression
value2.toString();
}
static void mapGetNullCheck() {
Object x = new Object();
Map<Object, Object> m = new HashMap<>();
if (m.get(x) != null) {
m.get(x).toString();
}
HashMap<Object, Object> m2 = (HashMap) m;
if (m2.get(x) != null) {
m2.get(x).hashCode();
}
}
static void mapContainsKeyCheck() {
Object x = new Object();
Map<Object, Object> m = new HashMap<>();
if (m.containsKey(x)) {
m.get(x).toString();
}
if (m.containsKey(x)) {
Object y = m.get(x);
y.toString();
}
HashMap<Object, Object> m2 = (HashMap) m;
if (m2.containsKey(x)) {
m2.get(x).hashCode();
}
if (m2.containsKey(x)) {
Object y = m2.get(x);
y.hashCode();
}
Object z = new Object();
if (m2.containsKey(z)) {
// BUG: Diagnostic contains: dereferenced expression
m2.get(x).hashCode();
}
if (m2.containsKey(z)) {
Object y = m2.get(x);
// BUG: Diagnostic contains: dereferenced expression
y.hashCode();
}
// test negation
if (!m2.containsKey(x)) {
return;
}
Object y = m2.get(x);
y.hashCode();
}
static class Wrapper {
Object wrapped = new Object();
public Object getWrapped() {
return wrapped;
}
}
static final String KEY = "key";
static void harderMapContainsKeyCheck() {
Map m = new HashMap();
Wrapper w = new Wrapper();
if (m.containsKey(w.getWrapped())) {
m.get(w.getWrapped()).toString();
}
if (m.containsKey(w.getWrapped())) {
Object o = m.get(w.getWrapped());
o.toString();
}
if (m.get(w.getWrapped()) != null) {
m.get(w.getWrapped()).toString();
}
if (m.get(w.getWrapped()) != null) {
Object o = m.get(w.getWrapped());
o.toString();
}
if (m.containsKey(KEY)) {
m.get(KEY).toString();
}
if (m.containsKey(KEY)) {
Object o = m.get(KEY);
o.toString();
}
}
static void testLinkedHashMap() {
LinkedHashMap m = new LinkedHashMap();
Object o = new Object();
if (m.containsKey(o)) {
m.get(o).toString();
}
}
static void mapContainsKeyPut() {
Object x = new Object();
Map<Object, Object> m = new HashMap<>();
if (!m.containsKey(x)) {
m.put(x, new Object());
}
m.get(x).toString();
HashMap<Object, Object> m2 = new HashMap<>();
if (!m2.containsKey(x)) {
m2.put(x, x);
}
m2.get(x).toString();
Object y = new Object(), z = new Object();
if (!m2.containsKey(z)) {
m2.put(y, new Object());
}
// BUG: Diagnostic contains: dereferenced expression
m2.get(z).toString();
LinkedHashMap m3 = new LinkedHashMap();
if (!m3.containsKey(y)) {
m3.put(y, new Object());
}
m3.get(y).hashCode();
}
static void immutableMapStuff() {
ImmutableMap m = ImmutableMap.of();
Object res = m.get(new Object());
// BUG: Diagnostic contains: dereferenced expression
res.toString();
Object x = new Object();
if (m.containsKey(x)) {
m.get(x).toString();
}
}
static void failIfNull(
@Nullable Object o1,
@Nullable Object o2,
@Nullable Object o3,
@Nullable Object o4,
@Nullable Object o5) {
org.junit.Assert.assertNotNull(o1);
o1.toString();
org.junit.Assert.assertNotNull("Null!", o2);
o2.toString();
org.junit.jupiter.api.Assertions.assertNotNull(o3);
o3.toString();
org.junit.jupiter.api.Assertions.assertNotNull(o4, "Null!");
o4.toString();
org.junit.jupiter.api.Assertions.assertNotNull(o5, () -> "Null!");
o5.toString();
}
static void nonNullParameters() {
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
NullAwayNativeModels.class.getResource(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
NullAwayNativeModels.class.isAssignableFrom(null);
String s = null;
// BUG: Diagnostic contains: passing @Nullable parameter 's' where @NonNull is required
File f = new File(s);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
URLClassLoader.newInstance(null, NullAwayNativeModels.class.getClassLoader());
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
Optional<Object> op = Optional.of(null);
}
static void elementStuff(Element e, Elements elems) {
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
e.getAnnotation(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
elems.getPackageElement(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
elems.getTypeElement(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
elems.getDocComment(null);
}
static void arrayDequeStuff() {
ArrayDeque<Object> d = new ArrayDeque<>();
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.add(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offer(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.push(null);
Object[] o = null;
// BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
d.toArray(o);
// this should be fine
d.toArray();
}
static void dequeStuff() {
Deque<Object> d = new ArrayDeque<>();
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.add(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.addLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerFirst(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offerLast(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.offer(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
d.push(null);
Object[] o = null;
// BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
d.toArray(o);
}
static void guavaStuff() {
Collection<String> c = null;
Object o = null;
// BUG: Diagnostic contains: passing @Nullable parameter 'c' where @NonNull is required
ImmutableList.builder().addAll(c).build();
// BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
ImmutableList.builder().add(o).build();
// BUG: Diagnostic contains: passing @Nullable parameter 'c' where @NonNull is required
ImmutableSet.builder().addAll(c).build();
// BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
ImmutableSet.builder().add(o).build();
// BUG: Diagnostic contains: passing @Nullable parameter 'c' where @NonNull is required
ImmutableSortedSet.builder().addAll(c).build();
// BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
ImmutableSortedSet.builder().add(o).build();
// BUG: Diagnostic contains: passing @Nullable parameter 'c' where @NonNull is required
Iterables.getFirst(c, "hi");
}
static void androidStuff() {
android.webkit.WebView webView = new WebView();
// BUG: Diagnostic contains: dereferenced expression
webView.getUrl().toString();
String s = null;
if (!android.text.TextUtils.isEmpty(s)) {
// no warning due to isEmpty check
s.hashCode();
}
}
static void apacheCommonsStuff() {
String s = null;
if (!org.apache.commons.lang.StringUtils.isEmpty(s)) {
// no warning due to isEmpty check
s.hashCode();
}
String t = null;
if (!org.apache.commons.lang3.StringUtils.isEmpty(t)) {
// no warning due to isEmpty check
t.hashCode();
}
}
}