-
Notifications
You must be signed in to change notification settings - Fork 727
/
DoNotCallCheckerTest.java
573 lines (539 loc) · 18.5 KB
/
DoNotCallCheckerTest.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Copyright 2017 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.bugpatterns;
import static org.junit.Assert.assertThrows;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.annotations.DoNotCall;
import java.sql.Date;
import java.sql.Time;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** {@link DoNotCallChecker}Test */
@SuppressWarnings("DoNotCall")
@RunWith(JUnit4.class)
public class DoNotCallCheckerTest {
private final CompilationTestHelper testHelper =
CompilationTestHelper.newInstance(DoNotCallChecker.class, getClass());
@Test
public void positive() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.DoNotCall;",
"class Test {",
" @DoNotCall(\"satisfying explanation\") final void f() {}",
" @DoNotCall final void g() {}",
" void m() {",
" // BUG: Diagnostic contains:",
" // This method should not be called: satisfying explanation",
" f();",
" // BUG: Diagnostic contains:",
" // This method should not be called, see its documentation for details",
" g();",
" // BUG: Diagnostic contains:",
" Runnable r = this::g;",
" }",
"}")
.doTest();
}
@Test
public void positiveWhereDeclaredTypeIsSuper() {
testHelperWithImmutableList()
.addSourceLines(
"Test.java",
"import java.util.List;",
"class Test {",
" void foo() {",
" List<Integer> xs = ImmutableList.of();",
" // BUG: Diagnostic contains:",
" xs.add(1);",
" xs.get(1);",
" }",
"}")
.doTest();
}
@Test
public void positiveWhereDeclaredTypeIsSuper_butAssignedMultipleTimes() {
testHelperWithImmutableList()
.addSourceLines(
"Test.java",
"import java.util.List;",
"class Test {",
" void foo() {",
" List<Integer> xs;",
" if (hashCode() == 0) {",
" xs = ImmutableList.of();",
" } else {",
" xs = ImmutableList.of();",
" }",
" // BUG: Diagnostic contains:",
" xs.add(1);",
" xs.get(1);",
" }",
"}")
.doTest();
}
private CompilationTestHelper testHelperWithImmutableList() {
// Stub out an ImmutableList with the annotations we need for testing.
return testHelper
.addSourceLines(
"ImmutableCollection.java",
"import com.google.errorprone.annotations.DoNotCall;",
"import java.util.List;",
"abstract class ImmutableCollection<T> implements java.util.Collection<T> {",
" @DoNotCall @Override public final boolean add(T t) { throw new"
+ " UnsupportedOperationException(); }",
"}")
.addSourceLines(
"ImmutableList.java",
"import com.google.errorprone.annotations.DoNotCall;",
"import java.util.List;",
"abstract class ImmutableList<T> extends ImmutableCollection<T> implements List<T> {",
" public static <T> ImmutableList<T> of() {",
" return null;",
" }",
"}");
}
@Test
public void positiveWhereDeclaredTypeIsSuper_flaggedOff() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.common.collect.ImmutableList;",
"import java.util.List;",
"class Test {",
" void foo() {",
" List<Integer> xs = ImmutableList.of();",
" xs.add(1);",
" xs.get(1);",
" }",
"}")
.setArgs("-XepOpt:DoNotCallChecker:CheckAssignedTypes=false")
.doTest();
}
@Test
public void positiveWhereDeclaredTypeIsSuper_butNotAssignedOnce() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.common.collect.ImmutableList;",
"import java.util.ArrayList;",
"import java.util.List;",
"class Test {",
" void foo() {",
" List<Integer> xs;",
" if (true) {",
" xs = ImmutableList.of();",
" } else {",
" xs = new ArrayList<>();",
" xs.add(2);",
" }",
" }",
"}")
.doTest();
}
@Test
public void concreteFinal() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public class Test {",
" // BUG: Diagnostic contains: should be final",
" @DoNotCall public void f() {}",
" @DoNotCall public final void g() {}",
"}")
.doTest();
}
@Test
public void requiredOverride() {
testHelper
.addSourceLines(
"A.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public interface A {",
" @DoNotCall public void f();",
"}")
.addSourceLines(
"B.java",
"public class B implements A {",
" // BUG: Diagnostic contains: overrides f in A which is annotated",
" @Override public void f() {}",
"}")
.doTest();
}
@Test
public void annotatedOverride() {
testHelper
.addSourceLines(
"A.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public interface A {",
" @DoNotCall public void f();",
"}")
.addSourceLines(
"B.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public class B implements A {",
" @DoNotCall @Override public final void f() {}",
"}")
.doTest();
}
// The interface tries to make Object#toString @DoNotCall, and because
// the declaration in B is implicit it doesn't get checked.
// In practice, making default Object methods @DoNotCall isn't super
// useful - typically users interface with the interface directly
// (e.g. Hasher) or there's an override that has unwanted behaviour (Localizable).
// TODO(cushon): check class declarations for super-interfaces that do this?
@Test
public void interfaceRedeclaresObjectMethod() {
testHelper
.addSourceLines(
"I.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public interface I {",
" @DoNotCall public String toString();",
"}")
.addSourceLines(
"B.java", //
"public class B implements I {",
"}")
.addSourceLines(
"Test.java", //
"public class Test {",
" void f(B b) {",
" b.toString();",
" I i = b;",
" // BUG: Diagnostic contains:",
" i.toString();",
" }",
"}")
.doTest();
}
@Test
public void finalClass() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public final class Test {",
" @DoNotCall public void f() {}",
"}")
.doTest();
}
@Test
public void privateMethod() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.DoNotCall;",
"public final class Test {",
" // BUG: Diagnostic contains: private method",
" @DoNotCall private void f() {}",
"}")
.doTest();
}
/** Test class containing a method annotated with @DNC. */
public static final class DNCTest {
@DoNotCall
public static void f() {}
private DNCTest() {}
}
@Test
public void noDNConClasspath() {
testHelper
.addSourceLines(
"Test.java",
"class Test {",
" void m() {",
" // BUG: Diagnostic contains: This method should not be called",
" com.google.errorprone.bugpatterns.DoNotCallCheckerTest.DNCTest.f();",
" }",
"}")
.withClasspath(DNCTest.class, DoNotCallCheckerTest.class)
.doTest();
}
@Test
public void thirdParty() {
testHelper
.addSourceLines(
"Test.java",
"import org.junit.Assert;",
"public class Test {",
" public void foo() {",
" // BUG: Diagnostic contains: DoNotCall",
" Assert.assertEquals(2.0, 2.0);",
" // BUG: Diagnostic contains: DoNotCall",
" Assert.assertEquals(\"msg\", 2.0, 2.0);",
// These are OK since they pass a tolerance
" Assert.assertEquals(2.0, 2.0, 0.01);",
" Assert.assertEquals(\"msg\", 2.0, 2.0, 0.01);",
" }",
"}")
.doTest();
}
@Test
public void javaSqlDate_toInstant() {
assertThrows(UnsupportedOperationException.class, () -> new Date(1234567890L).toInstant());
testHelper
.addSourceLines(
"TestClass.java",
"import java.sql.Date;",
"import java.time.Instant;",
"public class TestClass {",
" public void badApis(Date date) {",
" // BUG: Diagnostic contains: toLocalDate()",
" Instant instant = date.toInstant();",
" }",
"}")
.doTest();
}
@Test
public void javaSqlDate_timeGetters() {
Date date = new Date(1234567890L);
assertThrows(IllegalArgumentException.class, () -> date.getHours());
assertThrows(IllegalArgumentException.class, () -> date.getMinutes());
assertThrows(IllegalArgumentException.class, () -> date.getSeconds());
testHelper
.addSourceLines(
"TestClass.java",
"import java.sql.Date;",
"public class TestClass {",
" public void badApis(Date date) {",
" // BUG: Diagnostic contains: DoNotCall",
" int hour = date.getHours();",
" // BUG: Diagnostic contains: DoNotCall",
" int mins = date.getMinutes();",
" // BUG: Diagnostic contains: DoNotCall",
" int secs = date.getSeconds();",
" }",
"}")
.doTest();
}
@Test
public void javaSqlDate_timeSetters() {
Date date = new Date(1234567890L);
assertThrows(IllegalArgumentException.class, () -> date.setHours(1));
assertThrows(IllegalArgumentException.class, () -> date.setMinutes(1));
assertThrows(IllegalArgumentException.class, () -> date.setSeconds(1));
testHelper
.addSourceLines(
"TestClass.java",
"import java.sql.Date;",
"public class TestClass {",
" public void badApis(Date date) {",
" // BUG: Diagnostic contains: DoNotCall",
" date.setHours(1);",
" // BUG: Diagnostic contains: DoNotCall",
" date.setMinutes(1);",
" // BUG: Diagnostic contains: DoNotCall",
" date.setSeconds(1);",
" }",
"}")
.doTest();
}
@Test
public void javaSqlDate_staticallyTypedAsJavaUtilDate() {
testHelper
.addSourceLines(
"TestClass.java",
"import java.time.Instant;",
"import java.util.Date;",
"public class TestClass {",
" public void badApis() {",
" Date date = new java.sql.Date(1234567890L);",
" Instant instant = date.toInstant();",
" int hour = date.getHours();",
" int mins = date.getMinutes();",
" int secs = date.getSeconds();",
" date.setHours(1);",
" date.setMinutes(1);",
" date.setSeconds(1);",
" }",
"}")
.doTest();
}
@Test
public void javaSqlTime_toInstant() {
assertThrows(UnsupportedOperationException.class, () -> new Time(1234567890L).toInstant());
testHelper
.addSourceLines(
"TestClass.java",
"import java.sql.Time;",
"import java.time.Instant;",
"public class TestClass {",
" public void badApis(Time time) {",
" // BUG: Diagnostic contains: toLocalTime()",
" Instant instant = time.toInstant();",
" }",
"}")
.doTest();
}
@Test
public void javaSqlTime_dateGetters() {
Time time = new Time(1234567890L);
assertThrows(IllegalArgumentException.class, () -> time.getYear());
assertThrows(IllegalArgumentException.class, () -> time.getMonth());
assertThrows(IllegalArgumentException.class, () -> time.getDay());
assertThrows(IllegalArgumentException.class, () -> time.getDate());
testHelper
.addSourceLines(
"TestClass.java",
"import java.sql.Time;",
"public class TestClass {",
" public void badApis(Time time) {",
" // BUG: Diagnostic contains: DoNotCall",
" int year = time.getYear();",
" // BUG: Diagnostic contains: DoNotCall",
" int month = time.getMonth();",
" // BUG: Diagnostic contains: DoNotCall",
" int day = time.getDay();",
" // BUG: Diagnostic contains: DoNotCall",
" int date = time.getDate();",
" }",
"}")
.doTest();
}
@Test
public void javaSqlTime_dateSetters() {
Time time = new Time(1234567890L);
assertThrows(IllegalArgumentException.class, () -> time.setYear(1));
assertThrows(IllegalArgumentException.class, () -> time.setMonth(1));
assertThrows(IllegalArgumentException.class, () -> time.setDate(1));
testHelper
.addSourceLines(
"TestClass.java",
"import java.sql.Time;",
"public class TestClass {",
" public void badApis(Time time) {",
" // BUG: Diagnostic contains: DoNotCall",
" time.setYear(1);",
" // BUG: Diagnostic contains: DoNotCall",
" time.setMonth(1);",
" // BUG: Diagnostic contains: DoNotCall",
" time.setDate(1);",
" }",
"}")
.doTest();
}
@Test
public void javaSqlTime_staticallyTypedAsJavaUtilDate() {
testHelper
.addSourceLines(
"TestClass.java",
"import java.time.Instant;",
"import java.util.Date;",
"public class TestClass {",
" public void badApis() {",
" Date time = new java.sql.Time(1234567890L);",
" Instant instant = time.toInstant();",
" int year = time.getYear();",
" int month = time.getMonth();",
" int date = time.getDate();",
" int day = time.getDay();",
" time.setYear(1);",
" time.setMonth(1);",
" time.setDate(1);",
" }",
"}")
.doTest();
}
@Test
public void readLock_newCondition() {
assertThrows(
UnsupportedOperationException.class,
() -> new ReentrantReadWriteLock().readLock().newCondition());
testHelper
.addSourceLines(
"Test.java",
"import java.util.concurrent.locks.ReentrantReadWriteLock;",
"public class Test {",
" public void foo() {",
" ReentrantReadWriteLock.ReadLock lock = new ReentrantReadWriteLock().readLock();",
" // BUG: Diagnostic contains: DoNotCall",
" lock.newCondition();",
" }",
"}")
.doTest();
}
@Test
public void threadLocalRandom_setSeed() {
assertThrows(
UnsupportedOperationException.class, () -> ThreadLocalRandom.current().setSeed(42));
testHelper
.addSourceLines(
"Test.java",
"import java.util.concurrent.ThreadLocalRandom;",
"public class Test {",
" public void foo() {",
" ThreadLocalRandom random = ThreadLocalRandom.current();",
" // BUG: Diagnostic contains: DoNotCall",
" random.setSeed(42L);",
" }",
"}")
.doTest();
}
@Test
public void memberReferencesOnThirdPartyMethods() {
testHelper
.addSourceLines(
"Test.java",
"import java.util.concurrent.ThreadLocalRandom;",
"import java.util.Optional;",
"public class Test {",
" public void foo(Optional<Long> x) {",
" ThreadLocalRandom random = ThreadLocalRandom.current();",
" // BUG: Diagnostic contains: DoNotCall",
" x.ifPresent(random::setSeed);",
" }",
"}")
.doTest();
}
@Test
public void typeArgs_dontCrash() {
testHelper
.addSourceLines(
"Test.java",
"import java.util.List;",
"class Test<T extends java.util.Collection<Object>> {",
" @Override public boolean equals(Object o) {",
" T foo = (T) o;",
" return foo.equals(1);",
" }",
"}")
.doTest();
}
@Test
public void positive_getSimpleName_refactoredToGetClassName() {
testHelper
.addSourceLines(
"Test.java",
"class Test{",
" void f() {",
" try {",
" throw new Exception();",
" } catch (Exception ex) {",
" // BUG: Diagnostic contains: getClassName",
" ex.getStackTrace()[0].getClass().getSimpleName();",
" }",
" }",
"}")
.doTest();
}
}