1 /*******************************************************************************
2 * Copyright (c) 2005, 2019 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
14 * bug 325755 - [compiler] wrong initialization state after conditional expression
15 * bug 133125 - [compiler][null] need to report the null status of expressions and analyze them simultaneously
16 * bug 292478 - Report potentially null across variable assignment
17 * bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
18 * bug 320170 - [compiler] [null] Whitebox issues in null analysis
19 * bug 332637 - Dead Code detection removing code that isn't dead
20 * bug 338303 - Warning about Redundant assignment conflicts with definite assignment
21 * bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
22 * bug 324178 - [null] ConditionalExpression.nullStatus(..) doesn't take into account the analysis of condition itself
23 * bug 354554 - [null] conditional with redundant condition yields weak error message
24 * bug 358827 - [1.7] exception analysis for t-w-r spoils null analysis
25 * bug 349326 - [1.7] new warning for missing try-with-resources
26 * bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop)
27 * bug 367879 - Incorrect "Potential null pointer access" warning on statement after try-with-resources within try-finally
28 * bug 383690 - [compiler] location of error re uninitialized final field should be aligned
29 * bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
30 * bug 376263 - Bogus "Potential null pointer access" warning
31 * bug 331649 - [compiler][null] consider null annotations for fields
32 * bug 382789 - [compiler][null] warn when syntactically-nonnull expression is compared against null
33 * bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
34 * bug 401092 - [compiler][null] Wrong warning "Redundant null check" in outer catch of nested try
35 * bug 400761 - [compiler][null] null may be return as boolean without a diagnostic
36 * bug 402993 - [null] Follow up of bug 401088: Missing warning about redundant null check
37 * bug 403147 - [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking
38 * bug 384380 - False positive on a "Potential null pointer access" after a continue
39 * bug 406384 - Internal error with I20130413
40 * Bug 364326 - [compiler][null] NullPointerException is not found by compiler. FindBugs finds that one
41 * Bug 453483 - [compiler][null][loop] Improve null analysis for loops
42 * Bug 195638 - [compiler][null][refactoring] Wrong error : "Null pointer access: The variable xxx can only be null at this location " with try..catch in loop
43 * Bug 454031 - [compiler][null][loop] bug in null analysis; wrong "dead code" detection
44 *******************************************************************************/
45 package org.eclipse.jdt.core.tests.compiler.regression;
46
47 import java.io.File;
48 import java.util.HashMap;
49 import java.util.Map;
50
51 import junit.framework.Test;
52
53 import org.eclipse.jdt.core.JavaCore;
54 import org.eclipse.jdt.core.ToolFactory;
55 import org.eclipse.jdt.core.tests.util.Util;
56 import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
57 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
58 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
59
60 /* See also NullReferenceImplTests for low level, implementation dependent
61 * tests. */
62 @SuppressWarnings({ "unchecked", "rawtypes" })
63 public class NullReferenceTest extends AbstractRegressionTest {
64
NullReferenceTest(String name)65 public NullReferenceTest(String name) {
66 super(name);
67 }
68
69 // Static initializer to specify tests subset using TESTS_* static variables
70 // All specified tests which does not belong to the class are skipped...
71 // Only the highest compliance level is run; add the VM argument
72 // -Dcompliance=1.4 (for example) to lower it if needed
73 static {
74 // TESTS_NAMES = new String[] { "testBug542707_1" };
75 // TESTS_NAMES = new String[] { "testBug384380" };
76 // TESTS_NAMES = new String[] { "testBug384380_b" };
77 // TESTS_NAMES = new String[] { "testBug321926a2" };
78 // TESTS_NAMES = new String[] { "testBug432109" };
79 // TESTS_NAMES = new String[] { "testBug418500" };
80 // TESTS_NUMBERS = new int[] { 561 };
81 // TESTS_RANGE = new int[] { 1, 2049 };
82 }
83
suite()84 public static Test suite() {
85 return buildAllCompliancesTestSuite(testClass());
86 }
87
testClass()88 public static Class testClass() {
89 return NullReferenceTest.class;
90 }
91
92 // Conditionally augment problem detection settings
93 static boolean setNullRelatedOptions = true;
94 @Override
getCompilerOptions()95 protected Map getCompilerOptions() {
96 Map defaultOptions = super.getCompilerOptions();
97 if (setNullRelatedOptions) {
98 defaultOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
99 defaultOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.ERROR);
100 defaultOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
101 defaultOptions.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE);
102 defaultOptions.put(CompilerOptions.OPTION_IncludeNullInfoFromAsserts, CompilerOptions.ENABLED);
103 }
104 return defaultOptions;
105 }
106
runNegativeNullTest(String[] testFiles, String expectedCompilerLog)107 protected void runNegativeNullTest(String[] testFiles, String expectedCompilerLog) {
108 runNegativeTest(testFiles, expectedCompilerLog, JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
109 }
110
111 // null analysis -- simple case for local
test0001_simple_local()112 public void test0001_simple_local() {
113 runNegativeTest(
114 new String[] {
115 "X.java",
116 "public class X {\n" +
117 " void foo() {\n" +
118 " Object o = null;\n" +
119 " o.toString();\n" +
120 " }\n" +
121 "}\n"},
122 "----------\n" +
123 "1. ERROR in X.java (at line 4)\n" +
124 " o.toString();\n" +
125 " ^\n" +
126 "Null pointer access: The variable o can only be null at this location\n" +
127 "----------\n",
128 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
129 }
130
131 // null analysis -- simple case for field
132 // the current design leaves fields out of the analysis altogether
test0002_simple_field()133 public void test0002_simple_field() {
134 this.runNegativeTest(
135 new String[] {
136 "X.java",
137 "public class X {\n" +
138 " Object o;\n" +
139 " void foo() {\n" +
140 " o = null;\n" +
141 " o.toString();\n" +
142 " }\n" +
143 "}\n"},
144 ""
145 // "----------\n" +
146 // "1. ERROR in X.java (at line 5)\n" +
147 // " o.toString();\n" +
148 // " ^\n" +
149 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
150 // "----------\n"
151 );
152 }
153
154 // null analysis -- simple case for parameter
test0003_simple_parameter()155 public void test0003_simple_parameter() {
156 runNegativeTest(
157 new String[] {
158 "X.java",
159 "public class X {\n" +
160 " void foo(Object o) {\n" +
161 " o = null;\n" +
162 " o.toString();\n" +
163 " }\n" +
164 "}\n"},
165 "----------\n" +
166 "1. ERROR in X.java (at line 4)\n" +
167 " o.toString();\n" +
168 " ^\n" +
169 "Null pointer access: The variable o can only be null at this location\n" +
170 "----------\n",
171 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
172 }
173
174 // null analysis -- final local
test0004_final_local()175 public void test0004_final_local() {
176 runNegativeTest(
177 new String[] {
178 "X.java",
179 "public class X {\n" +
180 " void foo() {\n" +
181 " final Object o = null;\n" +
182 " o.toString();\n" +
183 " }\n" +
184 "}\n"},
185 "----------\n" +
186 "1. ERROR in X.java (at line 4)\n" +
187 " o.toString();\n" +
188 " ^\n" +
189 "Null pointer access: The variable o can only be null at this location\n" +
190 "----------\n",
191 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
192 }
193
194 // null analysis -- final local
test0005_final_local()195 public void test0005_final_local() {
196 this.runNegativeTest(
197 new String[] {
198 "X.java",
199 "public class X {\n" +
200 " void foo() {\n" +
201 " final Object o;\n" +
202 " o.toString();\n" +
203 " }\n" +
204 "}\n"},
205 "----------\n" +
206 "1. ERROR in X.java (at line 4)\n" +
207 " o.toString();\n" +
208 " ^\n" +
209 "The local variable o may not have been initialized\n" +
210 // hides the null related message, but complains once, which is good
211 "----------\n");
212 }
213
214 // null analysis -- final local
test0006_final_local()215 public void test0006_final_local() {
216 runNegativeTest(
217 new String[] {
218 "X.java",
219 "public class X {\n" +
220 " void foo() {\n" +
221 " final Object o = null;\n" +
222 " if (o != null) { /* */ }\n" + // complain
223 " }\n" +
224 "}\n"},
225 "----------\n" +
226 "1. ERROR in X.java (at line 4)\n" +
227 " if (o != null) { /* */ }\n" +
228 " ^\n" +
229 "Null comparison always yields false: The variable o can only be null at this location\n" +
230 "----------\n" +
231 "2. WARNING in X.java (at line 4)\n" +
232 " if (o != null) { /* */ }\n" +
233 " ^^^^^^^^^\n" +
234 "Dead code\n" +
235 "----------\n",
236 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
237 }
238
239 // null analysis -- local with member
test0007_local_with_member()240 public void test0007_local_with_member() {
241 runNegativeTest(
242 new String[] {
243 "X.java",
244 "public class X {\n" +
245 " Object m;\n" +
246 " void foo() {\n" +
247 " X x = null;\n" +
248 " x.m.toString();\n" + // complain
249 " }\n" +
250 "}\n"},
251 "----------\n" +
252 "1. ERROR in X.java (at line 5)\n" +
253 " x.m.toString();\n" +
254 " ^\n" +
255 "Null pointer access: The variable x can only be null at this location\n" +
256 "----------\n",
257 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
258 }
259
260 // null analysis -- local with member
test0008_local_with_member()261 public void test0008_local_with_member() {
262 runNegativeTest(
263 new String[] {
264 "X.java",
265 "public class X {\n" +
266 " Object m;\n" +
267 " void foo() {\n" +
268 " X x = null;\n" +
269 " System.out.println(x.m);\n" + // complain
270 " }\n" +
271 "}\n"},
272 "----------\n" +
273 "1. ERROR in X.java (at line 5)\n" +
274 " System.out.println(x.m);\n" +
275 " ^\n" +
276 "Null pointer access: The variable x can only be null at this location\n" +
277 "----------\n",
278 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
279 }
280
281 // null analysis -- local with member
test0009_local_with_member()282 public void test0009_local_with_member() {
283 this.runConformTest(
284 new String[] {
285 "X.java",
286 "public class X {\n" +
287 " Object m;\n" +
288 " void foo(X x) {\n" +
289 " x.m.toString();\n" + // quiet
290 " }\n" +
291 "}\n"},
292 "");
293 }
294
295 // null analysis -- field
test0010_field_with_method_call()296 public void test0010_field_with_method_call() {
297 this.runConformTest(
298 new String[] {
299 "X.java",
300 "public class X {\n" +
301 " Object o;\n" +
302 " void foo() {\n" +
303 " o = null;\n" +
304 " bar();\n" + // defuses null by side effect
305 " o.toString();\n" +
306 " }\n" +
307 " void bar() {\n" +
308 " }\n" +
309 "}\n"},
310 "");
311 }
312
313 // null analysis -- field
test0011_field_with_method_call()314 public void test0011_field_with_method_call() {
315 this.runConformTest(
316 new String[] {
317 "X.java",
318 "public class X {\n" +
319 " static Object o;\n" +
320 " void foo() {\n" +
321 " o = null;\n" +
322 " bar();\n" + // defuses null by side effect
323 " o.toString();\n" +
324 " }\n" +
325 " static void bar() {\n" +
326 " }\n" +
327 "}\n"},
328 "");
329 }
330
331 // null analysis -- field
test0012_field_with_method_call()332 public void test0012_field_with_method_call() {
333 this.runConformTest(
334 new String[] {
335 "X.java",
336 "public class X {\n" +
337 " Object o;\n" +
338 " void foo() {\n" +
339 " o = null;\n" +
340 " bar();\n" +
341 " o.toString();\n" +
342 " }\n" +
343 " static void bar() {\n" +
344 " }\n" +
345 "}\n"},
346 "" // still ok because the class may hold a pointer to this
347 );
348 }
349
350 // null analysis -- field
test0013_field_with_method_call()351 public void test0013_field_with_method_call() {
352 this.runConformTest(
353 new String[] {
354 "X.java",
355 "public class X {\n" +
356 " static Object o;\n" +
357 " void foo() {\n" +
358 " o = null;\n" +
359 " bar();\n" +
360 " o.toString();\n" +
361 " }\n" +
362 " void bar() {\n" +
363 " }\n" +
364 "}\n"},
365 "" // still ok because this may place a static call upon X
366 );
367 }
368
369 // null analysis -- field
test0014_field_with_explicit_this_access()370 public void test0014_field_with_explicit_this_access() {
371 this.runNegativeTest(
372 new String[] {
373 "X.java",
374 "public class X {\n" +
375 " Object o;\n" +
376 " void foo() {\n" +
377 " o = null;\n" +
378 " this.o.toString();\n" +
379 " }\n" +
380 "}\n"},
381 ""
382 // "----------\n" +
383 // "1. ERROR in X.java (at line 5)\n" +
384 // " this.o.toString();\n" +
385 // " ^^^^^^\n" +
386 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
387 // "----------\n"
388 );
389 }
390
391 // null analysis -- field
test0015_field_with_explicit_this_access()392 public void test0015_field_with_explicit_this_access() {
393 this.runNegativeTest(
394 new String[] {
395 "X.java",
396 "public class X {\n" +
397 " Object o;\n" +
398 " void foo() {\n" +
399 " this.o = null;\n" +
400 " o.toString();\n" +
401 " }\n" +
402 "}\n"},
403 ""
404 // "----------\n" +
405 // "1. ERROR in X.java (at line 5)\n" +
406 // " o.toString();\n" +
407 // " ^\n" +
408 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
409 // "----------\n"
410 );
411 }
412
413 // null analysis -- field
test0016_field_of_another_object()414 public void test0016_field_of_another_object() {
415 this.runConformTest(
416 new String[] {
417 "X.java",
418 "public class X {\n" +
419 " Object o;\n" +
420 " void foo() {\n" +
421 " X other = new X();\n" +
422 " other.o = null;\n" +
423 " other.o.toString();\n" +
424 " }\n" +
425 "}\n"},
426 "");
427 }
428
429 // null analysis -- field
test0017_field_of_another_object()430 public void test0017_field_of_another_object() {
431 this.runConformTest(
432 new String[] {
433 "X.java",
434 "public class X {\n" +
435 " Object o;\n" +
436 " void foo() {\n" +
437 " X other = this;\n" +
438 " o = null;\n" +
439 " other.o.toString();\n" +
440 " }\n" +
441 "}\n"},
442 "");
443 }
444
445 // null analysis -- field
test0018_field_of_enclosing_object()446 public void test0018_field_of_enclosing_object() {
447 this.runNegativeTest(
448 new String[] {
449 "X.java",
450 "public class X {\n" +
451 " Object o;\n" +
452 " public class Y {\n" +
453 " void foo() {\n" +
454 " X.this.o = null;\n" +
455 " X.this.o.toString();\n" + // complain
456 " }\n" +
457 " }\n" +
458 "}\n"},
459 ""
460 // "----------\n" +
461 // "1. ERROR in X.java (at line 6)\n" +
462 // " X.this.o.toString();\n" +
463 // " ^^^^^^^^\n" +
464 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
465 // "----------\n"
466 );
467 }
468
469 // null analysis -- fields
470 // check that fields that are protected against concurrent access
471 // behave as locals when no call to further methods can affect them
test0019_field_synchronized()472 public void test0019_field_synchronized() {
473 this.runNegativeTest(
474 new String[] {
475 "X.java",
476 "public class X {\n" +
477 " Object o;\n" +
478 " public synchronized void foo() {\n" +
479 " o = null;\n" +
480 " o.toString();\n" +
481 " }\n" +
482 " void bar() {/* */}\n" +
483 "}\n"},
484 ""
485 // "----------\n" +
486 // "1. ERROR in X.java (at line 5)\n" +
487 // " o.toString();\n" +
488 // " ^\n" +
489 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
490 // "----------\n"
491 );
492 }
493
494 // null analysis -- field
495 // check that final fields behave as locals despite calls to further
496 // methods
test0020_final_field()497 public void test0020_final_field() {
498 this.runNegativeTest(
499 new String[] {
500 "X.java",
501 "public class X {\n" +
502 " final Object o = null;\n" +
503 " public synchronized void foo() {\n" +
504 " bar();\n" +
505 " o.toString();\n" +
506 " }\n" +
507 " void bar() {/* */}\n" +
508 "}\n"},
509 ""
510 // "----------\n" +
511 // "1. ERROR in X.java (at line 5)\n" +
512 // " o.toString();\n" +
513 // " ^\n" +
514 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
515 // "----------\n"
516 );
517 }
518
519 // null analysis -- field
test0021_final_field()520 public void test0021_final_field() {
521 this.runNegativeTest(
522 new String[] {
523 "X.java",
524 "public class X {\n" +
525 " final Object o = null;\n" +
526 " X () {\n" +
527 " bar();\n" +
528 " o.toString();\n" +
529 " }\n" +
530 " void bar() {/* */}\n" +
531 "}\n"},
532 ""
533 // "----------\n" +
534 // "1. ERROR in X.java (at line 5)\n" +
535 // " o.toString();\n" +
536 // " ^\n" +
537 // "The field o is likely null; it was either set to null or checked for null when last used\n" +
538 // "----------\n"
539 );
540 }
541
542 // null analysis -- field
test0022_final_field()543 public void test0022_final_field() {
544 this.runNegativeTest(
545 new String[] {
546 "X.java",
547 "public class X {\n" +
548 " final Object o = new Object();\n" +
549 " X () {\n" +
550 " bar();\n" +
551 " if (o == null) { /* empty */ }\n" +
552 " }\n" +
553 " void bar() {/* */}\n" +
554 "}\n"},
555 ""
556 // "----------\n" +
557 // "1. ERROR in X.java (at line 5)\n" +
558 // " if (o == null) { /* empty */ }\n" +
559 // " ^\n" +
560 // "The field o is likely non null; it was either set to a non-null value or assumed to be non-null when last used\n" +
561 // "----------\n"
562 );
563 }
564
565 // null analysis -- field
test0023_field_assignment()566 public void test0023_field_assignment() {
567 this.runConformTest(
568 new String[] {
569 "X.java",
570 "public class X {\n" +
571 " Object m;\n" +
572 " void foo(X x) {\n" +
573 " Object o = x.m;\n" +
574 " if (o == null) { /* */ };\n" +
575 " }\n" +
576 "}\n"},
577 "");
578 }
579
580 // null analysis -- field
test0024_field_cast_assignment()581 public void test0024_field_cast_assignment() {
582 this.runConformTest(
583 new String[] {
584 "X.java",
585 "public class X {\n" +
586 " Object m;\n" +
587 " void foo(Object x) {\n" +
588 " Object o = ((X) x).m;\n" +
589 " if (o == null) { /* */ };\n" +
590 " }\n" +
591 "}\n"},
592 "");
593 }
594
595 // null analysis -- parameter
test0025_parameter()596 public void test0025_parameter() {
597 this.runConformTest(
598 new String[] {
599 "X.java",
600 "public class X {\n" +
601 " void foo(Object o) {\n" +
602 " o.toString();\n" + // quiet: parameters have unknown value
603 " }\n" +
604 "}\n"},
605 "");
606 }
607
608 // null analysis -- suppress warnings
test0026_suppress_warnings()609 public void test0026_suppress_warnings() {
610 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
611 Map compilerOptions = getCompilerOptions();
612 compilerOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
613 this.runConformTest(
614 new String[] {
615 "X.java",
616 "@SuppressWarnings(\"null\")\n" +
617 "public class X {\n" +
618 " void foo() {\n" +
619 " Object o = null;\n" +
620 " o.toString();\n" +
621 " }\n" +
622 "}\n"},
623 "", null, true, null, compilerOptions, null);
624 }
625 }
626
627 // null analysis -- embedded comparison
test0027_embedded_comparison()628 public void test0027_embedded_comparison() {
629 runNegativeTest(
630 new String[] {
631 "X.java",
632 "public class X {\n" +
633 " void foo(Object o) {\n" +
634 " boolean b = o != null;\n" + // shades doubts upon o
635 " if (b) { /* */ }\n" +
636 " o.toString();\n" + // complain
637 " }\n" +
638 "}\n"},
639 "----------\n" +
640 "1. ERROR in X.java (at line 5)\n" +
641 " o.toString();\n" +
642 " ^\n" +
643 "Potential null pointer access: The variable o may be null at this location\n" +
644 "----------\n",
645 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
646 }
647
648 // null analysis -- field
test0028_field_as_initializer()649 public void test0028_field_as_initializer() {
650 this.runConformTest(
651 new String[] {
652 "X.java",
653 "public class X {\n" +
654 " X f;\n" +
655 " void foo() {\n" +
656 " X x = f;\n" +
657 " if (x == null) { /* */ }\n" +
658 " }\n" +
659 "}\n"},
660 "");
661 }
662
663 // null analysis -- field
test0029_field_assignment()664 public void test0029_field_assignment() {
665 runNegativeTest(
666 new String[] {
667 "X.java",
668 "public class X {\n" +
669 " Object m;\n" +
670 " void foo() {\n" +
671 " X x = null;\n" +
672 " x.m = new Object();\n" +
673 " }\n" +
674 "}\n"},
675 "----------\n" +
676 "1. ERROR in X.java (at line 5)\n" +
677 " x.m = new Object();\n" +
678 " ^\n" +
679 "Null pointer access: The variable x can only be null at this location\n" +
680 "----------\n",
681 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
682 }
683
684 // null analysis -- conditional expression
test0030_conditional_expression()685 public void test0030_conditional_expression() {
686 runNegativeTest(
687 new String[] {
688 "X.java",
689 "public class X {\n" +
690 " void foo() {\n" +
691 " Object o = true ? null : null;\n" +
692 " o.toString();\n" +
693 " }\n" +
694 "}\n"},
695 "----------\n" +
696 "1. WARNING in X.java (at line 3)\n" +
697 " Object o = true ? null : null;\n" +
698 " ^^^^\n" +
699 "Dead code\n" +
700 "----------\n" +
701 "2. ERROR in X.java (at line 4)\n" +
702 " o.toString();\n" +
703 " ^\n" +
704 "Null pointer access: The variable o can only be null at this location\n" +
705 "----------\n",
706 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
707 }
708
709 // null analysis -- conditional expression
test0031_conditional_expression()710 public void test0031_conditional_expression() {
711 runNegativeTest(
712 new String[] {
713 "X.java",
714 "public class X {\n" +
715 " void foo() {\n" +
716 " Object o = true ? null : new Object();\n" +
717 " o.toString();\n" +
718 " }\n" +
719 "}\n"},
720 "----------\n" +
721 "1. WARNING in X.java (at line 3)\n" +
722 " Object o = true ? null : new Object();\n" +
723 " ^^^^^^^^^^^^\n" +
724 "Dead code\n" +
725 "----------\n" +
726 "2. ERROR in X.java (at line 4)\n" +
727 " o.toString();\n" +
728 " ^\n" +
729 "Null pointer access: The variable o can only be null at this location\n" +
730 "----------\n",
731 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
732 }
733
734 // null analysis -- conditional expression
test0032_conditional_expression()735 public void test0032_conditional_expression() {
736 this.runConformTest(
737 new String[] {
738 "X.java",
739 "public class X {\n" +
740 " void foo() {\n" +
741 " Object o = false ? null : new Object();\n" +
742 " o.toString();\n" +
743 " }\n" +
744 "}\n"},
745 "");
746 }
747
748 // null analysis -- conditional expression
test0033_conditional_expression()749 public void test0033_conditional_expression() {
750 runNegativeTest(
751 new String[] {
752 "X.java",
753 "public class X {\n" +
754 " void foo() {\n" +
755 " Object o = (1 == 1) ? null : new Object();\n" +
756 " o.toString();\n" +
757 " }\n" +
758 "}\n"},
759 "----------\n" +
760 "1. WARNING in X.java (at line 3)\n" +
761 " Object o = (1 == 1) ? null : new Object();\n" +
762 " ^^^^^^^^\n" +
763 "Comparing identical expressions\n" +
764 "----------\n" +
765 "2. WARNING in X.java (at line 3)\n" +
766 " Object o = (1 == 1) ? null : new Object();\n" +
767 " ^^^^^^^^^^^^\n" +
768 "Dead code\n" +
769 "----------\n" +
770 "3. ERROR in X.java (at line 4)\n" +
771 " o.toString();\n" +
772 " ^\n" +
773 "Null pointer access: The variable o can only be null at this location\n" +
774 "----------\n",
775 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
776 }
777
778 // null analysis -- conditional expression
779 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
test0034_conditional_expression()780 public void test0034_conditional_expression() {
781 runNegativeNullTest(
782 new String[] {
783 "X.java",
784 "public class X {\n" +
785 " boolean b;\n" +
786 " void foo() {\n" +
787 " Object o = b ? null : new Object();\n" +
788 " o.toString();\n" +
789 " }\n" +
790 "}\n"},
791 "----------\n" +
792 "1. ERROR in X.java (at line 5)\n" +
793 " o.toString();\n" +
794 " ^\n" +
795 "Potential null pointer access: The variable o may be null at this location\n" +
796 "----------\n");
797 }
798
799 // null analysis -- conditional expression
800 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
801 // variant with constant condition
test0034_conditional_expression_2()802 public void test0034_conditional_expression_2() {
803 this.runConformTest(
804 new String[] {
805 "X.java",
806 "public class X {\n" +
807 " boolean b;\n" +
808 " void foo() {\n" +
809 " Object o = false ? null : new Object();\n" +
810 " o.toString();\n" +
811 " }\n" +
812 "}\n"},
813 "");
814 }
815
816 // null analysis -- conditional expression
817 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
test0034_conditional_expression_3()818 public void test0034_conditional_expression_3() {
819 runNegativeNullTest(
820 new String[] {
821 "X.java",
822 "public class X {\n" +
823 " boolean b;\n" +
824 " void foo(Object a) {\n" +
825 " if (a == null) {}\n" +
826 " Object o = b ? a : new Object();\n" +
827 " o.toString();\n" +
828 " }\n" +
829 "}\n"},
830 "----------\n" +
831 "1. ERROR in X.java (at line 6)\n" +
832 " o.toString();\n" +
833 " ^\n" +
834 "Potential null pointer access: The variable o may be null at this location\n" +
835 "----------\n");
836 }
837
838 // null analysis -- conditional expression
839 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
840 // variant with dependency between condition and expression - LocalDeclaration
841 // TODO(stephan) cannot analyse this flow dependency
_test0034_conditional_expression_4()842 public void _test0034_conditional_expression_4() {
843 this.runConformTest(
844 new String[] {
845 "X.java",
846 "public class X {\n" +
847 " boolean b;\n" +
848 " void foo(Object u) {\n" +
849 " if (u == null) {}\n" + //taint
850 " Object o = (u == null) ? new Object() : u;\n" +
851 " o.toString();\n" +
852 " }\n" +
853 "}\n"},
854 "");
855 }
856
857 // null analysis -- conditional expression
858 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
859 // variant with dependency between condition and expression - Assignment
860 // TODO(stephan) cannot analyse this flow dependency
_test0034_conditional_expression_5()861 public void _test0034_conditional_expression_5() {
862 this.runConformTest(
863 new String[] {
864 "X.java",
865 "public class X {\n" +
866 " boolean b;\n" +
867 " void foo(Object u) {\n" +
868 " if (u == null) {}\n" + //taint
869 " Object o;\n" +
870 " o = (u == null) ? new Object() : u;\n" +
871 " o.toString();\n" +
872 " }\n" +
873 "}\n"},
874 "");
875 }
876
877 // null analysis -- conditional expression
test0035_conditional_expression()878 public void test0035_conditional_expression() {
879 this.runConformTest(
880 new String[] {
881 "X.java",
882 "public class X {\n" +
883 " boolean b;\n" +
884 " void foo() {\n" +
885 " Object o = b ? null : new Object();\n" +
886 " if (o == null) { /* */ }\n" +
887 " }\n" +
888 "}\n"},
889 "");
890 }
891
892 // null analysis -- conditional expression
test0036_conditional_expression()893 public void test0036_conditional_expression() {
894 runNegativeTest(
895 new String[] {
896 "X.java",
897 "public class X {\n" +
898 " boolean b;\n" +
899 " void foo() {\n" +
900 " Object o = b ? null : null;\n" +
901 " if (o == null) { /* */ }\n" +
902 " }\n" +
903 "}\n"},
904 "----------\n" +
905 "1. ERROR in X.java (at line 5)\n" +
906 " if (o == null) { /* */ }\n" +
907 " ^\n" +
908 "Redundant null check: The variable o can only be null at this location\n" +
909 "----------\n",
910 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
911 }
912
913 // https://bugs.eclipse.org/400761: [compiler][null] null may be return as boolean without a diagnostic
test0037_conditional_expression_1()914 public void test0037_conditional_expression_1() {
915 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // needs autoboxing
916 runNegativeTest(
917 new String[] {
918 "X.java",
919 "public class X {\n" +
920 " boolean badFunction(int i) {\n" +
921 " return i > 0 ? true : null;\n" +
922 " }\n" +
923 "}\n"},
924 "----------\n" +
925 "1. ERROR in X.java (at line 3)\n" +
926 " return i > 0 ? true : null;\n" +
927 " ^^^^^^^^^^^^^^^^^^^\n" +
928 "Potential null pointer access: This expression of type Boolean may be null but requires auto-unboxing\n" +
929 "----------\n",
930 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
931 }
932 // https://bugs.eclipse.org/400761: [compiler][null] null may be return as boolean without a diagnostic
test0037_conditional_expression_2()933 public void test0037_conditional_expression_2() {
934 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // needs autoboxing
935 Map options = getCompilerOptions();
936 options.put(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, JavaCore.ENABLED);
937 runNegativeTest(
938 true,
939 new String[] {
940 "X.java",
941 "public class X {\n" +
942 " int badFunction(int i) {\n" +
943 " return i > 0 ? null : Integer.MIN_VALUE;\n" +
944 " }\n" +
945 " @SuppressWarnings(\"null\")\n" +
946 " int silent(int i) {\n" +
947 " return i > 0 ? null : Integer.MIN_VALUE;\n" +
948 " }\n" +
949 "}\n"},
950 null,
951 options,
952 "----------\n" +
953 "1. ERROR in X.java (at line 3)\n" +
954 " return i > 0 ? null : Integer.MIN_VALUE;\n" +
955 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
956 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
957 "----------\n",
958 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
959 }
960 //https://bugs.eclipse.org/400761: [compiler][null] null may be return as boolean without a diagnostic
test0037_conditional_expression_3()961 public void test0037_conditional_expression_3() {
962 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // needs autoboxing
963 Map options = getCompilerOptions();
964 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
965 runNegativeTest(
966 true,
967 new String[] {
968 "X.java",
969 "public class X {\n" +
970 " boolean badFunction3(int i) {\n" +
971 " //expected a potential null problem:\n" +
972 " return i > 0 ? true : (Boolean) null;\n" +
973 " }\n" +
974 "}\n"},
975 null,
976 options,
977 "----------\n" +
978 "1. ERROR in X.java (at line 4)\n" +
979 " return i > 0 ? true : (Boolean) null;\n" +
980 " ^^^^^^^^^^^^^^\n" +
981 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
982 "----------\n",
983 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
984 }
985 // https://bugs.eclipse.org/400761: [compiler][null] null may be return as boolean without a diagnostic
986 // if-then-else instead of conditional expression
test0037_conditional_expression_4()987 public void test0037_conditional_expression_4() {
988 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // needs autoboxing
989 Map options = getCompilerOptions();
990 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
991 options.put(JavaCore.COMPILER_PB_UNNECESSARY_ELSE, JavaCore.IGNORE);
992 runNegativeTest(
993 true,
994 new String[] {
995 "X.java",
996 "public class X {\n" +
997 " boolean badFunction4(int i) {\n" +
998 " if (i > 0)\n" +
999 " return true;\n" +
1000 " else\n" +
1001 " // expected a null problem:\n" +
1002 " return (Boolean) null;\n" +
1003 " }\n" +
1004 "}\n"},
1005 null,
1006 options,
1007 "----------\n" +
1008 "1. ERROR in X.java (at line 7)\n" +
1009 " return (Boolean) null;\n" +
1010 " ^^^^^^^^^^^^^^\n" +
1011 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
1012 "----------\n",
1013 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1014 }
1015 // https://bugs.eclipse.org/400761: [compiler][null] null may be return as boolean without a diagnostic
1016 // pot-null cond-expr in receiver position
test0037_conditional_expression_5()1017 public void test0037_conditional_expression_5() {
1018 Map options = getCompilerOptions();
1019 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
1020 runNegativeTest(
1021 true,
1022 new String[] {
1023 "X.java",
1024 "public class X {\n" +
1025 " String badFunction3(int i) {\n" +
1026 " return (i > 0 ? this : null).toString();\n" +
1027 " }\n" +
1028 " String badFunction4(int i) {\n" +
1029 " Object o = null;\n" +
1030 " return (i > 0 ? o : null).toString();\n" +
1031 " }\n" +
1032 "}\n"},
1033 null,
1034 options,
1035 "----------\n" +
1036 "1. ERROR in X.java (at line 3)\n" +
1037 " return (i > 0 ? this : null).toString();\n" +
1038 " ^^^^^^^^^^^^^^^^^^^^^\n" +
1039 "Potential null pointer access: This expression may be null\n" +
1040 "----------\n" +
1041 "2. ERROR in X.java (at line 7)\n" +
1042 " return (i > 0 ? o : null).toString();\n" +
1043 " ^^^^^^^^^^^^^^^^^^\n" +
1044 "Null pointer access: This expression can only be null\n" +
1045 "----------\n",
1046 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1047 }
1048 // https://bugs.eclipse.org/403147 [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking
1049 // finally block injects pot-nn into itself via enclosing loop
test0037_autounboxing_1()1050 public void test0037_autounboxing_1() {
1051 if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
1052 Map options = getCompilerOptions();
1053 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
1054 runNegativeTest(
1055 true,
1056 new String[] {
1057 "X.java",
1058 "public class X {\n" +
1059 " void foo1(boolean b) {\n" +
1060 " int j = 0;\n" +
1061 " Integer i = null;\n" +
1062 " while (true) {\n" +
1063 " try {\n" +
1064 " j = 1;\n" +
1065 " } finally {\n" +
1066 " j = (b?i:1)+1;\n" +
1067 " i = 2;\n" +
1068 " }\n" +
1069 " }\n" +
1070 " }\n" +
1071 " void foo2(boolean b) {\n" +
1072 " int j = 0;\n" +
1073 " Integer i = null;\n" +
1074 " try {\n" +
1075 " j = 1;\n" +
1076 " } finally {\n" +
1077 " j = (b?i:1)+1;\n" +
1078 " i = 2;\n" +
1079 " }\n" +
1080 " }\n" +
1081 "}\n"},
1082 null,
1083 options,
1084 "----------\n" +
1085 "1. ERROR in X.java (at line 9)\n" +
1086 " j = (b?i:1)+1;\n" +
1087 " ^\n" +
1088 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
1089 "----------\n" +
1090 "2. ERROR in X.java (at line 20)\n" +
1091 " j = (b?i:1)+1;\n" +
1092 " ^\n" +
1093 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
1094 "----------\n",
1095 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1096 }
1097 // https://bugs.eclipse.org/403147 [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking
1098 // inject pot.nn from try into finally
test0037_autounboxing_2()1099 public void test0037_autounboxing_2() {
1100 if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
1101 Map options = getCompilerOptions();
1102 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
1103 runNegativeTest(
1104 true,
1105 new String[] {
1106 "X.java",
1107 "public class X {\n" +
1108 " void foo2(boolean b) {\n" +
1109 " int j = 0;\n" +
1110 " Integer i = null;\n" +
1111 " while (true) {\n" +
1112 " try {\n" +
1113 " if (b)\n" +
1114 " i = 3;\n" +
1115 " } finally {\n" +
1116 " j = (b?i:1)+1;\n" +
1117 " }\n" +
1118 " }\n" +
1119 " }\n" +
1120 " void foo3(boolean b) {\n" +
1121 " int j = 0;\n" +
1122 " Integer i = null;\n" +
1123 " try {\n" +
1124 " if (b)\n" +
1125 " i = 3;\n" +
1126 " } finally {\n" +
1127 " j = (b?i:1)+1;\n" +
1128 " }\n" +
1129 " }\n" +
1130 "}\n"},
1131 null,
1132 options,
1133 "----------\n" +
1134 "1. ERROR in X.java (at line 10)\n" +
1135 " j = (b?i:1)+1;\n" +
1136 " ^\n" +
1137 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
1138 "----------\n" +
1139 "2. ERROR in X.java (at line 21)\n" +
1140 " j = (b?i:1)+1;\n" +
1141 " ^\n" +
1142 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
1143 "----------\n",
1144 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1145 }
1146 // https://bugs.eclipse.org/403147 [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking
1147 // null from try, nn from catch, merge both into finally
test0037_autounboxing_3()1148 public void test0037_autounboxing_3() {
1149 if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
1150 Map options = getCompilerOptions();
1151 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
1152 runNegativeTest(
1153 true,
1154 new String[] {
1155 "X.java",
1156 "public class X {\n" +
1157 " void foo3(Integer i, boolean b) {\n" +
1158 " int j = 0;\n" +
1159 " while (true) {\n" +
1160 " try {\n" +
1161 " i = null;\n" +
1162 " unsafe();\n" +
1163 " } catch (Exception e) {\n" +
1164 " i = 3;\n" +
1165 " } finally {\n" +
1166 " j = (b?i:1)+1;\n" +
1167 " }\n" +
1168 " }\n" +
1169 " }\n" +
1170 " void foo4(Integer i, boolean b) {\n" +
1171 " int j = 0;\n" +
1172 " try {\n" +
1173 " i = null;\n" +
1174 " unsafe();\n" +
1175 " } catch (Exception e) {\n" +
1176 " i = 3;\n" +
1177 " } finally {\n" +
1178 " while (j < 0)\n" +
1179 " j = (b?i:1)+1;\n" +
1180 " }\n" +
1181 " }\n" +
1182 "\n" +
1183 " private void unsafe() throws Exception {\n" +
1184 " throw new Exception();\n" +
1185 " }\n" +
1186 "}\n"},
1187 null,
1188 options,
1189 "----------\n" +
1190 "1. ERROR in X.java (at line 11)\n" +
1191 " j = (b?i:1)+1;\n" +
1192 " ^\n" +
1193 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
1194 "----------\n" +
1195 "2. ERROR in X.java (at line 24)\n" +
1196 " j = (b?i:1)+1;\n" +
1197 " ^\n" +
1198 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
1199 "----------\n",
1200 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1201 }
1202 // https://bugs.eclipse.org/403147 [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking
1203 // effective protection locally within the finally block
test0037_autounboxing_4()1204 public void test0037_autounboxing_4() {
1205 if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
1206 Map options = getCompilerOptions();
1207 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
1208 runConformTest(
1209 new String[] {
1210 "X.java",
1211 "public class X {\n" +
1212 " void foo3(Integer i, boolean b) {\n" +
1213 " int j = 0;\n" +
1214 " while (true) {\n" +
1215 " try {\n" +
1216 " i = null;\n" +
1217 " unsafe();\n" +
1218 " } catch (Exception e) {\n" +
1219 " i = 3;\n" +
1220 " } finally {\n" +
1221 " if (i == null) i = 4;\n" +
1222 " j = (b?i:1)+1;\n" +
1223 " }\n" +
1224 " }\n" +
1225 " }\n" +
1226 " void foo4(Integer i, boolean b) {\n" +
1227 " int j = 0;\n" +
1228 " try {\n" +
1229 " i = null;\n" +
1230 " unsafe();\n" +
1231 " } catch (Exception e) {\n" +
1232 " i = 3;\n" +
1233 " } finally {\n" +
1234 " while (i == null)\n" +
1235 " i = 4;\n" +
1236 " while (j < 4)\n" +
1237 " j = (b?i:1)+1;\n" +
1238 " }\n" +
1239 " }\n" +
1240 "\n" +
1241 " private void unsafe() throws Exception {\n" +
1242 " throw new Exception();\n" +
1243 " }\n" +
1244 "}\n"},
1245 options);
1246 }
1247 // https://bugs.eclipse.org/403147 [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking
1248 // array reference in nested try
test0037_autounboxing_5()1249 public void test0037_autounboxing_5() {
1250 if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
1251 Map options = getCompilerOptions();
1252 options.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
1253 runNegativeTest(
1254 true,
1255 new String[] {
1256 "X.java",
1257 "public class X {\n" +
1258 " void foo(Object [] o, boolean b, Integer i) {\n" +
1259 " int j = 1;\n" +
1260 " try {\n" +
1261 " if (b) i = null;\n" +
1262 " } catch (RuntimeException r) {\n" +
1263 " i = 3;\n" +
1264 " } finally {\n" +
1265 " try {\n" +
1266 " System.out.println(o[i]); \n" +
1267 " } finally {\n" +
1268 " System.out.println(j);\n" +
1269 " }\n" +
1270 " }\n" +
1271 " }\n" +
1272 "}\n"},
1273 null,
1274 options,
1275 "----------\n" +
1276 "1. ERROR in X.java (at line 10)\n" +
1277 " System.out.println(o[i]); \n" +
1278 " ^\n" +
1279 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
1280 "----------\n",
1281 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1282 }
1283
1284 // Bug 406384 - Internal error with I20130413
test0037_autounboxing_6()1285 public void test0037_autounboxing_6() {
1286 if (this.complianceLevel < ClassFileConstants.JDK1_5)
1287 return;
1288 runConformTest(
1289 new String[] {
1290 "X.java",
1291 "import java.util.List;\n" +
1292 "public class X {\n" +
1293 " void test(List<String> l1, List<String> l2, int i, Object val) {\n" +
1294 " for (String s1 : l1) {\n" +
1295 " for (String s2 : l2) {\n" +
1296 " switch (i) {\n" +
1297 " case 1: \n" +
1298 " boolean booleanValue = (Boolean)val;\n" +
1299 " }\n" +
1300 " }\n" +
1301 " }\n" +
1302 " }\n" +
1303 "}\n"
1304 });
1305 }
1306
1307 // null analysis -- autoboxing
test0040_autoboxing_compound_assignment()1308 public void test0040_autoboxing_compound_assignment() {
1309 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
1310 runNegativeTest(
1311 new String[] {
1312 "X.java",
1313 "public class X {\n" +
1314 " void foo() {\n" +
1315 " Integer i = null;\n" +
1316 " i += 1;\n" +
1317 " }\n" +
1318 "}\n"},
1319 "----------\n" +
1320 "1. ERROR in X.java (at line 4)\n" +
1321 " i += 1;\n" +
1322 " ^\n" +
1323 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
1324 "----------\n",
1325 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1326 }
1327 }
1328
1329 // null analysis -- autoboxing
test0041_autoboxing_increment_operator()1330 public void test0041_autoboxing_increment_operator() {
1331 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
1332 runNegativeTest(
1333 new String[] {
1334 "X.java",
1335 "public class X {\n" +
1336 " void foo() {\n" +
1337 " Integer i = null;\n" +
1338 " i++;\n" + // complain: this is null
1339 " ++i;\n" + // quiet (because previous step guards it)
1340 " }\n" +
1341 "}\n"},
1342 "----------\n" +
1343 "1. ERROR in X.java (at line 4)\n" +
1344 " i++;\n" +
1345 " ^\n" +
1346 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
1347 "----------\n",
1348 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1349 }
1350 }
1351
1352 // null analysis -- autoboxing
test0042_autoboxing_literal()1353 public void test0042_autoboxing_literal() {
1354 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
1355 runNegativeTest(
1356 new String[] {
1357 "X.java",
1358 "public class X {\n" +
1359 " void foo() {\n" +
1360 " Integer i = 0;\n" +
1361 " if (i == null) {};\n" + // complain: this is non null
1362 " }\n" +
1363 "}\n"},
1364 "----------\n" +
1365 "1. ERROR in X.java (at line 4)\n" +
1366 " if (i == null) {};\n" +
1367 " ^\n" +
1368 "Null comparison always yields false: The variable i cannot be null at this location\n" +
1369 "----------\n" +
1370 "2. WARNING in X.java (at line 4)\n" +
1371 " if (i == null) {};\n" +
1372 " ^^\n" +
1373 "Dead code\n" +
1374 "----------\n",
1375 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1376 }
1377 }
1378
1379 // null analysis -- autoboxing
test0043_autoboxing_literal()1380 public void test0043_autoboxing_literal() {
1381 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
1382 runNegativeTest(
1383 new String[] {
1384 "X.java",
1385 "public class X {\n" +
1386 " void foo() {\n" +
1387 " Integer i = null;\n" +
1388 " System.out.println(i + 4);\n" + // complain: this is null
1389 " }\n" +
1390 "}\n"},
1391 "----------\n" +
1392 "1. ERROR in X.java (at line 4)\n" +
1393 " System.out.println(i + 4);\n" +
1394 " ^\n" +
1395 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
1396 "----------\n",
1397 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1398 }
1399 }
1400
1401 // null analysis -- autoboxing
1402 // origin: AssignmentTest#test020
test0044_autoboxing()1403 public void test0044_autoboxing() {
1404 this.runConformTest(
1405 new String[] {
1406 "X.java",
1407 "public class X {\n" +
1408 " void foo() {\n" +
1409 " int i = 0;\n" +
1410 " boolean b = i < 10;\n" +
1411 " }\n" +
1412 "}\n"},
1413 "");
1414 }
1415
1416 // null analysis -- autoboxing
1417 // variant of 42 for
1418 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=165346
test0045_autoboxing_operator()1419 public void test0045_autoboxing_operator() {
1420 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
1421 runNegativeTest(
1422 new String[] {
1423 "X.java",
1424 "public class X {\n" +
1425 " void foo() {\n" +
1426 " int j = 5;" +
1427 " Integer i = 0 + j;\n" +
1428 " if (i == null) {}\n" +
1429 " }\n" +
1430 "}\n"},
1431 "----------\n" +
1432 "1. ERROR in X.java (at line 4)\n" +
1433 " if (i == null) {}\n" +
1434 " ^\n" +
1435 "Null comparison always yields false: The variable i cannot be null at this location\n" +
1436 "----------\n" +
1437 "2. WARNING in X.java (at line 4)\n" +
1438 " if (i == null) {}\n" +
1439 " ^^\n" +
1440 "Dead code\n" +
1441 "----------\n",
1442 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1443 }
1444 }
1445
1446 // null analysis -- array
test0050_array()1447 public void test0050_array() {
1448 this.runConformTest(
1449 new String[] {
1450 "X.java",
1451 "public class X {\n" +
1452 " public static void main(String args[]) {\n" +
1453 " args = new String[] {\"zero\"};\n" +
1454 " args[0] = null;\n" +
1455 " if (args[0] == null) {};\n" +
1456 // quiet: we don't keep track of all array elements
1457 " }\n" +
1458 "}\n"},
1459 "");
1460 }
1461
1462 // null analysis -- array
test0051_array()1463 public void test0051_array() {
1464 runNegativeTest(
1465 new String[] {
1466 "X.java",
1467 "public class X {\n" +
1468 " public static void main(String args[]) {\n" +
1469 " args = null;\n" +
1470 " args[0].toString();\n" + // complain: args is null
1471 " }\n" +
1472 "}\n"},
1473 "----------\n" +
1474 "1. ERROR in X.java (at line 4)\n" +
1475 " args[0].toString();\n" +
1476 " ^^^^\n" +
1477 "Null pointer access: The variable args can only be null at this location\n" +
1478 "----------\n",
1479 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1480 }
1481
1482 // null analysis -- array
test0052_array()1483 public void test0052_array() {
1484 this.runConformTest(
1485 new String[] {
1486 "X.java",
1487 "public class X {\n" +
1488 " public void foo(String args[]) {\n" +
1489 " String s = args[0];\n" +
1490 " if (s == null) {};\n" +
1491 // quiet: we don't keep track of all array elements
1492 " }\n" +
1493 "}\n"},
1494 "");
1495 }
1496
1497 // null analysis -- array
test0053_array()1498 public void test0053_array() {
1499 this.runConformTest(
1500 new String[] {
1501 "X.java",
1502 "public class X {\n" +
1503 " public void foo(String args[]) {\n" +
1504 " for (int i = 0; i < args.length; i++) { /* */}\n" +
1505 " }\n" +
1506 "}\n"},
1507 "");
1508 }
1509
1510 // null analysis -- method call
test0061_method_call_guard()1511 public void test0061_method_call_guard() {
1512 runNegativeTest(
1513 new String[] {
1514 "X.java",
1515 "public class X {\n" +
1516 " void foo(Object o) {\n" +
1517 " o.toString();\n" + // guards o from being null
1518 " if (o == null) {};\n" + // complain
1519 " }\n" +
1520 "}\n"},
1521 "----------\n" +
1522 "1. ERROR in X.java (at line 4)\n" +
1523 " if (o == null) {};\n" +
1524 " ^\n" +
1525 "Null comparison always yields false: The variable o cannot be null at this location\n" +
1526 "----------\n" +
1527 "2. WARNING in X.java (at line 4)\n" +
1528 " if (o == null) {};\n" +
1529 " ^^\n" +
1530 "Dead code\n" +
1531 "----------\n",
1532 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1533 }
1534
1535 // null analysis - method call
test0062_method_call_isolation()1536 public void test0062_method_call_isolation() {
1537 runNegativeTest(
1538 new String[] {
1539 "X.java",
1540 "public class X {\n" +
1541 " void foo(Object o) {\n" +
1542 " if (bar(o = null)) {\n" +
1543 " if (o == null) {/* empty */}\n" + // complain
1544 " }\n" +
1545 " }\n" +
1546 " boolean bar(Object o) {\n" +
1547 " return true;\n" +
1548 " }\n" +
1549 "}\n"},
1550 "----------\n" +
1551 "1. ERROR in X.java (at line 4)\n" +
1552 " if (o == null) {/* empty */}\n" +
1553 " ^\n" +
1554 "Redundant null check: The variable o can only be null at this location\n" +
1555 "----------\n",
1556 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1557 }
1558
1559 // null analysis - method call
test0063_method_call_isolation()1560 public void test0063_method_call_isolation() {
1561 this.runConformTest(
1562 new String[] {
1563 "X.java",
1564 "public class X {\n" +
1565 " void foo(Object o) {\n" +
1566 " if (bar(o == null ? new Object() : o)) {\n" +
1567 " if (o == null) {/* empty */}\n" + // quiet
1568 " }\n" +
1569 " }\n" +
1570 " boolean bar(Object o) {\n" +
1571 " return true;\n" +
1572 " }\n" +
1573 "}\n"},
1574 "");
1575 }
1576
1577 // null analysis - method call
test0064_method_call_isolation()1578 public void test0064_method_call_isolation() {
1579 runNegativeTest(
1580 new String[] {
1581 "X.java",
1582 "public class X {\n" +
1583 " void foo(Object o) {\n" +
1584 " if (bar(o = new Object())) {\n" +
1585 " if (o == null) {/* empty */}\n" + // complain
1586 " }\n" +
1587 " }\n" +
1588 " boolean bar(Object o) {\n" +
1589 " return true;\n" +
1590 " }\n" +
1591 "}\n"},
1592 "----------\n" +
1593 "1. ERROR in X.java (at line 4)\n" +
1594 " if (o == null) {/* empty */}\n" +
1595 " ^\n" +
1596 "Null comparison always yields false: The variable o cannot be null at this location\n" +
1597 "----------\n" +
1598 "2. WARNING in X.java (at line 4)\n" +
1599 " if (o == null) {/* empty */}\n" +
1600 " ^^^^^^^^^^^^^\n" +
1601 "Dead code\n" +
1602 "----------\n",
1603 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1604 }
1605
1606 // null analysis - method call
test0065_method_call_invocation_target()1607 public void test0065_method_call_invocation_target() {
1608 this.runConformTest(
1609 new String[] {
1610 "X.java",
1611 "public class X {\n" +
1612 " void foo() {\n" +
1613 " Object o = null;\n" +
1614 " (o = new Object()).toString();\n" + // quiet
1615 " }\n" +
1616 "}\n"},
1617 "");
1618 }
1619
1620 // null analysis - method call
test0066_method_call_invocation_target()1621 public void test0066_method_call_invocation_target() {
1622 runNegativeTest(
1623 new String[] {
1624 "X.java",
1625 "public class X {\n" +
1626 " void foo() {\n" +
1627 " Object o = new Object();\n" +
1628 " (o = null).toString();\n" + // complain
1629 " }\n" +
1630 "}\n"},
1631 "----------\n" +
1632 "1. ERROR in X.java (at line 4)\n" +
1633 " (o = null).toString();\n" +
1634 " ^^^^^^^^^^\n" +
1635 "Null pointer access: The variable o can only be null at this location\n" +
1636 "----------\n",
1637 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1638 }
1639
1640 // null analysis - method call
test0067_method_call_invocation_target()1641 public void test0067_method_call_invocation_target() {
1642 runNegativeTest(
1643 new String[] {
1644 "X.java",
1645 "public class X {\n" +
1646 " void foo(Object o) {\n" +
1647 " (o = new Object()).toString();\n" + // quiet
1648 " if (o == null) { /* */ }\n" + // complain
1649 " }\n" +
1650 "}\n"},
1651 "----------\n" +
1652 "1. ERROR in X.java (at line 4)\n" +
1653 " if (o == null) { /* */ }\n" +
1654 " ^\n" +
1655 "Null comparison always yields false: The variable o cannot be null at this location\n" +
1656 "----------\n" +
1657 "2. WARNING in X.java (at line 4)\n" +
1658 " if (o == null) { /* */ }\n" +
1659 " ^^^^^^^^^\n" +
1660 "Dead code\n" +
1661 "----------\n",
1662 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1663 }
1664
1665 // null analysis - method call
test0068_method_call_assignment()1666 public void test0068_method_call_assignment() {
1667 this.runConformTest(
1668 new String[] {
1669 "X.java",
1670 "public class X {\n" +
1671 " X bar() {\n" +
1672 " return null;\n" +
1673 " }\n" +
1674 " void foo(X x) {\n" +
1675 " x = x.bar();\n" +
1676 " if (x == null) { /* */ }\n" + // quiet
1677 " }\n" +
1678 "}\n"},
1679 "");
1680 }
1681
1682 // null analysis -- type reference
test0070_type_reference()1683 public void test0070_type_reference() {
1684 runNegativeTest(
1685 new String[] {
1686 "X.java",
1687 "public class X {\n" +
1688 " public static void main(String args[]) {\n" +
1689 " Class c = java.lang.Object.class;\n" +
1690 " if (c == null) {};\n" +
1691 " }\n" +
1692 "}\n"},
1693 "----------\n" +
1694 "1. ERROR in X.java (at line 4)\n" +
1695 " if (c == null) {};\n" +
1696 " ^\n" +
1697 "Null comparison always yields false: The variable c cannot be null at this location\n" +
1698 "----------\n" +
1699 "2. WARNING in X.java (at line 4)\n" +
1700 " if (c == null) {};\n" +
1701 " ^^\n" +
1702 "Dead code\n" +
1703 "----------\n",
1704 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1705 }
1706
test0080_shortcut_boolean_expressions()1707 public void test0080_shortcut_boolean_expressions() {
1708 runNegativeTest(
1709 new String[] {
1710 "X.java",
1711 "public class X {\n" +
1712 " void foo(Object o1, Object o2) {\n" +
1713 " if (o1 != null && (o2 = o1) != null) { /* */ }\n" +
1714 " }\n" +
1715 "}\n"},
1716 "----------\n" +
1717 "1. ERROR in X.java (at line 3)\n" +
1718 " if (o1 != null && (o2 = o1) != null) { /* */ }\n" +
1719 " ^^^^^^^^^\n" +
1720 "Redundant null check: The variable o2 cannot be null at this location\n" +
1721 "----------\n",
1722 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1723 }
1724
test0081_shortcut_boolean_expressions()1725 public void test0081_shortcut_boolean_expressions() {
1726 runNegativeTest(
1727 new String[] {
1728 "X.java",
1729 "public class X {\n" +
1730 " void foo(Object o1, Object o2) {\n" +
1731 " while (o1 != null && (o2 = o1) != null) { /* */ }\n" +
1732 " }\n" +
1733 "}\n"},
1734 "----------\n" +
1735 "1. ERROR in X.java (at line 3)\n" +
1736 " while (o1 != null && (o2 = o1) != null) { /* */ }\n" +
1737 " ^^^^^^^^^\n" +
1738 "Redundant null check: The variable o2 cannot be null at this location\n" +
1739 "----------\n",
1740 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1741 }
1742
1743 // null analysis - shortcut boolean expression
test0082_shortcut_boolean_expression()1744 public void test0082_shortcut_boolean_expression() {
1745 runNegativeTest(
1746 new String[] {
1747 "X.java",
1748 "public class X {\n" +
1749 " void foo(Object o) {\n" +
1750 " if (o == null || o == null) {\n" +
1751 " o = new Object();\n" +
1752 " }\n" +
1753 " if (o == null) { /* */ }\n" +
1754 " }\n" +
1755 "}"},
1756 "----------\n" +
1757 "1. ERROR in X.java (at line 3)\n" +
1758 " if (o == null || o == null) {\n" +
1759 " ^\n" +
1760 "Null comparison always yields false: The variable o cannot be null at this location\n" +
1761 "----------\n" +
1762 "2. ERROR in X.java (at line 6)\n" +
1763 " if (o == null) { /* */ }\n" +
1764 " ^\n" +
1765 "Null comparison always yields false: The variable o cannot be null at this location\n" +
1766 "----------\n" +
1767 "3. WARNING in X.java (at line 6)\n" +
1768 " if (o == null) { /* */ }\n" +
1769 " ^^^^^^^^^\n" +
1770 "Dead code\n" +
1771 "----------\n",
1772 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1773 }
1774
1775 // null analysis - shortcut boolean expression
test0083_shortcut_boolean_expression()1776 public void test0083_shortcut_boolean_expression() {
1777 runNegativeTest(
1778 new String[] {
1779 "X.java",
1780 "public class X {\n" +
1781 " void foo(Object o) {\n" +
1782 " if (o == null && o == null) {\n" +
1783 " o = new Object();\n" +
1784 " }\n" +
1785 " if (o == null) { /* */ }\n" +
1786 " }\n" +
1787 "}"},
1788 "----------\n" +
1789 "1. ERROR in X.java (at line 3)\n" +
1790 " if (o == null && o == null) {\n" +
1791 " ^\n" +
1792 "Redundant null check: The variable o can only be null at this location\n" +
1793 "----------\n" +
1794 "2. ERROR in X.java (at line 6)\n" +
1795 " if (o == null) { /* */ }\n" +
1796 " ^\n" +
1797 "Null comparison always yields false: The variable o cannot be null at this location\n" +
1798 "----------\n" +
1799 "3. WARNING in X.java (at line 6)\n" +
1800 " if (o == null) { /* */ }\n" +
1801 " ^^^^^^^^^\n" +
1802 "Dead code\n" +
1803 "----------\n",
1804 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1805 }
1806
1807 // null analysis - shortcut boolean expression
1808 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=130311
test0084_shortcut_boolean_expression()1809 public void test0084_shortcut_boolean_expression() {
1810 runNegativeTest(
1811 new String[] {
1812 "X.java",
1813 "public class X {\n" +
1814 " boolean foo(Integer i1, Integer i2) {\n" +
1815 " return (i1 == null && i2 == null)\n" +
1816 " || (i1.byteValue() == i2.byteValue());\n" +
1817 " }\n" +
1818 "}"},
1819 "----------\n" +
1820 "1. ERROR in X.java (at line 4)\n" +
1821 " || (i1.byteValue() == i2.byteValue());\n" +
1822 " ^^\n" +
1823 "Potential null pointer access: The variable i1 may be null at this location\n" +
1824 "----------\n",
1825 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1826 }
1827
1828 // null analysis - shortcut boolean expression
1829 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=130311
test0085_shortcut_boolean_expression()1830 public void test0085_shortcut_boolean_expression() {
1831 runNegativeTest(
1832 new String[] {
1833 "X.java",
1834 "public class X {\n" +
1835 " boolean foo(Integer i1, Integer i2) {\n" +
1836 " return (i1 == null & i2 == null)\n" +
1837 " || (i1.byteValue() == i2.byteValue());\n" +
1838 " }\n" +
1839 "}"},
1840 "----------\n" +
1841 "1. ERROR in X.java (at line 4)\n" +
1842 " || (i1.byteValue() == i2.byteValue());\n" +
1843 " ^^\n" +
1844 "Potential null pointer access: The variable i1 may be null at this location\n" +
1845 "----------\n" +
1846 "2. ERROR in X.java (at line 4)\n" +
1847 " || (i1.byteValue() == i2.byteValue());\n" +
1848 " ^^\n" +
1849 "Potential null pointer access: The variable i2 may be null at this location\n" +
1850 "----------\n",
1851 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1852 }
1853
1854 // null analysis - shortcut boolean expression and correlation
1855 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=195774
test0086_shortcut_boolean_expression()1856 public void test0086_shortcut_boolean_expression() {
1857 this.runConformTest(
1858 new String[] {
1859 "X.java",
1860 "public class X {\n" +
1861 " public static int foo(Integer i, Integer j) {\n" +
1862 " if (i == null && j == null) {\n" +
1863 " return 1;\n" +
1864 " }\n" +
1865 " if (i == null) {\n" +
1866 " return j.intValue();\n" +
1867 " }\n" +
1868 " if (j == null) {\n" +
1869 " return i.intValue();\n" +
1870 " }\n" +
1871 " return 0;\n" +
1872 " }\n" +
1873 "}"},
1874 "");
1875 }
1876
1877 // null analysis - shortcut boolean expression and correlation
1878 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=195774
_test0087_shortcut_boolean_expression()1879 public void _test0087_shortcut_boolean_expression() {
1880 this.runConformTest(
1881 new String[] {
1882 "X.java",
1883 "public class X {\n" +
1884 " public static int foo(Integer i, Integer j) {\n" +
1885 " if (i == null && j == null) {\n" +
1886 " return 1;\n" +
1887 " }\n" +
1888 " if (j == null) {\n" +
1889 " return i.intValue();\n" +
1890 " }\n" +
1891 " if (i == null) {\n" +
1892 " return j.intValue();\n" +
1893 " }\n" +
1894 " return 0;\n" +
1895 " }\n" +
1896 "}"},
1897 "");
1898 }
1899
1900 // null analysis -- instanceof
1901 // JLS: instanceof returns false if o turns out to be null
test0090_instanceof()1902 public void test0090_instanceof() {
1903 this.runConformTest(
1904 new String[] {
1905 "X.java",
1906 "class X {\n" +
1907 " boolean dummy;\n" +
1908 " void foo (Object o) {\n" +
1909 " if (dummy) {\n" +
1910 " o = null;\n" +
1911 " }\n" +
1912 " if (o instanceof X) { /* */ }\n" +
1913 " }\n" +
1914 "}"},
1915 "");
1916 }
1917
1918 // null analysis -- instanceof
test0091_instanceof()1919 public void test0091_instanceof() {
1920 this.runConformTest(
1921 new String[] {
1922 "X.java",
1923 "class X {\n" +
1924 " boolean dummy;\n" +
1925 " void foo (Object o) {\n" +
1926 " if (dummy) {\n" +
1927 " o = null;\n" +
1928 " }\n" +
1929 " if (o instanceof X) { /* */ }\n" +
1930 " if (o == null) { /* */ }\n" +
1931 " }\n" +
1932 "}"},
1933 "");
1934 }
1935
1936 // null analysis -- instanceof
1937 // can only be null always yields false
test0092_instanceof()1938 public void test0092_instanceof() {
1939 runNegativeTest(
1940 new String[] {
1941 "X.java",
1942 "class X {\n" +
1943 " boolean dummy;\n" +
1944 " void foo () {\n" +
1945 " Object o = null;\n" +
1946 " if (o instanceof X) { /* */ }\n" +
1947 " }\n" +
1948 "}"},
1949 "----------\n" +
1950 "1. ERROR in X.java (at line 5)\n" +
1951 " if (o instanceof X) { /* */ }\n" +
1952 " ^\n" +
1953 "instanceof always yields false: The variable o can only be null at this location\n" +
1954 "----------\n",
1955 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1956 }
1957
1958 // null analysis -- instanceof
test0093_instanceof()1959 public void test0093_instanceof() {
1960 runNegativeTest(
1961 new String[] {
1962 "X.java",
1963 "class X {\n" +
1964 " void foo(Object x) {\n" +
1965 " if (x instanceof X) {\n" +
1966 " if (x == null) { /* */ }\n" + // cannot happen
1967 " }\n" +
1968 " }\n" +
1969 "}"},
1970 "----------\n" +
1971 "1. ERROR in X.java (at line 4)\n" +
1972 " if (x == null) { /* */ }\n" +
1973 " ^\n" +
1974 "Null comparison always yields false: The variable x cannot be null at this location\n" +
1975 "----------\n" +
1976 "2. WARNING in X.java (at line 4)\n" +
1977 " if (x == null) { /* */ }\n" +
1978 " ^^^^^^^^^\n" +
1979 "Dead code\n" +
1980 "----------\n",
1981 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
1982 }
1983
1984 // null analysis -- instanceof
test0094_instanceof()1985 public void test0094_instanceof() {
1986 this.runConformTest(
1987 new String[] {
1988 "X.java",
1989 "class X {\n" +
1990 " void foo(Object x) {\n" +
1991 " if (x instanceof X) {\n" +
1992 " return;\n" +
1993 " }\n" +
1994 " if (x != null) { /* */ }\n" +
1995 // cannot decide: could be null of new Object() for example
1996 " }\n" +
1997 "}"},
1998 "");
1999 }
2000
2001 // null analysis -- instanceof combined with conditional or
2002 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=145202
test0095_instanceof_conditional_or()2003 public void test0095_instanceof_conditional_or() {
2004 runNegativeTest(
2005 new String[] {
2006 "X.java",
2007 "class X {\n" +
2008 " void foo(Object x) {\n" +
2009 " if (! (x instanceof String)\n" +
2010 " || x == null) {\n" +
2011 " return;\n" +
2012 " }\n" +
2013 " }\n" +
2014 "}"},
2015 "----------\n" +
2016 "1. ERROR in X.java (at line 4)\n" +
2017 " || x == null) {\n" +
2018 " ^\n" +
2019 "Null comparison always yields false: The variable x cannot be null at this location\n" +
2020 "----------\n",
2021 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2022 }
2023
2024 // null analysis -- strings concatenation
2025 // JLS 15.18.1: if one of the operands is null, it is replaced by "null"
2026 // Note: having the diagnostic could come handy when the initialization path
2027 // is non trivial; to get the diagnostic, simply put in place an
2028 // extraneous call to toString() -- and remove it before releasing.
test0120_strings_concatenation()2029 public void test0120_strings_concatenation() {
2030 this.runConformTest(
2031 new String[] {
2032 "X.java",
2033 "public class X {\n" +
2034 " String foo(String s1, String s2) {\n" +
2035 " if (s1 == null) { /* */ };\n" +
2036 " return s1 + s2;\n" +
2037 " }\n" +
2038 "}\n"},
2039 "");
2040 }
2041
2042 // null analysis -- strings concatenation
test0121_strings_concatenation()2043 public void test0121_strings_concatenation() {
2044 this.runConformTest(
2045 new String[] {
2046 "X.java",
2047 "public class X {\n" +
2048 " String foo(String s1, String s2) {\n" +
2049 " if (s1 == null) { /* */ };\n" +
2050 " s1 += s2;\n" +
2051 " return s1;\n" +
2052 " }\n" +
2053 "}\n"},
2054 "");
2055 }
2056
2057 // null analysis -- strings concatenation
test0122_strings_concatenation()2058 public void test0122_strings_concatenation() {
2059 this.runNegativeTest(
2060 new String[] {
2061 "X.java",
2062 "public class X {\n" +
2063 " String foo(String s1) {\n" +
2064 " if (s1 == null) { /* */ };\n" +
2065 " return s1.toString();\n" +
2066 " }\n" +
2067 "}\n"},
2068 "----------\n" +
2069 "1. ERROR in X.java (at line 4)\n" +
2070 " return s1.toString();\n" +
2071 " ^^\n" +
2072 "Potential null pointer access: The variable s1 may be null at this location\n" +
2073 "----------\n",
2074 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2075 }
2076
2077 // null analysis -- strings concatenation
2078 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127919
2079 // it should suffice that the return type is String to avoid
2080 // errors
test0123_strings_concatenation()2081 public void test0123_strings_concatenation() {
2082 this.runConformTest(
2083 new String[] {
2084 "X.java",
2085 "public class X {\n" +
2086 " String foo(String s, Object o, Integer i) {\n" +
2087 " if (s == null || o == null || i == null) { /* */ };\n" +
2088 " if (bar()) {\n" +
2089 " return s + i;\n" + // quiet: i replaced by "null" if null
2090 " }\n" +
2091 " return o + s;\n" + // quiet: o replaced by "null" if null
2092 " }\n" +
2093 " boolean bar() {\n" +
2094 " return false;\n" +
2095 " }\n" +
2096 "}\n"},
2097 "");
2098 }
2099
2100 // null analysis -- strings concatenation
2101 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127919
2102 // variant
test0124_strings_concatenation()2103 public void test0124_strings_concatenation() {
2104 this.runConformTest(
2105 new String[] {
2106 "X.java",
2107 "public class X {\n" +
2108 " String foo(String s, Object o, Integer i) {\n" +
2109 " if (s == null || o == null || i == null) { /* */ };\n" +
2110 " s += o;\n" + // quiet: o replaced by "null" if null
2111 " s += i;\n" + // quiet: i replaced by "null" if null
2112 " return s;\n" +
2113 " }\n" +
2114 "}\n"},
2115 "");
2116 }
2117
2118 // null analysis -- strings concatenation
2119 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127919
2120 // variant
test0125_strings_concatenation()2121 public void test0125_strings_concatenation() {
2122 this.runConformTest(
2123 new String[] {
2124 "X.java",
2125 "public class X {\n" +
2126 " void foo(Object o, Integer i) {\n" +
2127 " System.out.println(o + (o == null ? \"\" : o.toString()));\n" + // quiet: o replaced by "null" if null
2128 " System.out.println(i + (i == null ? \"\" : i.toString()));\n" + // quiet: o replaced by "null" if null
2129 " }\n" +
2130 "}\n"},
2131 "");
2132 }
2133
2134 // null analysis -- strings concatenation
2135 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=132867
test0126_strings_concatenation()2136 public void test0126_strings_concatenation() {
2137 this.runConformTest(
2138 new String[] {
2139 "X.java",
2140 "public class X {\n" +
2141 " void foo(Object o) {\n" +
2142 " System.out.println(o + \"\");\n" +
2143 " if (o != null) { /* */ };\n" +
2144 " }\n" +
2145 "}\n"},
2146 "");
2147 }
2148
2149 // null analysis -- strings concatenation
2150 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=132867
test0127_strings_concatenation()2151 public void test0127_strings_concatenation() {
2152 this.runConformTest(
2153 new String[] {
2154 "X.java",
2155 "public class X {\n" +
2156 " void foo() {\n" +
2157 " Object o = null;\n" +
2158 " System.out.println(o + \"\");\n" +
2159 " }\n" +
2160 "}\n"},
2161 "");
2162 }
2163
2164 // null analysis -- if/else
2165 // check that obviously unreachable code does not modify the null
2166 // status of a local
2167 // the said code is not marked as unreachable per JLS 14.21 (the rationale
2168 // being the accommodation for the if (constant_flag_evaluating_to_false)
2169 // {code...} volontary code exclusion pattern)
test0300_if_else()2170 public void test0300_if_else() {
2171 this.runNegativeTest(
2172 new String[] {
2173 "X.java",
2174 "public class X {\n" +
2175 " public void foo() {\n" +
2176 " Object o = null;\n" +
2177 " if (false) {\n" +
2178 " o = new Object();\n" + // skipped
2179 " }\n" +
2180 " if (true) {\n" +
2181 " //\n" +
2182 " }\n" +
2183 " else {\n" +
2184 " o = new Object();\n" + // skipped
2185 " }\n" +
2186 " o.toString();\n" +
2187 " }\n" +
2188 "}\n"},
2189 "----------\n" +
2190 "1. WARNING in X.java (at line 4)\n" +
2191 " if (false) {\n" +
2192 " o = new Object();\n" +
2193 " }\n" +
2194 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
2195 "Dead code\n" +
2196 "----------\n" +
2197 "2. WARNING in X.java (at line 10)\n" +
2198 " else {\n" +
2199 " o = new Object();\n" +
2200 " }\n" +
2201 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
2202 "Dead code\n" +
2203 "----------\n" +
2204 "3. ERROR in X.java (at line 13)\n" +
2205 " o.toString();\n" +
2206 " ^\n" +
2207 "Null pointer access: The variable o can only be null at this location\n" +
2208 "----------\n",
2209 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2210 }
2211
2212 // null analysis - if/else
test0301_if_else()2213 public void test0301_if_else() {
2214 this.runNegativeTest(
2215 new String[] {
2216 "X.java",
2217 "public class X {\n" +
2218 " void foo() {\n" +
2219 " Object o = new Object();\n" +
2220 " if (o != null) {\n" +
2221 " }\n" +
2222 " }\n" +
2223 "}\n"},
2224 "----------\n" +
2225 "1. ERROR in X.java (at line 4)\n" +
2226 " if (o != null) {\n" +
2227 " ^\n" +
2228 "Redundant null check: The variable o cannot be null at this location\n" +
2229 "----------\n",
2230 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2231 }
2232
2233 // null analysis - if/else
test0302_if_else()2234 public void test0302_if_else() {
2235 this.runNegativeTest(
2236 new String[] {
2237 "X.java",
2238 "public class X {\n" +
2239 " void foo(Object o) throws Exception {\n" +
2240 " if (o == null) {\n" +
2241 " throw new Exception();\n" +
2242 " }\n" +
2243 " if (o != null) {\n" + // only get there if o non null
2244 " }\n" +
2245 " }\n" +
2246 "}\n"},
2247 "----------\n" +
2248 "1. ERROR in X.java (at line 6)\n" +
2249 " if (o != null) {\n" +
2250 " ^\n" +
2251 "Redundant null check: The variable o cannot be null at this location\n" +
2252 "----------\n",
2253 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2254 }
2255
2256 // null analysis - if/else
test0303_if_else()2257 public void test0303_if_else() {
2258 this.runNegativeTest(
2259 new String[] {
2260 "X.java",
2261 "public class X {\n" +
2262 " void foo(Object o) {\n" +
2263 " if (o == null) {\n" +
2264 " return;\n" +
2265 " }\n" +
2266 " if (o != null) {\n" +
2267 " }\n" +
2268 " }\n" +
2269 "}\n"},
2270 "----------\n" +
2271 "1. ERROR in X.java (at line 6)\n" +
2272 " if (o != null) {\n" +
2273 " ^\n" +
2274 "Redundant null check: The variable o cannot be null at this location\n" +
2275 "----------\n",
2276 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2277 }
2278
2279 // null analysis - if/else
test0304_if_else()2280 public void test0304_if_else() {
2281 this.runNegativeTest(
2282 new String[] {
2283 "X.java",
2284 "public class X {\n" +
2285 " void foo(Object o) {\n" +
2286 " if (o == null) {\n" +
2287 " o.toString();\n" +
2288 " }\n" +
2289 " }\n" +
2290 "}\n"},
2291 "----------\n" +
2292 "1. ERROR in X.java (at line 4)\n" +
2293 " o.toString();\n" +
2294 " ^\n" +
2295 "Null pointer access: The variable o can only be null at this location\n" +
2296 "----------\n",
2297 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2298 }
2299
2300 // null analysis - if/else
test0305_if_else()2301 public void test0305_if_else() {
2302 this.runNegativeTest(
2303 new String[] {
2304 "X.java",
2305 "public class X {\n" +
2306 " void foo(Object o) {\n" +
2307 " if (o == null) {\n" +
2308 " // do nothing\n" +
2309 " }\n" +
2310 " o.toString();\n" +
2311 " }\n" +
2312 "}\n"},
2313 "----------\n" +
2314 "1. ERROR in X.java (at line 6)\n" +
2315 " o.toString();\n" +
2316 " ^\n" +
2317 "Potential null pointer access: The variable o may be null at this location\n" +
2318 "----------\n",
2319 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2320 }
2321
2322 // null analysis - if/else
test0306_if_else()2323 public void test0306_if_else() {
2324 this.runNegativeTest(
2325 new String[] {
2326 "X.java",
2327 "public class X {\n" +
2328 " void foo(Object o) {\n" +
2329 " if (o.toString().equals(\"\")) {\n" +
2330 " if (o == null) {\n" + // complain: could not get here
2331 " // do nothing\n" +
2332 " }\n" +
2333 " }\n" +
2334 " }\n" +
2335 "}\n"},
2336 "----------\n" +
2337 "1. ERROR in X.java (at line 4)\n" +
2338 " if (o == null) {\n" +
2339 " ^\n" +
2340 "Null comparison always yields false: The variable o cannot be null at this location\n" +
2341 "----------\n" +
2342 "2. WARNING in X.java (at line 4)\n" +
2343 " if (o == null) {\n" +
2344 " // do nothing\n" +
2345 " }\n" +
2346 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
2347 "Dead code\n" +
2348 "----------\n",
2349 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2350 }
2351
2352 // null analysis - if/else
test0307_if_else()2353 public void test0307_if_else() {
2354 this.runConformTest(
2355 new String[] {
2356 "X.java",
2357 "public class X {\n" +
2358 " void foo(Object o) {\n" +
2359 " if (o == null) {\n" +
2360 " System.exit(0);\n" +
2361 " }\n" +
2362 " if (o == null) {\n" +
2363 // quiet
2364 // a direct call to System.exit() can be recognized as such; yet,
2365 // a lot of other methods may have the same property (aka calling
2366 // System.exit() themselves.)
2367 " // do nothing\n" +
2368 " }\n" +
2369 " }\n" +
2370 "}\n"},
2371 "");
2372 }
2373
2374 // null analysis - if/else
test0308_if_else()2375 public void test0308_if_else() {
2376 this.runNegativeTest(
2377 new String[] {
2378 "X.java",
2379 "public class X {\n" +
2380 " boolean b;\n" +
2381 " void foo(Object o) {\n" +
2382 " if (b) {\n" +
2383 " o = null;\n" +
2384 " }\n" +
2385 " o.toString();\n" + // complain
2386 " }\n" +
2387 "}\n"},
2388 "----------\n" +
2389 "1. ERROR in X.java (at line 7)\n" +
2390 " o.toString();\n" +
2391 " ^\n" +
2392 "Potential null pointer access: The variable o may be null at this location\n" +
2393 "----------\n",
2394 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2395 }
2396
2397 // null analysis - if/else
test0309_if_else()2398 public void test0309_if_else() {
2399 this.runNegativeTest(
2400 new String[] {
2401 "X.java",
2402 "public class X {\n" +
2403 " boolean b1, b2;\n" +
2404 " void foo(Object o) {\n" +
2405 " if (b1) {\n" +
2406 " o = null;\n" +
2407 " }\n" +
2408 " if (b2) {\n" +
2409 " o = new Object();\n" +
2410 " }\n" +
2411 " o.toString();\n" + // complain
2412 " }\n" +
2413 "}\n"},
2414 "----------\n" +
2415 "1. ERROR in X.java (at line 10)\n" +
2416 " o.toString();\n" +
2417 " ^\n" +
2418 "Potential null pointer access: The variable o may be null at this location\n" +
2419 "----------\n",
2420 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2421 }
2422
2423 // null analysis - if/else
test0310_if_else()2424 public void test0310_if_else() {
2425 this.runNegativeTest(
2426 new String[] {
2427 "X.java",
2428 "public class X {\n" +
2429 " boolean b1, b2;\n" +
2430 " void foo(Object o) {\n" +
2431 " if (b1) {\n" +
2432 " o = null;\n" +
2433 " }\n" +
2434 " if (b2) {\n" +
2435 " o.toString();\n" + // complain
2436 " o.toString();\n" + // silent
2437 " }\n" +
2438 " o.toString();\n" + // complain
2439 " }\n" +
2440 "}\n"},
2441 "----------\n" +
2442 "1. ERROR in X.java (at line 8)\n" +
2443 " o.toString();\n" +
2444 " ^\n" +
2445 "Potential null pointer access: The variable o may be null at this location\n" +
2446 "----------\n" +
2447 "2. ERROR in X.java (at line 11)\n" +
2448 " o.toString();\n" +
2449 " ^\n" +
2450 "Potential null pointer access: The variable o may be null at this location\n" +
2451 "----------\n",
2452 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2453 }
2454
2455 // null analysis - if/else
test0311_if_else()2456 public void test0311_if_else() {
2457 this.runConformTest(
2458 new String[] {
2459 "X.java",
2460 "public class X {\n" +
2461 " void foo(Object o) {\n" +
2462 " if (o == null)\n" +
2463 " o = new Object();\n" +
2464 " o.toString();\n" + // quiet
2465 " }\n" +
2466 "}" },
2467 "");
2468 }
2469
2470 // null analysis - if/else
test0312_if_else()2471 public void test0312_if_else() {
2472 this.runNegativeTest(
2473 new String[] {
2474 "X.java",
2475 "public class X {\n" +
2476 "\n" +
2477 " void foo() {\n" +
2478 " Object o = new Object();\n" +
2479 " if (o == null) { /* */ }\n" + // complain
2480 " if (o != null) { /* */ }\n" +
2481 " o.toString();\n" +
2482 " }\n" +
2483 "}\n"},
2484 "----------\n" +
2485 "1. ERROR in X.java (at line 5)\n" +
2486 " if (o == null) { /* */ }\n" +
2487 " ^\n" +
2488 "Null comparison always yields false: The variable o cannot be null at this location\n" +
2489 "----------\n" +
2490 "2. WARNING in X.java (at line 5)\n" +
2491 " if (o == null) { /* */ }\n" +
2492 " ^^^^^^^^^\n" +
2493 "Dead code\n" +
2494 "----------\n" +
2495 "3. ERROR in X.java (at line 6)\n" +
2496 " if (o != null) { /* */ }\n" +
2497 " ^\n" +
2498 "Redundant null check: The variable o cannot be null at this location\n" +
2499 "----------\n",
2500 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2501 }
2502
2503 // null analysis - if/else
test0313_if_else()2504 public void test0313_if_else() {
2505 this.runNegativeTest(
2506 new String[] {
2507 "X.java",
2508 "public class X {\n" +
2509 " void foo(Object o) {\n" +
2510 " if (o == null) {\n" + // quiet
2511 " o = new Object();\n" +
2512 " }\n" +
2513 " if (o == null) { /* */ }\n" +
2514 // complain: o set to non null iff it was null
2515 " }\n" +
2516 "}"},
2517 "----------\n" +
2518 "1. ERROR in X.java (at line 6)\n" +
2519 " if (o == null) { /* */ }\n" +
2520 " ^\n" +
2521 "Null comparison always yields false: The variable o cannot be null at this location\n" +
2522 "----------\n" +
2523 "2. WARNING in X.java (at line 6)\n" +
2524 " if (o == null) { /* */ }\n" +
2525 " ^^^^^^^^^\n" +
2526 "Dead code\n" +
2527 "----------\n",
2528 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2529 }
2530
2531 // null analysis - if/else
test0314_if_else()2532 public void test0314_if_else() {
2533 this.runNegativeTest(
2534 new String[] {
2535 "X.java",
2536 "public class X {\n" +
2537 " void foo(Object o) {\n" +
2538 " if (o != null) {\n" + // quiet
2539 " o = null;\n" +
2540 " }\n" +
2541 " if (o == null) { /* */ }\n" + // complain
2542 " }\n" +
2543 "}"},
2544 "----------\n" +
2545 "1. ERROR in X.java (at line 6)\n" +
2546 " if (o == null) { /* */ }\n" +
2547 " ^\n" +
2548 "Redundant null check: The variable o can only be null at this location\n" +
2549 "----------\n",
2550 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2551 }
2552
2553 // null analysis - if/else
test0315_if_else()2554 public void test0315_if_else() {
2555 this.runNegativeTest(
2556 new String[] {
2557 "X.java",
2558 "public class X {\n" +
2559 " void foo(Object o) {\n" +
2560 " if (o != null) {\n" + // quiet
2561 " o = null;\n" +
2562 " }\n" +
2563 " o.toString();\n" + // complain
2564 " }\n" +
2565 "}"},
2566 "----------\n" +
2567 "1. ERROR in X.java (at line 6)\n" +
2568 " o.toString();\n" +
2569 " ^\n" +
2570 "Null pointer access: The variable o can only be null at this location\n" +
2571 "----------\n",
2572 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2573 }
2574
2575 // null analysis - if/else
test0316_if_else()2576 public void test0316_if_else() {
2577 this.runNegativeTest(
2578 new String[] {
2579 "X.java",
2580 "public class X {\n" +
2581 " void foo(Object o, boolean b) {\n" +
2582 " if (o == null || b) { /* */ }\n" + // quiet
2583 " else { /* */ }\n" +
2584 " o.toString();\n" + // complain
2585 " }\n" +
2586 "}"},
2587 "----------\n" +
2588 "1. ERROR in X.java (at line 5)\n" +
2589 " o.toString();\n" +
2590 " ^\n" +
2591 "Potential null pointer access: The variable o may be null at this location\n" +
2592 "----------\n",
2593 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2594 }
2595
2596 // null analysis - if/else
test0317_if_else_nested()2597 public void test0317_if_else_nested() {
2598 this.runConformTest(
2599 new String[] {
2600 "X.java",
2601 "public class X {\n" +
2602 " void foo(Object o, boolean b) {\n" +
2603 " if (o != null) {\n" + // quiet
2604 " if (b) {\n" + // quiet
2605 " o = null;\n" +
2606 " }\n" +
2607 " }\n" +
2608 " if (o == null) { /* */ }\n" + // quiet
2609 " }\n" +
2610 "}"},
2611 "");
2612 }
2613
2614 // null analysis - if/else
test0318_if_else_nested()2615 public void test0318_if_else_nested() {
2616 this.runConformTest(
2617 new String[] {
2618 "X.java",
2619 "public class X {\n" +
2620 " void foo(Object o, boolean b) {\n" +
2621 " if (o != null) {\n" + // quiet
2622 " if (b) {\n" + // quiet
2623 " o = null;\n" +
2624 " }\n" +
2625 " if (o == null) { /* */ }\n" + // quiet
2626 " }\n" +
2627 " }\n" +
2628 "}"},
2629 "");
2630 }
2631
2632 // null analysis - if/else
2633 // we do nothing to diagnose the contents of fake reachable code
test0319_if_else_dead_branch()2634 public void test0319_if_else_dead_branch() {
2635 this.runConformTest(
2636 new String[] {
2637 "X.java",
2638 "public class X {\n" +
2639 " void foo(Object o, boolean b) {\n" +
2640 " if (false) {\n" +
2641 " o = null;\n" +
2642 " if (o == null) { /* */ }\n" + // may have considered complaining here
2643 " }\n" +
2644 " }\n" +
2645 "}"},
2646 "");
2647 }
2648
2649 // null analysis - if/else
test0320_if_else()2650 public void test0320_if_else() {
2651 this.runNegativeTest(
2652 new String[] {
2653 "X.java",
2654 "public class X {\n" +
2655 " void foo(Object o) {\n" +
2656 " o.toString();\n" +
2657 " if (o == null) { /* */ }\n" + // complain
2658 " }\n" +
2659 "}"},
2660 "----------\n" +
2661 "1. ERROR in X.java (at line 4)\n" +
2662 " if (o == null) { /* */ }\n" +
2663 " ^\n" +
2664 "Null comparison always yields false: The variable o cannot be null at this location\n" +
2665 "----------\n" +
2666 "2. WARNING in X.java (at line 4)\n" +
2667 " if (o == null) { /* */ }\n" +
2668 " ^^^^^^^^^\n" +
2669 "Dead code\n" +
2670 "----------\n",
2671 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2672 }
2673
2674 // null analysis - if/else
test0321_if_else()2675 public void test0321_if_else() {
2676 this.runConformTest(
2677 new String[] {
2678 "X.java",
2679 "public class X {\n" +
2680 " void foo(Object o, boolean b) {\n" +
2681 " Object other = new Object();\n" +
2682 " if (b) {\n" +
2683 " other = o;\n" +
2684 " }\n" +
2685 " if (o != null) { /* */ }\n" + // quiet
2686 " }\n" +
2687 "}"},
2688 "");
2689 }
2690
2691 // null analysis - if/else
test0322_if_else()2692 public void test0322_if_else() {
2693 this.runNegativeTest(
2694 new String[] {
2695 "X.java",
2696 "public class X {\n" +
2697 " void foo(Object o, boolean b) {\n" +
2698 " o.toString();\n" +
2699 " if (b) { /* */ }\n" +
2700 " if (o == null) { /* */ }\n" + // complain
2701 " }\n" +
2702 "}"},
2703 "----------\n" +
2704 "1. ERROR in X.java (at line 5)\n" +
2705 " if (o == null) { /* */ }\n" +
2706 " ^\n" +
2707 "Null comparison always yields false: The variable o cannot be null at this location\n" +
2708 "----------\n" +
2709 "2. WARNING in X.java (at line 5)\n" +
2710 " if (o == null) { /* */ }\n" +
2711 " ^^^^^^^^^\n" +
2712 "Dead code\n" +
2713 "----------\n",
2714 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2715 }
2716
2717 // null analysis - if/else
test0323_if_else()2718 public void test0323_if_else() {
2719 this.runConformTest(
2720 new String[] {
2721 "X.java",
2722 "public class X {\n" +
2723 " void foo(Object o, boolean b) {\n" +
2724 " if (o == null && b) {\n" +
2725 " o = new Object();\n" +
2726 " }\n" +
2727 " if (o == null) { /* */ }\n" + // quiet
2728 " }\n" +
2729 "}"},
2730 "");
2731 }
2732
2733 // null analysis - if/else
test0324_if_else_nested()2734 public void test0324_if_else_nested() {
2735 this.runConformTest(
2736 new String[] {
2737 "X.java",
2738 "class X {\n" +
2739 " void foo (boolean b) {\n" +
2740 " String s = null;\n" +
2741 " if (b) {\n" +
2742 " if (b) {\n" +
2743 " s = \"1\";\n" +
2744 " } \n" +
2745 " else {\n" +
2746 " s = \"2\";\n" +
2747 " }\n" +
2748 " } \n" +
2749 " else if (b) {\n" +
2750 " s = \"3\"; \n" +
2751 " } \n" +
2752 " else {\n" +
2753 " s = \"4\";\n" +
2754 " }\n" +
2755 " s.toString();\n" +
2756 " }\n" +
2757 "}"},
2758 "");
2759 }
2760
2761 // null analysis - if/else
test0325_if_else_nested()2762 public void test0325_if_else_nested() {
2763 this.runNegativeTest(
2764 new String[] {
2765 "X.java",
2766 "class X {\n" +
2767 " void foo (boolean b) {\n" +
2768 " String s = null;\n" +
2769 " if (b) {\n" +
2770 " if (b) {\n" +
2771 " s = \"1\";\n" +
2772 " } \n" +
2773 " else {\n" +
2774 " s = \"2\";\n" +
2775 " }\n" +
2776 " } \n" +
2777 " else if (b) {\n" +
2778 " if (b) {\n" +
2779 " s = \"3\"; \n" +
2780 " }\n" +
2781 " } \n" +
2782 " else {\n" +
2783 " s = \"4\";\n" +
2784 " }\n" +
2785 " s.toString();\n" +
2786 " }\n" +
2787 "}"},
2788 "----------\n" +
2789 "1. ERROR in X.java (at line 20)\n" +
2790 " s.toString();\n" +
2791 " ^\n" +
2792 "Potential null pointer access: The variable s may be null at this location\n" +
2793 "----------\n",
2794 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2795 }
2796
2797 // null analysis - if/else
2798 // limit: we cannot sync on external factors, even if this is a pattern
2799 // that is quite used
test0326_if_else()2800 public void test0326_if_else() {
2801 this.runNegativeTest(
2802 new String[] {
2803 "X.java",
2804 "class X {\n" +
2805 " void foo (boolean b) {\n" +
2806 " String s1 = null;\n" +
2807 " if (b) {\n" +
2808 " s1 = \"1\";\n" +
2809 " }\n" +
2810 " s1.toString();\n" + // complain: can't guess if b means anything for s1 init
2811 " }\n" +
2812 "}"},
2813 "----------\n" +
2814 "1. ERROR in X.java (at line 7)\n" +
2815 " s1.toString();\n" +
2816 " ^^\n" +
2817 "Potential null pointer access: The variable s1 may be null at this location\n" +
2818 "----------\n",
2819 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2820 }
2821
2822 // null analysis - if/else
2823 // limit: we cannot sync on external factors, even if this is a pattern
2824 // that is quite used
test0327_if_else()2825 public void test0327_if_else() {
2826 this.runNegativeTest(
2827 new String[] {
2828 "X.java",
2829 "class X {\n" +
2830 " void foo (String s1) {\n" +
2831 " String s2 = null;\n" +
2832 " if (s1 == null) {\n" +
2833 " s1 = \"1\";\n" +
2834 " s2 = \"2\";\n" +
2835 " }\n" +
2836 " s1.toString();\n" + // quiet
2837 " s2.toString();\n" + // complain: can't guess whether s2 depends on s1 for init
2838 " }\n" +
2839 "}"},
2840 "----------\n" +
2841 "1. ERROR in X.java (at line 9)\n" +
2842 " s2.toString();\n" +
2843 " ^^\n" +
2844 "Potential null pointer access: The variable s2 may be null at this location\n" +
2845 "----------\n",
2846 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2847 }
2848
2849 // null analysis - if/else
test0328_if_else()2850 public void test0328_if_else() {
2851 this.runNegativeTest(
2852 new String[] {
2853 "X.java",
2854 "public class X {\n" +
2855 " void foo(Object o, boolean b) {\n" +
2856 " if (o != null || b) {\n" +
2857 " if (b) {\n" +
2858 " o = new Object();\n" +
2859 " }\n" +
2860 " }\n" + // quiet
2861 " else { /* */ }\n" +
2862 " o.toString();\n" + // complain
2863 " }\n" +
2864 "}"},
2865 "----------\n" +
2866 "1. ERROR in X.java (at line 9)\n" +
2867 " o.toString();\n" +
2868 " ^\n" +
2869 "Potential null pointer access: The variable o may be null at this location\n" +
2870 "----------\n",
2871 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2872 }
2873
2874 // null analysis - if/else
test0329_if_else_nested()2875 public void test0329_if_else_nested() {
2876 this.runNegativeTest(
2877 new String[] {
2878 "X.java",
2879 "public class X {\n" +
2880 " void foo(Object o, boolean b) {\n" +
2881 " if (b) {\n" +
2882 " if (o != null) { /* */ }\n" + // shade doubts on o
2883 " }\n" +
2884 " o.toString();\n" + // complain
2885 " }\n" +
2886 "}"},
2887 "----------\n" +
2888 "1. ERROR in X.java (at line 6)\n" +
2889 " o.toString();\n" +
2890 " ^\n" +
2891 "Potential null pointer access: The variable o may be null at this location\n" +
2892 "----------\n",
2893 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2894 }
2895
2896 // null analysis - if/else
test0330_if_else_nested()2897 public void test0330_if_else_nested() {
2898 this.runConformTest(
2899 new String[] {
2900 "X.java",
2901 "public class X {\n" +
2902 " void foo(Object o, boolean b) {\n" +
2903 " if (b) {\n" +
2904 " if (o == null) {\n" +
2905 " o = new Object();\n" +
2906 " }\n" +
2907 " }\n" +
2908 " o.toString();\n" + // quiet
2909 " }\n" +
2910 "}"},
2911 "");
2912 }
2913
2914 // null analysis - if/else
test0331_if_else_nested()2915 public void test0331_if_else_nested() {
2916 this.runConformTest(
2917 new String[] {
2918 "X.java",
2919 "public class X {\n" +
2920 " void foo(Object o1, Object o2) {\n" +
2921 " Object o3 = o2;\n" +
2922 " if (o1 != null) {\n" +
2923 " o3.toString(); // guards o3\n" +
2924 " }\n" +
2925 " o1 = o3;\n" +
2926 " if (o1 != null) { /* */ }\n" +
2927 " }\n" +
2928 "}"},
2929 "");
2930 }
2931
2932 // null analysis - if/else
test0332_if_else()2933 public void test0332_if_else() {
2934 this.runNegativeTest(
2935 new String[] {
2936 "X.java",
2937 "public class X {\n" +
2938 " void foo(Object o, boolean b) {\n" +
2939 " o = new Object();\n" +
2940 " if (b) {\n" +
2941 " o = new Object();\n" +
2942 " }\n" +
2943 " if (o != null) { /* */ }\n" + // complain
2944 " }\n" +
2945 "}"},
2946 "----------\n" +
2947 "1. ERROR in X.java (at line 7)\n" +
2948 " if (o != null) { /* */ }\n" +
2949 " ^\n" +
2950 "Redundant null check: The variable o cannot be null at this location\n" +
2951 "----------\n",
2952 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2953 }
2954
2955 // null analysis - if/else
2956 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128014
2957 // invalid analysis when redundant check is done
test0333_if_else()2958 public void test0333_if_else() {
2959 this.runNegativeTest(
2960 new String[] {
2961 "X.java",
2962 "public class X {\n" +
2963 " void foo(Object o) {\n" +
2964 " o = new Object();\n" +
2965 " if (o != null) {\n" + // complain
2966 " o.toString();\n" +
2967 " }\n" +
2968 " o.toString();\n" + // quiet asked
2969 " }\n" +
2970 "}"},
2971 "----------\n" +
2972 "1. ERROR in X.java (at line 4)\n" +
2973 " if (o != null) {\n" +
2974 " ^\n" +
2975 "Redundant null check: The variable o cannot be null at this location\n" +
2976 "----------\n",
2977 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
2978 }
2979
2980 // null analysis - if/else
2981 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128014
2982 // invalid analysis when redundant check is done - variant
test0334_if_else()2983 public void test0334_if_else() {
2984 this.runNegativeTest(
2985 new String[] {
2986 "X.java",
2987 "public class X {\n" +
2988 " void foo(Object o) {\n" +
2989 " o = new Object();\n" +
2990 " if (o != null) {\n" + // complain
2991 " o.toString();\n" +
2992 " }\n" +
2993 " else {\n" +
2994 " o.toString();\n" +
2995 " }\n" +
2996 " o.toString();\n" + // quiet
2997 " }\n" +
2998 "}"},
2999 "----------\n" +
3000 "1. ERROR in X.java (at line 4)\n" +
3001 " if (o != null) {\n" +
3002 " ^\n" +
3003 "Redundant null check: The variable o cannot be null at this location\n" +
3004 "----------\n" +
3005 "2. WARNING in X.java (at line 7)\n" +
3006 " else {\n" +
3007 " o.toString();\n" +
3008 " }\n" +
3009 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
3010 "Dead code\n" +
3011 "----------\n",
3012 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3013 }
3014
3015 // null analysis - if/else
3016 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=129581
3017 // Test that no false null reference warning is issued for a variable
3018 // that has been wrongly tainted by a redundant null check upstream.
test0335_if_else()3019 public void test0335_if_else() {
3020 runNegativeNullTest(
3021 new String[] {
3022 "X.java",
3023 "public class X {\n" +
3024 " void foo(Object o) {\n" +
3025 " if (o != null) {\n" +
3026 " if (o != null) {\n" + // complain
3027 " o.toString();\n" +
3028 " }\n" +
3029 " o.toString();\n" + // quiet
3030 " }\n" +
3031 " }\n" +
3032 "}"},
3033 "----------\n" +
3034 "1. ERROR in X.java (at line 4)\n" +
3035 " if (o != null) {\n" +
3036 " ^\n" +
3037 "Redundant null check: The variable o cannot be null at this location\n" +
3038 "----------\n");
3039 }
3040
3041 // null analysis - if/else
3042 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128014
3043 // invalid analysis when redundant check is done - variant
test0336_if_else()3044 public void test0336_if_else() {
3045 this.runNegativeTest(
3046 new String[] {
3047 "X.java",
3048 "public class X {\n" +
3049 " void foo(Object o) {\n" +
3050 " if (o != null) {\n" +
3051 " if (o != null) {\n" + // complain
3052 " o.toString();\n" +
3053 " }\n" +
3054 " else {\n" +
3055 " o.toString();\n" + // must complain anyway (could be quite distant from the if test)
3056 " }\n" +
3057 " o.toString();\n" + // quiet
3058 " }\n" +
3059 " }\n" +
3060 "}"},
3061 "----------\n" +
3062 "1. ERROR in X.java (at line 4)\n" +
3063 " if (o != null) {\n" +
3064 " ^\n" +
3065 "Redundant null check: The variable o cannot be null at this location\n" +
3066 "----------\n" +
3067 "2. WARNING in X.java (at line 7)\n" +
3068 " else {\n" +
3069 " o.toString();\n" +
3070 " }\n" +
3071 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
3072 "Dead code\n" +
3073 "----------\n",
3074 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3075 }
3076
3077
3078 // null analysis - if/else nested with correlation
3079 // reconsider if we implement correlation
3080 // TODO (maxime) https://bugs.eclipse.org/bugs/show_bug.cgi?id=128861
_test0337_if_else_nested_correlation()3081 public void _test0337_if_else_nested_correlation() {
3082 this.runConformTest(
3083 new String[] {
3084 "X.java",
3085 "public class X {\n" +
3086 " public int foo (Object o1, Object o2) {\n" +
3087 " int result = 0;\n" +
3088 " if (o1 == null && o2 != null) {\n" +
3089 " result = -1;\n" +
3090 " } else {\n" +
3091 " if (o1 == null && o2 == null) {\n" +
3092 " result = 0;\n" +
3093 " } else {\n" +
3094 " if (o1 != null && o2 == null) {\n" +
3095 " result = 1;\n" +
3096 " } else {\n" +
3097 " int lhs = ((Y) o1).foo(); // may be null\n" +
3098 " int rhs = ((Y) o2).foo();\n" +
3099 " result = lhs - rhs;\n" +
3100 " }\n" +
3101 " }\n" +
3102 " }\n" +
3103 " return result;\n" +
3104 " }\n" +
3105 "}\n" +
3106 "abstract class Y {\n" +
3107 " abstract int foo();\n" +
3108 "}\n" +
3109 "\n"},
3110 "");
3111 }
3112
3113 // null analysis - if/else nested with correlation
3114 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128861
3115 // workaround
test0338_if_else_nested()3116 public void test0338_if_else_nested() {
3117 this.runConformTest(
3118 new String[] {
3119 "X.java",
3120 "public class X {\n" +
3121 " public int foo (Object o1, Object o2) {\n" +
3122 " int result = 0;\n" +
3123 " if (o1 == null && o2 == null) {\n" +
3124 " result = 0;\n" +
3125 " } else {\n" +
3126 " if (o1 == null) {\n" +
3127 " result = -1;\n" +
3128 " } else {\n" +
3129 " if (o2 == null) {\n" +
3130 " result = 1;\n" +
3131 " } else {\n" +
3132 " int lhs = ((Y) o1).foo();\n" +
3133 " int rhs = ((Y) o2).foo();\n" +
3134 " result = lhs - rhs;\n" +
3135 " }\n" +
3136 " }\n" +
3137 " }\n" +
3138 " return result;\n" +
3139 " }\n" +
3140 "}\n" +
3141 "abstract class Y {\n" +
3142 " abstract int foo();\n" +
3143 "}\n" +
3144 "\n"},
3145 "");
3146 }
3147
3148 // null analysis - if/else nested with unknown protection: unknown cannot protect
test0339_if_else_nested()3149 public void test0339_if_else_nested() {
3150 this.runNegativeTest(
3151 new String[] {
3152 "X.java",
3153 "public class X {\n" +
3154 " void foo(Object o, boolean b) {\n" +
3155 " if (o == null || b) {\n" +
3156 " if (bar() == o) {\n" +
3157 " o.toString();\n" +
3158 " }\n" +
3159 " }\n" +
3160 " }\n" +
3161 " Object bar() {\n" +
3162 " return new Object();\n" +
3163 " }\n" +
3164 "}"},
3165 "----------\n" +
3166 "1. ERROR in X.java (at line 5)\n" +
3167 " o.toString();\n" +
3168 " ^\n" +
3169 "Potential null pointer access: The variable o may be null at this location\n" +
3170 "----------\n",
3171 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3172 }
3173
3174 // null analysis - if/else nested
test0340_if_else_nested()3175 public void test0340_if_else_nested() {
3176 this.runNegativeTest(
3177 new String[] {
3178 "X.java",
3179 "public class X {\n" +
3180 " void foo(Object o) {\n" +
3181 " if (o == null) {\n" +
3182 " if (bar() == o) {\n" +
3183 " o.toString();\n" +
3184 " }\n" +
3185 " }\n" +
3186 " }\n" +
3187 " Object bar() {\n" +
3188 " return new Object();\n" +
3189 " }\n" +
3190 "}"},
3191 "----------\n" +
3192 "1. ERROR in X.java (at line 5)\n" +
3193 " o.toString();\n" +
3194 " ^\n" +
3195 "Null pointer access: The variable o can only be null at this location\n" +
3196 "----------\n",
3197 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3198 }
3199
3200 // null analysis - if/else nested
test0341_if_else_nested()3201 public void test0341_if_else_nested() {
3202 this.runNegativeTest(
3203 new String[] {
3204 "X.java",
3205 "public class X {\n" +
3206 " void foo(Object o1, Object o2, boolean b) {\n" +
3207 " if (o1 == null || b) {\n" +
3208 " if (o1 == o2) {\n" +
3209 " o1.toString();\n" +
3210 " }\n" +
3211 " }\n" +
3212 " }\n" +
3213 "}"},
3214 "----------\n" +
3215 "1. ERROR in X.java (at line 5)\n" +
3216 " o1.toString();\n" +
3217 " ^^\n" +
3218 "Potential null pointer access: The variable o1 may be null at this location\n" +
3219 "----------\n",
3220 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3221 }
3222
3223 // null analysis - if/else nested
test0342_if_else_nested()3224 public void test0342_if_else_nested() {
3225 this.runNegativeTest(
3226 new String[] {
3227 "X.java",
3228 "public class X {\n" +
3229 " void foo(Object o1, Object o2, boolean b) {\n" +
3230 " if (o1 == null || b) {\n" +
3231 " if (o2 == o1) {\n" +
3232 " o1.toString();\n" +
3233 " }\n" +
3234 " }\n" +
3235 " }\n" +
3236 "}"},
3237 "----------\n" +
3238 "1. ERROR in X.java (at line 5)\n" +
3239 " o1.toString();\n" +
3240 " ^^\n" +
3241 "Potential null pointer access: The variable o1 may be null at this location\n" +
3242 "----------\n",
3243 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3244 }
3245
3246 // null analysis -- while
test0401_while()3247 public void test0401_while() {
3248 this.runNegativeTest(
3249 new String[] {
3250 "X.java",
3251 "public class X {\n" +
3252 " void foo() {\n" +
3253 " Object o = null;\n" +
3254 " while (o.toString() != null) {/* */}\n" +
3255 // complain: NPE
3256 " }\n" +
3257 "}\n"},
3258 "----------\n" +
3259 "1. ERROR in X.java (at line 4)\n" +
3260 " while (o.toString() != null) {/* */}\n" +
3261 " ^\n" +
3262 "Null pointer access: The variable o can only be null at this location\n" +
3263 "----------\n",
3264 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3265 }
3266
3267 // null analysis -- while
test0402_while()3268 public void test0402_while() {
3269 this.runNegativeTest(
3270 new String[] {
3271 "X.java",
3272 "public class X {\n" +
3273 " void foo() {\n" +
3274 " Object o = null;\n" +
3275 " while (o != null) {/* */}\n" +
3276 // complain: get o null first time and forever
3277 " }\n" +
3278 "}\n"},
3279 "----------\n" +
3280 "1. ERROR in X.java (at line 4)\n" +
3281 " while (o != null) {/* */}\n" +
3282 " ^\n" +
3283 "Null comparison always yields false: The variable o can only be null at this location\n" +
3284 "----------\n",
3285 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3286 }
3287
3288 // null analysis -- while
test0403_while()3289 public void test0403_while() {
3290 this.runConformTest(
3291 new String[] {
3292 "X.java",
3293 "public class X {\n" +
3294 " void foo() {\n" +
3295 " Object o = null;\n" +
3296 " while (o == null) {\n" +
3297 // quiet: first iteration is sure to find o null,
3298 // but other iterations may change it
3299 " o = new Object();\n" +
3300 " }\n" +
3301 " }\n" +
3302 "}\n"},
3303 "");
3304 }
3305
3306 // null analysis -- while
test0404_while()3307 public void test0404_while() {
3308 this.runConformTest(
3309 new String[] {
3310 "X.java",
3311 "public class X {\n" +
3312 " void foo() {\n" +
3313 " Object o = null;\n" +
3314 " while (o == null) {\n" +
3315 // quiet: first iteration is sure to find o null,
3316 // but other iterations may change it
3317 " if (System.currentTimeMillis() > 10L) {\n" +
3318 " o = new Object();\n" +
3319 " }\n" +
3320 " }\n" +
3321 " }\n" +
3322 "}\n"},
3323 "");
3324 }
3325
3326 // null analysis -- while
test0405_while()3327 public void test0405_while() {
3328 this.runNegativeTest(
3329 new String[] {
3330 "X.java",
3331 "public class X {\n" +
3332 " boolean bar() {\n" +
3333 " return true;\n" +
3334 " }\n" +
3335 " void foo(Object o) {\n" +
3336 " while (bar() && o == null) {\n" +
3337 " o.toString();\n" + // complain: NPE
3338 " o = new Object();\n" +
3339 " }\n" +
3340 " }\n" +
3341 "}\n"},
3342 "----------\n" +
3343 "1. ERROR in X.java (at line 7)\n" +
3344 " o.toString();\n" +
3345 " ^\n" +
3346 "Null pointer access: The variable o can only be null at this location\n" +
3347 "----------\n",
3348 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3349 }
3350
3351 // null analysis -- while
test0406_while()3352 public void test0406_while() {
3353 this.runNegativeTest(
3354 new String[] {
3355 "X.java",
3356 "public class X {\n" +
3357 " boolean dummy;\n" +
3358 " void foo(Object o) {\n" +
3359 " o = null;\n" +
3360 " while (dummy || o != null) { /* */ }\n" + // o can only be null
3361 " }\n" +
3362 "}\n"},
3363 "----------\n" +
3364 "1. ERROR in X.java (at line 5)\n" +
3365 " while (dummy || o != null) { /* */ }\n" +
3366 " ^\n" +
3367 "Null comparison always yields false: The variable o can only be null at this location\n" +
3368 "----------\n",
3369 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3370 }
3371
3372 // null analysis -- while
test0407_while()3373 public void test0407_while() {
3374 this.runNegativeTest(
3375 new String[] {
3376 "X.java",
3377 "public class X {\n" +
3378 " boolean dummy;\n" +
3379 " void foo() {\n" +
3380 " Object o = null;\n" +
3381 " while (dummy) {\n" +
3382 " o.toString();\n" + // complain: NPE on first iteration
3383 " o = new Object();\n" +
3384 " }\n" +
3385 " }\n" +
3386 "}\n"},
3387 "----------\n" +
3388 "1. ERROR in X.java (at line 6)\n" +
3389 " o.toString();\n" +
3390 " ^\n" +
3391 "Potential null pointer access: The variable o may be null at this location\n" +
3392 "----------\n",
3393 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3394 }
3395
3396 // null analysis -- while
3397 // this test shows that, as long as we do not explore all possible
3398 // paths, we have to take potential initializations into account
3399 // even in branches that could be pruned in the first passes
3400 // first approximation is to stop pruning code conditioned by
3401 // variables
3402 // second approximation could still rely upon variables that are
3403 // never affected by the looping code (unassigned variables)
3404 // complete solution would call for multiple iterations in the
3405 // null analysis
test0408_while()3406 public void test0408_while() {
3407 this.runConformTest(
3408 new String[] {
3409 "X.java",
3410 "public class X {\n" +
3411 " void foo() {\n" +
3412 " Object o = null,\n" +
3413 " u = new Object(),\n" +
3414 " v = new Object();\n" +
3415 " while (o == null) {\n" +
3416 " if (v == null) {\n" +
3417 " o = new Object();\n" +
3418 " };\n" +
3419 " if (u == null) {\n" +
3420 " v = null;\n" +
3421 " };\n" +
3422 " u = null;\n" +
3423 " }\n" +
3424 " }\n" +
3425 "}\n"},
3426 "");
3427 }
3428
3429 // null analysis -- while
test0409_while()3430 public void test0409_while() {
3431 this.runNegativeTest(
3432 new String[] {
3433 "X.java",
3434 "public class X {\n" +
3435 " boolean dummy;\n" +
3436 " void foo() {\n" +
3437 " Object o = null;\n" +
3438 " while (dummy || (o = new Object()).equals(o)) {\n" +
3439 " o.toString();\n" +
3440 " }\n" +
3441 " }\n" +
3442 "}\n"},
3443 "----------\n" +
3444 "1. ERROR in X.java (at line 6)\n" +
3445 " o.toString();\n" +
3446 " ^\n" +
3447 "Potential null pointer access: The variable o may be null at this location\n" +
3448 "----------\n",
3449 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3450 }
3451
3452 // null analysis -- while
test0410_while_nested()3453 public void test0410_while_nested() {
3454 this.runConformTest(
3455 new String[] {
3456 "X.java",
3457 "public class X {\n" +
3458 " boolean dummy;\n" +
3459 " void foo() {\n" +
3460 " Object o = null;\n" +
3461 " while (dummy) {\n" +
3462 " while (o != null) {\n" +
3463 " o.toString();\n" +
3464 " }\n" +
3465 " if (System.currentTimeMillis() > 10L) {\n" +
3466 " o = new Object();\n" +
3467 " }\n" +
3468 " }\n" +
3469 " }\n" +
3470 "}\n"},
3471 "");
3472 }
3473
3474 // null analysis -- while
test0411_while_nested()3475 public void test0411_while_nested() {
3476 this.runConformTest(
3477 new String[] {
3478 "X.java",
3479 "public class X {\n" +
3480 " void foo() {\n" +
3481 " Object o = null,\n" +
3482 " u = new Object(),\n" +
3483 " v = new Object();\n" +
3484 " while (o == null) {\n" +
3485 " if (v == null) {\n" +
3486 " o = new Object();\n" +
3487 " };\n" +
3488 " while (o == null) {\n" +
3489 " if (u == null) {\n" +
3490 " v = null;\n" +
3491 " };\n" +
3492 " u = null;\n" +
3493 " }\n" +
3494 " }\n" +
3495 " }\n" +
3496 "}\n"},
3497 "");
3498 }
3499
3500 // null analysis -- while
test0412_while_if_nested()3501 public void test0412_while_if_nested() {
3502 this.runNegativeTest(
3503 new String[] {
3504 "X.java",
3505 "public class X {\n" +
3506 " boolean dummy, other;\n" +
3507 " void foo() {\n" +
3508 " Object o = null;\n" +
3509 " while (dummy) {\n" +
3510 " if (other) {\n" +
3511 " o.toString();\n" +
3512 " }\n" +
3513 " o = new Object();\n" +
3514 " }\n" +
3515 " }\n" +
3516 "}\n"},
3517 "----------\n" +
3518 "1. ERROR in X.java (at line 7)\n" +
3519 " o.toString();\n" +
3520 " ^\n" +
3521 "Potential null pointer access: The variable o may be null at this location\n" +
3522 "----------\n",
3523 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3524 }
3525
3526 // null analysis -- while
test0413_while_unknown_field()3527 public void test0413_while_unknown_field() {
3528 this.runConformTest(
3529 new String[] {
3530 "X.java",
3531 "public class X {\n" +
3532 " Object o;\n" +
3533 " void foo(boolean dummy) {\n" +
3534 " while (dummy) {\n" +
3535 " o = null;\n" +
3536 " }\n" +
3537 " o.toString();\n" +
3538 " }\n" +
3539 "}\n"},
3540 "");
3541 }
3542
3543 // null analysis -- while
test0414_while_unknown_parameter()3544 public void test0414_while_unknown_parameter() {
3545 this.runNegativeTest(
3546 new String[] {
3547 "X.java",
3548 "public class X {\n" +
3549 " boolean dummy;\n" +
3550 " void foo(Object o) {\n" +
3551 " while (dummy) {\n" +
3552 " o = null;\n" + // quiet: first iteration doesn't know
3553 " }\n" +
3554 " o.toString();\n" + // complain: only get out of the loop with null
3555 " }\n" +
3556 "}\n"},
3557 "----------\n" +
3558 "1. ERROR in X.java (at line 7)\n" +
3559 " o.toString();\n" +
3560 " ^\n" +
3561 "Potential null pointer access: The variable o may be null at this location\n" +
3562 "----------\n",
3563 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3564 }
3565
3566 // null analysis -- while
test0415_while_unknown_if_else()3567 public void test0415_while_unknown_if_else() {
3568 this.runConformTest(
3569 new String[] {
3570 "X.java",
3571 "public class X {\n" +
3572 " boolean dummy;\n" +
3573 " void foo() {\n" +
3574 " Object o = null;\n" +
3575 " if (dummy) {\n" +
3576 " o = new Object();\n" +
3577 " }\n" +
3578 " while (dummy) {\n" +
3579 // limit of the analysis: we do not correlate if and while conditions
3580 " if (o == null) {/* */}\n" +
3581 " }\n" +
3582 " }\n" +
3583 "}\n"},
3584 "");
3585 }
3586
3587 // null analysis -- while
test0416_while()3588 public void test0416_while() {
3589 this.runNegativeTest(
3590 new String[] {
3591 "X.java",
3592 "public class X {\n" +
3593 " boolean dummy;\n" +
3594 " void foo() {\n" +
3595 " Object o = null;\n" +
3596 " while (dummy) {\n" +
3597 " o = new Object();\n" +
3598 " }\n" +
3599 " o.toString();\n" +
3600 " }\n" +
3601 "}\n"},
3602 "----------\n" +
3603 "1. ERROR in X.java (at line 8)\n" +
3604 " o.toString();\n" +
3605 " ^\n" +
3606 "Potential null pointer access: The variable o may be null at this location\n" +
3607 "----------\n",
3608 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3609 }
3610
3611 // null analysis -- while
test0417_while()3612 public void test0417_while() {
3613 this.runNegativeTest(
3614 new String[] {
3615 "X.java",
3616 "public class X {\n" +
3617 " boolean dummy;\n" +
3618 " void foo() {\n" +
3619 " Object o = null;\n" +
3620 " while (dummy) { /* */ }\n" + // doesn't affect o
3621 " o.toString();\n" +
3622 " }\n" +
3623 "}\n"},
3624 "----------\n" +
3625 "1. ERROR in X.java (at line 6)\n" +
3626 " o.toString();\n" +
3627 " ^\n" +
3628 "Null pointer access: The variable o can only be null at this location\n" +
3629 "----------\n",
3630 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3631 }
3632
3633 // null analysis -- while
3634 // origin AssignmentTest.testO22
test0418_while_try()3635 public void test0418_while_try() {
3636 this.runConformTest(
3637 new String[] {
3638 "X.java",
3639 "public class X {\n" +
3640 " boolean bool() { return true; }\n" +
3641 " void foo() {\n" +
3642 " Object o = null;\n" +
3643 " while (bool()) {\n" +
3644 " try {\n" +
3645 " if (o == null) {\n" +
3646 " o = new Object();\n" +
3647 " }\n" +
3648 " } finally { /* */ }\n" +
3649 " }\n" +
3650 " }\n" +
3651 "}"},
3652 "");
3653 }
3654
3655 // null analysis -- while
test0419_while()3656 public void test0419_while() {
3657 this.runNegativeTest(
3658 new String[] {
3659 "X.java",
3660 "public class X {\n" +
3661 " boolean bool;\n" +
3662 " void foo(Object o) {\n" +
3663 " while (bool) {\n" +
3664 " o.toString();" + // complain NPE because of second iteration
3665 " o = null;\n" +
3666 " }\n" +
3667 " }\n" +
3668 "}"},
3669 "----------\n" +
3670 "1. ERROR in X.java (at line 5)\n" +
3671 " o.toString(); o = null;\n" +
3672 " ^\n" +
3673 "Potential null pointer access: The variable o may be null at this location\n" +
3674 "----------\n",
3675 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3676 }
3677
3678 // null analysis -- while
test0420_while()3679 public void test0420_while() {
3680 this.runNegativeTest(
3681 new String[] {
3682 "X.java",
3683 "public class X {\n" +
3684 " boolean bool;\n" +
3685 " void foo(Object compare) {\n" +
3686 " Object o = new Object();\n" +
3687 " while ((o = null) == compare) {\n" +
3688 " if (true) {\n" +
3689 " break;\n" +
3690 " }\n" +
3691 " }\n" +
3692 " if (o == null) { /* */ }\n" + // complain can only be null
3693 " }\n" +
3694 "}"},
3695 "----------\n" +
3696 "1. ERROR in X.java (at line 10)\n" +
3697 " if (o == null) { /* */ }\n" +
3698 " ^\n" +
3699 "Redundant null check: The variable o can only be null at this location\n" +
3700 "----------\n",
3701 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3702 }
3703
3704 // null analysis -- while
test0421_while()3705 public void test0421_while() {
3706 this.runConformTest(
3707 new String[] {
3708 "X.java",
3709 "public class X {\n" +
3710 " boolean bool;\n" +
3711 " void foo(Object compare) {\n" +
3712 " Object o = null;\n" +
3713 " while (bool) {\n" +
3714 " o = new Object();\n" +
3715 " o.toString();\n" +
3716 " }\n" +
3717 " }\n" +
3718 "}"},
3719 "");
3720 }
3721
3722 // null analysis -- while
test0422_while()3723 public void test0422_while() {
3724 this.runNegativeTest(
3725 new String[] {
3726 "X.java",
3727 "public class X {\n" +
3728 " boolean bool;\n" +
3729 " void foo() {\n" +
3730 " Object o;\n" +
3731 " while (bool) {\n" +
3732 " o = new Object();\n" +
3733 " if (o == null) { /* */ }\n" +
3734 " o = null;\n" +
3735 " }\n" +
3736 " }\n" +
3737 "}"},
3738 "----------\n" +
3739 "1. ERROR in X.java (at line 7)\n" +
3740 " if (o == null) { /* */ }\n" +
3741 " ^\n" +
3742 "Null comparison always yields false: The variable o cannot be null at this location\n" +
3743 "----------\n" +
3744 "2. WARNING in X.java (at line 7)\n" +
3745 " if (o == null) { /* */ }\n" +
3746 " ^^^^^^^^^\n" +
3747 "Dead code\n" +
3748 "----------\n",
3749 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3750 }
3751
3752 // null analysis -- while
test0423_while()3753 public void test0423_while() {
3754 this.runNegativeTest(
3755 new String[] {
3756 "X.java",
3757 "public class X {\n" +
3758 " boolean bool;\n" +
3759 " void foo() {\n" +
3760 " Object o = null;\n" +
3761 " while (bool) {\n" +
3762 " o = new Object();\n" +
3763 " if (o == null) { /* */ }\n" +
3764 " o = null;\n" +
3765 " }\n" +
3766 " }\n" +
3767 "}"},
3768 "----------\n" +
3769 "1. ERROR in X.java (at line 7)\n" +
3770 " if (o == null) { /* */ }\n" +
3771 " ^\n" +
3772 "Null comparison always yields false: The variable o cannot be null at this location\n" +
3773 "----------\n" +
3774 "2. WARNING in X.java (at line 7)\n" +
3775 " if (o == null) { /* */ }\n" +
3776 " ^^^^^^^^^\n" +
3777 "Dead code\n" +
3778 "----------\n",
3779 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3780 }
3781
3782 // null analysis -- while
test0424_while_try()3783 public void test0424_while_try() {
3784 this.runConformTest(
3785 new String[] {
3786 "X.java",
3787 "public class X {\n" +
3788 " void foo(boolean b) {\n" +
3789 " Object o = null;\n" +
3790 " while (o == null) {\n" +
3791 // quiet: first iteration is sure to find o null,
3792 // but other iterations may change it
3793 " try { /* */ }\n" +
3794 " finally {\n" +
3795 " if (b) {\n" +
3796 " o = new Object();\n" +
3797 " }\n" +
3798 " }\n" +
3799 " }\n" +
3800 " }\n" +
3801 "}\n"},
3802 "");
3803 }
3804
3805 // null analysis -- while
test0425_while()3806 public void test0425_while() {
3807 this.runNegativeTest(
3808 new String[] {
3809 "X.java",
3810 "public class X {\n" +
3811 " boolean dummy;\n" +
3812 " void foo(Object u) {\n" +
3813 " Object o = null;\n" +
3814 " while (dummy) {\n" +
3815 " o = u;\n" +
3816 " }\n" +
3817 " o.toString();\n" +
3818 " }\n" +
3819 "}\n"},
3820 "----------\n" +
3821 "1. ERROR in X.java (at line 8)\n" +
3822 " o.toString();\n" +
3823 " ^\n" +
3824 "Potential null pointer access: The variable o may be null at this location\n" +
3825 "----------\n",
3826 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3827 }
3828
3829 // null analysis -- while
test0426_while()3830 public void test0426_while() {
3831 this.runNegativeTest(
3832 new String[] {
3833 "X.java",
3834 "public class X {\n" +
3835 " boolean dummy;\n" +
3836 " void foo(Object o) {\n" +
3837 " o.toString();\n" +
3838 " while (dummy) { /* */ }\n" +
3839 " if (o == null) { /* */ }\n" + // complain
3840 " }\n" +
3841 "}\n"},
3842 "----------\n" +
3843 "1. ERROR in X.java (at line 6)\n" +
3844 " if (o == null) { /* */ }\n" +
3845 " ^\n" +
3846 "Null comparison always yields false: The variable o cannot be null at this location\n" +
3847 "----------\n" +
3848 "2. WARNING in X.java (at line 6)\n" +
3849 " if (o == null) { /* */ }\n" +
3850 " ^^^^^^^^^\n" +
3851 "Dead code\n" +
3852 "----------\n",
3853 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3854 }
3855
3856 // null analysis -- while
test0427_while_return()3857 public void test0427_while_return() {
3858 this.runNegativeTest(
3859 new String[] {
3860 "X.java",
3861 "public class X {\n" +
3862 " boolean dummy;\n" +
3863 " void foo() {\n" +
3864 " Object o = null;\n" +
3865 " while (dummy) {\n" +
3866 " if (o == null) {\n" +
3867 " return;\n" +
3868 " }\n" +
3869 " }\n" +
3870 " }\n" +
3871 "}\n"},
3872 "----------\n" +
3873 "1. ERROR in X.java (at line 6)\n" +
3874 " if (o == null) {\n" +
3875 " ^\n" +
3876 "Redundant null check: The variable o can only be null at this location\n" +
3877 "----------\n",
3878 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
3879 }
3880
3881 // null analysis - while
test0428_while()3882 public void test0428_while() {
3883 this.runConformTest(
3884 new String[] {
3885 "X.java",
3886 "public class X {\n" +
3887 " X bar() {\n" +
3888 " return null;\n" +
3889 " }\n" +
3890 " void foo(X x) {\n" +
3891 " x.bar();\n" +
3892 " while (x != null) {\n" +
3893 " x = x.bar();\n" +
3894 " }\n" +
3895 " }\n" +
3896 "}\n"},
3897 "");
3898 }
3899
3900 // null analysis - while
test0429_while_nested()3901 public void test0429_while_nested() {
3902 this.runConformTest(
3903 new String[] {
3904 "X.java",
3905 "class X {\n" +
3906 " boolean dummy;\n" +
3907 " void foo (X[] xa) {\n" +
3908 " while (dummy) {\n" +
3909 " xa = null;\n" +
3910 " if (dummy) {\n" +
3911 " xa = new X[5];\n" +
3912 " }\n" +
3913 " if (xa != null) {\n" +
3914 " int i = 0;\n" +
3915 " while (dummy) {\n" +
3916 " X x = xa[i++];\n" +
3917 " x.toString();\n" +
3918 " }\n" +
3919 " }\n" +
3920 " }\n" +
3921 " }\n" +
3922 "}"},
3923 "");
3924 }
3925
3926 // null analysis - while
test0430_while_for_nested()3927 public void test0430_while_for_nested() {
3928 this.runConformTest(
3929 new String[] {
3930 "X.java",
3931 "class X {\n" +
3932 " boolean dummy;\n" +
3933 " void foo (X[] xa) {\n" +
3934 " while (dummy) {\n" +
3935 " xa = null;\n" +
3936 " if (dummy) {\n" +
3937 " xa = new X[5];\n" +
3938 " }\n" +
3939 " if (xa != null) {\n" +
3940 " for (int i = 0; i < xa.length; i++) {\n" +
3941 " X x = xa[i];\n" +
3942 " x.toString();\n" +
3943 " }\n" +
3944 " }\n" +
3945 " }\n" +
3946 " }\n" +
3947 "}"},
3948 "");
3949 }
3950
3951 // null analysis - while
test0431_while()3952 public void test0431_while() {
3953 this.runConformTest(
3954 new String[] {
3955 "X.java",
3956 "class X {\n" +
3957 " boolean dummy;\n" +
3958 " void foo (X x) {\n" +
3959 " x = null;\n" +
3960 " while (dummy) {\n" +
3961 " x = bar();\n" +
3962 " x.toString();\n" +
3963 " }\n" +
3964 " }\n" +
3965 " X bar() {\n" +
3966 " return null;\n" +
3967 " }\n" +
3968 "}"},
3969 "");
3970 }
3971
3972 // null analysis - while
test0432_while()3973 public void test0432_while() {
3974 this.runConformTest(
3975 new String[] {
3976 "X.java",
3977 "class X {\n" +
3978 " boolean dummy;\n" +
3979 " void foo (X x) {\n" +
3980 " while (dummy) {\n" +
3981 " x = bar();\n" +
3982 " x.toString();\n" +
3983 " }\n" +
3984 " }\n" +
3985 " X bar() {\n" +
3986 " return null;\n" +
3987 " }\n" +
3988 "}"},
3989 "");
3990 }
3991
3992 // null analysis - while
test0433_while()3993 public void test0433_while() {
3994 this.runNegativeTest(
3995 new String[] {
3996 "X.java",
3997 "class X {\n" +
3998 " boolean dummy;\n" +
3999 " void foo (X x) {\n" +
4000 " x = null;\n" +
4001 " while (dummy) {\n" +
4002 " x.toString();\n" + // complain and protect
4003 " x.toString();\n" + // quiet
4004 " }\n" +
4005 " }\n" +
4006 "}"},
4007 "----------\n" +
4008 "1. ERROR in X.java (at line 6)\n" +
4009 " x.toString();\n" +
4010 " ^\n" +
4011 "Null pointer access: The variable x can only be null at this location\n" +
4012 "----------\n",
4013 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4014 }
4015
4016 // null analysis - while
4017 // this one shows that we cannot project definitely unknown onto potentially unknown too soon
test0434_while_switch_nested()4018 public void test0434_while_switch_nested() {
4019 this.runConformTest(
4020 new String[] {
4021 "X.java",
4022 "class X {\n" +
4023 " Object bar() {\n" +
4024 " return new Object();\n" +
4025 " }\n" +
4026 " void foo(boolean b, int selector) {\n" +
4027 " Object o = null;\n" +
4028 " while (b) {\n" +
4029 " switch (selector) {\n" +
4030 " case 0:\n" +
4031 " o = bar();\n" +
4032 " if (o != null) { \n" +
4033 " return;\n" +
4034 " }\n" +
4035 " }\n" +
4036 " }\n" +
4037 " }\n" +
4038 "}"},
4039 "");
4040 }
4041
4042 // null analysis - while
test0435_while_init()4043 public void test0435_while_init() {
4044 this.runConformTest(
4045 new String[] {
4046 "X.java",
4047 "class X {\n" +
4048 " int f1;\n" +
4049 " X f2;\n" +
4050 " void foo(X x1, boolean b) {\n" +
4051 " X x2;\n" +
4052 " x2 = x1;\n" +
4053 " while (b) {\n" +
4054 // " if (x2.f1 > 0) { /* */ }\n" +
4055 " if (x2.toString().equals(\"\")) { /* */ }\n" +
4056 " x2 = x2.f2;\n" +
4057 " }\n" +
4058 " }\n" +
4059 "}"},
4060 "");
4061 }
4062
4063 // null analysis - while
test0436_while_init()4064 public void test0436_while_init() {
4065 this.runConformTest(
4066 new String[] {
4067 "X.java",
4068 "class X {\n" +
4069 " int f1;\n" +
4070 " X f2;\n" +
4071 " void foo(X x1, boolean b) {\n" +
4072 " X x2 = x1;\n" +
4073 " while (b) {\n" +
4074 " if (x2.f1 > 0) { /* */ }\n" +
4075 " x2 = x2.f2;\n" +
4076 " }\n" +
4077 " }\n" +
4078 "}"},
4079 "");
4080 }
4081
4082 // null analysis - while
test0437_while_exit()4083 public void test0437_while_exit() {
4084 this.runConformTest(
4085 new String[] {
4086 "X.java",
4087 "class X {\n" +
4088 " void foo(boolean b) {\n" +
4089 " Object o = null;\n" +
4090 " while (b) {\n" +
4091 " if (b) {\n" +
4092 " o = new Object();\n" +
4093 " }\n" +
4094 " if (o != null) {\n" +
4095 " throw new RuntimeException(); \n" +
4096 " }\n" +
4097 " }\n" +
4098 " }\n" +
4099 "}"},
4100 "");
4101 }
4102
4103
4104 // null analysis - while
test0438_while()4105 public void test0438_while() {
4106 this.runNegativeTest(
4107 new String[] {
4108 "X.java",
4109 "class X {\n" +
4110 " void foo(Object o) {\n" +
4111 " while (o == null) { /* */ }\n" +
4112 " o.toString();\n" + // quiet
4113 " if (o != null) { /* */ }\n" + // complain
4114 " }\n" +
4115 "}"},
4116 "----------\n" +
4117 "1. ERROR in X.java (at line 5)\n" +
4118 " if (o != null) { /* */ }\n" +
4119 " ^\n" +
4120 "Redundant null check: The variable o cannot be null at this location\n" +
4121 "----------\n",
4122 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4123 }
4124
4125 // null analysis - while
test0439_while()4126 public void test0439_while() {
4127 this.runNegativeTest(
4128 new String[] {
4129 "X.java",
4130 "class X {\n" +
4131 " void foo(Object o) {\n" +
4132 " while (o == null) {\n" +
4133 " o = new Object();\n" +
4134 " }\n" +
4135 " o.toString();\n" + // quiet
4136 " }\n" +
4137 "}"},
4138 "");
4139 }
4140
4141 // null analysis - while
test0440_while()4142 public void test0440_while() {
4143 this.runNegativeTest(
4144 new String[] {
4145 "X.java",
4146 "class X {\n" +
4147 " void foo(Object o) {\n" +
4148 " while (o == null) {\n" +
4149 " o = new Object();\n" +
4150 " }\n" +
4151 " if (o != null) { /* */ }\n" + // complain
4152 " }\n" +
4153 "}"},
4154 "----------\n" +
4155 "1. ERROR in X.java (at line 6)\n" +
4156 " if (o != null) { /* */ }\n" +
4157 " ^\n" +
4158 "Redundant null check: The variable o cannot be null at this location\n" +
4159 "----------\n",
4160 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4161 }
4162
4163 // null analysis - while
test0441_while()4164 public void test0441_while() {
4165 this.runNegativeTest(
4166 new String[] {
4167 "X.java",
4168 "class X {\n" +
4169 " X bar() {\n" +
4170 " return new X();\n" +
4171 " }\n" +
4172 " void foo(Object o) {\n" +
4173 " while (o == null) {\n" +
4174 " o = bar();\n" +
4175 " }\n" +
4176 " if (o != null) { /* */ }\n" + // complain
4177 " }\n" +
4178 "}"},
4179 "----------\n" +
4180 "1. ERROR in X.java (at line 9)\n" +
4181 " if (o != null) { /* */ }\n" +
4182 " ^\n" +
4183 "Redundant null check: The variable o cannot be null at this location\n" +
4184 "----------\n",
4185 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4186 }
4187
4188 // null analysis - while
test0442_while()4189 public void test0442_while() {
4190 this.runNegativeTest(
4191 new String[] {
4192 "X.java",
4193 "class X {\n" +
4194 " boolean bar() {\n" +
4195 " return true;\n" +
4196 " }\n" +
4197 " void foo(Object o) {\n" +
4198 " while (o == null && bar()) { /* */ }\n" +
4199 " o.toString();\n" + // complain
4200 " }\n" +
4201 "}"},
4202 "----------\n" +
4203 "1. ERROR in X.java (at line 7)\n" +
4204 " o.toString();\n" +
4205 " ^\n" +
4206 "Potential null pointer access: The variable o may be null at this location\n" +
4207 "----------\n",
4208 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4209 }
4210
4211 // null analysis - while
test0443_while_nested()4212 public void test0443_while_nested() {
4213 this.runConformTest(
4214 new String[] {
4215 "X.java",
4216 "class X {\n" +
4217 " void foo() {\n" +
4218 " Object o = null;\n" +
4219 " ext: for (int i = 0; i < 5 ; i++) {\n" +
4220 " if (o != null) {\n" +
4221 " break;\n" +
4222 " }\n" +
4223 " o = new Object();\n" +
4224 " int j = 0;\n" +
4225 " while (j++ < 2) {\n" +
4226 " continue ext;\n" +
4227 " }\n" +
4228 " return;\n" +
4229 " }\n" +
4230 " }\n" +
4231 "}"},
4232 "");
4233 }
4234
4235 // null analysis - while
test0444_while_deeply_nested()4236 public void test0444_while_deeply_nested() {
4237 this.runConformTest(
4238 new String[] {
4239 "X.java",
4240 "class X {\n" +
4241 " void foo(boolean b) {\n" +
4242 " Object o = null;\n" +
4243 " ext: for (int i = 0; i < 5 ; i++) {\n" +
4244 " if (o != null) {\n" +
4245 " break;\n" +
4246 " }\n" +
4247 " do {\n" +
4248 " o = new Object();\n" +
4249 " int j = 0;\n" +
4250 " while (j++ < 2) {\n" +
4251 " continue ext;\n" +
4252 " }\n" +
4253 " } while (b);\n" +
4254 " return;\n" +
4255 " }\n" +
4256 " }\n" +
4257 "}"},
4258 "");
4259 }
4260
4261 // null analysis - while
test0445_while_deeply_nested()4262 public void test0445_while_deeply_nested() {
4263 this.runNegativeTest(
4264 new String[] {
4265 "X.java",
4266 "class X {\n" +
4267 " void foo(boolean b) {\n" +
4268 " Object o = null;\n" +
4269 " ext: for (int i = 0; i < 5 ; i++) {\n" +
4270 " if (o != null) {\n" +
4271 " break;\n" +
4272 " }\n" +
4273 " do {\n" +
4274 " // o = new Object();\n" +
4275 " int j = 0;\n" +
4276 " while (j++ < 2) {\n" +
4277 " continue ext;\n" +
4278 " }\n" +
4279 " } while (b);\n" +
4280 " return;\n" +
4281 " }\n" +
4282 " }\n" +
4283 "}"},
4284 "----------\n" +
4285 "1. ERROR in X.java (at line 5)\n" +
4286 " if (o != null) {\n" +
4287 " ^\n" +
4288 "Null comparison always yields false: The variable o can only be null at this location\n" +
4289 "----------\n",
4290 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4291 }
4292
4293 // null analysis - while
test0446_while()4294 public void test0446_while() {
4295 this.runNegativeTest(
4296 new String[] {
4297 "X.java",
4298 "class X {\n" +
4299 " void foo(Object o, boolean b) {\n" +
4300 " while (o == null || b) {\n" +
4301 " o = new Object();\n" +
4302 " }\n" +
4303 " if (o != null) { /* */ }\n" + // complain
4304 " }\n" +
4305 "}"},
4306 "----------\n" +
4307 "1. ERROR in X.java (at line 6)\n" +
4308 " if (o != null) { /* */ }\n" +
4309 " ^\n" +
4310 "Redundant null check: The variable o cannot be null at this location\n" +
4311 "----------\n",
4312 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4313 }
4314
4315 // null analysis - while
test0447_while()4316 public void test0447_while() {
4317 this.runConformTest(
4318 new String[] {
4319 "X.java",
4320 "class X {\n" +
4321 " void foo(Object o, boolean b) {\n" +
4322 " while (o == null & b) {\n" +
4323 " o = new Object();\n" +
4324 " }\n" +
4325 " if (o != null) { /* */ }\n" +
4326 " }\n" +
4327 "}"},
4328 "");
4329 }
4330
4331 // null analysis - while
test0448_while()4332 public void test0448_while() {
4333 this.runNegativeTest(
4334 new String[] {
4335 "X.java",
4336 "class X {\n" +
4337 " void foo(boolean b[]) {\n" +
4338 " Object o = null;\n" +
4339 " ext: for (int i = 0; i < 5 ; i++) {\n" +
4340 " if (o != null) {\n" +
4341 " break;\n" +
4342 " }\n" +
4343 " while (b[1]) {\n" +
4344 " continue ext;\n" +
4345 " }\n" +
4346 " while (b[2]) {\n" +
4347 " continue ext;\n" +
4348 " }\n" +
4349 " while (b[3]) {\n" +
4350 " continue ext;\n" +
4351 " }\n" +
4352 " while (b[4]) {\n" +
4353 " continue ext;\n" +
4354 " }\n" +
4355 " while (b[5]) {\n" +
4356 " continue ext;\n" +
4357 " }\n" +
4358 " while (b[6]) {\n" +
4359 " continue ext;\n" +
4360 " }\n" +
4361 " return;\n" +
4362 " }\n" +
4363 " }\n" +
4364 "}"},
4365 "----------\n" +
4366 "1. ERROR in X.java (at line 5)\n" +
4367 " if (o != null) {\n" +
4368 " ^\n" +
4369 "Null comparison always yields false: The variable o can only be null at this location\n" +
4370 "----------\n",
4371 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4372 }
4373
4374 // null analysis - while
4375 // this series (up to 451) shows that the merge of the states
4376 // potential non null and potential unknown yields damages in
4377 // case of nested loops (unested loops still OK because we can
4378 // carry the definite non null property)
test0449_while_nested()4379 public void test0449_while_nested() {
4380 this.runConformTest(
4381 new String[] {
4382 "X.java",
4383 "class X {\n" +
4384 " void foo(Object p, boolean b) {\n" +
4385 " Object o = new Object();\n" +
4386 " while (b) {\n" +
4387 " while (b) {\n" +
4388 " o = p;\n" + // now o is unknown
4389 " }\n" +
4390 " }\n" +
4391 " if (o != null) { /* */ }\n" +
4392 " }\n" +
4393 "}"},
4394 "");
4395 }
4396
4397 // null analysis - while
test0450_while()4398 public void test0450_while() {
4399 this.runNegativeTest(
4400 new String[] {
4401 "X.java",
4402 "class X {\n" +
4403 " void foo(boolean b) {\n" +
4404 " Object o = new Object();\n" +
4405 " while (b) {\n" +
4406 " o = new Object();\n" + // o still non null
4407 " }\n" +
4408 " if (o != null) { /* */ }\n" +
4409 " }\n" +
4410 "}"},
4411 "----------\n" +
4412 "1. ERROR in X.java (at line 7)\n" +
4413 " if (o != null) { /* */ }\n" +
4414 " ^\n" +
4415 "Redundant null check: The variable o cannot be null at this location\n" +
4416 "----------\n",
4417 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4418 }
4419
4420 // null analysis - while
4421 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133131
test0451_while_nested()4422 public void test0451_while_nested() {
4423 this.runNegativeTest(
4424 new String[] {
4425 "X.java",
4426 "class X {\n" +
4427 " void foo(boolean b) {\n" +
4428 " Object o = new Object();\n" +
4429 " while (b) {\n" +
4430 " while (b) {\n" +
4431 " o = new Object();\n" + // o still non null
4432 " }\n" +
4433 " }\n" +
4434 " if (o != null) { /* */ }\n" +
4435 " }\n" +
4436 "}"},
4437 "----------\n" +
4438 "1. ERROR in X.java (at line 9)\n" +
4439 " if (o != null) { /* */ }\n" +
4440 " ^\n" +
4441 "Redundant null check: The variable o cannot be null at this location\n" +
4442 "----------\n",
4443 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4444 }
4445
4446 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123399
4447 // variant - the bug is not specific to the do while loop
_test0452_while()4448 public void _test0452_while() {
4449 runNegativeNullTest(
4450 new String[] {
4451 "X.java",
4452 "public class X {\n" +
4453 " void foo(Object doubt) {\n" +
4454 " Object o = null;\n" +
4455 " while (true) {\n" +
4456 " if (o == null) {\n" +
4457 " return;\n" +
4458 " }\n" +
4459 " o = doubt;\n" +
4460 " }\n" +
4461 " }\n" +
4462 "}"},
4463 "----------\n" +
4464 "1. ERROR in X.java (at line 6)\n" +
4465 " if (o == null) {\n" +
4466 " ^\n" +
4467 "Redundant null check: The variable o can only be null at this location\n" +
4468 "----------\n"
4469 );
4470 }
4471
4472 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123399
4473 // variant - cannot refine the diagnostic without engaging into conditionals
4474 // dedicated flow context
_test0453_while()4475 public void _test0453_while() {
4476 this.runNegativeTest(
4477 new String[] {
4478 "X.java",
4479 "public class X {\n" +
4480 " void foo(Object doubt, boolean b) {\n" +
4481 " Object o1 = null, o2 = null;\n" +
4482 " while (true) {\n" +
4483 " if (o1 == null) { /* empty */ }\n" +
4484 " if (b) {\n" +
4485 " if (o2 == null) {\n" +
4486 " return;\n" +
4487 " }\n" +
4488 " }\n" +
4489 " o1 = o2 = doubt;\n" +
4490 " }\n" +
4491 " }\n" +
4492 "}"},
4493 "ERROR: complain on line 7, but not on line 5"
4494 );
4495 }
4496
4497 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=129122
test0454_while()4498 public void test0454_while() {
4499 this.runConformTest(
4500 new String[] {
4501 "X.java",
4502 "public class X {\n" +
4503 " Object bar() {\n" +
4504 " return new Object();\n" +
4505 " }\n" +
4506 " void foo() {\n" +
4507 " Object o = null;\n" +
4508 " while (true) {\n" +
4509 " o = bar();\n" +
4510 " if (o != null) {\n" +
4511 " o = new Object();\n" +
4512 " }\n" +
4513 " o = null; // quiet pls\n" +
4514 " }\n" +
4515 " }\n" +
4516 "}"},
4517 ""
4518 );
4519 }
4520
4521 // null analysis - while
4522 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133131
4523 // variant
test0455_while_nested()4524 public void test0455_while_nested() {
4525 this.runNegativeTest(
4526 new String[] {
4527 "X.java",
4528 "class X {\n" +
4529 " void foo(boolean b) {\n" +
4530 " Object o = new Object();\n" +
4531 " while (b) {\n" +
4532 " o = new Object();\n" + // o still non null
4533 " }\n" +
4534 " if (o != null) { /* */ }\n" +
4535 " }\n" +
4536 "}"},
4537 "----------\n" +
4538 "1. ERROR in X.java (at line 7)\n" +
4539 " if (o != null) { /* */ }\n" +
4540 " ^\n" +
4541 "Redundant null check: The variable o cannot be null at this location\n" +
4542 "----------\n",
4543 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4544 }
4545
4546 // null analysis - while
4547 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=134848
4548 // false positive after nested loop with break to explicit label
test0456_while_nested_explicit_label()4549 public void test0456_while_nested_explicit_label() {
4550 this.runConformTest(
4551 new String[] {
4552 "X.java",
4553 "public class X {\n" +
4554 " void foo(Object o) {\n" +
4555 " while (true) {\n" +
4556 " if (o != null) {\n" +
4557 " o.toString();\n" +
4558 " loop: while (true) {\n" +
4559 " break loop;\n" +
4560 " }\n" +
4561 " o.toString();\n" + // must not complain here
4562 " }\n" +
4563 " }\n" +
4564 " }\n" +
4565 "}"},
4566 "");
4567 }
4568
4569 // null analysis - while
4570 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=154995
test0457_while_nested_break()4571 public void test0457_while_nested_break() {
4572 this.runConformTest(
4573 new String[] {
4574 "X.java",
4575 "public class X {\n" +
4576 " public void test(String p, String q, boolean b) {\n" +
4577 " while (b) {\n" +
4578 " String e = q;\n" +
4579 " e.trim();\n" +
4580 " while (true) {\n" +
4581 " if (b)\n" +
4582 " e = q;\n" +
4583 " else\n" +
4584 " e = null;\n" +
4585 " if (e == null || p != null) {\n" +
4586 " if (e != null) {\n" + // should not complain here
4587 " // Do something\n" +
4588 " }\n" +
4589 " break;\n" +
4590 " }\n" +
4591 " }\n" +
4592 " }\n" +
4593 " }\n" +
4594 "}"},
4595 "");
4596 }
4597
4598 // null analysis - while
4599 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=134848
4600 // variant: no label yields no problem
test0458_while_nested_explicit_label()4601 public void test0458_while_nested_explicit_label() {
4602 this.runConformTest(
4603 new String[] {
4604 "X.java",
4605 "public class X {\n" +
4606 " void foo(Object o) {\n" +
4607 " while (true) {\n" +
4608 " if (o != null) {\n" +
4609 " o.toString();\n" +
4610 " while (true) {\n" +
4611 " break;\n" +
4612 " }\n" +
4613 " o.toString();\n" + // must not complain here
4614 " }\n" +
4615 " }\n" +
4616 " }\n" +
4617 "}"},
4618 "");
4619 }
4620
4621 // null analysis -- while nested hits CAN_ONLY_NON_NULL
test0459_while_nested()4622 public void test0459_while_nested() {
4623 this.runNegativeTest(
4624 new String[] {
4625 "X.java",
4626 "public class X {\n" +
4627 " void foo(boolean b) {\n" +
4628 " Object o = b ? null : new Object(),\n" +
4629 " u = new Object(),\n" +
4630 " v = new Object();\n" +
4631 " while (o != null) {\n" +
4632 " while (b) {\n" +
4633 " if (v == null) {\n" +
4634 " o = new Object();\n" +
4635 " };\n" +
4636 " while (o == null) {\n" +
4637 " if (u == null) {\n" +
4638 " v = null;\n" +
4639 " };\n" +
4640 " u = null;\n" +
4641 " }\n" +
4642 " }\n" +
4643 " }\n" +
4644 " }\n" +
4645 "}\n"},
4646 "----------\n" +
4647 "1. ERROR in X.java (at line 11)\n" +
4648 " while (o == null) {\n" +
4649 " ^\n" +
4650 "Null comparison always yields false: The variable o cannot be null at this location\n" +
4651 "----------\n",
4652 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4653 }
4654
4655 // null analysis - while
4656 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
4657 // extraneous error in case of a labeled while(true) statement
test0460_while_explicit_label()4658 public void test0460_while_explicit_label() {
4659 this.runConformTest(
4660 new String[] {
4661 "X.java",
4662 "public class X {\n" +
4663 " void foo(int i) {\n" +
4664 " Object o = null;\n" +
4665 " done: while (true) {\n" +
4666 " switch (i) {\n" +
4667 " case 0:\n" +
4668 " o = new Object();\n" +
4669 " break;\n" +
4670 " case 1:\n" +
4671 " break done;\n" +
4672 " }\n" +
4673 " }\n" +
4674 " if (o == null) {\n" +
4675 " }\n" +
4676 " }\n" +
4677 "}\n"},
4678 "");
4679 }
4680
4681 // null analysis - while
4682 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
4683 // extraneous error in case of a labeled while(true) statement
test0461_while_explicit_label()4684 public void test0461_while_explicit_label() {
4685 this.runConformTest(
4686 new String[] {
4687 "X.java",
4688 "public class X {\n" +
4689 " boolean test() {\n" +
4690 " return true;\n" +
4691 " }\n" +
4692 " void foo() {\n" +
4693 " Object o = null;\n" +
4694 " done: while (true) {\n" +
4695 " if (test()) {\n" +
4696 " break done;\n" +
4697 " }\n" +
4698 " o = new Object();\n" +
4699 " }\n" +
4700 " if (o == null) {\n" +
4701 " }\n" +
4702 " }\n" +
4703 "}\n"},
4704 "");
4705 }
4706
4707 // null analysis - while
4708 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
4709 // variant
test0462_while_explicit_label()4710 public void test0462_while_explicit_label() {
4711 this.runConformTest(
4712 new String[] {
4713 "X.java",
4714 "public class X {\n" +
4715 " boolean test() {\n" +
4716 " return true;\n" +
4717 " }\n" +
4718 " void foo() {\n" +
4719 " Object o = null;\n" +
4720 " done: while (true) {\n" +
4721 " try {\n" +
4722 " while (true) {\n" +
4723 " if (test()) {\n" +
4724 " break done;\n" +
4725 " }\n" +
4726 " }\n" +
4727 " }\n" +
4728 " finally {\n" +
4729 " if (test()) {\n" +
4730 " o = new Object();\n" +
4731 " }\n" +
4732 " }\n" +
4733 " }\n" +
4734 " if (o == null) {\n" +
4735 " }\n" +
4736 " }\n" +
4737 "}\n"},
4738 "");
4739 }
4740
4741 // null analysis - while
4742 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
4743 // variant
test0463_while_infinite()4744 public void test0463_while_infinite() {
4745 this.runConformTest(
4746 new String[] {
4747 "X.java",
4748 "public class X {\n" +
4749 " public void test(String[] a) {\n" +
4750 " String key = null;\n" +
4751 " while(true)\n" +
4752 " {\n" +
4753 " if (a[0] == null)\n" +
4754 " break;\n" +
4755 " key = a[0];\n" +
4756 " }\n" +
4757 " if (key != null) {\n" +
4758 " // empty\n" +
4759 " }\n" +
4760 " }\n" +
4761 "}"},
4762 "");
4763 }
4764
4765 // null analysis - while
4766 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
4767 // variant
test0464_while_infinite()4768 public void test0464_while_infinite() {
4769 this.runConformTest(
4770 new String[] {
4771 "X.java",
4772 "public class X {\n" +
4773 " public void test(String[] a) {\n" +
4774 " String key = null;\n" +
4775 " loop: while(true)\n" +
4776 " {\n" +
4777 " if (a[0] == null)\n" +
4778 " break loop;\n" +
4779 " key = a[0];\n" +
4780 " }\n" +
4781 " if (key != null) {\n" +
4782 " // empty\n" +
4783 " }\n" +
4784 " }\n" +
4785 "}"},
4786 "");
4787 }
4788
4789 // null analysis - while
4790 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
4791 // variant
test0465_while_infinite()4792 public void test0465_while_infinite() {
4793 this.runConformTest(
4794 new String[] {
4795 "X.java",
4796 "public class X {\n" +
4797 " public void test(String[] a) {\n" +
4798 " String key = null;\n" +
4799 " while(true)\n" +
4800 " {\n" +
4801 " if (a[0] == null)\n" +
4802 " break;\n" +
4803 " key = \"non null\";\n" +
4804 " }\n" +
4805 " if (key != null) {\n" +
4806 " // empty\n" +
4807 " }\n" +
4808 " }\n" +
4809 "}"},
4810 "");
4811 }
4812
4813 // null analysis - while
4814 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=198955
4815 // dupe of bug 184298 in fact
test0466_while_infinite()4816 public void test0466_while_infinite() {
4817 this.runConformTest(
4818 new String[] {
4819 "X.java",
4820 "public class X {\n" +
4821 " void foo() {\n" +
4822 " Object o = null;\n" +
4823 " while (true) {\n" +
4824 " if (bar()) {\n" +
4825 " break;\n" +
4826 " }\n" +
4827 " if (o == null) {\n" +
4828 " o = new Object();\n" +
4829 " }\n" +
4830 " }\n" +
4831 " if (o == null) {}\n" +
4832 " }\n" +
4833 " boolean bar() {\n" +
4834 " return false;\n" +
4835 " }\n" +
4836 "}\n"},
4837 "");
4838 }
4839 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=212283
4840 // (which is a dupe of 184298)
test0467_while_break()4841 public void test0467_while_break() {
4842 this.runConformTest(
4843 new String[] {
4844 "X.java",
4845 "public class X {\n" +
4846 " void foo() {\n" +
4847 " RuntimeException e = null;\n" +
4848 " while (e != null || bar()) {\n" +
4849 " if (e != null || bar()) {\n" +
4850 " break;\n" + // always breaks out of the loop if e non-null
4851 " }\n" +
4852 " if (bar()) {\n" +
4853 " e = new RuntimeException();\n" +
4854 " }\n" +
4855 " }\n" +
4856 " if (e != null) {\n" +
4857 " throw e;\n" +
4858 " }\n" +
4859 " }\n" +
4860 " boolean bar() {\n" +
4861 " return false;\n" +
4862 " }\n" +
4863 "}"
4864 },
4865 "");
4866 }
4867 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=212283
4868 // (which is a dupe of 184298)
test0468_while_break()4869 public void test0468_while_break() {
4870 this.runConformTest(
4871 new String[] {
4872 "X.java",
4873 "public class X {\n" +
4874 " void foo() {\n" +
4875 " RuntimeException e = null;\n" +
4876 " while (e != null || bar()) {\n" +
4877 " if (bar()) {\n" +
4878 " break;\n" +
4879 " }\n" +
4880 " if (bar()) {\n" +
4881 " e = new RuntimeException();\n" +
4882 " }\n" +
4883 " }\n" +
4884 " if (e != null) {\n" +
4885 " throw e;\n" +
4886 " }\n" +
4887 " }\n" +
4888 " boolean bar() {\n" +
4889 " return false;\n" +
4890 " }\n" +
4891 "}"
4892 },
4893 "");
4894 }
4895 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=212283
4896 // (which is a dupe of 184298)
test0469_while_break()4897 public void test0469_while_break() {
4898 this.runConformTest(
4899 new String[] {
4900 "X.java",
4901 "public class X {\n" +
4902 " void foo() {\n" +
4903 " RuntimeException e = null;\n" +
4904 " while (e != null || bar()) {\n" +
4905 " if (e != null) {\n" +
4906 " break;\n" +
4907 " }\n" +
4908 " if (bar()) {\n" +
4909 " e = new RuntimeException();\n" +
4910 " }\n" +
4911 " }\n" +
4912 " if (e != null) {\n" +
4913 " throw e;\n" +
4914 " }\n" +
4915 " }\n" +
4916 " boolean bar() {\n" +
4917 " return false;\n" +
4918 " }\n" +
4919 "}"
4920 },
4921 "");
4922 }
4923 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=220788
test0470_while()4924 public void test0470_while() {
4925 runNegativeNullTest(
4926 new String[] {
4927 "X.java",
4928 "public class X {\n" +
4929 " void foo() {\n" +
4930 " Object o = new Object();\n" +
4931 " while (bar()) {\n" +
4932 " if (o != null && o.toString().equals(\"o\")) {\n" +
4933 " }\n" +
4934 " }\n" +
4935 " if (o.toString().equals(\"o\")) {\n" +
4936 " }\n" +
4937 " }\n" +
4938 " boolean bar() {\n" +
4939 " return false;\n" +
4940 " }\n" +
4941 "}"
4942 },
4943 "----------\n" +
4944 "1. ERROR in X.java (at line 5)\n" +
4945 " if (o != null && o.toString().equals(\"o\")) {\n" +
4946 " ^\n" +
4947 "Redundant null check: The variable o cannot be null at this location\n" +
4948 "----------\n");
4949 }
4950 // null analysis -- try/finally
test0500_try_finally()4951 public void test0500_try_finally() {
4952 this.runConformTest(
4953 new String[] {
4954 "X.java",
4955 "public class X {\n" +
4956 " Object m;\n" +
4957 " void foo() {\n" +
4958 " Object o = null;\n" +
4959 " try { /* */ }\n" +
4960 " finally {\n" +
4961 " o = m;\n" +
4962 " }\n" +
4963 " o.toString();\n" +
4964 " }\n" +
4965 "}\n"},
4966 "" // because finally assigns to unknown value
4967 );
4968 }
4969
4970 // null analysis -- try/finally
test0501_try_finally()4971 public void test0501_try_finally() {
4972 this.runNegativeTest(
4973 new String[] {
4974 "X.java",
4975 "public class X {\n" +
4976 " void foo() {\n" +
4977 " Object o = new Object();\n" +
4978 " try { /* */ }\n" +
4979 " finally {\n" +
4980 " o = null;\n" +
4981 " }\n" +
4982 " o.toString();\n" +
4983 " }\n" +
4984 "}\n"},
4985 "----------\n" +
4986 "1. ERROR in X.java (at line 8)\n" +
4987 " o.toString();\n" +
4988 " ^\n" +
4989 "Null pointer access: The variable o can only be null at this location\n" +
4990 "----------\n", // because finally assigns to null
4991 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
4992 }
4993
4994 // null analysis -- try/finally
test0502_try_finally()4995 public void test0502_try_finally() {
4996 this.runConformTest(
4997 new String[] {
4998 "X.java",
4999 "public class X {\n" +
5000 " void foo() {\n" +
5001 " Object o = null;\n" +
5002 " try {\n" +
5003 " System.out.println();\n" + // might throw a runtime exception
5004 " o = new Object();\n" +
5005 " }\n" +
5006 " finally { /* */ }\n" +
5007 " o.toString();\n" +
5008 // still OK because in case of exception this code is
5009 // not reached
5010 " }\n" +
5011 "}\n"},
5012 "");
5013 }
5014
5015 // null analysis -- try/finally
test0503_try_finally()5016 public void test0503_try_finally() {
5017 this.runNegativeTest(
5018 new String[] {
5019 "X.java",
5020 "public class X {\n" +
5021 " void foo(X x) {\n" +
5022 " x = null;\n" +
5023 " try {\n" +
5024 " x = null;\n" + // complain, already null
5025 " } finally { /* */ }\n" +
5026 " }\n" +
5027 "}\n"},
5028 "----------\n" +
5029 "1. ERROR in X.java (at line 5)\n" +
5030 " x = null;\n" +
5031 " ^\n" +
5032 "Redundant assignment: The variable x can only be null at this location\n" +
5033 "----------\n",
5034 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5035 }
5036
5037 // null analysis -- try/finally
test0504_try_finally()5038 public void test0504_try_finally() {
5039 this.runNegativeTest(
5040 new String[] {
5041 "X.java",
5042 "public class X {\n" +
5043 " void foo(X x) {\n" +
5044 " x = null;\n" +
5045 " try {\n" +
5046 " } finally {\n" +
5047 " if (x != null) { /* */ }\n" + // complain null
5048 " }\n" +
5049 " }\n" +
5050 "}\n"},
5051 "----------\n" +
5052 "1. ERROR in X.java (at line 6)\n" +
5053 " if (x != null) { /* */ }\n" +
5054 " ^\n" +
5055 "Null comparison always yields false: The variable x can only be null at this location\n" +
5056 "----------\n",
5057 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5058 }
5059
5060 // null analysis -- try/finally
5061 // origin: AssignmentTest#test017
5062 // The whole issue here is whether or not to detect premature exits.
5063 // Previously, we followed JLS's conservative approach, which considers
5064 // that the try block may exit before the assignment is completed.
5065 // As of Bug 345305 this has been changed to a more accurate analysis.
test0505_try_finally()5066 public void test0505_try_finally() {
5067 runNegativeNullTest(
5068 new String[] {
5069 "X.java",
5070 "public class X {\n" +
5071 " void foo(X x) {\n" +
5072 " x = this;\n" + // 1
5073 " try {\n" +
5074 " x = null;\n" +
5075 " } finally {\n" +
5076 " if (x == null) {/* */}\n" + // 2
5077 " }\n" +
5078 " }\n" +
5079 "}\n"},
5080 "----------\n" +
5081 "1. ERROR in X.java (at line 7)\n" +
5082 " if (x == null) {/* */}\n" +
5083 " ^\n" +
5084 "Redundant null check: The variable x can only be null at this location\n" +
5085 "----------\n");
5086 }
5087
5088 // null analysis -- try finally
test0506_try_finally()5089 public void test0506_try_finally() {
5090 this.runNegativeTest(
5091 new String[] {
5092 "X.java",
5093 "public class X {\n" +
5094 " void foo(Object o) {\n" +
5095 " try { /* */ }\n" +
5096 " finally {\n" +
5097 " o = new Object();\n" +
5098 " }\n" +
5099 " if (o == null) { /* */ }\n" +
5100 " }\n" +
5101 "}\n"},
5102 "----------\n" +
5103 "1. ERROR in X.java (at line 7)\n" +
5104 " if (o == null) { /* */ }\n" +
5105 " ^\n" +
5106 "Null comparison always yields false: The variable o cannot be null at this location\n" +
5107 "----------\n" +
5108 "2. WARNING in X.java (at line 7)\n" +
5109 " if (o == null) { /* */ }\n" +
5110 " ^^^^^^^^^\n" +
5111 "Dead code\n" +
5112 "----------\n",
5113 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5114 }
5115
5116 // null analysis -- try finally
test0507_try_finally()5117 public void test0507_try_finally() {
5118 this.runNegativeTest(
5119 new String[] {
5120 "X.java",
5121 "public class X {\n" +
5122 " void foo(Object o, boolean b) {\n" +
5123 " try { /* */ }\n" +
5124 " finally {\n" +
5125 " o.toString();\n" + // protect
5126 " }\n" +
5127 " if (o == null) {\n" + // complain
5128 " o = new Object();\n" +
5129 " }\n" +
5130 " }\n" +
5131 "}\n"},
5132 "----------\n" +
5133 "1. ERROR in X.java (at line 7)\n" +
5134 " if (o == null) {\n" +
5135 " ^\n" +
5136 "Null comparison always yields false: The variable o cannot be null at this location\n" +
5137 "----------\n" +
5138 "2. WARNING in X.java (at line 7)\n" +
5139 " if (o == null) {\n" +
5140 " o = new Object();\n" +
5141 " }\n" +
5142 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
5143 "Dead code\n" +
5144 "----------\n",
5145 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5146 }
5147
5148 // null analysis -- try finally
test0508_try_finally()5149 public void test0508_try_finally() {
5150 this.runNegativeTest(
5151 new String[] {
5152 "X.java",
5153 "public class X {\n" +
5154 " void foo(Object o) {\n" +
5155 " o = null;" +
5156 " try { /* */ }\n" +
5157 " finally {\n" +
5158 " o.toString();\n" + // complain and protect
5159 " o.toString();\n" + // quiet
5160 " }\n" +
5161 " o.toString();\n" + // quiet
5162 " }\n" +
5163 "}\n"},
5164 "----------\n" +
5165 "1. ERROR in X.java (at line 5)\n" +
5166 " o.toString();\n" +
5167 " ^\n" +
5168 "Null pointer access: The variable o can only be null at this location\n" +
5169 "----------\n",
5170 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5171 }
5172
5173 // null analysis -- try finally
test0509_try_finally_embedded()5174 public void test0509_try_finally_embedded() {
5175 this.runNegativeTest(
5176 new String[] {
5177 "X.java",
5178 "public class X {\n" +
5179 " void foo(Object o1) {\n" +
5180 " Object o2 = null;" +
5181 " while (true) {\n" +
5182 " // o2 = o1;\n" +
5183 " try { /* */ }\n" +
5184 " finally {\n" +
5185 " o2.toString();\n" + // complain and protect
5186 " o2.toString();\n" + // quiet
5187 " }\n" +
5188 " o2.toString();\n" + // quiet
5189 " }\n" +
5190 " }\n" +
5191 "}\n"},
5192 "----------\n" +
5193 "1. ERROR in X.java (at line 7)\n" +
5194 " o2.toString();\n" +
5195 " ^^\n" +
5196 "Null pointer access: The variable o2 can only be null at this location\n" +
5197 "----------\n",
5198 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5199 }
5200
5201 // null analysis -- try finally
test0510_try_finally()5202 public void test0510_try_finally() {
5203 this.runConformTest(
5204 new String[] {
5205 "X.java",
5206 "public class X {\n" +
5207 " void bar() throws Exception {\n" +
5208 " // empty\n" +
5209 " }\n" +
5210 " void foo(Object o, boolean b) throws Exception {\n" +
5211 " try {\n" +
5212 " bar();\n" +
5213 " if (b) {\n" +
5214 " o.toString();\n" +
5215 " }\n" +
5216 " }\n" +
5217 " finally {\n" +
5218 " if (o != null) {\n" +
5219 " o.toString();\n" +
5220 " }\n" +
5221 " }\n" +
5222 " }\n" +
5223 "}\n"},
5224 "");
5225 }
5226
5227 // null analysis -- try finally
test0511_try_finally()5228 public void test0511_try_finally() {
5229 this.runConformTest(
5230 new String[] {
5231 "X.java",
5232 "public class X {\n" +
5233 " void foo(Object o1, boolean b) {\n" +
5234 " Object o2 = null;\n" +
5235 " if (b) {\n" +
5236 " o2 = new Object();\n" +
5237 " }\n" + // 0011
5238 " try { /* */ }\n" +
5239 " finally {\n" +
5240 " o2 = o1;\n" + // 1011
5241 " }\n" +
5242 " o2.toString();\n" + // 1011 -- quiet
5243 " }\n" +
5244 "}\n"},
5245 "");
5246 }
5247
5248 // null analysis -- try/finally
test0512_try_finally()5249 public void test0512_try_finally() {
5250 this.runNegativeTest(
5251 new String[] {
5252 "X.java",
5253 "public class X {\n" +
5254 " void foo(X x) {\n" +
5255 " x = null;\n" +
5256 " try {\n" +
5257 " x = new X();\n" +
5258 " } finally {\n" +
5259 " x.toString();\n" +
5260 " }\n" +
5261 " }\n" +
5262 "}\n"},
5263 "----------\n" +
5264 "1. ERROR in X.java (at line 7)\n" +
5265 " x.toString();\n" +
5266 " ^\n" +
5267 "Potential null pointer access: The variable x may be null at this location\n" +
5268 "----------\n",
5269 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5270 }
5271
5272 // null analysis -- try/finally
5273 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128547
test0513_try_finally()5274 public void test0513_try_finally() {
5275 this.runConformTest(
5276 new String[] {
5277 "X.java",
5278 "public class X {\n" +
5279 " X bar() {\n" +
5280 " return null;\n" +
5281 " }\n" +
5282 " Object foo() {\n" +
5283 " X x = null;\n" +
5284 " try {\n" +
5285 " x = bar();\n" +
5286 " x.toString();\n" +
5287 " return x;\n" +
5288 " } finally {\n" +
5289 " if (x != null) {\n" +
5290 " x.toString();\n" +
5291 " }\n" +
5292 " }\n" +
5293 " }\n" +
5294 "}\n"},
5295 "");
5296 }
5297
5298 // null analysis -- try/finally
5299 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128547
5300 // embedded variant 1
test0514_try_finally()5301 public void test0514_try_finally() {
5302 this.runConformTest(
5303 new String[] {
5304 "X.java",
5305 "public class X {\n" +
5306 " X bar() {\n" +
5307 " return null;\n" +
5308 " }\n" +
5309 " Object foo() {\n" +
5310 " X x = null;\n" +
5311 " try {\n" +
5312 " try {\n" +
5313 " x = bar();\n" +
5314 " x.toString();\n" +
5315 " return x;\n" +
5316 " }\n" +
5317 " finally {\n" +
5318 " }\n" +
5319 " }\n" +
5320 " finally {\n" +
5321 " if (x != null) {\n" +
5322 " x.toString();\n" +
5323 " }\n" +
5324 " }\n" +
5325 " }\n" +
5326 "}\n"},
5327 "");
5328 }
5329
5330 // null analysis -- try/finally
5331 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128547
5332 // embedded variant 2
test0515_try_finally()5333 public void test0515_try_finally() {
5334 this.runConformTest(
5335 new String[] {
5336 "X.java",
5337 "public class X {\n" +
5338 " X bar() {\n" +
5339 " return null;\n" +
5340 " }\n" +
5341 " Object foo() {\n" +
5342 " X x = null;\n" +
5343 " try {\n" +
5344 " try {\n" +
5345 " x = bar();\n" +
5346 " x.toString();\n" +
5347 " return x;\n" +
5348 " }\n" +
5349 " finally {\n" +
5350 " System.out.println();\n" +
5351 " }\n" +
5352 " }\n" +
5353 " finally {\n" +
5354 " if (x != null) {\n" +
5355 " x.toString();\n" +
5356 " }\n" +
5357 " }\n" +
5358 " }\n" +
5359 "}\n"},
5360 "");
5361 }
5362
5363 // null analysis -- try/finally
5364 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128547
5365 // variant
5366 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184546
5367 // variant
test0516_try_finally()5368 public void test0516_try_finally() {
5369 this.runConformTest(
5370 new String[] {
5371 "X.java",
5372 "public class X {\n" +
5373 " Object foo() {\n" +
5374 " X x = null;\n" +
5375 " try {\n" +
5376 " x = new X();\n" +
5377 " return x;\n" +
5378 " }\n" +
5379 " finally {\n" +
5380 " if (x != null) {\n" +
5381 " x.toString();\n" +
5382 " }\n" +
5383 " }\n" +
5384 " }\n" +
5385 "}\n"},
5386 "");
5387 }
5388
5389 // null analysis -- try/finally
5390 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=132072
5391 // AIOOBE in null check compiling com.sun.org.apache.xalan.internal.res.XSLTErrorResources from JDK 1.5 source
test0517_try_finally()5392 public void test0517_try_finally() {
5393 this.runConformTest(
5394 new String[] {
5395 "X.java",
5396 "public class X {\n" +
5397 " Object foo() {\n" +
5398 " String s00, s01, s02, s03, s04, s05, s06, s07, s08, s09;\n" +
5399 " String s10, s11, s12, s13, s14, s15, s16, s17, s18, s19;\n" +
5400 " String s20, s21, s22, s23, s24, s25, s26, s27, s28, s29;\n" +
5401 " String s30, s31, s32, s33, s34, s35, s36, s37, s38, s39;\n" +
5402 " String s40, s41, s42, s43, s44, s45, s46, s47, s48, s49;\n" +
5403 " String s50, s51, s52, s53, s54, s55, s56, s57, s58, s59;\n" +
5404 " String s60, s61, s62, s63, s64, s65, s66, s67, s68, s69;\n" +
5405 " String s100, s101, s102, s103, s104, s105, s106, s107, s108, s109;\n" +
5406 " String s110, s111, s112, s113, s114, s115, s116, s117, s118, s119;\n" +
5407 " String s120, s121, s122, s123, s124, s125, s126, s127, s128, s129;\n" +
5408 " String s130, s131, s132, s133, s134, s135, s136, s137, s138, s139;\n" +
5409 " String s140, s141, s142, s143, s144, s145, s146, s147, s148, s149;\n" +
5410 " String s150, s151, s152, s153, s154, s155, s156, s157, s158, s159;\n" +
5411 " String s160, s161, s162, s163, s164, s165, s166, s167, s168, s169;\n" +
5412 " String s200, s201, s202, s203, s204, s205, s206, s207, s208, s209;\n" +
5413 " String s210, s211, s212, s213, s214, s215, s216, s217, s218, s219;\n" +
5414 " String s220, s221, s222, s223, s224, s225, s226, s227, s228, s229;\n" +
5415 " String s230, s231, s232, s233, s234, s235, s236, s237, s238, s239;\n" +
5416 " String s240, s241, s242, s243, s244, s245, s246, s247, s248, s249;\n" +
5417 " String s250, s251, s252, s253, s254, s255, s256, s257, s258, s259;\n" +
5418 " String s260, s261, s262, s263, s264, s265, s266, s267, s268, s269;\n" +
5419 " X x = new X();\n" +
5420 " try {\n" +
5421 " return x;\n" +
5422 " }\n" +
5423 " finally {\n" +
5424 " }\n" +
5425 " }\n" +
5426 "}\n"},
5427 "");
5428 }
5429
5430 // null analysis -- try/finally
5431 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=132120
5432 // [compiler][null] NPE batch compiling JDT/Core from HEAD
test0518_try_finally()5433 public void test0518_try_finally() {
5434 this.runNegativeTest(
5435 new String[] {
5436 "X.java",
5437 "public class X {\n" +
5438 " void foo() {\n" +
5439 " String s00, s01, s02, s03, s04, s05, s06, s07, s08, s09;\n" +
5440 " String s10, s11, s12, s13, s14, s15, s16, s17, s18, s19;\n" +
5441 " String s20, s21, s22, s23, s24, s25, s26, s27, s28, s29;\n" +
5442 " String s30, s31, s32, s33, s34, s35, s36, s37, s38, s39;\n" +
5443 " String s40, s41, s42, s43, s44, s45, s46, s47, s48, s49;\n" +
5444 " String s50, s51, s52, s53, s54, s55, s56, s57, s58, s59;\n" +
5445 " String s60, s61, s62, s63, s64, s65, s66, s67, s68, s69;\n" +
5446 " String s100, s101, s102, s103, s104, s105, s106, s107, s108, s109;\n" +
5447 " String s110, s111, s112, s113, s114, s115, s116, s117, s118, s119;\n" +
5448 " String s120, s121, s122, s123, s124, s125, s126, s127, s128, s129;\n" +
5449 " String s130, s131, s132, s133, s134, s135, s136, s137, s138, s139;\n" +
5450 " String s140, s141, s142, s143, s144, s145, s146, s147, s148, s149;\n" +
5451 " String s150, s151, s152, s153, s154, s155, s156, s157, s158, s159;\n" +
5452 " String s160, s161, s162, s163, s164, s165, s166, s167, s168, s169;\n" +
5453 " String s200, s201, s202, s203, s204, s205, s206, s207, s208, s209;\n" +
5454 " String s210, s211, s212, s213, s214, s215, s216, s217, s218, s219;\n" +
5455 " String s220, s221, s222, s223, s224, s225, s226, s227, s228, s229;\n" +
5456 " String s230, s231, s232, s233, s234, s235, s236, s237, s238, s239;\n" +
5457 " String s240, s241, s242, s243, s244, s245, s246, s247, s248, s249;\n" +
5458 " String s250, s251, s252, s253, s254, s255, s256, s257, s258, s259;\n" +
5459 " String s260, s261, s262, s263, s264, s265, s266, s267, s268, s269;\n" +
5460 " X x = null;\n" +
5461 " try {\n" +
5462 " x = new X();\n" +
5463 " } finally {\n" +
5464 " x.toString();\n" +
5465 " }\n" +
5466 " }\n" +
5467 "}\n"},
5468 "----------\n" +
5469 "1. ERROR in X.java (at line 28)\n" +
5470 " x.toString();\n" +
5471 " ^\n" +
5472 "Potential null pointer access: The variable x may be null at this location\n" +
5473 "----------\n",
5474 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5475 }
5476
5477 // null analysis -- try/finally
5478 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128962
5479 // incorrect analysis within try finally with a constructor throwing an exception
test0519_try_finally_constructor_exc()5480 public void test0519_try_finally_constructor_exc() {
5481 this.runConformTest(
5482 new String[] {
5483 "X.java",
5484 "public class X {\n" +
5485 " public void foo(Y y) throws E {\n" +
5486 " try {\n" +
5487 " new Y();\n" +
5488 " y.toString();\n" + // should be quiet
5489 " } finally {\n" +
5490 " y = null;\n" +
5491 " }\n" +
5492 " }\n" +
5493 "}\n" +
5494 "class Y {\n" +
5495 " Y() throws E {\n" +
5496 " }\n" +
5497 "}\n" +
5498 "class E extends Exception {\n" +
5499 " private static final long serialVersionUID = 1L;\n" +
5500 "}\n"},
5501 "");
5502 }
5503
5504 // null analysis -- try/finally
5505 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=128962
5506 // incorrect analysis within try finally with a constructor throwing an exception
5507 // variant
test0520_try_finally_constructor_exc()5508 public void test0520_try_finally_constructor_exc() {
5509 this.runConformTest(
5510 new String[] {
5511 "X.java",
5512 "public class X {\n" +
5513 " public void foo(Y y) throws E { \n" +
5514 " try { \n" +
5515 " new Y() {\n" +
5516 " void bar() {\n" +
5517 " // do nothing\n" +
5518 " }\n" +
5519 " }; \n" +
5520 " y.toString();\n" +
5521 " } finally { \n" +
5522 " y = null; \n" +
5523 " } \n" +
5524 " } \n" +
5525 "}\n" +
5526 "abstract class Y {\n" +
5527 " Y() throws E { \n" +
5528 " }\n" +
5529 " abstract void bar();\n" +
5530 "} \n" +
5531 "class E extends Exception {\n" +
5532 " private static final long serialVersionUID = 1L;\n" +
5533 "}"},
5534 "");
5535 }
5536
5537 // null analysis -- try/finally
5538 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=149665
5539 // incorrect analysis within try finally with an embedded && expression
test0521_try_finally()5540 public void test0521_try_finally() {
5541 this.runConformTest(
5542 new String[] {
5543 "X.java",
5544 "public class X\n" +
5545 "{\n" +
5546 " X m;\n" +
5547 " public void foo() {\n" +
5548 " for(int j = 0; j < 10; j++) {\n" +
5549 " try {\n" +
5550 " j++;\n" +
5551 " } finally {\n" +
5552 " X t = m;\n" +
5553 " if( t != null && t.bar()) {\n" +
5554 " }\n" +
5555 " }\n" +
5556 " }\n" +
5557 " }\n" +
5558 " boolean bar() {\n" +
5559 " return false;\n" +
5560 " }\n" +
5561 "}"},
5562 "");
5563 }
5564
5565 // null analysis -- try/finally
5566 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=149665
5567 // variant
test0522_try_finally()5568 public void test0522_try_finally() {
5569 this.runNegativeTest(
5570 new String[] {
5571 "X.java",
5572 "public class X\n" +
5573 "{\n" +
5574 " X m;\n" +
5575 " public void foo() {\n" +
5576 " for(int j = 0; j < 10; j++) {\n" +
5577 " try {\n" +
5578 " j++;\n" +
5579 " } finally {\n" +
5580 " X t = null;\n" +
5581 " if(t.bar()) {\n" +
5582 " }\n" +
5583 " }\n" +
5584 " }\n" +
5585 " }\n" +
5586 " boolean bar() {\n" +
5587 " return false;\n" +
5588 " }\n" +
5589 "}"},
5590 "----------\n" +
5591 "1. ERROR in X.java (at line 10)\n" +
5592 " if(t.bar()) {\n" +
5593 " ^\n" +
5594 "Null pointer access: The variable t can only be null at this location\n" +
5595 "----------\n",
5596 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5597 }
5598
5599 // null analysis -- try/finally
5600 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=149665
5601 // variant
test0523_try_finally()5602 public void test0523_try_finally() {
5603 this.runNegativeTest(
5604 new String[] {
5605 "X.java",
5606 "public class X\n" +
5607 "{\n" +
5608 " X m;\n" +
5609 " public void foo() {\n" +
5610 " for(int j = 0; j < 10; j++) {\n" +
5611 " try {\n" +
5612 " j++;\n" +
5613 " } finally {\n" +
5614 " X t = m;\n" +
5615 " if(t == null ? false : (t == null ? false : t.bar())) {\n" +
5616 " }\n" +
5617 " }\n" +
5618 " }\n" +
5619 " }\n" +
5620 " boolean bar() {\n" +
5621 " return false;\n" +
5622 " }\n" +
5623 "}"},
5624 "----------\n" +
5625 "1. ERROR in X.java (at line 10)\n" +
5626 " if(t == null ? false : (t == null ? false : t.bar())) {\n" +
5627 " ^\n" +
5628 "Null comparison always yields false: The variable t cannot be null at this location\n" +
5629 "----------\n",
5630 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5631 }
5632
5633 // null analysis -- try/finally
5634 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=149665
5635 // variant
test0524_try_finally()5636 public void test0524_try_finally() {
5637 this.runNegativeTest(
5638 new String[] {
5639 "X.java",
5640 "public class X\n" +
5641 "{\n" +
5642 " X m;\n" +
5643 " public void foo() {\n" +
5644 " for(int j = 0; j < 10; j++) {\n" +
5645 " try {\n" +
5646 " j++;\n" +
5647 " } finally {\n" +
5648 " X t = m;\n" +
5649 " if(t != null ? false : (t == null ? false : t.bar())) {\n" +
5650 " }\n" +
5651 " }\n" +
5652 " }\n" +
5653 " }\n" +
5654 " boolean bar() {\n" +
5655 " return false;\n" +
5656 " }\n" +
5657 "}"},
5658 "----------\n" +
5659 "1. ERROR in X.java (at line 10)\n" +
5660 " if(t != null ? false : (t == null ? false : t.bar())) {\n" +
5661 " ^\n" +
5662 "Redundant null check: The variable t can only be null at this location\n" +
5663 "----------\n",
5664 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5665 }
5666
5667 // null analysis -- try/finally
5668 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=150082
_test0525_try_finally_unchecked_exception()5669 public void _test0525_try_finally_unchecked_exception() {
5670 this.runNegativeTest(
5671 new String[] {
5672 "X.java",
5673 "public class X\n" +
5674 "{\n" +
5675 " String foo(Object p) {\n" +
5676 " String s = null;\n" +
5677 " Object o = null;\n" +
5678 " try {\n" +
5679 " o = p;\n" +
5680 " if (o == null) {\n" +
5681 " return null;\n" +
5682 " }\n" +
5683 " s = o.getClass().getName();\n" +
5684 " } catch (RuntimeException e) {\n" +
5685 " o.toString();\n" +
5686 " s = null;\n" +
5687 " } finally {\n" +
5688 " if (o != null) {\n" +
5689 " }\n" +
5690 " }\n" +
5691 " return s;\n" +
5692 " }\n" +
5693 "}"},
5694 "----------\n" +
5695 "1. ERROR in X.java (at line 13)\n" +
5696 " o.toString();\n" +
5697 " ^\n" +
5698 "Potential null pointer access: The variable o may be null at this location\n" +
5699 "----------\n",
5700 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5701 }
5702
5703 // null analysis -- try/finally
5704 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=150082
5705 // variant
test0526_try_finally_unchecked_exception()5706 public void test0526_try_finally_unchecked_exception() {
5707 this.runNegativeTest(
5708 new String[] {
5709 "X.java",
5710 "public class X\n" +
5711 "{\n" +
5712 " String foo(Object p) {\n" +
5713 " String s = null;\n" +
5714 " Object o = p;\n" +
5715 " try {\n" +
5716 " if (o == null) {\n" + // shades doubts upon o
5717 " return null;\n" + // may throw a RuntimeException by spec
5718 " }\n" +
5719 " s = o.getClass().getName();\n" +
5720 " } catch (RuntimeException e) {\n" +
5721 " o.toString();\n" +
5722 " s = null;\n" +
5723 " } finally {\n" +
5724 " if (o != null) {\n" +
5725 " }\n" +
5726 " }\n" +
5727 " return s;\n" +
5728 " }\n" +
5729 "}"},
5730 "----------\n" +
5731 "1. ERROR in X.java (at line 12)\n" +
5732 " o.toString();\n" +
5733 " ^\n" +
5734 "Potential null pointer access: The variable o may be null at this location\n" +
5735 "----------\n",
5736 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5737 }
5738
5739 //null analysis -- try/finally
5740 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=150082
5741 //variant
test0527_try_finally_unchecked_exception()5742 public void test0527_try_finally_unchecked_exception() {
5743 this.runNegativeTest(
5744 new String[] {
5745 "X.java",
5746 "public class X\n" +
5747 "{\n" +
5748 " String foo(Object p) {\n" +
5749 " String s = null;\n" +
5750 " Object o = p;\n" +
5751 " try {\n" +
5752 " if (o == null) {\n" + // shades doubts upon o
5753 " return null;\n" + // may throw a RuntimeException by spec
5754 " }\n" +
5755 " s = o.getClass().getName();\n" +
5756 " } catch (RuntimeException e) {\n" +
5757 " o.toString();\n" +
5758 " s = null;\n" +
5759 " }\n" +
5760 " return s;\n" +
5761 " }\n" +
5762 "}"},
5763 "----------\n" +
5764 "1. ERROR in X.java (at line 12)\n" +
5765 " o.toString();\n" +
5766 " ^\n" +
5767 "Potential null pointer access: The variable o may be null at this location\n" +
5768 "----------\n",
5769 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5770 }
5771
5772 // null analysis -- try/finally
5773 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=158000
test0528_try_finally()5774 public void test0528_try_finally() {
5775 this.runNegativeTest(
5776 new String[] {
5777 "X.java",
5778 "public class X {\n" +
5779 " void foo(X x) {\n" +
5780 " x = null;\n" +
5781 " X y = null;\n" +
5782 " try {\n" +
5783 " } finally {\n" +
5784 " if (x != null) { /* */ }\n" + // complain null
5785 " if (y != null) { /* */ }\n" + // complain null as well
5786 " }\n" +
5787 " }\n" +
5788 "}\n"},
5789 "----------\n" +
5790 "1. ERROR in X.java (at line 7)\n" +
5791 " if (x != null) { /* */ }\n" +
5792 " ^\n" +
5793 "Null comparison always yields false: The variable x can only be null at this location\n" +
5794 "----------\n" +
5795 "2. ERROR in X.java (at line 8)\n" +
5796 " if (y != null) { /* */ }\n" +
5797 " ^\n" +
5798 "Null comparison always yields false: The variable y can only be null at this location\n" +
5799 "----------\n",
5800 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5801 }
5802
5803 // null analysis -- try finally
5804 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=158000
test0529_try_finally()5805 public void test0529_try_finally() {
5806 this.runNegativeTest(
5807 new String[] {
5808 "X.java",
5809 "public class X {\n" +
5810 " void foo(Object o) {\n" +
5811 " o = null;\n" +
5812 " Object o2 = null;\n" +
5813 " try { /* */ }\n" +
5814 " finally {\n" +
5815 " o.toString();\n" + // complain
5816 " o2.toString();\n" + // complain
5817 " }\n" +
5818 " }\n" +
5819 "}\n"},
5820 "----------\n" +
5821 "1. ERROR in X.java (at line 7)\n" +
5822 " o.toString();\n" +
5823 " ^\n" +
5824 "Null pointer access: The variable o can only be null at this location\n" +
5825 "----------\n" +
5826 "2. ERROR in X.java (at line 8)\n" +
5827 " o2.toString();\n" +
5828 " ^^\n" +
5829 "Null pointer access: The variable o2 can only be null at this location\n" +
5830 "----------\n",
5831 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5832 }
5833
5834 // null analysis -- try/finally
5835 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=158000
test0530_try_finally()5836 public void test0530_try_finally() {
5837 this.runNegativeTest(
5838 new String[] {
5839 "X.java",
5840 "public class X {\n" +
5841 " void foo(X x) {\n" +
5842 " x = null;\n" +
5843 " X y = null;\n" +
5844 " try {\n" +
5845 " x = new X();\n" +
5846 " } finally {\n" +
5847 " x.toString();\n" +
5848 " y.toString();\n" + // complain
5849 " }\n" +
5850 " }\n" +
5851 "}\n"},
5852 "----------\n" +
5853 "1. ERROR in X.java (at line 8)\n" +
5854 " x.toString();\n" +
5855 " ^\n" +
5856 "Potential null pointer access: The variable x may be null at this location\n" +
5857 "----------\n" +
5858 "2. ERROR in X.java (at line 9)\n" +
5859 " y.toString();\n" +
5860 " ^\n" +
5861 "Null pointer access: The variable y can only be null at this location\n" +
5862 "----------\n",
5863 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5864 }
5865
5866 // null analysis -- try/finally
5867 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=158000
test0531_try_finally()5868 public void test0531_try_finally() {
5869 this.runNegativeTest(
5870 new String[] {
5871 "X.java",
5872 "public class X {\n" +
5873 " void foo() {\n" +
5874 " X x = new X();\n" +
5875 " X y = null;\n" +
5876 " try {\n" +
5877 " } finally {\n" +
5878 " if (x != null) {\n" +
5879 " x.toString();\n" +
5880 " }\n" +
5881 " y.toString();\n" + // complain
5882 " }\n" +
5883 " }\n" +
5884 "}\n"},
5885 "----------\n" +
5886 "1. ERROR in X.java (at line 7)\n" +
5887 " if (x != null) {\n" +
5888 " ^\n" +
5889 "Redundant null check: The variable x cannot be null at this location\n" +
5890 "----------\n" +
5891 "2. ERROR in X.java (at line 10)\n" +
5892 " y.toString();\n" +
5893 " ^\n" +
5894 "Null pointer access: The variable y can only be null at this location\n" +
5895 "----------\n",
5896 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
5897 }
5898
5899 // null analysis -- try/finally
5900 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=177863
test0532_try_finally()5901 public void test0532_try_finally() {
5902 this.runConformTest(
5903 new String[] {
5904 "X.java",
5905 "public class X {\n" +
5906 " public void foo() {\n" +
5907 " Object o = null;\n" +
5908 " try {\n" +
5909 " } finally {\n" +
5910 " o = Object.class.getClass();\n" +
5911 " o.getClass();\n" +
5912 " }\n" +
5913 " }\n" +
5914 "}"},
5915 "");
5916 }
5917
5918 // null analysis -- try/finally
5919 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184546
test0533_try_finally_field()5920 public void test0533_try_finally_field() {
5921 this.runConformTest(
5922 new String[] {
5923 "X.java",
5924 "public class X {\n" +
5925 " static char SHOULD_NOT_MATTER = '?';\n" +
5926 " Object foo() {\n" +
5927 " X x = null;\n" +
5928 " try {\n" +
5929 " x = new X();\n" +
5930 " return x;\n" +
5931 " }\n" +
5932 " finally {\n" +
5933 " if (x != null) {\n" +
5934 " x.toString();\n" +
5935 " }\n" +
5936 " }\n" +
5937 " }\n" +
5938 "}\n"},
5939 "");
5940 }
5941
5942 // null analysis - try finally
5943 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=198970
_test0534_try_finally()5944 public void _test0534_try_finally() {
5945 runTest(
5946 new String[] {
5947 "X.java",
5948 "public class X {\n" +
5949 " public static void main(String[] args) {\n" +
5950 " String foo = null;\n" +
5951 " boolean a = true;\n" +
5952 " try {\n" +
5953 " }\n" +
5954 " catch(Exception e) {\n" +
5955 " }\n" +
5956 " finally {\n" +
5957 " if (a) {\n" +
5958 " foo = new String();\n" +
5959 " }\n" +
5960 " if (foo != null) {\n" +
5961 " }\n" +
5962 " }\n" +
5963 " }\n" +
5964 "}\n"
5965 },
5966 false /* expectingCompilerErrors */,
5967 "" /* expectedCompilerLog */,
5968 "" /* expectedOutputString */,
5969 "" /* expectedErrorString */,
5970 false /* forceExecution */,
5971 null /* classLib */,
5972 true /* shouldFlushOutputDirectory */,
5973 null /* vmArguments */,
5974 null /* customOptions */,
5975 null /* clientRequestor */,
5976 false /* skipJavac */);
5977 }
5978
5979 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=295260
test0535_try_finally()5980 public void test0535_try_finally() {
5981 this.runConformTest(
5982 new String[] {
5983 "X.java",
5984 "public class X {\n" +
5985 " public void test3(String[] args) {\n" +
5986 " while (true) {\n" +
5987 " Object a = null;\n" +
5988 " try {\n" +
5989 " a = new Object();\n" +
5990 " } catch (Exception e) {\n" +
5991 " } finally {\n" +
5992 " if (a != null)\n" +
5993 " a = null;\n" + // quiet
5994 " }\n" +
5995 " }\n" +
5996 " }\n"+
5997 "}",
5998 },
5999 "");
6000 }
6001
6002 // null analysis -- try/finally
6003 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=320170 - [compiler] [null] Whitebox issues in null analysis
6004 // trigger nullbits 0111 (pot n|nn|un), don't let "definitely unknown" override previous information
test0536_try_finally()6005 public void test0536_try_finally() {
6006 this.runNegativeTest(
6007 new String[] {
6008 "X.java",
6009 "public class X {\n" +
6010 " X bar () { return null; }\n" +
6011 " void foo() {\n" +
6012 " X x = new X();\n" +
6013 " try {\n" +
6014 " x = null;\n" +
6015 " x = new X();\n" + // if this throws an exception finally finds x==null
6016 " x = bar();\n" +
6017 " } finally {\n" +
6018 " x.toString();\n" + // complain
6019 " }\n" +
6020 " }\n" +
6021 "}\n"},
6022 "----------\n" +
6023 "1. ERROR in X.java (at line 10)\n" +
6024 " x.toString();\n" +
6025 " ^\n" +
6026 "Potential null pointer access: The variable x may be null at this location\n" +
6027 "----------\n",
6028 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6029 }
6030
6031 // null analysis -- try/finally
6032 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=320170 - [compiler] [null] Whitebox issues in null analysis
6033 // trigger nullbits 0111 (pot n|nn|un), don't let "definitely unknown" override previous information
6034 // multiple variables
test0537_try_finally()6035 public void test0537_try_finally() {
6036 this.runNegativeTest(
6037 new String[] {
6038 "X.java",
6039 "public class X {\n" +
6040 " X bar () { return null; }\n" +
6041 " void foo() {\n" +
6042 " X x1 = new X();\n" +
6043 " X x2 = new X();\n" +
6044 " X x3 = new X();\n" +
6045 " try {\n" +
6046 " x1 = null;\n" +
6047 " x2 = null;\n" +
6048 " x1 = new X();\n" + // if this throws an exception finally finds x1==null
6049 " x2 = new X();\n" + // if this throws an exception finally finds x2==null
6050 " x3 = new X();\n" + // if this throws an exception finally still finds x3!=null
6051 " x1 = bar();\n" +
6052 " x2 = bar();\n" +
6053 " } finally {\n" +
6054 " x1.toString();\n" + // complain
6055 " x2.toString();\n" + // complain
6056 " x3.toString();\n" + // don't complain
6057 " }\n" +
6058 " }\n" +
6059 "}\n"},
6060 "----------\n" +
6061 "1. ERROR in X.java (at line 16)\n" +
6062 " x1.toString();\n" +
6063 " ^^\n" +
6064 "Potential null pointer access: The variable x1 may be null at this location\n" +
6065 "----------\n" +
6066 "2. ERROR in X.java (at line 17)\n" +
6067 " x2.toString();\n" +
6068 " ^^\n" +
6069 "Potential null pointer access: The variable x2 may be null at this location\n" +
6070 "----------\n",
6071 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6072 }
6073
6074 // null analysis -- try/catch
test0550_try_catch()6075 public void test0550_try_catch() {
6076 this.runConformTest(
6077 new String[] {
6078 "X.java",
6079 "public class X {\n" +
6080 " void foo() {\n" +
6081 " Object o = null;\n" +
6082 " try {\n" +
6083 " System.out.println();\n" + // might throw a runtime exception
6084 " o = new Object();\n" +
6085 " }\n" +
6086 " catch (Throwable t) {\n" + // catches everything
6087 " return;\n" + // gets out
6088 " }\n" +
6089 " o.toString();\n" + // non null
6090 " }\n" +
6091 "}\n"},
6092 "");
6093 }
6094
6095 // null analysis - try/catch
test0551_try_catch()6096 public void test0551_try_catch() {
6097 this.runNegativeTest(
6098 new String[] {
6099 "X.java",
6100 "public class X {\n" +
6101 " boolean dummy;\n" +
6102 " void foo() {\n" +
6103 " Object o = new Object();\n" +
6104 " try {\n" +
6105 " System.out.println();\n" +
6106 " if (dummy) {\n" +
6107 " o = null;\n" +
6108 " throw new Exception();\n" +
6109 " }\n" +
6110 " }\n" +
6111 " catch (Exception e) {\n" +
6112 " o.toString();\n" + // complain
6113 " }\n" +
6114 " }\n" +
6115 "}\n"},
6116 "----------\n" +
6117 "1. ERROR in X.java (at line 13)\n" +
6118 " o.toString();\n" +
6119 " ^\n" +
6120 "Potential null pointer access: The variable o may be null at this location\n" +
6121 "----------\n",
6122 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6123 }
6124
6125 // null analysis - try/catch
test0552_try_catch()6126 public void test0552_try_catch() {
6127 this.runConformTest(
6128 new String[] {
6129 "X.java",
6130 "public class X {\n" +
6131 " boolean dummy;\n" +
6132 " void foo() throws Exception {\n" +
6133 " Object o = new Object();\n" +
6134 " try {\n" +
6135 " if (dummy) {\n" +
6136 " o = null;\n" +
6137 " throw new Exception();\n" +
6138 " }\n" +
6139 " }\n" +
6140 " catch (Exception e) {\n" +
6141 " }\n" +
6142 " if (o != null) {\n" +
6143 // quiet: get out of try either through normal flow, leaves a new
6144 // object, or through Exception, leaves a null
6145 " }\n" +
6146 " }\n" +
6147 "}\n"},
6148 "");
6149 }
6150
6151 // null analysis - try/catch
test0553_try_catch()6152 public void test0553_try_catch() {
6153 this.runNegativeTest(
6154 new String[] {
6155 "X.java",
6156 "public class X {\n" +
6157 " boolean dummy, other;\n" +
6158 " void foo() {\n" +
6159 " Object o = new Object();\n" +
6160 " try {\n" +
6161 " if (dummy) {\n" +
6162 " if (other) {\n" +
6163 " throw new LocalException();\n" + // may launch new exception
6164 " }\n" +
6165 " o = null;\n" +
6166 " throw new LocalException();\n" + // must launch new exception
6167 " }\n" +
6168 " }\n" +
6169 " catch (LocalException e) {\n" +
6170 " o.toString();\n" + // complain
6171 " }\n" +
6172 " }\n" +
6173 " class LocalException extends Exception {\n" +
6174 " private static final long serialVersionUID = 1L;\n" +
6175 " }\n" +
6176 "}\n"},
6177 "----------\n" +
6178 "1. ERROR in X.java (at line 15)\n" +
6179 " o.toString();\n" +
6180 " ^\n" +
6181 "Potential null pointer access: The variable o may be null at this location\n" +
6182 "----------\n",
6183 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6184 }
6185
6186 // null analysis - try/catch
test0554_try_catch()6187 public void test0554_try_catch() {
6188 runNegativeNullTest(
6189 new String[] {
6190 "X.java",
6191 "public class X {\n" +
6192 " void foo(Object o) throws Exception {\n" +
6193 " try {\n" +
6194 " o = null;\n" +
6195 " throwLocalException();\n" +
6196 " throw new Exception();\n" +
6197 " }\n" +
6198 " catch (LocalException e) {\n" +
6199 " }\n" +
6200 " if (o != null) {\n" +
6201 // complain: only way to get out of try and get there is to go
6202 // through throwLocalException, after the assignment
6203 " }\n" +
6204 " }\n" +
6205 " class LocalException extends Exception {\n" +
6206 " private static final long serialVersionUID = 1L;\n" +
6207 " }\n" +
6208 " void throwLocalException() throws LocalException {\n" +
6209 " throw new LocalException();\n" +
6210 " }\n" +
6211 "}\n"},
6212 "----------\n" +
6213 "1. ERROR in X.java (at line 10)\n" +
6214 " if (o != null) {\n" +
6215 " ^\n" +
6216 "Null comparison always yields false: The variable o can only be null at this location\n" +
6217 "----------\n" +
6218 "2. WARNING in X.java (at line 10)\n" +
6219 " if (o != null) {\n" +
6220 " }\n" +
6221 " ^^^^^^^\n" +
6222 "Dead code\n" +
6223 "----------\n"
6224 );
6225 }
6226
6227 // null analysis - try/catch
test0555_try_catch()6228 public void test0555_try_catch() {
6229 this.runNegativeTest(
6230 new String[] {
6231 "X.java",
6232 "public class X {\n" +
6233 " void foo() {\n" +
6234 " Object o = new Object();\n" +
6235 " try {\n" +
6236 " o = null;\n" +
6237 " throwException();\n" +
6238 " }\n" +
6239 " catch (Exception e) {\n" +
6240 " o.toString();\n" + // complain NPE
6241 " }\n" +
6242 " }\n" +
6243 " void throwException() throws Exception {\n" +
6244 " throw new Exception();\n" +
6245 " }\n" +
6246 "}\n"},
6247 "----------\n" +
6248 "1. ERROR in X.java (at line 9)\n" +
6249 " o.toString();\n" +
6250 " ^\n" +
6251 "Null pointer access: The variable o can only be null at this location\n" +
6252 "----------\n",
6253 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6254 }
6255
6256 // null analysis - try/catch
test0556_try_catch()6257 public void test0556_try_catch() {
6258 this.runNegativeTest(
6259 new String[] {
6260 "X.java",
6261 "public class X {\n" +
6262 " void foo() {\n" +
6263 " Object o = new Object();\n" +
6264 " try {\n" +
6265 " o = null;\n" +
6266 " throwException();\n" +
6267 " }\n" +
6268 " catch (Throwable t) {\n" +
6269 " o.toString();\n" + // complain NPE
6270 " }\n" +
6271 " }\n" +
6272 " void throwException() throws Exception {\n" +
6273 " throw new Exception();\n" +
6274 " }\n" +
6275 "}\n"},
6276 "----------\n" +
6277 "1. ERROR in X.java (at line 9)\n" +
6278 " o.toString();\n" +
6279 " ^\n" +
6280 "Null pointer access: The variable o can only be null at this location\n" +
6281 "----------\n",
6282 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6283 }
6284
6285 // null analysis - try/catch
test0557_try_catch()6286 public void test0557_try_catch() {
6287 this.runNegativeTest(
6288 new String[] {
6289 "X.java",
6290 "public class X {\n" +
6291 " boolean dummy;\n" +
6292 " void foo() {\n" +
6293 " Object o = new Object();\n" +
6294 " try {\n" +
6295 " if (dummy) {\n" +
6296 " o = null;\n" +
6297 " throw new Exception();\n" +
6298 " }\n" +
6299 " }\n" +
6300 " catch (Exception e) {\n" +
6301 " o.toString();\n" + // complain NPE
6302 " }\n" +
6303 " }\n" +
6304 "}\n"},
6305 "----------\n" +
6306 "1. ERROR in X.java (at line 12)\n" +
6307 " o.toString();\n" +
6308 " ^\n" +
6309 "Potential null pointer access: The variable o may be null at this location\n" +
6310 "----------\n",
6311 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6312 }
6313
6314 // null analysis - try/catch
test0558_try_catch()6315 public void test0558_try_catch() {
6316 this.runNegativeTest(
6317 new String[] {
6318 "X.java",
6319 "public class X {\n" +
6320 " boolean dummy;\n" +
6321 " void foo() {\n" +
6322 " Object o = new Object();\n" +
6323 " try {\n" +
6324 " if (dummy) {\n" +
6325 " System.out.print(0);\n" + // may thow RuntimeException
6326 " o = null;\n" +
6327 " throw new LocalException();\n" +
6328 " }\n" +
6329 " }\n" +
6330 " catch (LocalException e) {\n" + // doesn't catch RuntimeException
6331 " o.toString();\n" + // complain NPE
6332 " }\n" +
6333 " }\n" +
6334 " class LocalException extends Exception {\n" +
6335 " private static final long serialVersionUID = 1L;\n" +
6336 " }\n" +
6337 "}\n"},
6338 "----------\n" +
6339 "1. ERROR in X.java (at line 13)\n" +
6340 " o.toString();\n" +
6341 " ^\n" +
6342 "Null pointer access: The variable o can only be null at this location\n" +
6343 "----------\n",
6344 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6345 }
6346
6347 // null analysis - try/catch
test0559_try_catch()6348 public void test0559_try_catch() {
6349 this.runNegativeTest(
6350 new String[] {
6351 "X.java",
6352 "public class X {\n" +
6353 " boolean dummy;\n" +
6354 " void foo() {\n" +
6355 " Object o = new Object();\n" +
6356 " try {\n" +
6357 " if (dummy) {\n" +
6358 " o = null;\n" +
6359 " throw new SubException();\n" +
6360 " }\n" +
6361 " }\n" +
6362 " catch (LocalException e) {\n" + // must catch SubException
6363 " o.toString();\n" + // complain NPE
6364 " }\n" +
6365 " }\n" +
6366 " class LocalException extends Exception {\n" +
6367 " private static final long serialVersionUID = 1L;\n" +
6368 " }\n" +
6369 " class SubException extends LocalException {\n" +
6370 " private static final long serialVersionUID = 1L;\n" +
6371 " }\n" +
6372 "}\n"},
6373 "----------\n" +
6374 "1. ERROR in X.java (at line 12)\n" +
6375 " o.toString();\n" +
6376 " ^\n" +
6377 "Null pointer access: The variable o can only be null at this location\n" +
6378 "----------\n",
6379 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6380 }
6381
6382 // null analysis - try/catch
test0560_try_catch()6383 public void test0560_try_catch() {
6384 this.runNegativeTest(
6385 new String[] {
6386 "X.java",
6387 "public class X {\n" +
6388 " Class bar(boolean b) throws ClassNotFoundException {\n" +
6389 " if (b) {\n" +
6390 " throw new ClassNotFoundException();\n" +
6391 " }\n" +
6392 " return null;\n" +
6393 " }\n" +
6394 " public Class foo(Class c, boolean b) {\n" +
6395 " if (c != null)\n" +
6396 " return c;\n" +
6397 " if (b) {\n" +
6398 " try {\n" +
6399 " c = bar(b);\n" +
6400 " return c;\n" +
6401 " } catch (ClassNotFoundException e) {\n" +
6402 " // empty\n" +
6403 " }\n" +
6404 " }\n" +
6405 " if (c == null) { // should complain: c can only be null\n" +
6406 " }\n" +
6407 " return c;\n" +
6408 " }\n" +
6409 "}\n"},
6410 "----------\n" +
6411 "1. ERROR in X.java (at line 19)\n" +
6412 " if (c == null) { // should complain: c can only be null\n" +
6413 " ^\n" +
6414 "Redundant null check: The variable c can only be null at this location\n" +
6415 "----------\n",
6416 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6417 }
6418
6419 // null analysis - try/catch
6420 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=130359
test0561_try_catch_unchecked_exception()6421 public void test0561_try_catch_unchecked_exception() {
6422 this.runNegativeTest(
6423 new String[] {
6424 "X.java",
6425 "public class X {\n" +
6426 " void foo() {\n" +
6427 " Object o = null;\n" +
6428 " try {\n" +
6429 " o = bar();\n" +
6430 " } catch (RuntimeException e) {\n" +
6431 " o.toString();\n" + // may be null
6432 " }\n" +
6433 " }\n" +
6434 " private Object bar() {\n" +
6435 " return new Object();\n" +
6436 " }\n" +
6437 "}\n"},
6438 "----------\n" +
6439 "1. ERROR in X.java (at line 7)\n" +
6440 " o.toString();\n" +
6441 " ^\n" +
6442 "Potential null pointer access: The variable o may be null at this location\n" +
6443 "----------\n",
6444 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6445 }
6446
6447 // null analysis - try/catch
6448 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=150854
6449 // (slightly different) variant of 561
test0562_try_catch_unchecked_exception()6450 public void test0562_try_catch_unchecked_exception() {
6451 Map options = getCompilerOptions();
6452 options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.WARNING);
6453 options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
6454 this.runNegativeTest(
6455 true,
6456 new String[] {
6457 "X.java",
6458 "import java.io.*;\n" +
6459 "public class X {\n" +
6460 " void foo() {\n" +
6461 " LineNumberReader o = null;\n" +
6462 " try {\n" +
6463 " o = new LineNumberReader(new FileReader(\"dummy\"));\n" +
6464 " } catch (NumberFormatException e) {\n" +
6465 " o.toString();\n" + // may be null
6466 " } catch (IOException e) {\n" +
6467 " }\n" +
6468 " }\n" +
6469 "}\n"},
6470 null,
6471 options,
6472 "----------\n" +
6473 "1. WARNING in X.java (at line 6)\n" +
6474 " o = new LineNumberReader(new FileReader(\"dummy\"));\n" +
6475 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
6476 "Potential resource leak: \'o\' may not be closed\n" +
6477 "----------\n" +
6478 "2. ERROR in X.java (at line 8)\n" +
6479 " o.toString();\n" +
6480 " ^\n" +
6481 "Potential null pointer access: The variable o may be null at this location\n" +
6482 "----------\n",
6483 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6484 }
6485
6486 // null analysis - try/catch
6487 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=155117
test0563_try_catch()6488 public void test0563_try_catch() {
6489 this.runNegativeTest(
6490 new String[] {
6491 "X.java",
6492 "public class X {\n" +
6493 " public void foo(boolean b) {\n" +
6494 " Exception ex = null;\n" +
6495 " if (b) {\n" +
6496 " try {\n" +
6497 " System.out.println();\n" +
6498 " return;\n" +
6499 " } catch (Exception e) {\n" +
6500 " ex = e;\n" +
6501 " }\n" +
6502 " }\n" +
6503 " else {\n" +
6504 " try {\n" +
6505 " System.out.println();\n" +
6506 " return;\n" +
6507 " } catch (Exception e) {\n" +
6508 " ex = e;\n" +
6509 " }\n" +
6510 " }\n" +
6511 " if (ex == null) {\n" + // complain: ex cannot be null\n" +
6512 " }\n" +
6513 " }\n" +
6514 "}\n"},
6515 "----------\n" +
6516 "1. ERROR in X.java (at line 20)\n" +
6517 " if (ex == null) {\n" +
6518 " ^^\n" +
6519 "Null comparison always yields false: The variable ex cannot be null at this location\n" +
6520 "----------\n" +
6521 "2. WARNING in X.java (at line 20)\n" +
6522 " if (ex == null) {\n" +
6523 " }\n" +
6524 " ^^^^^^^\n" +
6525 "Dead code\n" +
6526 "----------\n",
6527 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6528 }
6529
6530 // null analysis - try/catch
6531 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=150854
test0564_try_catch_unchecked_exception()6532 public void test0564_try_catch_unchecked_exception() {
6533 Map options = getCompilerOptions();
6534 options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.WARNING);
6535 this.runConformTest(
6536 new String[] {
6537 "X.java",
6538 "public class X {\n" +
6539 " public static Object foo() {\n" +
6540 " Object result = null;\n" +
6541 " try {\n" +
6542 " result = new Object();\n" +
6543 " } catch (Exception e) {\n" +
6544 " result = null;\n" +
6545 " }\n" +
6546 " return result;\n" +
6547 " }\n" +
6548 "}"},
6549 "",
6550 null /* no extra class libraries */,
6551 true /* flush output directory */,
6552 null /* no vm arguments */,
6553 options,
6554 null /* no custom requestor*/,
6555 false /* do not skip javac for this peculiar test */);
6556 }
6557
6558 // null analysis - try/catch
6559 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=150854
6560 // variant
test0565_try_catch_unchecked_exception()6561 public void test0565_try_catch_unchecked_exception() {
6562 Map options = getCompilerOptions();
6563 options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.WARNING);
6564 this.runConformTest(
6565 new String[] {
6566 "X.java",
6567 "public class X {\n" +
6568 " public static Object foo() {\n" +
6569 " Object result = null;\n" +
6570 " try {\n" +
6571 " result = new Object();\n" +
6572 " result = new Object();\n" +
6573 " } catch (Exception e) {\n" +
6574 " result = null;\n" +
6575 " }\n" +
6576 " return result;\n" +
6577 " }\n" +
6578 "}"},
6579 "",
6580 null /* no extra class libraries */,
6581 true /* flush output directory */,
6582 null /* no vm arguments */,
6583 options,
6584 null /* no custom requestor*/,
6585 false /* do not skip javac for this peculiar test */);
6586 }
6587
6588 // null analysis - try/catch
6589 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=150854
6590 // variant
test0566_try_catch_unchecked_exception()6591 public void test0566_try_catch_unchecked_exception() {
6592 Map options = getCompilerOptions();
6593 options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.WARNING);
6594 this.runConformTest(
6595 new String[] {
6596 "X.java",
6597 "public class X {\n" +
6598 " public static Object foo(Y y) {\n" +
6599 " Object result = null;\n" +
6600 " try {\n" +
6601 " while (y.next()) {\n" +
6602 " result = y.getObject();\n" +
6603 " }\n" +
6604 " } catch (Exception e) {\n" +
6605 " result = null;\n" +
6606 " }\n" +
6607 " return result;\n" +
6608 " }\n" +
6609 "}\n" +
6610 "class Y {\n" +
6611 " boolean next() {\n" +
6612 " return false;\n" +
6613 " }\n" +
6614 " Object getObject() {\n" +
6615 " return null;\n" +
6616 " }\n" +
6617 "}"},
6618 "",
6619 null /* no extra class libraries */,
6620 true /* flush output directory */,
6621 null /* no vm arguments */,
6622 options,
6623 null /* no custom requestor*/,
6624 false /* do not skip javac for this peculiar test */);
6625 }
6626
6627 // null analysis - try/catch for checked exceptions
6628 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=295260
test0567_try_catch_checked_exception()6629 public void test0567_try_catch_checked_exception() {
6630 this.runConformTest(
6631 new String[] {
6632 "X.java",
6633 "import java.net.MalformedURLException;\n" +
6634 "import java.net.URL;\n" +
6635 "public class X {\n" +
6636 " public void test1(String[] args) {\n" +
6637 " URL[] urls = null;\n" +
6638 " try {\n" +
6639 " urls = new URL[args.length];\n" +
6640 " for (int i = 0; i < args.length; i++)\n" +
6641 " urls[i] = new URL(\"http\", \"\", -1, args[i]);\n" +
6642 " }\n" +
6643 " catch (MalformedURLException mfex) {\n" +
6644 " urls = null;\n" + // quiet
6645 " }\n" +
6646 " }\n" +
6647 "}",
6648 },
6649 "");
6650 }
6651
6652 // null analysis - try/catch for checked exceptions with finally block
6653 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=295260
test0568_try_catch_checked_exception()6654 public void test0568_try_catch_checked_exception() {
6655 this.runConformTest(
6656 new String[] {
6657 "X.java",
6658 "import java.net.MalformedURLException;\n" +
6659 "import java.net.URL;\n" +
6660 "public class X {\n" +
6661 " public void test1(String[] args) {\n" +
6662 " URL[] urls = null;\n" +
6663 " try {\n" +
6664 " urls = new URL[args.length];\n" +
6665 " for (int i = 0; i < args.length; i++)\n" +
6666 " urls[i] = new URL(\"http\", \"\", -1, args[i]);\n" +
6667 " }\n" +
6668 " catch (MalformedURLException mfex) {\n" +
6669 " urls = null;\n" + // quiet
6670 " }\n" +
6671 " finally{\n"+
6672 " System.out.println(\"complete\");\n" +
6673 " }\n" +
6674 " }\n" +
6675 "}",
6676 },
6677 "");
6678 }
6679 // null analysis -- try/catch
6680 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=302446
test0569_try_catch()6681 public void test0569_try_catch() {
6682 this.runNegativeTest(
6683 new String[] {
6684 "X.java",
6685 "public class X {\n" +
6686 " void foo() {\n" +
6687 " Object o = null;\n" +
6688 " int i;\n" +
6689 " if (o == null)\n" + // redundant check
6690 " i = 0;\n" +
6691 " try {\n" +
6692 " System.out.println(i);\n" + // might throw a runtime exception
6693 " o = new Object();\n" +
6694 " throw new Exception(\"Exception thrown from try block\");\n" +
6695 " }\n" +
6696 " catch (Throwable t) {\n" + // catches everything
6697 " return;\n" + // gets out
6698 " }\n" +
6699 " }\n" +
6700 "}\n"},
6701 "----------\n" +
6702 "1. ERROR in X.java (at line 5)\n" +
6703 " if (o == null)\n" +
6704 " ^\n" +
6705 "Redundant null check: The variable o can only be null at this location\n" +
6706 "----------\n" +
6707 "2. ERROR in X.java (at line 8)\n" +
6708 " System.out.println(i);\n" +
6709 " ^\n" +
6710 "The local variable i may not have been initialized\n" +
6711 "----------\n");
6712 }
6713 // null analysis -- try/catch
6714 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=302446
test0570_try_catch()6715 public void test0570_try_catch() {
6716 this.runNegativeTest(
6717 new String[] {
6718 "X.java",
6719 "public class X {\n" +
6720 " void foo() {\n" +
6721 " Object o = null;\n" +
6722 " int i;\n" +
6723 " if (o == null)\n" + // redundant check
6724 " i = 0;\n" +
6725 " try {\n" +
6726 " System.out.println();\n" + // might throw a runtime exception
6727 " o = new Object();\n" +
6728 " if (o != null)\n" + // redundant check
6729 " i = 1\n;" +
6730 " throw new Exception(\"Exception thrown from try block\");\n" +
6731 " }\n" +
6732 " catch (Exception e) {\n" +
6733 " if(i == 0)\n" +
6734 " System.out.println(\"o was initialised\");\n" +
6735 " }\n" +
6736 " }\n" +
6737 "}\n"},
6738 "----------\n" +
6739 "1. ERROR in X.java (at line 5)\n" +
6740 " if (o == null)\n" +
6741 " ^\n" +
6742 "Redundant null check: The variable o can only be null at this location\n" +
6743 "----------\n" +
6744 "2. ERROR in X.java (at line 10)\n" +
6745 " if (o != null)\n" +
6746 " ^\n" +
6747 "Redundant null check: The variable o cannot be null at this location\n" +
6748 "----------\n" +
6749 "3. ERROR in X.java (at line 15)\n" +
6750 " if(i == 0)\n" +
6751 " ^\n" +
6752 "The local variable i may not have been initialized\n" +
6753 "----------\n");
6754 }
6755 //null analysis -- try/catch
6756 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=302446
test0571_try_catch_finally()6757 public void test0571_try_catch_finally() {
6758 this.runNegativeTest(
6759 new String[] {
6760 "X.java",
6761 "public class X {\n" +
6762 " void foo() {\n" +
6763 " Object o = null;\n" +
6764 " int i;\n" +
6765 " if (o == null)\n" + // redundant check
6766 " i = 0;\n" +
6767 " try {\n" +
6768 " o = new Object();\n" +
6769 " i = 1\n;" +
6770 " throw new Exception(\"Exception thrown from try block\");\n" +
6771 " }\n" +
6772 " catch (Exception e) {\n" +
6773 " if(o == null)\n" +
6774 " o = new Object();\n" +
6775 " i = 1;\n" +
6776 " }\n" +
6777 " finally {\n" +
6778 " if (i==1) {\n" +
6779 " System.out.println(\"Method ended with o being initialised\");\n" +
6780 " System.out.println(o.toString());\n" + // may be null
6781 " } \n" +
6782 " }\n" +
6783 " }\n" +
6784 "}\n"},
6785 "----------\n" +
6786 "1. ERROR in X.java (at line 5)\n" +
6787 " if (o == null)\n" +
6788 " ^\n" +
6789 "Redundant null check: The variable o can only be null at this location\n" +
6790 "----------\n" +
6791 "2. ERROR in X.java (at line 18)\n" +
6792 " if (i==1) {\n" +
6793 " ^\n" +
6794 "The local variable i may not have been initialized\n" +
6795 "----------\n" +
6796 "3. ERROR in X.java (at line 20)\n" +
6797 " System.out.println(o.toString());\n" +
6798 " ^\n" +
6799 "Potential null pointer access: The variable o may be null at this location\n" +
6800 "----------\n");
6801 }
6802 //null analysis -- if statement
6803 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=302446
test0572_if_statement()6804 public void test0572_if_statement() {
6805 this.runNegativeTest(
6806 new String[] {
6807 "X.java",
6808 "public class X {\n" +
6809 " void foo() {\n" +
6810 " Object o = null;\n" +
6811 " int i;\n" +
6812 " if (o == null) // redundant check\n" +
6813 " i = 0;\n" +
6814 " System.out.println(i);\n" +
6815 " }\n" +
6816 "}\n" +
6817 ""},
6818 "----------\n" +
6819 "1. ERROR in X.java (at line 5)\n" +
6820 " if (o == null) // redundant check\n" +
6821 " ^\n" +
6822 "Redundant null check: The variable o can only be null at this location\n" +
6823 "----------\n" +
6824 "2. ERROR in X.java (at line 7)\n" +
6825 " System.out.println(i);\n" +
6826 " ^\n" +
6827 "The local variable i may not have been initialized\n" +
6828 "----------\n");
6829 }
6830
6831 // take care for Java7 changes
test0573_try_catch_unchecked_and_checked_exception()6832 public void test0573_try_catch_unchecked_and_checked_exception() {
6833 this.runNegativeTest(
6834 new String[] {
6835 "X.java",
6836 "import java.io.IOException;\n" +
6837 "public class X {\n" +
6838 " void foo() {\n" +
6839 " Object o = null;\n" +
6840 " try {\n" +
6841 " bar();\n" +
6842 " o = new Object();\n" +
6843 " } catch (IOException e) {\n" +
6844 " o.toString();\n" +
6845 " } catch(RuntimeException e) {\n" +
6846 " o.toString();\n" + // may be null
6847 " }\n" +
6848 " }\n" +
6849 " private Object bar() throws IOException{\n" +
6850 " return new Object();\n" +
6851 " }\n" +
6852 "}\n"},
6853 "----------\n" +
6854 "1. ERROR in X.java (at line 9)\n" +
6855 " o.toString();\n" +
6856 " ^\n" +
6857 "Null pointer access: The variable o can only be null at this location\n" +
6858 "----------\n" +
6859 "2. ERROR in X.java (at line 11)\n" +
6860 " o.toString();\n" +
6861 " ^\n" +
6862 "Potential null pointer access: The variable o may be null at this location\n" +
6863 "----------\n",
6864 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6865 }
6866
6867 // similar to test0573 using multi catch parameters
test0574_try_multi_catch_unchecked_and_checked_exception()6868 public void test0574_try_multi_catch_unchecked_and_checked_exception() {
6869 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
6870 this.runNegativeTest(
6871 new String[] {
6872 "X.java",
6873 "import java.io.IOException;\n" +
6874 "public class X {\n" +
6875 " void foo() {\n" +
6876 " Object o = null;\n" +
6877 " try {\n" +
6878 " bar();\n" +
6879 " o = new Object();\n" +
6880 " } catch (IOException | RuntimeException e) {\n" +
6881 " o.toString();\n" +
6882 " }\n" +
6883 " }\n" +
6884 " private Object bar() throws IOException{\n" +
6885 " return new Object();\n" +
6886 " }\n" +
6887 "}\n"},
6888 "----------\n" +
6889 "1. ERROR in X.java (at line 9)\n" +
6890 " o.toString();\n" +
6891 " ^\n" +
6892 "Potential null pointer access: The variable o may be null at this location\n" +
6893 "----------\n",
6894 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6895 }
6896 }
6897 //multi catch variant of test0561_try_catch_unchecked_exception
test0575_try_multi_catch_finally_unchecked_and_checked_exception()6898 public void test0575_try_multi_catch_finally_unchecked_and_checked_exception() {
6899 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
6900 this.runNegativeTest(
6901 new String[] {
6902 "X.java",
6903 "import java.io.IOException;\n" +
6904 "public class X {\n" +
6905 " void foo() {\n" +
6906 " Object o = null;\n" +
6907 " try {\n" +
6908 " o = bar();\n" +
6909 " } catch (IOException | RuntimeException e) {\n" +
6910 " o.toString();\n" + // may be null
6911 " } finally {}\n" +
6912 " }\n" +
6913 " private Object bar() throws IOException{\n" +
6914 " return new Object();\n" +
6915 " }\n" +
6916 "}\n"},
6917 "----------\n" +
6918 "1. ERROR in X.java (at line 8)\n" +
6919 " o.toString();\n" +
6920 " ^\n" +
6921 "Potential null pointer access: The variable o may be null at this location\n" +
6922 "----------\n",
6923 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6924 }
6925 }
6926
6927 // null test for resources inside try with resources statement
test0576_try_with_resources()6928 public void test0576_try_with_resources() {
6929 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
6930 this.runNegativeTest(
6931 new String[] {
6932 "X.java",
6933 "import java.io.FileInputStream;\n" +
6934 "import java.io.IOException;\n" +
6935 "import java.io.FileNotFoundException;\n" +
6936 "class MyException extends Exception {}\n" +
6937 "public class X {\n" +
6938 " static void m(int n) throws IllegalArgumentException, MyException {}\n" +
6939 " void foo(String name, boolean b) throws FileNotFoundException, IOException{\n" +
6940 " FileInputStream fis;\n" +
6941 " if (b) fis = new FileInputStream(\"\");\n" +
6942 " else fis = null;\n" +
6943 " try (FileInputStream fis2 = fis; FileInputStream fis3 = fis2; FileInputStream fis4 = null) {\n" +
6944 " fis = new FileInputStream(\"\");\n" +
6945 " fis2.available();\n" + // may be null since fis may be null
6946 " fis3.close();\n" +
6947 " fis4.available();\n" + // will always be null
6948 " m(1);\n" +
6949 " } catch (IllegalArgumentException e) {\n" +
6950 " fis.available();\n" + // may be null
6951 " } catch (MyException e) {\n" +
6952 " fis.available();\n" + // cannot be null
6953 " } finally {}\n" +
6954 " }\n" +
6955 "}\n"},
6956 "----------\n" +
6957 "1. WARNING in X.java (at line 4)\n" +
6958 " class MyException extends Exception {}\n" +
6959 " ^^^^^^^^^^^\n" +
6960 "The serializable class MyException does not declare a static final serialVersionUID field of type long\n" +
6961 "----------\n" +
6962 "2. ERROR in X.java (at line 13)\n" +
6963 " fis2.available();\n" +
6964 " ^^^^\n" +
6965 "Potential null pointer access: The variable fis2 may be null at this location\n" +
6966 "----------\n" +
6967 "3. ERROR in X.java (at line 14)\n" +
6968 " fis3.close();\n" +
6969 " ^^^^\n" +
6970 "Potential null pointer access: The variable fis3 may be null at this location\n" +
6971 "----------\n" +
6972 "4. ERROR in X.java (at line 15)\n" +
6973 " fis4.available();\n" +
6974 " ^^^^\n" +
6975 "Null pointer access: The variable fis4 can only be null at this location\n" +
6976 "----------\n" +
6977 "5. ERROR in X.java (at line 18)\n" +
6978 " fis.available();\n" +
6979 " ^^^\n" +
6980 "Potential null pointer access: The variable fis may be null at this location\n" +
6981 "----------\n",
6982 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
6983 }
6984 }
6985
6986 // null analysis - throw
6987 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=201182
test0595_throw()6988 public void test0595_throw() {
6989 runTest(
6990 new String[] {
6991 "X.java",
6992 "public class X {\n" +
6993 " public static void main(String[] args) throws Throwable {\n" +
6994 " Throwable t = null;\n" +
6995 " throw t;\n" +
6996 " }\n" +
6997 "}\n"
6998 },
6999 true /* expectingCompilerErrors */,
7000 "----------\n" +
7001 "1. ERROR in X.java (at line 4)\n" +
7002 " throw t;\n" +
7003 " ^\n" +
7004 "Null pointer access: The variable t can only be null at this location\n" +
7005 "----------\n" /* expectedCompilerLog */,
7006 "" /* expectedOutputString */,
7007 "" /* expectedErrorString */,
7008 false /* forceExecution */,
7009 null /* classLib */,
7010 true /* shouldFlushOutputDirectory */,
7011 null /* vmArguments */,
7012 null /* customOptions */,
7013 null /* clientRequestor */,
7014 true /* skipJavac */);
7015 }
7016
7017 // null analysis - throw
7018 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=201182
7019 // variant - potential NPE
test0596_throw()7020 public void test0596_throw() {
7021 runTest(
7022 new String[] {
7023 "X.java",
7024 "public class X {\n" +
7025 " public static void main(String[] args) throws Throwable {\n" +
7026 " Throwable t = null;\n" +
7027 " if (args.length > 0) {\n" +
7028 " t = new Throwable();\n" +
7029 " }\n" +
7030 " throw t;\n" +
7031 " }\n" +
7032 "}\n"
7033 },
7034 true /* expectingCompilerErrors */,
7035 "----------\n" +
7036 "1. ERROR in X.java (at line 7)\n" +
7037 " throw t;\n" +
7038 " ^\n" +
7039 "Potential null pointer access: The variable t may be null at this location\n" +
7040 "----------\n" /* expectedCompilerLog */,
7041 "" /* expectedOutputString */,
7042 "" /* expectedErrorString */,
7043 false /* forceExecution */,
7044 null /* classLib */,
7045 true /* shouldFlushOutputDirectory */,
7046 null /* vmArguments */,
7047 null /* customOptions */,
7048 null /* clientRequestor */,
7049 true /* skipJavac */);
7050 }
7051
7052
7053 // null analysis - throw
7054 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=201182
7055 // variant - unknown
test0597_throw()7056 public void test0597_throw() {
7057 runTest(
7058 new String[] {
7059 "X.java",
7060 "public class X {\n" +
7061 " void foo() throws Throwable {\n" +
7062 " throw t();\n" +
7063 " }\n" +
7064 " Throwable t() {\n" +
7065 " return new Throwable();\n" +
7066 " }\n" +
7067 "}\n"
7068 },
7069 false /* expectingCompilerErrors */,
7070 "" /* expectedCompilerLog */,
7071 "" /* expectedOutputString */,
7072 "" /* expectedErrorString */,
7073 false /* forceExecution */,
7074 null /* classLib */,
7075 true /* shouldFlushOutputDirectory */,
7076 null /* vmArguments */,
7077 null /* customOptions */,
7078 null /* clientRequestor */,
7079 true /* skipJavac */);
7080 }
7081
7082 // null analysis -- do while
test0601_do_while()7083 public void test0601_do_while() {
7084 this.runNegativeTest(
7085 new String[] {
7086 "X.java",
7087 "public class X {\n" +
7088 " void foo() {\n" +
7089 " Object o = null;\n" +
7090 " do {/* */}\n" +
7091 " while (o.toString() != null);\n" +
7092 // complain: NPE
7093 " }\n" +
7094 "}\n"},
7095 "----------\n" +
7096 "1. ERROR in X.java (at line 5)\n" +
7097 " while (o.toString() != null);\n" +
7098 " ^\n" +
7099 "Null pointer access: The variable o can only be null at this location\n" +
7100 "----------\n",
7101 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7102 }
7103
7104 // null analysis -- do while
test0602_do_while()7105 public void test0602_do_while() {
7106 this.runNegativeTest(
7107 new String[] {
7108 "X.java",
7109 "public class X {\n" +
7110 " void foo() {\n" +
7111 " Object o = null;\n" +
7112 " do {/* */}\n" +
7113 " while (o != null);\n" +
7114 // complain: get o null first time and forever
7115 " }\n" +
7116 "}\n"},
7117 "----------\n" +
7118 "1. ERROR in X.java (at line 5)\n" +
7119 " while (o != null);\n" +
7120 " ^\n" +
7121 "Null comparison always yields false: The variable o can only be null at this location\n" +
7122 "----------\n",
7123 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7124 }
7125
7126 // null analysis -- do while
test0603_do_while()7127 public void test0603_do_while() {
7128 this.runNegativeTest(
7129 new String[] {
7130 "X.java",
7131 "public class X {\n" +
7132 " void foo() {\n" +
7133 " Object o = null;\n" +
7134 " do {\n" +
7135 " o = new Object();\n" +
7136 " }\n" +
7137 " while (o == null);\n" +
7138 // complain: set it to non null before test, for each iteration
7139 " }\n" +
7140 "}\n"},
7141 "----------\n" +
7142 "1. ERROR in X.java (at line 7)\n" +
7143 " while (o == null);\n" +
7144 " ^\n" +
7145 "Null comparison always yields false: The variable o cannot be null at this location\n" +
7146 "----------\n",
7147 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7148 }
7149
7150 // null analysis -- do while
test0604_do_while()7151 public void test0604_do_while() {
7152 this.runConformTest(
7153 new String[] {
7154 "X.java",
7155 "public class X {\n" +
7156 " void foo() {\n" +
7157 " Object o = null;\n" +
7158 " do {\n" +
7159 " if (System.currentTimeMillis() > 10L) {\n" +
7160 " o = new Object();\n" +
7161 " }\n" +
7162 " }\n" +
7163 " while (o == null);\n" +
7164 " }\n" +
7165 "}\n"},
7166 "");
7167 }
7168
7169 // null analysis -- do while
test0605_do_while()7170 public void test0605_do_while() {
7171 this.runNegativeTest(
7172 new String[] {
7173 "X.java",
7174 "public class X {\n" +
7175 " boolean dummy;\n" +
7176 " void foo(Object o) {\n" +
7177 " o = null;\n" +
7178 " do {\n" +
7179 " // do nothing\n" +
7180 " }\n" +
7181 " while (dummy || o != null);\n" +
7182 " }\n" +
7183 "}\n"},
7184 "----------\n" +
7185 "1. ERROR in X.java (at line 8)\n" +
7186 " while (dummy || o != null);\n" +
7187 " ^\n" +
7188 "Null comparison always yields false: The variable o can only be null at this location\n" +
7189 "----------\n",
7190 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7191 }
7192
7193 // null analysis -- do while
test0606_do_while()7194 public void test0606_do_while() {
7195 this.runConformTest(
7196 new String[] {
7197 "X.java",
7198 "public class X {\n" +
7199 " void foo() {\n" +
7200 " Object o = null,\n" +
7201 " u = new Object(),\n" +
7202 " v = new Object();\n" +
7203 " do {\n" +
7204 " if (v == null) {\n" +
7205 " o = new Object();\n" +
7206 " };\n" +
7207 " if (u == null) {\n" +
7208 " v = null;\n" +
7209 " };\n" +
7210 " u = null;\n" +
7211 " }\n" +
7212 " while (o == null);\n" +
7213 " }\n" +
7214 "}\n"},
7215 "");
7216 }
7217
7218 // null analysis -- do while
test0607_do_while()7219 public void test0607_do_while() {
7220 this.runNegativeTest(
7221 new String[] {
7222 "X.java",
7223 "public class X {\n" +
7224 " boolean dummy;\n" +
7225 " void foo() {\n" +
7226 " Object o = null;\n" +
7227 " do {\n" +
7228 " o.toString();\n" +
7229 // complain: NPE
7230 " o = new Object();\n" +
7231 " }\n" +
7232 " while (dummy);\n" +
7233 " }\n" +
7234 "}\n"},
7235 "----------\n" +
7236 "1. ERROR in X.java (at line 6)\n" +
7237 " o.toString();\n" +
7238 " ^\n" +
7239 "Potential null pointer access: The variable o may be null at this location\n" +
7240 "----------\n",
7241 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7242 }
7243
7244 // null analysis -- do while
test0608_do_while()7245 public void test0608_do_while() {
7246 this.runConformTest(
7247 new String[] {
7248 "X.java",
7249 "public class X {\n" +
7250 " boolean dummy;\n" +
7251 " void foo() {\n" +
7252 " Object o = null;\n" +
7253 " do {\n" +
7254 " o = new Object();\n" +
7255 " }\n" +
7256 " while (dummy);\n" +
7257 " o.toString();\n" +
7258 " }\n" +
7259 "}\n"},
7260 "");
7261 }
7262
7263 // null analysis -- do while
test0609_do_while()7264 public void test0609_do_while() {
7265 this.runNegativeTest(
7266 new String[] {
7267 "X.java",
7268 "public class X {\n" +
7269 " boolean dummy;\n" +
7270 " void foo() {\n" +
7271 " Object o = null;\n" +
7272 " do { /* */ }\n" +
7273 " while (dummy);\n" +
7274 " o.toString();\n" +
7275 " }\n" +
7276 "}\n"},
7277 "----------\n" +
7278 "1. ERROR in X.java (at line 7)\n" +
7279 " o.toString();\n" +
7280 " ^\n" +
7281 "Null pointer access: The variable o can only be null at this location\n" +
7282 "----------\n",
7283 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7284 }
7285
7286 // null analysis - do while
test0610_do_while()7287 public void test0610_do_while() {
7288 this.runConformTest(
7289 new String[] {
7290 "X.java",
7291 "public class X {\n" +
7292 " X bar() {\n" +
7293 " return null;\n" +
7294 " }\n" +
7295 " void foo(X x) {\n" +
7296 " x.bar();\n" +
7297 " do {\n" +
7298 " x = x.bar();\n" +
7299 " } while (x != null);\n" + // quiet
7300 " }\n" +
7301 "}\n"},
7302 "");
7303 }
7304
7305 // null analysis - do while
test0611_do_while()7306 public void test0611_do_while() {
7307 this.runNegativeTest(
7308 new String[] {
7309 "X.java",
7310 "class X {\n" +
7311 " X bar() {\n" +
7312 " return new X();\n" +
7313 " }\n" +
7314 " void foo(Object o) {\n" +
7315 " do {\n" +
7316 " o = bar();\n" +
7317 " } while (o == null);\n" +
7318 " if (o != null) { /* */ }\n" + // complain
7319 " }\n" +
7320 "}"},
7321 "----------\n" +
7322 "1. ERROR in X.java (at line 9)\n" +
7323 " if (o != null) { /* */ }\n" +
7324 " ^\n" +
7325 "Redundant null check: The variable o cannot be null at this location\n" +
7326 "----------\n",
7327 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7328 }
7329
7330 // the problem here is that a single pass cannot know for the return
7331 // embedded into the if; prior approach did use the upstream flow
7332 // info to catch this, but this is inappropriate in many cases (eg
7333 // test0606)
7334 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123399
_test0612_do_while()7335 public void _test0612_do_while() {
7336 runNegativeNullTest(
7337 new String[] {
7338 "X.java",
7339 "public class X {\n" +
7340 " void foo(Object doubt) {\n" +
7341 " Object o = null;\n" +
7342 " do {\n" +
7343 " if (o == null) {\n" +
7344 " return;\n" +
7345 " }\n" +
7346 " o = doubt;\n" +
7347 " } while (true);\n" +
7348 " }\n" +
7349 "}"},
7350 "----------\n" +
7351 "1. ERROR in X.java (at line 6)\n" +
7352 " if (o == null) {\n" +
7353 " ^\n" +
7354 "Redundant null check: The variable o can only be null at this location\n" +
7355 "----------\n"
7356 );
7357 }
7358
7359 // null analysis - do while
7360 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=147118
test0613_do_while()7361 public void test0613_do_while() {
7362 this.runConformTest(
7363 new String[] {
7364 "X.java",
7365 "public class X {\n" +
7366 " String f;\n" +
7367 " void foo (boolean b) {\n" +
7368 " X x = new X();\n" +
7369 " do {\n" +
7370 " System.out.println(x.f);\n" +
7371 " if (b) {\n" +
7372 " x = null;\n" +
7373 " }\n" +
7374 " } while (x != null);\n" +
7375 " }\n" +
7376 "}\n"},
7377 "");
7378 }
7379
7380
7381 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123399
7382 // variant
_test0614_do_while()7383 public void _test0614_do_while() {
7384 this.runNegativeTest(
7385 new String[] {
7386 "X.java",
7387 "public class X {\n" +
7388 " void foo(Object doubt) {\n" +
7389 " Object o = null;\n" +
7390 " exit: do {\n" +
7391 " if (o == null) {\n" +
7392 " continue exit;\n" +
7393 " }\n" +
7394 " o = doubt;\n" +
7395 " } while (true);\n" +
7396 " }\n" +
7397 "}"},
7398 "----------\n" +
7399 "1. ERROR in X.java (at line 6)\n" +
7400 " if (o == null) {\n" +
7401 " ^\n" +
7402 "Redundant null check: The variable o can only be null at this location\n" +
7403 "----------\n",
7404 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7405 }
7406
7407 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123399
7408 // variant
_test0615_do_while()7409 public void _test0615_do_while() {
7410 this.runNegativeTest(
7411 new String[] {
7412 "X.java",
7413 "public class X {\n" +
7414 " void foo(Object doubt) {\n" +
7415 " Object o = null;\n" +
7416 " do {\n" +
7417 " if (o == null) {\n" +
7418 " throw new RuntimeException();\n" +
7419 " }\n" +
7420 " o = doubt;\n" +
7421 " } while (true);\n" +
7422 " }\n" +
7423 "}"},
7424 "----------\n" +
7425 "1. ERROR in X.java (at line 6)\n" +
7426 " if (o == null) {\n" +
7427 " ^\n" +
7428 "Redundant null check: The variable o can only be null at this location\n" +
7429 "----------\n",
7430 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7431 }
7432
7433 // null analysis - do while
7434 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
7435 // variant
test0616_do_while_explicit_label()7436 public void test0616_do_while_explicit_label() {
7437 this.runConformTest(
7438 new String[] {
7439 "X.java",
7440 "public class X {\n" +
7441 " void foo(int i) {\n" +
7442 " Object o = null;\n" +
7443 " done: do {\n" +
7444 " switch (i) {\n" +
7445 " case 0:\n" +
7446 " o = new Object();\n" +
7447 " break;\n" +
7448 " case 1:\n" +
7449 " break done;\n" +
7450 " }\n" +
7451 " } while (true);\n" +
7452 " if (o == null) {\n" +
7453 " }\n" +
7454 " }\n" +
7455 "}\n"},
7456 "");
7457 }
7458
7459 // null analysis - do while
7460 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
7461 // variant
test0617_do_while_explicit_label()7462 public void test0617_do_while_explicit_label() {
7463 this.runConformTest(
7464 new String[] {
7465 "X.java",
7466 "public class X {\n" +
7467 " boolean test() {\n" +
7468 " return true;\n" +
7469 " }\n" +
7470 " void foo() {\n" +
7471 " Object o = null;\n" +
7472 " done: do {\n" +
7473 " if (test()) {\n" +
7474 " break done;\n" +
7475 " }\n" +
7476 " o = new Object();\n" +
7477 " } while (true);\n" +
7478 " if (o == null) {\n" +
7479 " }\n" +
7480 " }\n" +
7481 "}\n"},
7482 "");
7483 }
7484
7485 // null analysis - do while
7486 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
7487 // variant
test0618_do_while_infinite()7488 public void test0618_do_while_infinite() {
7489 this.runConformTest(
7490 new String[] {
7491 "X.java",
7492 "public class X {\n" +
7493 " public void test(String[] a) {\n" +
7494 " String key = null;\n" +
7495 " do {\n" +
7496 " if (a[0] == null)\n" +
7497 " break;\n" +
7498 " key = a[0];\n" +
7499 " } while (true);\n" +
7500 " if (key != null) {\n" +
7501 " // empty\n" +
7502 " }\n" +
7503 " }\n" +
7504 "}"},
7505 "");
7506 }
7507
7508 // null analysis - do while
7509 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
7510 // variant
test0619_do_while_infinite()7511 public void test0619_do_while_infinite() {
7512 this.runConformTest(
7513 new String[] {
7514 "X.java",
7515 "public class X {\n" +
7516 " public void test(String[] a) {\n" +
7517 " String key = null;\n" +
7518 " loop: do {\n" +
7519 " if (a[0] == null)\n" +
7520 " break loop;\n" +
7521 " key = a[0];\n" +
7522 " } while (true);\n" +
7523 " if (key != null) {\n" +
7524 " // empty\n" +
7525 " }\n" +
7526 " }\n" +
7527 "}"},
7528 "");
7529 }
7530
7531 // null analysis -- for
test0701_for()7532 public void test0701_for() {
7533 this.runNegativeTest(
7534 new String[] {
7535 "X.java",
7536 "public class X {\n" +
7537 " void foo() {\n" +
7538 " Object o = null;\n" +
7539 " for (;o.toString() != null;) {/* */}\n" +
7540 // complain: NPE
7541 " }\n" +
7542 "}\n"},
7543 "----------\n" +
7544 "1. ERROR in X.java (at line 4)\n" +
7545 " for (;o.toString() != null;) {/* */}\n" +
7546 " ^\n" +
7547 "Null pointer access: The variable o can only be null at this location\n" +
7548 "----------\n",
7549 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7550 }
7551
7552 // null analysis -- for
test0702_for()7553 public void test0702_for() {
7554 this.runNegativeTest(
7555 new String[] {
7556 "X.java",
7557 "public class X {\n" +
7558 " void foo() {\n" +
7559 " Object o = null;\n" +
7560 " for (;o != null;) {/* */}\n" +
7561 // complain: get o null first time and forever
7562 " }\n" +
7563 "}\n"},
7564 "----------\n" +
7565 "1. ERROR in X.java (at line 4)\n" +
7566 " for (;o != null;) {/* */}\n" +
7567 " ^\n" +
7568 "Null comparison always yields false: The variable o can only be null at this location\n" +
7569 "----------\n",
7570 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7571 }
7572
7573 // null analysis -- for
test0703_for()7574 public void test0703_for() {
7575 this.runConformTest(
7576 new String[] {
7577 "X.java",
7578 "public class X {\n" +
7579 " void foo() {\n" +
7580 " Object o = null;\n" +
7581 " for (;o == null;) {\n" +
7582 // quiet: first iteration is sure to find it null,
7583 // but other iterations may change it
7584 " o = new Object();\n" +
7585 " }\n" +
7586 " }\n" +
7587 "}\n"},
7588 "");
7589 }
7590
7591 // null analysis -- for
test0704_for()7592 public void test0704_for() {
7593 this.runConformTest(
7594 new String[] {
7595 "X.java",
7596 "public class X {\n" +
7597 " void foo() {\n" +
7598 " Object o = null;\n" +
7599 " for (;o == null;) {\n" +
7600 // quiet: first iteration is sure to find it null,
7601 // but other iterations may change it
7602 " if (System.currentTimeMillis() > 10L) {\n" +
7603 " o = new Object();\n" +
7604 " }\n" +
7605 " }\n" +
7606 " }\n" +
7607 "}\n"},
7608 "");
7609 }
7610
7611 // null analysis -- for
test0705_for()7612 public void test0705_for() {
7613 this.runNegativeTest(
7614 new String[] {
7615 "X.java",
7616 "public class X {\n" +
7617 " boolean bar() {\n" +
7618 " return true;\n" +
7619 " }\n" +
7620 " void foo(Object o) {\n" +
7621 " for (;bar() && o == null;) {\n" +
7622 " o.toString();\n" + // complain: NPE because of condition
7623 " o = new Object();\n" +
7624 " }\n" +
7625 " }\n" +
7626 "}\n"},
7627 "----------\n" +
7628 "1. ERROR in X.java (at line 7)\n" +
7629 " o.toString();\n" +
7630 " ^\n" +
7631 "Null pointer access: The variable o can only be null at this location\n" +
7632 "----------\n",
7633 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7634 }
7635
7636 // null analysis -- for
test0707_for()7637 public void test0707_for() {
7638 this.runConformTest(
7639 new String[] {
7640 "X.java",
7641 "public class X {\n" +
7642 " void foo(Object o) {\n" +
7643 " for (;o == null; o.toString()) {\n" +
7644 " o = new Object();\n" +
7645 " }\n" +
7646 " }\n" +
7647 "}\n"},
7648 "");
7649 }
7650
7651 // null analysis -- for
test0708_for()7652 public void test0708_for() {
7653 this.runNegativeTest(
7654 new String[] {
7655 "X.java",
7656 "public class X {\n" +
7657 " void foo(Object o) {\n" +
7658 " for (;o == null; o.toString()) {\n" +
7659 " }\n" +
7660 " }\n" +
7661 "}\n"},
7662 "----------\n" +
7663 "1. ERROR in X.java (at line 3)\n" +
7664 " for (;o == null; o.toString()) {\n" +
7665 " ^\n" +
7666 "Null pointer access: The variable o can only be null at this location\n" +
7667 "----------\n",
7668 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7669 }
7670
7671 // null analysis -- for
test0709_for()7672 public void test0709_for() {
7673 this.runNegativeTest(
7674 new String[] {
7675 "X.java",
7676 "public class X {\n" +
7677 " void foo(Object o) {\n" +
7678 " for (o.toString(); o == null;) { /* */ }\n" + // complain: protected then unchanged
7679 " }\n" +
7680 "}\n"},
7681 "----------\n" +
7682 "1. ERROR in X.java (at line 3)\n" +
7683 " for (o.toString(); o == null;) { /* */ }\n" +
7684 " ^\n" +
7685 "Null comparison always yields false: The variable o cannot be null at this location\n" +
7686 "----------\n",
7687 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7688 }
7689
7690 // null analysis -- for
test0710_for()7691 public void test0710_for() {
7692 this.runNegativeTest(
7693 new String[] {
7694 "X.java",
7695 "public class X {\n" +
7696 " boolean bar() {\n" +
7697 " return true;\n" +
7698 " }\n" +
7699 " void foo(Object o) {\n" +
7700 " o = null;\n" +
7701 " for (o.toString(); bar();) {\n" +
7702 " }\n" +
7703 " }\n" +
7704 "}\n"},
7705 "----------\n" +
7706 "1. ERROR in X.java (at line 7)\n" +
7707 " for (o.toString(); bar();) {\n" +
7708 " ^\n" +
7709 "Null pointer access: The variable o can only be null at this location\n" +
7710 "----------\n",
7711 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7712 }
7713
7714 // null analysis -- for
test0711_for()7715 public void test0711_for() {
7716 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7717 this.runNegativeTest(
7718 new String[] {
7719 "X.java",
7720 "public class X {\n" +
7721 " void foo() {\n" +
7722 " Object t[] = null;\n" +
7723 " for (Object o : t) {/* */}\n" +
7724 // complain: NPE
7725 " }\n" +
7726 "}\n"},
7727 "----------\n" +
7728 "1. ERROR in X.java (at line 4)\n" +
7729 " for (Object o : t) {/* */}\n" +
7730 " ^\n" +
7731 "Null pointer access: The variable t can only be null at this location\n" +
7732 "----------\n",
7733 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7734 }
7735 }
7736
7737 // null analysis -- for
test0712_for()7738 public void test0712_for() {
7739 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7740 this.runNegativeTest(
7741 new String[] {
7742 "X.java",
7743 "public class X {\n" +
7744 " void foo() {\n" +
7745 " Iterable i = null;\n" +
7746 " for (Object o : i) {/* */}\n" +
7747 // complain: NPE
7748 " }\n" +
7749 "}\n"},
7750 "----------\n" +
7751 "1. ERROR in X.java (at line 4)\n" +
7752 " for (Object o : i) {/* */}\n" +
7753 " ^\n" +
7754 "Null pointer access: The variable i can only be null at this location\n" +
7755 "----------\n",
7756 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7757 }
7758 }
7759
7760 // null analysis -- for
test0713_for()7761 public void test0713_for() {
7762 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7763 this.runConformTest(
7764 new String[] {
7765 "X.java",
7766 "public class X {\n" +
7767 " void foo() {\n" +
7768 " Object t[] = new Object[1];\n" +
7769 " for (Object o : t) {/* */}\n" +
7770 " }\n" +
7771 "}\n"},
7772 "");
7773 }
7774 }
7775
7776 // null analysis -- for
test0714_for()7777 public void test0714_for() {
7778 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7779 this.runConformTest(
7780 new String[] {
7781 "X.java",
7782 "public class X {\n" +
7783 " void foo() {\n" +
7784 " Iterable i = new java.util.Vector<Object>();\n" +
7785 " for (Object o : i) {/* */}\n" +
7786 " }\n" +
7787 "}\n"},
7788 "");
7789 }
7790 }
7791
7792 // null analysis -- for
test0715_for()7793 public void test0715_for() {
7794 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7795 this.runNegativeTest(
7796 new String[] {
7797 "X.java",
7798 "public class X {\n" +
7799 " void foo() {\n" +
7800 " Iterable i = new java.util.Vector<Object>();\n" +
7801 " Object flag = null;\n" +
7802 " for (Object o : i) {\n" +
7803 " flag = new Object();\n" +
7804 " }\n" +
7805 " flag.toString();\n" +
7806 // complain: cannot know if at least one iteration got executed
7807 " }\n" +
7808 "}\n"},
7809 "----------\n" +
7810 "1. ERROR in X.java (at line 8)\n" +
7811 " flag.toString();\n" +
7812 " ^^^^\n" +
7813 "Potential null pointer access: The variable flag may be null at this location\n" +
7814 "----------\n",
7815 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7816 }
7817 }
7818
7819 // null analysis -- for
test0716_for()7820 public void test0716_for() {
7821 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7822 this.runNegativeTest(
7823 new String[] {
7824 "X.java",
7825 "public class X {\n" +
7826 " void foo() {\n" +
7827 " Iterable i = new java.util.Vector<Object>();\n" +
7828 " Object flag = null;\n" +
7829 " for (Object o : i) { /* */ }\n" +
7830 " flag.toString();\n" +
7831 " }\n" +
7832 "}\n"},
7833 "----------\n" +
7834 "1. ERROR in X.java (at line 6)\n" +
7835 " flag.toString();\n" +
7836 " ^^^^\n" +
7837 "Null pointer access: The variable flag can only be null at this location\n" +
7838 "----------\n",
7839 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7840 }
7841 }
7842
7843 // null analysis -- for
test0717_for()7844 public void test0717_for() {
7845 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
7846 this.runNegativeTest(
7847 new String[] {
7848 "X.java",
7849 "public class X {\n" +
7850 " void foo(boolean dummy) {\n" +
7851 " Object flag = null;\n" +
7852 " for (;dummy;) {\n" +
7853 " flag = new Object();\n" +
7854 " }\n" +
7855 " flag.toString();\n" +
7856 " }\n" +
7857 "}\n"},
7858 "----------\n" +
7859 "1. ERROR in X.java (at line 7)\n" +
7860 " flag.toString();\n" +
7861 " ^^^^\n" +
7862 "Potential null pointer access: The variable flag may be null at this location\n" +
7863 "----------\n",
7864 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7865 }
7866 }
7867
7868 // null analysis -- for
test0718_for()7869 public void test0718_for() {
7870 this.runNegativeTest(
7871 new String[] {
7872 "X.java",
7873 "public class X {\n" +
7874 " void foo(boolean dummy) {\n" +
7875 " Object flag = null;\n" +
7876 " for (;dummy;) { /* */ }\n" +
7877 " flag.toString();\n" +
7878 " }\n" +
7879 "}\n"},
7880 "----------\n" +
7881 "1. ERROR in X.java (at line 5)\n" +
7882 " flag.toString();\n" +
7883 " ^^^^\n" +
7884 "Null pointer access: The variable flag can only be null at this location\n" +
7885 "----------\n",
7886 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7887 }
7888
7889 // null analysis -- for
7890 // origin: AssignmentTest#test019
test0719_for()7891 public void test0719_for() {
7892 this.runConformTest(
7893 new String[] {
7894 "X.java",
7895 "public class X {\n" +
7896 " public static final char[] foo(char[] a, char c1, char c2) {\n" +
7897 " char[] r = null;\n" +
7898 " for (int i = 0, length = a.length; i < length; i++) {\n" +
7899 " char c = a[i];\n" +
7900 " if (c == c1) {\n" +
7901 " if (r == null) {\n" +
7902 " r = new char[length];\n" +
7903 " }\n" +
7904 " r[i] = c2;\n" +
7905 " } else if (r != null) {\n" +
7906 " r[i] = c;\n" +
7907 " }\n" +
7908 " }\n" +
7909 " if (r == null) return a;\n" +
7910 " return r;\n" +
7911 " }\n" +
7912 "}\n"},
7913 "");
7914 }
7915
7916 // null analysis -- for
test0720_for_continue_break()7917 public void test0720_for_continue_break() {
7918 this.runNegativeTest(
7919 new String[] {
7920 "X.java",
7921 "public class X {\n" +
7922 " void foo() {\n" +
7923 " Object o = new Object();\n" +
7924 " for (int i = 0; i < 10; i++) {\n" +
7925 " if (o == null) {\n" + // complain: o cannot be null
7926 " continue;\n" +
7927 " }\n" +
7928 " o = null;\n" +
7929 " break;\n" +
7930 " }\n" +
7931 " }\n" +
7932 "}\n"},
7933 "----------\n" +
7934 "1. ERROR in X.java (at line 5)\n" +
7935 " if (o == null) {\n" +
7936 " ^\n" +
7937 "Null comparison always yields false: The variable o cannot be null at this location\n" +
7938 "----------\n",
7939 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7940 }
7941
7942 // null analysis -- for
test0721_for()7943 public void test0721_for() {
7944 this.runConformTest(
7945 new String[] {
7946 "X.java",
7947 "public class X {\n" +
7948 " void foo(boolean b) {\n" +
7949 " Object o = null;\n" +
7950 " for (; b ? (o = new Object()).equals(o) : false ;) {\n" +
7951 // contrast this with test0238; here the condition shades doubts
7952 // upon o being null
7953 " /* */\n" +
7954 " }\n" +
7955 " if (o == null) { /* */ }\n" +
7956 " }\n" +
7957 "}\n"},
7958 "");
7959 }
7960
7961 // null analysis -- for
test0722_for_return()7962 public void test0722_for_return() {
7963 this.runNegativeTest(
7964 new String[] {
7965 "X.java",
7966 "public class X {\n" +
7967 " void foo (boolean b) {\n" +
7968 " Object o = null;\n" +
7969 " for (int i = 0; i < 25; i++) {\n" +
7970 " if (b) {\n" +
7971 " if (o == null) {\n" +
7972 " o = new Object();\n" + // cleared by return downstream
7973 " }\n" +
7974 " return;\n" +
7975 " }\n" +
7976 " }\n" +
7977 " }\n" +
7978 "}\n"},
7979 "----------\n" +
7980 "1. ERROR in X.java (at line 6)\n" +
7981 " if (o == null) {\n" +
7982 " ^\n" +
7983 "Redundant null check: The variable o can only be null at this location\n" +
7984 "----------\n",
7985 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
7986 }
7987
7988 // null analysis -- for
test0723_for()7989 public void test0723_for() {
7990 this.runConformTest(
7991 new String[] {
7992 "X.java",
7993 "public class X {\n" +
7994 " void foo () {\n" +
7995 " Object o[] = new Object[1];\n" +
7996 " for (int i = 0; i < 1; i++) {\n" +
7997 " if (i < 1) {\n" +
7998 " o[i].toString();\n" +
7999 " }\n" +
8000 " }\n" +
8001 " }\n" +
8002 "}\n"},
8003 "");
8004 }
8005
8006 // null analysis -- for
test0724_for_with_initialization()8007 public void test0724_for_with_initialization() {
8008 this.runConformTest(
8009 new String[] {
8010 "X.java",
8011 "public class X {\n" +
8012 " X field;\n" +
8013 " void foo(X x1) {\n" +
8014 " // X x2;\n" +
8015 " outer: for (int i = 0; i < 30; i++) {\n" +
8016 " X x2 = x1;\n" +
8017 " do {\n" +
8018 " if (x2.equals(x1)) {\n" +
8019 " continue outer;\n" +
8020 " }\n" +
8021 " x2 = x2.field;\n" +
8022 " } while (x2 != null);\n" +
8023 " }\n" +
8024 " }\n" +
8025 "}\n"},
8026 "");
8027 }
8028
8029 // null analysis -- for
test0725_for_with_assignment()8030 public void test0725_for_with_assignment() {
8031 this.runConformTest(
8032 new String[] {
8033 "X.java",
8034 "public class X {\n" +
8035 " X field;\n" +
8036 " void foo(X x1) {\n" +
8037 " X x2;\n" +
8038 " outer: for (int i = 0; i < 30; i++) {\n" +
8039 " x2 = x1;\n" +
8040 " do {\n" +
8041 " if (x2.equals(x1)) {\n" +
8042 " continue outer;\n" +
8043 " }\n" +
8044 " x2 = x2.field;\n" +
8045 " } while (x2 != null);\n" +
8046 " }\n" +
8047 " }\n" +
8048 "}\n"},
8049 "");
8050 }
8051
8052 // null analysis -- for
8053 // changed with https://bugs.eclipse.org/bugs/show_bug.cgi?id=127570
8054 // we are now able to see that x2 is reinitialized with x1, which is unknown
test0726_for()8055 public void test0726_for() {
8056 this.runConformTest(
8057 new String[] {
8058 "X.java",
8059 "public class X {\n" +
8060 " void foo(X x1) {\n" +
8061 " X x2 = null;\n" +
8062 " for (int i = 0; i < 5; i++) {\n" +
8063 " if (x2 == null) {\n" +
8064 " x2 = x1;\n" +
8065 " }\n" +
8066 " x2.toString();\n" +
8067 " }\n" +
8068 " }\n" +
8069 "}\n"},
8070 "");
8071 }
8072
8073 // null analysis -- for
test0727_for()8074 public void test0727_for() {
8075 this.runConformTest(
8076 new String[] {
8077 "X.java",
8078 "public class X {\n" +
8079 " void foo() {\n" +
8080 " for (; true;) { /* */ }\n" +
8081 " }\n" +
8082 "}\n"},
8083 "");
8084 }
8085
8086 // null analysis -- for
test0728_for()8087 public void test0728_for() {
8088 this.runNegativeTest(
8089 new String[] {
8090 "X.java",
8091 "public class X {\n" +
8092 " void foo(X x) {\n" +
8093 " for (; true; x.toString()) { /* */ }\n" +
8094 " if (x == null) { /* */ }\n" + // complain
8095 " }\n" +
8096 "}\n"},
8097 "----------\n" +
8098 "1. ERROR in X.java (at line 4)\n" +
8099 " if (x == null) { /* */ }\n" +
8100 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
8101 "Unreachable code\n" +
8102 "----------\n");
8103 }
8104
8105 // null analysis -- for
test0729_for_try_catch_finally()8106 public void test0729_for_try_catch_finally() {
8107 this.runConformTest(
8108 new String[] {
8109 "X.java",
8110 "import java.io.IOException;\n" +
8111 "class X {\n" +
8112 " X f;\n" +
8113 " void bar() throws IOException {\n" +
8114 " throw new IOException();\n" +
8115 " }\n" +
8116 " void foo(boolean b) {\n" +
8117 " for (int i = 0 ; i < 5 ; i++) {\n" +
8118 " X x = this.f;\n" +
8119 " if (x == null) { \n" +
8120 " continue;\n" +
8121 " }\n" +
8122 " if (b) {\n" +
8123 " try {\n" +
8124 " bar();\n" +
8125 " } \n" +
8126 " catch(IOException e) { /* */ }\n" +
8127 " finally {\n" +
8128 " x.toString();\n" +
8129 " }\n" +
8130 " }\n" +
8131 " }\n" +
8132 " }\n" +
8133 "}"},
8134 "");
8135 }
8136
8137 // null analysis - for
test0730_for()8138 public void test0730_for() {
8139 this.runNegativeTest(
8140 new String[] {
8141 "X.java",
8142 "class X {\n" +
8143 " void foo(Object o) {\n" +
8144 " for ( ; o == null ; ) {\n" +
8145 " o = new Object();\n" +
8146 " }\n" +
8147 " if (o != null) { /* */ }\n" + // complain
8148 " }\n" +
8149 "}"},
8150 "----------\n" +
8151 "1. ERROR in X.java (at line 6)\n" +
8152 " if (o != null) { /* */ }\n" +
8153 " ^\n" +
8154 "Redundant null check: The variable o cannot be null at this location\n" +
8155 "----------\n",
8156 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8157 }
8158
8159 // null analysis - for
test0731_for()8160 public void test0731_for() {
8161 this.runNegativeTest(
8162 new String[] {
8163 "X.java",
8164 "class X {\n" +
8165 " X bar() {\n" +
8166 " return new X();\n" +
8167 " }\n" +
8168 " void foo(Object o) {\n" +
8169 " for ( ; o == null ; ) {\n" +
8170 " o = bar();\n" +
8171 " }\n" +
8172 " if (o != null) { /* */ }\n" + // complain
8173 " }\n" +
8174 "}"},
8175 "----------\n" +
8176 "1. ERROR in X.java (at line 9)\n" +
8177 " if (o != null) { /* */ }\n" +
8178 " ^\n" +
8179 "Redundant null check: The variable o cannot be null at this location\n" +
8180 "----------\n",
8181 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8182 }
8183
8184 // null analysis - for nested with break
8185 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=129371
test0732_for_nested_break()8186 public void test0732_for_nested_break() {
8187 this.runConformTest(
8188 new String[] {
8189 "X.java",
8190 "public class X {\n" +
8191 " void foo(String doubt) {\n" +
8192 " for(int i = 0; i < 10; i++) {\n" +
8193 " String s = doubt;\n" +
8194 " if(s != null) {\n" +
8195 " for(int j = 0; j < 1; j++) {\n" +
8196 " break;\n" +
8197 " }\n" +
8198 " s.length();\n" +
8199 " }\n" +
8200 " }\n" +
8201 " }\n" +
8202 "}\n" +
8203 "\n"},
8204 "");
8205 }
8206
8207 // null analysis - for while with break
8208 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=129371
8209 // variant
test0733_for_while_break()8210 public void test0733_for_while_break() {
8211 this.runConformTest(
8212 new String[] {
8213 "X.java",
8214 "public class X {\n" +
8215 " void foo(String doubt, boolean b) {\n" +
8216 " for(int i = 0; i < 10; i++) {\n" +
8217 " String s = doubt;\n" +
8218 " if (s != null) {\n" +
8219 " while (b) {\n" +
8220 " break;\n" +
8221 " }\n" +
8222 " s.length();\n" +
8223 " }\n" +
8224 " }\n" +
8225 " }\n" +
8226 "}\n" +
8227 "\n"},
8228 "");
8229 }
8230
8231 // null analysis - for while with break
8232 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=129371
8233 // variant
test0734_for_while_break()8234 public void test0734_for_while_break() {
8235 this.runConformTest(
8236 new String[] {
8237 "X.java",
8238 "public class X {\n" +
8239 " void foo(String doubt, boolean b) {\n" +
8240 " for(int i = 0; i < 10; i++) {\n" +
8241 " String s = doubt;\n" +
8242 " if (s != null) {\n" +
8243 " do {\n" +
8244 " break;\n" +
8245 " } while (b);\n" +
8246 " s.length();\n" +
8247 " }\n" +
8248 " }\n" +
8249 " }\n" +
8250 "}\n" +
8251 "\n"},
8252 "");
8253 }
8254
8255 // null analysis - for nested with break
8256 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=129371
8257 // variant
test0735_for_nested_break()8258 public void test0735_for_nested_break() {
8259 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
8260 this.runConformTest(
8261 new String[] {
8262 "X.java",
8263 "public class X {\n" +
8264 " void foo(Object[] a, String doubt) {\n" +
8265 " for(int i = 0; i < 10; i++) {\n" +
8266 " String s = doubt;\n" +
8267 " if(s != null) {\n" +
8268 " for(Object o : a) {\n" +
8269 " break;\n" +
8270 " }\n" +
8271 " s.length();\n" +
8272 " }\n" +
8273 " }\n" +
8274 " }\n" +
8275 "}\n" +
8276 "\n"},
8277 "");
8278 }
8279 }
8280
8281 // null analysis - for
8282 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127570
test0736_for_embedded_lazy_init()8283 public void test0736_for_embedded_lazy_init() {
8284 this.runConformTest(
8285 new String[] {
8286 "X.java",
8287 "class X {\n" +
8288 " public boolean foo() {\n" +
8289 " Boolean b = null;\n" +
8290 " for (int i = 0; i < 1; i++) {\n" +
8291 " if (b == null) {\n" +
8292 " b = Boolean.TRUE;\n" +
8293 " }\n" +
8294 " if (b.booleanValue()) {\n" + // quiet
8295 " return b.booleanValue();\n" +
8296 " }\n" +
8297 " }\n" +
8298 " return false;\n" +
8299 " }\n" +
8300 "}"},
8301 "");
8302 }
8303
8304 // null analysis - for with unknown protection: unknown cannot protect anything
8305 // suggested by https://bugs.eclipse.org/bugs/show_bug.cgi?id=127570
test0737_for_unknown_protection()8306 public void test0737_for_unknown_protection() {
8307 this.runNegativeTest(
8308 new String[] {
8309 "X.java",
8310 "class X {\n" +
8311 " public boolean foo(Boolean p) {\n" +
8312 " Boolean b = null;\n" +
8313 " for (int i = 0; i < 1; i++) {\n" +
8314 " if (b == p) {\n" + // tells us that p is null as well
8315 " // empty\n" +
8316 " }\n" +
8317 " else {\n" +
8318 " continue;\n" +
8319 " }\n" +
8320 " if (b.booleanValue()) {\n" + // complain b can only be null
8321 " return b.booleanValue();\n" +
8322 " }\n" +
8323 " }\n" +
8324 " return false;\n" +
8325 " }\n" +
8326 "}"},
8327 "----------\n" +
8328 "1. ERROR in X.java (at line 11)\n" +
8329 " if (b.booleanValue()) {\n" +
8330 " ^\n" +
8331 "Null pointer access: The variable b can only be null at this location\n" +
8332 "----------\n",
8333 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8334 }
8335
8336 // null analysis - for with unknown protection
8337 // suggested by https://bugs.eclipse.org/bugs/show_bug.cgi?id=127570
8338 // the issue is that we cannot do less than full aliasing analysis to
8339 // catch this one
8340 // PREMATURE (maxime) reconsider when/if we bring full aliasing in
_test0738_for_unknown_protection()8341 public void _test0738_for_unknown_protection() {
8342 this.runConformTest(
8343 new String[] {
8344 "X.java",
8345 "class X {\n" +
8346 " public boolean foo(Boolean p) {\n" +
8347 " Boolean b = null;\n" +
8348 " for (int i = 0; i < 1; i++) {\n" +
8349 " if (b == p) {\n" +
8350 " // empty\n" +
8351 " }\n" +
8352 " else {\n" +
8353 " b = p;\n" +
8354 " }\n" +
8355 " if (b.booleanValue()) {\n" + // quiet because b is an alias for p, unknown
8356 " return b.booleanValue();\n" +
8357 " }\n" +
8358 " }\n" +
8359 " return false;\n" +
8360 " }\n" +
8361 "}"},
8362 "");
8363 }
8364
8365 // null analysis -- for
8366 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=178895
test0739_for()8367 public void test0739_for() {
8368 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
8369 this.runConformTest(
8370 new String[] {
8371 "X.java",
8372 "import java.util.List;\n" +
8373 "public class X {\n" +
8374 " void foo(List<Object> l, boolean b) {\n" +
8375 " for (Object o : l) {\n" +
8376 " if (b) {\n" +
8377 " if (o != null) {\n" +
8378 " return;\n" +
8379 " }\n" +
8380 " } else {\n" +
8381 " o.toString();\n" +
8382 " }\n" +
8383 " }\n" +
8384 " }\n" +
8385 "}\n"},
8386 "");
8387 }
8388 }
8389
8390 // null analysis - for
8391 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
8392 // variant
test0740_for_explicit_label()8393 public void test0740_for_explicit_label() {
8394 this.runConformTest(
8395 new String[] {
8396 "X.java",
8397 "public class X {\n" +
8398 " void foo(int i) {\n" +
8399 " Object o = null;\n" +
8400 " done: for (;;) {\n" +
8401 " switch (i) {\n" +
8402 " case 0:\n" +
8403 " o = new Object();\n" +
8404 " break;\n" +
8405 " case 1:\n" +
8406 " break done;\n" +
8407 " }\n" +
8408 " }\n" +
8409 " if (o == null) {\n" +
8410 " }\n" +
8411 " }\n" +
8412 "}\n"},
8413 "");
8414 }
8415
8416 // null analysis - for
8417 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
8418 // variant
test0741_for_explicit_label()8419 public void test0741_for_explicit_label() {
8420 this.runConformTest(
8421 new String[] {
8422 "X.java",
8423 "public class X {\n" +
8424 " boolean test() {\n" +
8425 " return true;\n" +
8426 " }\n" +
8427 " void foo() {\n" +
8428 " Object o = null;\n" +
8429 " done: for (;;) {\n" +
8430 " if (test()) {\n" +
8431 " break done;\n" +
8432 " }\n" +
8433 " o = new Object();\n" +
8434 " }\n" +
8435 " if (o == null) {\n" +
8436 " }\n" +
8437 " }\n" +
8438 "}\n"},
8439 "");
8440 }
8441
8442 // null analysis - for
8443 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=176472
8444 // variant
test0742_for_explicit_label()8445 public void test0742_for_explicit_label() {
8446 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
8447 this.runConformTest(
8448 new String[] {
8449 "X.java",
8450 "import java.util.List;\n" +
8451 "public class X {\n" +
8452 " void foo(int i, List<Object> l) {\n" +
8453 " Object o = null;\n" +
8454 " done: for (Object j: l) {\n" +
8455 " switch (i) {\n" +
8456 " case 0:\n" +
8457 " o = new Object();\n" +
8458 " break;\n" +
8459 " case 1:\n" +
8460 " break done;\n" +
8461 " }\n" +
8462 " }\n" +
8463 " if (o == null) {\n" +
8464 " }\n" +
8465 " }\n" +
8466 "}\n"},
8467 "");
8468 }
8469 }
8470
8471 // null analysis - for
8472 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
test0743_for_infinite()8473 public void test0743_for_infinite() {
8474 this.runConformTest(
8475 new String[] {
8476 "X.java",
8477 "public class X {\n" +
8478 " public void test(String[] a) {\n" +
8479 " String key = null;\n" +
8480 " for( int i = 0; ; i++ )\n" +
8481 " {\n" +
8482 " if (a[i] == null)\n" +
8483 " break;\n" +
8484 " key = a[i];\n" +
8485 " }\n" +
8486 " if (key != null) {\n" +
8487 " // empty\n" +
8488 " }\n" +
8489 " }\n" +
8490 "}"},
8491 "");
8492 }
8493
8494 // null analysis - for
8495 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=184298
8496 // variant
test0744_for_infinite()8497 public void test0744_for_infinite() {
8498 this.runConformTest(
8499 new String[] {
8500 "X.java",
8501 "public class X {\n" +
8502 " public void test(String[] a) {\n" +
8503 " String key = null;\n" +
8504 " loop: for( int i = 0; ; i++ )\n" +
8505 " {\n" +
8506 " if (a[i] == null)\n" +
8507 " break loop;\n" +
8508 " key = a[i];\n" +
8509 " }\n" +
8510 " if (key != null) {\n" +
8511 " // empty\n" +
8512 " }\n" +
8513 " }\n" +
8514 "}"},
8515 "");
8516 }
8517
8518 // null analysis - for
8519 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=195638
test0746_for_try_catch()8520 public void test0746_for_try_catch() {
8521 runNegativeNullTest(
8522 new String[] {
8523 "X.java",
8524 "public class X {\n" +
8525 " void foo() {\n" +
8526 " String str = null;\n" +
8527 " for (int i = 0; i < 2; i++) {\n" +
8528 " try {\n" +
8529 " str = new String(\"Test\");\n" +
8530 " } catch (Exception ex) {\n" +
8531 " ex.printStackTrace();\n" +
8532 " }\n" +
8533 " str.charAt(i);\n" +
8534 " str = null;\n" +
8535 " }\n" +
8536 " }\n" +
8537 "}\n"
8538 },
8539 "----------\n" +
8540 "1. ERROR in X.java (at line 10)\n" +
8541 " str.charAt(i);\n" +
8542 " ^^^\n" +
8543 "Potential null pointer access: The variable str may be null at this location\n" +
8544 "----------\n");
8545 }
8546
8547 // null analysis - for
8548 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=195638
8549 // variant: do not reset to null
test0747_for_try_catch()8550 public void test0747_for_try_catch() {
8551 runNegativeTest(
8552 new String[] {
8553 "X.java",
8554 "public class X {\n" +
8555 " void foo() {\n" +
8556 " String str = null;\n" +
8557 " for (int i = 0; i < 2; i++) {\n" +
8558 " try {\n" +
8559 " str = new String(\"Test\");\n" +
8560 " } catch (Exception ex) {\n" +
8561 " ex.printStackTrace();\n" +
8562 " }\n" +
8563 " str.charAt(i);\n" +
8564 " }\n" +
8565 " }\n" +
8566 "}\n"
8567 },
8568 "----------\n" +
8569 "1. ERROR in X.java (at line 10)\n" +
8570 " str.charAt(i);\n" +
8571 " ^^^\n" +
8572 "Potential null pointer access: The variable str may be null at this location\n" +
8573 "----------\n" /* expectedCompilerLog */,
8574 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8575 }
8576
8577 // null analysis -- switch
test0800_switch()8578 public void test0800_switch() {
8579 this.runConformTest(
8580 new String[] {
8581 "X.java",
8582 "public class X {\n" +
8583 " int k;\n" +
8584 " void foo() {\n" +
8585 " Object o = null;\n" +
8586 " switch (k) {\n" +
8587 " case 0 :\n" +
8588 " o = new Object();\n" +
8589 " break;\n" +
8590 " case 2 :\n" +
8591 " return;\n" +
8592 " }\n" +
8593 " if(o == null) { /* */ }\n" + // quiet: don't know whether came from 0 or default
8594 " }\n" +
8595 "}\n"},
8596 "");
8597 }
8598
8599 // null analysis -- switch
test0801_switch()8600 public void test0801_switch() {
8601 this.runNegativeTest(
8602 new String[] {
8603 "X.java",
8604 "public class X {\n" +
8605 " int k;\n" +
8606 " void foo() {\n" +
8607 " Object o = null;\n" +
8608 " switch (k) {\n" +
8609 " case 0 :\n" +
8610 " o = new Object();\n" +
8611 " break;\n" +
8612 " default :\n" +
8613 " return;\n" +
8614 " }\n" +
8615 " if(o == null) { /* */ }\n" + // complain: only get there through 0, o non null
8616 " }\n" +
8617 "}\n"},
8618 "----------\n" +
8619 "1. ERROR in X.java (at line 12)\n" +
8620 " if(o == null) { /* */ }\n" +
8621 " ^\n" +
8622 "Null comparison always yields false: The variable o cannot be null at this location\n" +
8623 "----------\n" +
8624 "2. WARNING in X.java (at line 12)\n" +
8625 " if(o == null) { /* */ }\n" +
8626 " ^^^^^^^^^\n" +
8627 "Dead code\n" +
8628 "----------\n",
8629 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8630 }
8631
8632 // null analysis -- switch
test0802_switch()8633 public void test0802_switch() {
8634 this.runNegativeTest(
8635 new String[] {
8636 "X.java",
8637 "public class X {\n" +
8638 " int k;\n" +
8639 " void foo() {\n" +
8640 " Object o = null;\n" +
8641 " switch (k) {\n" +
8642 " case 0 :\n" +
8643 " o.toString();\n" + // complain: o can only be null
8644 " break;\n" +
8645 " }\n" +
8646 " }\n" +
8647 "}\n"},
8648 "----------\n" +
8649 "1. ERROR in X.java (at line 7)\n" +
8650 " o.toString();\n" +
8651 " ^\n" +
8652 "Null pointer access: The variable o can only be null at this location\n" +
8653 "----------\n",
8654 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8655 }
8656
8657 // null analysis -- switch
test0803_switch()8658 public void test0803_switch() {
8659 this.runNegativeTest(
8660 new String[] {
8661 "X.java",
8662 "public class X {\n" +
8663 " int k;\n" +
8664 " void foo() {\n" +
8665 " Object o = null;\n" +
8666 " switch (k) {\n" +
8667 " case 0 :\n" +
8668 " o = new Object();\n" +
8669 " case 1 :\n" +
8670 " o.toString();\n" + // complain: may come through 0 or 1
8671 " break;\n" +
8672 " }\n" +
8673 " }\n" +
8674 "}\n"},
8675 "----------\n" +
8676 "1. ERROR in X.java (at line 9)\n" +
8677 " o.toString();\n" +
8678 " ^\n" +
8679 "Potential null pointer access: The variable o may be null at this location\n" +
8680 "----------\n",
8681 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8682 }
8683
8684 // null analysis -- switch
test0804_switch()8685 public void test0804_switch() {
8686 this.runNegativeTest(
8687 new String[] {
8688 "X.java",
8689 "public class X {\n" +
8690 " void foo (Object o, int info) {\n" +
8691 " o = null;\n" +
8692 " switch (info) {\n" +
8693 " case 0 :\n" +
8694 " o = new Object();\n" +
8695 " break;\n" +
8696 " case 1 :\n" +
8697 " o = new String();\n" +
8698 " break;\n" +
8699 " default :\n" +
8700 " o = new X();\n" +
8701 " break;\n" +
8702 " }\n" +
8703 " if(o != null) { /* */ }\n" + // complain: all branches allocate a new o
8704 " }\n" +
8705 "}\n"},
8706 "----------\n" +
8707 "1. ERROR in X.java (at line 15)\n" +
8708 " if(o != null) { /* */ }\n" +
8709 " ^\n" +
8710 "Redundant null check: The variable o cannot be null at this location\n" +
8711 "----------\n",
8712 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8713 }
8714
8715 // null analysis -- switch
test0805_switch()8716 public void test0805_switch() {
8717 this.runConformTest(
8718 new String[] {
8719 "X.java",
8720 "public class X {\n" +
8721 " void foo(X p) {\n" +
8722 " X x = this;\n" +
8723 " for (int i = 0; i < 5; i++) {\n" +
8724 " switch (i) {\n" +
8725 " case 1:\n" +
8726 " x = p;\n" +
8727 " }\n" +
8728 " }\n" +
8729 " if (x != null) { /* */ }\n" +
8730 " }\n" +
8731 "}\n"},
8732 "");
8733 }
8734
8735 // null analysis -- non null protection tag
_test0900_non_null_protection_tag()8736 public void _test0900_non_null_protection_tag() {
8737 this.runConformTest(
8738 new String[] {
8739 "X.java",
8740 "public class X {\n" +
8741 " void foo(Object o) {\n" +
8742 " boolean b = o != null;\n" + // shades doubts upon o
8743 " o/*NN*/.toString();\n" + // protection => do not complain
8744 " o.toString();\n" + // protected by previous line
8745 " }\n" +
8746 "}\n"},
8747 "");
8748 }
8749
8750 // null analysis -- non null protection tag
_test0901_non_null_protection_tag()8751 public void _test0901_non_null_protection_tag() {
8752 this.runConformTest(
8753 new String[] {
8754 "X.java",
8755 "public class X {\n" +
8756 " void foo(Object o, boolean b) {\n" +
8757 " if (b) {\n" +
8758 " o = null;\n" +
8759 " }\n" +
8760 " o/*NN*/.toString();\n" +
8761 " if (b) {\n" +
8762 " o = null;\n" +
8763 " }\n" +
8764 " o/*\n" +
8765 " NN comment */.toString();\n" +
8766 " if (b) {\n" +
8767 " o = null;\n" +
8768 " }\n" +
8769 " o/* NN\n" +
8770 " */.toString();\n" +
8771 " if (b) {\n" +
8772 " o = null;\n" +
8773 " }\n" +
8774 " o // NN \n" +
8775 " .toString();\n" +
8776 " }\n" +
8777 "}\n"},
8778 "");
8779 }
8780
8781 // null analysis -- non null protection tag
_test0902_non_null_protection_tag()8782 public void _test0902_non_null_protection_tag() {
8783 this.runConformTest(
8784 new String[] {
8785 "X.java",
8786 "public class X {\n" +
8787 " void foo(Object o, boolean b) {\n" +
8788 " if (b) {\n" +
8789 " o = null;\n" +
8790 " }\n" +
8791 " o/*NON-NULL*/.toString();\n" +
8792 " if (b) {\n" +
8793 " o = null;\n" +
8794 " }\n" +
8795 " o/* NON-NULL comment */.toString();\n" +
8796 " if (b) {\n" +
8797 " o = null;\n" +
8798 " }\n" +
8799 " o/* NON-NULL \n" +
8800 " */.toString();\n" +
8801 " if (b) {\n" +
8802 " o = null;\n" +
8803 " }\n" +
8804 " o // NON-NULL \n" +
8805 " .toString();\n" +
8806 " }\n" +
8807 "}\n"},
8808 "");
8809 }
8810
8811 // null analysis -- non null protection tag
test0903_non_null_protection_tag()8812 public void test0903_non_null_protection_tag() {
8813 this.runNegativeTest(
8814 new String[] {
8815 "X.java",
8816 "public class X {\n" +
8817 " void foo(Object o, boolean b) {\n" +
8818 " if (b) {\n" +
8819 " o = null;\n" +
8820 " }\n" +
8821 " o/*N N*/.toString();\n" +
8822 " if (b) {\n" +
8823 " o = null;\n" +
8824 " }\n" +
8825 " o/*NNa*/.toString();\n" +
8826 " if (b) {\n" +
8827 " o = null;\n" +
8828 " }\n" +
8829 " o/*aNN */.toString();\n" +
8830 " if (b) {\n" +
8831 " o = null;\n" +
8832 " }\n" +
8833 " o/*NON NULL*/.toString();\n" +
8834 " if (b) {\n" +
8835 " o = null;\n" +
8836 " }\n" +
8837 " o/*Non-Null*/.toString();\n" +
8838 " if (b) {\n" +
8839 " o = null;\n" +
8840 " }\n" +
8841 " o/*aNON-NULL */.toString();\n" +
8842 " }\n" +
8843 "}\n"},
8844 "----------\n" +
8845 "1. ERROR in X.java (at line 6)\n" +
8846 " o/*N N*/.toString();\n" +
8847 " ^\n" +
8848 "Potential null pointer access: The variable o may be null at this location\n" +
8849 "----------\n" +
8850 "2. ERROR in X.java (at line 10)\n" +
8851 " o/*NNa*/.toString();\n" +
8852 " ^\n" +
8853 "Potential null pointer access: The variable o may be null at this location\n" +
8854 "----------\n" +
8855 "3. ERROR in X.java (at line 14)\n" +
8856 " o/*aNN */.toString();\n" +
8857 " ^\n" +
8858 "Potential null pointer access: The variable o may be null at this location\n" +
8859 "----------\n" +
8860 "4. ERROR in X.java (at line 18)\n" +
8861 " o/*NON NULL*/.toString();\n" +
8862 " ^\n" +
8863 "Potential null pointer access: The variable o may be null at this location\n" +
8864 "----------\n" +
8865 "5. ERROR in X.java (at line 22)\n" +
8866 " o/*Non-Null*/.toString();\n" +
8867 " ^\n" +
8868 "Potential null pointer access: The variable o may be null at this location\n" +
8869 "----------\n" +
8870 "6. ERROR in X.java (at line 26)\n" +
8871 " o/*aNON-NULL */.toString();\n" +
8872 " ^\n" +
8873 "Potential null pointer access: The variable o may be null at this location\n" +
8874 "----------\n",
8875 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8876 }
8877
8878
8879 // null analysis -- non null protection tag
test0905_non_null_protection_tag()8880 public void test0905_non_null_protection_tag() {
8881 this.runNegativeTest(
8882 new String[] {
8883 "X.java",
8884 "public class X {\n" +
8885 " void foo(Object o) {\n" +
8886 " boolean b = o != null;\n" + // shades doubts upon o
8887 " o.toString();/*NN*/\n" + // too late to protect => complain
8888 " }\n" +
8889 "}\n"},
8890 "----------\n" +
8891 "1. ERROR in X.java (at line 4)\n" +
8892 " o.toString();/*NN*/\n" +
8893 " ^\n" +
8894 "Potential null pointer access: The variable o may be null at this location\n" +
8895 "----------\n",
8896 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8897 }
8898
8899 // null analysis -- non null protection tag
test0906_non_null_protection_tag()8900 public void test0906_non_null_protection_tag() {
8901 this.runNegativeTest(
8902 new String[] {
8903 "X.java",
8904 "public class X {\n" +
8905 " void foo(Object o) {\n" +
8906 " boolean b = o != null;\n" + // shades doubts upon o
8907 " /*NN*/o.toString();\n" + // too soon to protect => complain
8908 " }\n" +
8909 "}\n"},
8910 "----------\n" +
8911 "1. ERROR in X.java (at line 4)\n" +
8912 " /*NN*/o.toString();\n" +
8913 " ^\n" +
8914 "Potential null pointer access: The variable o may be null at this location\n" +
8915 "----------\n",
8916 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8917 }
8918
8919 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
8920 // [compiler] Null reference analysis doesn't understand assertions
test0950_assert()8921 public void test0950_assert() {
8922 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8923 this.runConformTest(
8924 new String[] {
8925 "X.java",
8926 "public class X {\n" +
8927 " void foo(Object o) {\n" +
8928 " boolean b = o != null;\n" + // shades doubts upon o
8929 " assert(o != null);\n" + // protection
8930 " o.toString();\n" + // quiet
8931 " }\n" +
8932 "}\n"},
8933 "");
8934 }
8935 }
8936
8937 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
8938 // [compiler] Null reference analysis doesn't understand assertions
test0951_assert()8939 public void test0951_assert() {
8940 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8941 this.runNegativeTest(
8942 new String[] {
8943 "X.java",
8944 "public class X {\n" +
8945 " void foo(Object o) {\n" +
8946 " assert(o == null);\n" + // forces null
8947 " o.toString();\n" + // can only be null
8948 " }\n" +
8949 "}\n"},
8950 "----------\n" +
8951 "1. ERROR in X.java (at line 4)\n" +
8952 " o.toString();\n" +
8953 " ^\n" +
8954 "Null pointer access: The variable o can only be null at this location\n" +
8955 "----------\n",
8956 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8957 }
8958 }
8959
8960 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
8961 // [compiler] Null reference analysis doesn't understand assertions
test0952_assert()8962 public void test0952_assert() {
8963 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8964 this.runNegativeTest(
8965 new String[] {
8966 "X.java",
8967 "public class X {\n" +
8968 " void foo(Object o, boolean b) {\n" +
8969 " assert(o != null || b);\n" + // shade doubts
8970 " o.toString();\n" + // complain
8971 " }\n" +
8972 "}\n"},
8973 "----------\n" +
8974 "1. ERROR in X.java (at line 4)\n" +
8975 " o.toString();\n" +
8976 " ^\n" +
8977 "Potential null pointer access: The variable o may be null at this location\n" +
8978 "----------\n",
8979 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
8980 }
8981 }
8982
8983 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
8984 // [compiler] Null reference analysis doesn't understand assertions
test0953_assert_combined()8985 public void test0953_assert_combined() {
8986 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
8987 this.runNegativeTest(
8988 new String[] {
8989 "X.java",
8990 "public class X {\n" +
8991 " void foo(Object o1, Object o2) {\n" +
8992 " assert(o1 != null && o2 == null);\n" +
8993 " if (o1 == null) { };\n" + // complain
8994 " if (o2 == null) { };\n" + // complain
8995 " }\n" +
8996 "}\n"},
8997 "----------\n" +
8998 "1. ERROR in X.java (at line 4)\n" +
8999 " if (o1 == null) { };\n" +
9000 " ^^\n" +
9001 "Null comparison always yields false: The variable o1 cannot be null at this location\n" +
9002 "----------\n" +
9003 "2. WARNING in X.java (at line 4)\n" +
9004 " if (o1 == null) { };\n" +
9005 " ^^^\n" +
9006 "Dead code\n" +
9007 "----------\n" +
9008 "3. ERROR in X.java (at line 5)\n" +
9009 " if (o2 == null) { };\n" +
9010 " ^^\n" +
9011 "Redundant null check: The variable o2 can only be null at this location\n" +
9012 "----------\n",
9013 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9014 }
9015 }
9016
9017 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
9018 // [compiler] Null reference analysis doesn't understand assertions
test0954_assert_fake_reachable()9019 public void test0954_assert_fake_reachable() {
9020 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9021 runConformTest(
9022 true/*flush*/,
9023 new String[] {
9024 "X.java",
9025 "public class X {\n" +
9026 " void foo(Object o) {\n" +
9027 " assert(false && o != null);\n" +
9028 " if (o == null) { };\n" + // quiet
9029 " }\n" +
9030 "}\n"
9031 },
9032 "----------\n" +
9033 "1. WARNING in X.java (at line 3)\n" +
9034 " assert(false && o != null);\n" +
9035 " ^^^^^^^^^\n" +
9036 "Dead code\n" +
9037 "----------\n",
9038 "",
9039 "",
9040 JavacTestOptions.Excuse.EclipseHasSomeMoreWarnings);
9041 }
9042 }
9043
9044 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
9045 // [compiler] Null reference analysis doesn't understand assertions
test0955_assert_combined()9046 public void test0955_assert_combined() {
9047 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9048 this.runNegativeTest(
9049 new String[] {
9050 "X.java",
9051 "public class X {\n" +
9052 " void foo(Object o) {\n" +
9053 " assert(false || o != null);\n" +
9054 " if (o == null) { };\n" + // complain
9055 " }\n" +
9056 "}\n"},
9057 "----------\n" +
9058 "1. ERROR in X.java (at line 4)\n" +
9059 " if (o == null) { };\n" +
9060 " ^\n" +
9061 "Null comparison always yields false: The variable o cannot be null at this location\n" +
9062 "----------\n" +
9063 "2. WARNING in X.java (at line 4)\n" +
9064 " if (o == null) { };\n" +
9065 " ^^^\n" +
9066 "Dead code\n" +
9067 "----------\n",
9068 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9069 }
9070 }
9071
9072 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=127244
9073 // [compiler] Null reference analysis doesn't understand assertions
test0956_assert_combined()9074 public void test0956_assert_combined() {
9075 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9076 this.runNegativeTest(
9077 new String[] {
9078 "X.java",
9079 "public class X {\n" +
9080 " void foo() {\n" +
9081 " Object o = null;\n" +
9082 " assert(o != null);\n" + // complain
9083 " if (o == null) { };\n" + // complain
9084 " }\n" +
9085 "}\n"},
9086 "----------\n" +
9087 "1. ERROR in X.java (at line 4)\n" +
9088 " assert(o != null);\n" +
9089 " ^\n" +
9090 "Null comparison always yields false: The variable o can only be null at this location\n" +
9091 "----------\n" +
9092 "2. ERROR in X.java (at line 5)\n" +
9093 " if (o == null) { };\n" +
9094 " ^\n" +
9095 "Null comparison always yields false: The variable o cannot be null at this location\n" +
9096 "----------\n" +
9097 "3. WARNING in X.java (at line 5)\n" +
9098 " if (o == null) { };\n" +
9099 " ^^^\n" +
9100 "Dead code\n" +
9101 "----------\n",
9102 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9103 }
9104 }
9105
9106 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
9107 // Test to verify that asserts are exempted from redundant null check warnings,
9108 // but this doesn't affect the downstream info.
test0957_assert()9109 public void test0957_assert() {
9110 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9111 this.runNegativeTest(
9112 new String[] {
9113 "X.java",
9114 "public class X {\n" +
9115 " void m() {\n" +
9116 " X foo = new X();\n" +
9117 " assert (foo != null);\n" + //don't warn
9118 " if (foo == null) {}\n" +
9119 " X foo2 = new X();\n" +
9120 " assert (foo2 == null);\n" + //don't warn
9121 " if (foo2 == null) {}\n" +
9122 " X bar = null;\n" +
9123 " assert (bar == null);\n" + //don't warn
9124 " if (bar == null) {}\n" +
9125 " X bar2 = null;\n" +
9126 " assert (bar2 != null);\n" + //don't warn
9127 " if (bar2 == null) {}\n" +
9128 " }\n" +
9129 "}\n"},
9130 "----------\n" +
9131 "1. ERROR in X.java (at line 5)\n" +
9132 " if (foo == null) {}\n" +
9133 " ^^^\n" +
9134 "Null comparison always yields false: The variable foo cannot be null at this location\n" +
9135 "----------\n" +
9136 "2. WARNING in X.java (at line 5)\n" +
9137 " if (foo == null) {}\n" +
9138 " ^^\n" +
9139 "Dead code\n" +
9140 "----------\n" +
9141 "3. ERROR in X.java (at line 7)\n" +
9142 " assert (foo2 == null);\n" +
9143 " ^^^^\n" +
9144 "Null comparison always yields false: The variable foo2 cannot be null at this location\n" +
9145 "----------\n" +
9146 "4. ERROR in X.java (at line 8)\n" +
9147 " if (foo2 == null) {}\n" +
9148 " ^^^^\n" +
9149 "Redundant null check: The variable foo2 can only be null at this location\n" +
9150 "----------\n" +
9151 "5. ERROR in X.java (at line 11)\n" +
9152 " if (bar == null) {}\n" +
9153 " ^^^\n" +
9154 "Redundant null check: The variable bar can only be null at this location\n" +
9155 "----------\n" +
9156 "6. ERROR in X.java (at line 13)\n" +
9157 " assert (bar2 != null);\n" +
9158 " ^^^^\n" +
9159 "Null comparison always yields false: The variable bar2 can only be null at this location\n" +
9160 "----------\n" +
9161 "7. ERROR in X.java (at line 14)\n" +
9162 " if (bar2 == null) {}\n" +
9163 " ^^^^\n" +
9164 "Null comparison always yields false: The variable bar2 cannot be null at this location\n" +
9165 "----------\n" +
9166 "8. WARNING in X.java (at line 14)\n" +
9167 " if (bar2 == null) {}\n" +
9168 " ^^\n" +
9169 "Dead code\n" +
9170 "----------\n",
9171 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9172 }
9173 }
9174
9175 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
9176 // Test to verify that asserts are exempted from null comparison warnings,
9177 // but this doesn't affect the downstream info.
test0958_assert()9178 public void test0958_assert() {
9179 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
9180 this.runNegativeTest(
9181 new String[] {
9182 "X.java",
9183 "import java.util.HashMap;\n" +
9184 "public class X {\n" +
9185 " void m() {\n" +
9186 " HashMap<Integer,X> map = new HashMap<Integer,X>();\n" +
9187 " X bar = null;\n" +
9188 " X foo = map.get(1);\n" +
9189 " if (foo == null) {\n" +
9190 " foo = new X();\n" +
9191 " map.put(1, foo);\n" +
9192 " }\n" +
9193 " assert (foo != null && bar == null);\n" + // don't warn but do the null analysis
9194 " if (foo != null) {}\n" + // warn
9195 " if (bar == null) {}\n" + // warn
9196 " }\n" +
9197 "}\n"},
9198 "----------\n" +
9199 "1. ERROR in X.java (at line 12)\n" +
9200 " if (foo != null) {}\n" +
9201 " ^^^\n" +
9202 "Redundant null check: The variable foo cannot be null at this location\n" +
9203 "----------\n" +
9204 "2. ERROR in X.java (at line 13)\n" +
9205 " if (bar == null) {}\n" +
9206 " ^^^\n" +
9207 "Redundant null check: The variable bar can only be null at this location\n" +
9208 "----------\n",
9209 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9210 }
9211 }
9212
9213 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
9214 // Test to verify that asserts are exempted from redundant null check warnings in a looping context,
9215 // but this doesn't affect the downstream info.
test0959a_assert_loop()9216 public void test0959a_assert_loop() {
9217 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9218 this.runNegativeTest(
9219 new String[] {
9220 "X.java",
9221 "public class X {\n" +
9222 " void m() {\n" +
9223 " X foo = new X();\n" +
9224 " X foo2 = new X();\n" +
9225 " X bar = null;\n" +
9226 " X bar2 = null;\n" +
9227 " while (true) {\n" +
9228 " assert (foo != null);\n" + //don't warn
9229 " if (foo == null) {}\n" +
9230 " assert (foo2 == null);\n" + //don't warn
9231 " if (foo2 == null) {}\n" +
9232 " assert (bar == null);\n" + //don't warn
9233 " if (bar == null) {}\n" +
9234 " assert (bar2 != null);\n" + //don't warn
9235 " if (bar2 == null) {}\n" +
9236 " }\n" +
9237 " }\n" +
9238 "}\n"},
9239 "----------\n" +
9240 "1. ERROR in X.java (at line 9)\n" +
9241 " if (foo == null) {}\n" +
9242 " ^^^\n" +
9243 "Null comparison always yields false: The variable foo cannot be null at this location\n" +
9244 "----------\n" +
9245 "2. WARNING in X.java (at line 9)\n" +
9246 " if (foo == null) {}\n" +
9247 " ^^\n" +
9248 "Dead code\n" +
9249 "----------\n" +
9250 "3. ERROR in X.java (at line 10)\n" +
9251 " assert (foo2 == null);\n" +
9252 " ^^^^\n" +
9253 "Null comparison always yields false: The variable foo2 cannot be null at this location\n" +
9254 "----------\n" +
9255 "4. ERROR in X.java (at line 11)\n" +
9256 " if (foo2 == null) {}\n" +
9257 " ^^^^\n" +
9258 "Redundant null check: The variable foo2 can only be null at this location\n" +
9259 "----------\n" +
9260 "5. ERROR in X.java (at line 13)\n" +
9261 " if (bar == null) {}\n" +
9262 " ^^^\n" +
9263 "Redundant null check: The variable bar can only be null at this location\n" +
9264 "----------\n" +
9265 "6. ERROR in X.java (at line 14)\n" +
9266 " assert (bar2 != null);\n" +
9267 " ^^^^\n" +
9268 "Null comparison always yields false: The variable bar2 can only be null at this location\n" +
9269 "----------\n" +
9270 "7. ERROR in X.java (at line 15)\n" +
9271 " if (bar2 == null) {}\n" +
9272 " ^^^^\n" +
9273 "Null comparison always yields false: The variable bar2 cannot be null at this location\n" +
9274 "----------\n" +
9275 "8. WARNING in X.java (at line 15)\n" +
9276 " if (bar2 == null) {}\n" +
9277 " ^^\n" +
9278 "Dead code\n" +
9279 "----------\n",
9280 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9281 }
9282 }
9283
9284 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
9285 // Test to verify that asserts are exempted from redundant null check warnings in a looping context,
9286 // but this doesn't affect the downstream info.
test0959b_assert_loop()9287 public void test0959b_assert_loop() {
9288 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9289 this.runNegativeTest(
9290 new String[] {
9291 "X.java",
9292 "public class X {\n" +
9293 " void m() {\n" +
9294 " while (true) {\n" +
9295 " X foo = new X();\n" +
9296 " assert (foo != null);\n" + //don't warn
9297 " if (foo == null) {}\n" +
9298 " X foo2 = new X();\n" +
9299 " assert (foo2 == null);\n" + //don't warn
9300 " if (foo2 == null) {}\n" +
9301 " X bar = null;\n" +
9302 " assert (bar == null);\n" + //don't warn
9303 " if (bar == null) {}\n" +
9304 " X bar2 = null;\n" +
9305 " assert (bar2 != null);\n" + //don't warn
9306 " if (bar2 == null) {}\n" +
9307 " }\n" +
9308 " }\n" +
9309 "}\n"},
9310 "----------\n" +
9311 "1. ERROR in X.java (at line 6)\n" +
9312 " if (foo == null) {}\n" +
9313 " ^^^\n" +
9314 "Null comparison always yields false: The variable foo cannot be null at this location\n" +
9315 "----------\n" +
9316 "2. WARNING in X.java (at line 6)\n" +
9317 " if (foo == null) {}\n" +
9318 " ^^\n" +
9319 "Dead code\n" +
9320 "----------\n" +
9321 "3. ERROR in X.java (at line 8)\n" +
9322 " assert (foo2 == null);\n" +
9323 " ^^^^\n" +
9324 "Null comparison always yields false: The variable foo2 cannot be null at this location\n" +
9325 "----------\n" +
9326 "4. ERROR in X.java (at line 9)\n" +
9327 " if (foo2 == null) {}\n" +
9328 " ^^^^\n" +
9329 "Redundant null check: The variable foo2 can only be null at this location\n" +
9330 "----------\n" +
9331 "5. ERROR in X.java (at line 12)\n" +
9332 " if (bar == null) {}\n" +
9333 " ^^^\n" +
9334 "Redundant null check: The variable bar can only be null at this location\n" +
9335 "----------\n" +
9336 "6. ERROR in X.java (at line 14)\n" +
9337 " assert (bar2 != null);\n" +
9338 " ^^^^\n" +
9339 "Null comparison always yields false: The variable bar2 can only be null at this location\n" +
9340 "----------\n" +
9341 "7. ERROR in X.java (at line 15)\n" +
9342 " if (bar2 == null) {}\n" +
9343 " ^^^^\n" +
9344 "Null comparison always yields false: The variable bar2 cannot be null at this location\n" +
9345 "----------\n" +
9346 "8. WARNING in X.java (at line 15)\n" +
9347 " if (bar2 == null) {}\n" +
9348 " ^^\n" +
9349 "Dead code\n" +
9350 "----------\n",
9351 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9352 }
9353 }
9354
9355 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=250056
9356 // Test to verify that asserts are exempted from redundant null check warnings in a finally context,
9357 // but this doesn't affect the downstream info.
test0960_assert_finally()9358 public void test0960_assert_finally() {
9359 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
9360 this.runNegativeTest(
9361 new String[] {
9362 "X.java",
9363 "public class X {\n" +
9364 " void m() {\n" +
9365 " X foo = new X();\n" +
9366 " X foo2 = new X();\n" +
9367 " X bar = null;\n" +
9368 " X bar2 = null;\n" +
9369 " try {\n" +
9370 " System.out.println(\"Inside try\");\n" +
9371 " }\n" +
9372 " finally {\n" +
9373 " assert (foo != null);\n" + //don't warn
9374 " if (foo == null) {}\n" +
9375 " assert (foo2 == null);\n" + //don't warn
9376 " if (foo2 == null) {}\n" +
9377 " assert (bar == null);\n" + //don't warn
9378 " if (bar == null) {}\n" +
9379 " assert (bar2 != null);\n" + //don't warn
9380 " if (bar2 == null) {}\n" +
9381 " }\n" +
9382 " }\n" +
9383 "}\n"},
9384 "----------\n" +
9385 "1. ERROR in X.java (at line 12)\n" +
9386 " if (foo == null) {}\n" +
9387 " ^^^\n" +
9388 "Null comparison always yields false: The variable foo cannot be null at this location\n" +
9389 "----------\n" +
9390 "2. WARNING in X.java (at line 12)\n" +
9391 " if (foo == null) {}\n" +
9392 " ^^\n" +
9393 "Dead code\n" +
9394 "----------\n" +
9395 "3. ERROR in X.java (at line 13)\n" +
9396 " assert (foo2 == null);\n" +
9397 " ^^^^\n" +
9398 "Null comparison always yields false: The variable foo2 cannot be null at this location\n" +
9399 "----------\n" +
9400 "4. ERROR in X.java (at line 14)\n" +
9401 " if (foo2 == null) {}\n" +
9402 " ^^^^\n" +
9403 "Redundant null check: The variable foo2 can only be null at this location\n" +
9404 "----------\n" +
9405 "5. ERROR in X.java (at line 16)\n" +
9406 " if (bar == null) {}\n" +
9407 " ^^^\n" +
9408 "Redundant null check: The variable bar can only be null at this location\n" +
9409 "----------\n" +
9410 "6. ERROR in X.java (at line 17)\n" +
9411 " assert (bar2 != null);\n" +
9412 " ^^^^\n" +
9413 "Null comparison always yields false: The variable bar2 can only be null at this location\n" +
9414 "----------\n" +
9415 "7. ERROR in X.java (at line 18)\n" +
9416 " if (bar2 == null) {}\n" +
9417 " ^^^^\n" +
9418 "Null comparison always yields false: The variable bar2 cannot be null at this location\n" +
9419 "----------\n" +
9420 "8. WARNING in X.java (at line 18)\n" +
9421 " if (bar2 == null) {}\n" +
9422 " ^^\n" +
9423 "Dead code\n" +
9424 "----------\n",
9425 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9426 }
9427 }
9428
9429 // null analysis -- notNull protection tag
_test0900_notNull_protection_tag()9430 public void _test0900_notNull_protection_tag() {
9431 this.runNegativeTest(
9432 new String[] {
9433 "X.java",
9434 "public class X {\n" +
9435 " void foo(/** @notNull */ Object o) {\n" +
9436 " boolean b = o != null;\n" +
9437 " }\n" +
9438 "}\n"},
9439 "ERR cannot be null");
9440 }
9441
9442 // null analysis -- notNull protection tag
_test0901_notNull_protection_tag()9443 public void _test0901_notNull_protection_tag() {
9444 this.runNegativeTest(
9445 new String[] {
9446 "X.java",
9447 "public class X {\n" +
9448 " void foo(Object o) {\n" +
9449 " /** @notNull */ Object l = o;\n" +
9450 " }\n" +
9451 "}\n"},
9452 "ERR cannot be null... ou pas ?");
9453 }
9454
9455 // null analysis -- notNull protection tag
_test0902_notNull_protection_tag()9456 public void _test0902_notNull_protection_tag() {
9457 this.runNegativeTest(
9458 new String[] {
9459 "X.java",
9460 "public class X {\n" +
9461 " void foo(/** @nullable */ Object o) {\n" +
9462 " /** @notNull */ Object l = o;\n" +
9463 " }\n" +
9464 "}\n"},
9465 "ERR cannot be null");
9466 }
9467
9468 // null analysis -- notNull protection tag
test0903_notNull_protection_tag()9469 public void test0903_notNull_protection_tag() {
9470 this.runConformTest(
9471 new String[] {
9472 "X.java",
9473 "public class X {\n" +
9474 " Object bar() {\n" +
9475 " return null;\n" +
9476 " }\n" +
9477 " void foo() {\n" +
9478 " /** @notNull */ Object l = bar();\n" +
9479 " }\n" +
9480 "}\n"},
9481 "");
9482 }
9483
9484 // null analysis -- notNull protection tag
_test0904_notNull_protection_tag()9485 public void _test0904_notNull_protection_tag() {
9486 this.runNegativeTest(
9487 new String[] {
9488 "X.java",
9489 "public class X {\n" +
9490 " /** @notNull */\n" +
9491 " Object bar() {\n" +
9492 " return new Object();\n" +
9493 " }\n" +
9494 " void foo() {\n" +
9495 " Object l = bar();\n" +
9496 " if (l == null) { /* empty */ }\n" +
9497 " }\n" +
9498 "}\n"},
9499 "ERR cannot be null");
9500 }
9501
9502 // null analysis -- notNull protection tag
_test0905_notNull_protection_tag()9503 public void _test0905_notNull_protection_tag() {
9504 this.runNegativeTest(
9505 new String[] {
9506 "X.java",
9507 "public class X {\n" +
9508 " /** @notNull */\n" +
9509 " Object bar() {\n" +
9510 " return null;\n" +
9511 " }\n" +
9512 "}\n"},
9513 "ERR cannot be null");
9514 }
9515
9516 // null analysis -- nullable tag
_test0950_nullable_tag()9517 public void _test0950_nullable_tag() {
9518 this.runConformTest(
9519 new String[] {
9520 "X.java",
9521 "public class X {\n" +
9522 " void foo(/** @nullable */ Object o) {\n" +
9523 " o.toString();\n" +
9524 " }\n" +
9525 "}\n"},
9526 "ERR may be null");
9527 }
9528
9529 // null analysis -- nullable tag
_test0951_nullable_tag()9530 public void _test0951_nullable_tag() {
9531 this.runConformTest(
9532 new String[] {
9533 "X.java",
9534 "public class X {\n" +
9535 " void foo(/** @nullable */ Object o) {\n" +
9536 " Object l = o;\n" +
9537 " l.toString();\n" +
9538 " }\n" +
9539 "}\n"},
9540 "ERR may be null");
9541 }
9542
9543 // null analysis -- nullable tag
_test0952_nullable_tag()9544 public void _test0952_nullable_tag() {
9545 this.runConformTest(
9546 new String[] {
9547 "X.java",
9548 "public class X {\n" +
9549 " void foo(boolean b) {\n" +
9550 " /** @nullable */ Object o;\n" +
9551 " if (b) {\n" +
9552 " o = new Object();\n" +
9553 " }\n" +
9554 " o.toString();\n" +
9555 " }\n" +
9556 "}\n"},
9557 "ERR may be null");
9558 }
9559
9560 // moved from AssignmentTest
test1004()9561 public void test1004() {
9562 this.runNegativeTest(
9563 new String[] {
9564 "X.java",
9565 "public class X {\n" +
9566 " X foo(X x) {\n" +
9567 " x.foo(null); // 0\n" +
9568 " if (x != null) { // 1\n" +
9569 " if (x == null) { // 2\n" +
9570 " x.foo(null); // 3\n" +
9571 " } else if (x instanceof X) { // 4\n" +
9572 " x.foo(null); // 5 \n" +
9573 " } else if (x != null) { // 6\n" +
9574 " x.foo(null); // 7\n" +
9575 " }\n" +
9576 " x.foo(null); // 8\n" +
9577 " }\n" +
9578 " return this;\n" +
9579 " }\n" +
9580 "}\n"},
9581 "----------\n" +
9582 "1. ERROR in X.java (at line 4)\n" +
9583 " if (x != null) { // 1\n" +
9584 " ^\n" +
9585 "Redundant null check: The variable x cannot be null at this location\n" +
9586 "----------\n" +
9587 "2. ERROR in X.java (at line 5)\n" +
9588 " if (x == null) { // 2\n" +
9589 " ^\n" +
9590 "Null comparison always yields false: The variable x cannot be null at this location\n" +
9591 "----------\n" +
9592 "3. WARNING in X.java (at line 5)\n" +
9593 " if (x == null) { // 2\n" +
9594 " x.foo(null); // 3\n" +
9595 " } else if (x instanceof X) { // 4\n" +
9596 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
9597 "Dead code\n" +
9598 "----------\n" +
9599 "4. ERROR in X.java (at line 9)\n" +
9600 " } else if (x != null) { // 6\n" +
9601 " ^\n" +
9602 "Redundant null check: The variable x cannot be null at this location\n" +
9603 "----------\n",
9604 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9605 }
9606
test1005()9607 public void test1005() {
9608 this.runConformTest(
9609 new String[] {
9610 "X.java",
9611 "public class X {\n" +
9612 " void foo(Class c) {\n" +
9613 " if (c.isArray() ) {\n" +
9614 " } else if (c == java.lang.String.class ) {\n" +
9615 " }\n" +
9616 " }\n" +
9617 "}\n"},
9618 "");
9619 }
9620
test1006()9621 public void test1006() {
9622 this.runConformTest(
9623 new String[] {
9624 "X.java",
9625 "public class X {\n" +
9626 " void foo(X x) {\n" +
9627 " if (x == this)\n" +
9628 " return;\n" +
9629 " x.foo(this);\n" +
9630 " }\n" +
9631 "}\n"},
9632 "");
9633 }
9634
test1007()9635 public void test1007() {
9636 this.runConformTest(
9637 new String[] {
9638 "X.java",
9639 "public class X {\n" +
9640 " void foo(X x, X x2) {\n" +
9641 " if (x != null)\n" +
9642 " return;\n" +
9643 " x = x2;\n" +
9644 " if (x == null) {\n" +
9645 " }\n" +
9646 " }\n" +
9647 "}\n"},
9648 "");
9649 }
9650
test1008()9651 public void test1008() {
9652 this.runConformTest(
9653 new String[] {
9654 "X.java",
9655 "public class X {\n" +
9656 " void foo(X x, X x2) {\n" +
9657 " if (x != null)\n" +
9658 " return;\n" +
9659 " try {\n" +
9660 " x = x2;\n" +
9661 " } catch(Exception e) {}\n" +
9662 " if (x == null) {\n" +
9663 " }\n" +
9664 " }\n" +
9665 "}\n"},
9666 "");
9667 }
9668
test1009()9669 public void test1009() {
9670 this.runNegativeTest(
9671 new String[] {
9672 "X.java",
9673 "import java.io.File;\n" +
9674 "\n" +
9675 "public class X {\n" +
9676 " boolean check(String name) { return true; }\n" +
9677 " Class bar(String name) throws ClassNotFoundException { return null; }\n" +
9678 " File baz(String name) { return null; }\n" +
9679 " \n" +
9680 " public Class foo(String name, boolean resolve) throws ClassNotFoundException {\n" +
9681 " \n" +
9682 " Class c = bar(name);\n" +
9683 " if (c != null)\n" +
9684 " return c;\n" +
9685 " if (check(name)) {\n" +
9686 " try {\n" +
9687 " c= bar(name);\n" +
9688 " return c;\n" +
9689 " } catch (ClassNotFoundException e) {\n" +
9690 " // keep searching\n" +
9691 " // only path to here left c unassigned from try block, means it was assumed to be null\n" +
9692 " }\n" +
9693 " }\n" +
9694 " if (c == null) {// should complain: c can only be null\n" +
9695 " File file= baz(name);\n" +
9696 " if (file == null)\n" +
9697 " throw new ClassNotFoundException();\n" +
9698 " }\n" +
9699 " return c;\n" +
9700 " }\n" +
9701 "\n" +
9702 "}\n"},
9703 "----------\n" +
9704 "1. ERROR in X.java (at line 22)\n" +
9705 " if (c == null) {// should complain: c can only be null\n" +
9706 " ^\n" +
9707 "Redundant null check: The variable c can only be null at this location\n" +
9708 "----------\n",
9709 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9710 }
9711
test1010()9712 public void test1010() {
9713 this.runConformTest(
9714 new String[] {
9715 "X.java",
9716 "public class X {\n" +
9717 "\n" +
9718 " X itself() { return this; }\n" +
9719 "\n" +
9720 " void bar() {\n" +
9721 " X itself = this.itself();\n" +
9722 " if (this == itself) {\n" +
9723 " System.out.println(itself.toString()); //1\n" +
9724 " } else {\n" +
9725 " System.out.println(itself.toString()); //2\n" +
9726 " }\n" +
9727 " }\n" +
9728 "}\n"},
9729 "");
9730 }
9731
test1011()9732 public void test1011() {
9733 this.runNegativeTest(
9734 new String[] {
9735 "X.java",
9736 "public class X {\n" +
9737 "\n" +
9738 " X itself() { return this; }\n" +
9739 "\n" +
9740 " void bar() {\n" +
9741 " X itself = this.itself();\n" +
9742 " if (this == itself) {\n" +
9743 " X other = (X)itself;\n" +
9744 " if (other != null) {\n" +
9745 " }\n" +
9746 " if (other == null) {\n" +
9747 " }\n" +
9748 " }\n" +
9749 " }\n" +
9750 "}\n"},
9751 "----------\n" +
9752 "1. ERROR in X.java (at line 9)\n" +
9753 " if (other != null) {\n" +
9754 " ^^^^^\n" +
9755 "Redundant null check: The variable other cannot be null at this location\n" +
9756 "----------\n" +
9757 "2. ERROR in X.java (at line 11)\n" +
9758 " if (other == null) {\n" +
9759 " ^^^^^\n" +
9760 "Null comparison always yields false: The variable other cannot be null at this location\n" +
9761 "----------\n" +
9762 "3. WARNING in X.java (at line 11)\n" +
9763 " if (other == null) {\n" +
9764 " }\n" +
9765 " ^^^^^^^^^\n" +
9766 "Dead code\n" +
9767 "----------\n",
9768 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9769 }
9770
test1012()9771 public void test1012() {
9772 this.runNegativeTest(
9773 new String[] {
9774 "X.java",
9775 "public class X {\n" +
9776 " \n" +
9777 " void foo() {\n" +
9778 " Object o = null;\n" +
9779 " do {\n" +
9780 " if (o == null) {\n" +
9781 " return;\n" +
9782 " }\n" +
9783 " // o = bar();\n" +
9784 " } while (true);\n" +
9785 " }\n" +
9786 " X bar() { \n" +
9787 " return null; \n" +
9788 " }\n" +
9789 "}"},
9790 "----------\n" +
9791 "1. ERROR in X.java (at line 6)\n" +
9792 " if (o == null) {\n" +
9793 " ^\n" +
9794 "Redundant null check: The variable o can only be null at this location\n" +
9795 "----------\n",
9796 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9797 }
9798
9799 // x cannot equal this then null with no assignment in between
9800 // each diagnostic is locally sound though
test1013()9801 public void test1013() {
9802 this.runNegativeTest(
9803 new String[] {
9804 "X.java",
9805 "public class X {\n" +
9806 " void foo(X x) {\n" +
9807 " if (x == this) {\n" +
9808 " if (x == null) {\n" +
9809 " x.foo(this);\n" +
9810 " }\n" +
9811 " }\n" +
9812 " }\n" +
9813 "}\n"},
9814 "----------\n" +
9815 "1. ERROR in X.java (at line 4)\n" +
9816 " if (x == null) {\n" +
9817 " ^\n" +
9818 "Null comparison always yields false: The variable x cannot be null at this location\n" +
9819 "----------\n" +
9820 "2. WARNING in X.java (at line 4)\n" +
9821 " if (x == null) {\n" +
9822 " x.foo(this);\n" +
9823 " }\n" +
9824 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
9825 "Dead code\n" +
9826 "----------\n",
9827 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9828 }
9829
test1014()9830 public void test1014() {
9831 this.runConformTest(
9832 new String[] {
9833 "X.java",
9834 "public class X {\n" +
9835 " void foo(X x) {\n" +
9836 " x = null;\n" +
9837 " try {\n" +
9838 " x = this;\n" +
9839 " } finally {\n" +
9840 " x.foo(null);\n" +
9841 " }\n" +
9842 " }\n" +
9843 "}\n"},
9844 "");
9845 }
9846
test1015()9847 public void test1015() {
9848 this.runConformTest(
9849 new String[] {
9850 "X.java",
9851 "public class X {\n" +
9852 " void foo() {\n" +
9853 " Object o = null;\n" +
9854 " int i = 1;\n" +
9855 " switch (i) {\n" +
9856 " case 1:\n" +
9857 " o = new Object();\n" +
9858 " break;\n" +
9859 " }\n" +
9860 " if (o != null)\n" +
9861 " o.toString();\n" +
9862 " }\n" +
9863 "}\n"},
9864 "");
9865 }
9866
test1016()9867 public void test1016() {
9868 this.runNegativeTest(
9869 new String[] {
9870 "X.java",
9871 "public class X {\n" +
9872 " void foo(X x) {\n" +
9873 " x = null;\n" +
9874 " try {\n" +
9875 " x = null;\n" +
9876 " } finally {\n" +
9877 " if (x != null) {\n" +
9878 " x.foo(null);\n" +
9879 " }\n" +
9880 " }\n" +
9881 " }\n" +
9882 "}\n"},
9883 "----------\n" +
9884 "1. ERROR in X.java (at line 5)\n" +
9885 " x = null;\n" +
9886 " ^\n" +
9887 "Redundant assignment: The variable x can only be null at this location\n" +
9888 "----------\n" +
9889 "2. ERROR in X.java (at line 7)\n" +
9890 " if (x != null) {\n" +
9891 " ^\n" +
9892 "Null comparison always yields false: The variable x can only be null at this location\n" +
9893 "----------\n",
9894 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9895 }
9896
test1017()9897 public void test1017() {
9898 this.runNegativeTest(
9899 new String[] {
9900 "X.java",
9901 "public class X {\n" +
9902 " void foo(X x) {\n" +
9903 " x = this;\n" +
9904 " try {\n" +
9905 " x = null;\n" +
9906 " } finally {\n" +
9907 " if (x == null) {\n" +
9908 " x.foo(null);\n" +
9909 " }\n" +
9910 " }\n" +
9911 " }\n" +
9912 "}\n"},
9913 "----------\n" +
9914 "1. ERROR in X.java (at line 7)\n" +
9915 " if (x == null) {\n" +
9916 " ^\n" +
9917 "Redundant null check: The variable x can only be null at this location\n" +
9918 "----------\n" +
9919 "2. ERROR in X.java (at line 8)\n" +
9920 " x.foo(null);\n" +
9921 " ^\n" +
9922 "Null pointer access: The variable x can only be null at this location\n" +
9923 "----------\n",
9924 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9925 }
9926
test1018()9927 public void test1018() {
9928 this.runNegativeTest(
9929 new String[] {
9930 "X.java",
9931 "public class X {\n" +
9932 " \n" +
9933 " void foo() {\n" +
9934 " Object o = null;\n" +
9935 " do {\n" +
9936 " if (o != null) return;\n" +
9937 " o = null;\n" +
9938 " } while (true);\n" +
9939 " }\n" +
9940 " X bar() { \n" +
9941 " return null; \n" +
9942 " }\n" +
9943 "}"},
9944 "----------\n" +
9945 "1. ERROR in X.java (at line 6)\n" +
9946 " if (o != null) return;\n" +
9947 " ^\n" +
9948 "Null comparison always yields false: The variable o can only be null at this location\n" +
9949 "----------\n" +
9950 "2. ERROR in X.java (at line 7)\n" +
9951 " o = null;\n" +
9952 " ^\n" +
9953 "Redundant assignment: The variable o can only be null at this location\n" +
9954 "----------\n",
9955 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
9956 }
9957
test1019()9958 public void test1019() {
9959 this.runConformTest(
9960 new String[] {
9961 "X.java",
9962 "public class X {\n" +
9963 " public static final char[] replaceOnCopy(\n" +
9964 " char[] array,\n" +
9965 " char toBeReplaced,\n" +
9966 " char replacementChar) {\n" +
9967 " \n" +
9968 " char[] result = null;\n" +
9969 " for (int i = 0, length = array.length; i < length; i++) {\n" +
9970 " char c = array[i];\n" +
9971 " if (c == toBeReplaced) {\n" +
9972 " if (result == null) {\n" +
9973 " result = new char[length];\n" +
9974 " System.arraycopy(array, 0, result, 0, i);\n" +
9975 " }\n" +
9976 " result[i] = replacementChar;\n" +
9977 " } else if (result != null) {\n" +
9978 " result[i] = c;\n" +
9979 " }\n" +
9980 " }\n" +
9981 " if (result == null) return array;\n" +
9982 " return result;\n" +
9983 " }\n" +
9984 "}\n"},
9985 "");
9986 }
9987
test1021()9988 public void test1021() {
9989 this.runConformTest(
9990 new String[] {
9991 "X.java",
9992 "public class X {\n" +
9993 " int kind;\n" +
9994 " X parent;\n" +
9995 " Object[] foo() { return null; }\n" +
9996 " void findTypeParameters(X scope) {\n" +
9997 " Object[] typeParameters = null;\n" +
9998 " while (scope != null) {\n" +
9999 " typeParameters = null;\n" +
10000 " switch (scope.kind) {\n" +
10001 " case 0 :\n" +
10002 " typeParameters = foo();\n" +
10003 " break;\n" +
10004 " case 1 :\n" +
10005 " typeParameters = foo();\n" +
10006 " break;\n" +
10007 " case 2 :\n" +
10008 " return;\n" +
10009 " }\n" +
10010 " if(typeParameters != null) {\n" +
10011 " foo();\n" +
10012 " }\n" +
10013 " scope = scope.parent;\n" +
10014 " }\n" +
10015 " }\n" +
10016 "}\n"},
10017 "");
10018 }
10019
test1022()10020 public void test1022() {
10021 this.runConformTest(
10022 new String[] {
10023 "X.java",
10024 "public class X {\n" +
10025 " boolean bool() { return true; }\n" +
10026 " void doSomething() {}\n" +
10027 " \n" +
10028 " void foo() {\n" +
10029 " Object progressJob = null;\n" +
10030 " while (bool()) {\n" +
10031 " if (bool()) {\n" +
10032 " if (progressJob != null)\n" +
10033 " progressJob = null;\n" +
10034 " doSomething();\n" +
10035 " }\n" +
10036 " try {\n" +
10037 " if (progressJob == null) {\n" +
10038 " progressJob = new Object();\n" +
10039 " }\n" +
10040 " } finally {\n" +
10041 " doSomething();\n" +
10042 " }\n" +
10043 " }\n" +
10044 " }\n" +
10045 "}"},
10046 "");
10047 }
10048
test1023()10049 public void test1023() {
10050 this.runNegativeTest(
10051 new String[] {
10052 "X.java",
10053 "public class X {\n" +
10054 "\n" +
10055 " void foo(Object that) {\n" +
10056 " Object o = new Object();\n" +
10057 " while (that != null) {\n" +
10058 " try {\n" +
10059 " o = null;\n" +
10060 " break;\n" +
10061 " } finally {\n" +
10062 " o = new Object();\n" +
10063 " }\n" +
10064 " }\n" +
10065 " if (o == null) return;\n" +
10066 " }\n" +
10067 "}\n"},
10068 "----------\n" +
10069 "1. ERROR in X.java (at line 13)\n" +
10070 " if (o == null) return;\n" +
10071 " ^\n" +
10072 "Null comparison always yields false: The variable o cannot be null at this location\n" +
10073 "----------\n" +
10074 "2. WARNING in X.java (at line 13)\n" +
10075 " if (o == null) return;\n" +
10076 " ^^^^^^^\n" +
10077 "Dead code\n" +
10078 "----------\n",
10079 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10080 }
10081
test1024()10082 public void test1024() {
10083 this.runNegativeTest(
10084 new String[] {
10085 "X.java",
10086 "public class X {\n" +
10087 " \n" +
10088 " boolean bool() { return true; }\n" +
10089 " void doSomething() {}\n" +
10090 " \n" +
10091 " void foo() {\n" +
10092 " Object progressJob = null;\n" +
10093 " while (bool()) {\n" +
10094 " if (progressJob != null)\n" +
10095 " progressJob = null;\n" +
10096 " doSomething();\n" +
10097 " try {\n" +
10098 " if (progressJob == null) {\n" +
10099 " progressJob = new Object();\n" +
10100 " }\n" +
10101 " } finally {\n" +
10102 " doSomething();\n" +
10103 " }\n" +
10104 " }\n" +
10105 " }\n" +
10106 "}"},
10107 "----------\n" +
10108 "1. ERROR in X.java (at line 13)\n" +
10109 " if (progressJob == null) {\n" +
10110 " ^^^^^^^^^^^\n" +
10111 "Redundant null check: The variable progressJob can only be null at this location\n" +
10112 "----------\n",
10113 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10114 }
10115
test1025()10116 public void test1025() {
10117 this.runNegativeTest(
10118 new String[] {
10119 "X.java",
10120 "public class X {\n" +
10121 " \n" +
10122 " void foo() {\n" +
10123 " Object o;\n" +
10124 " try {\n" +
10125 " o = null;\n" +
10126 " } finally {\n" +
10127 " o = new Object();\n" +
10128 " }\n" +
10129 " if (o == null) return;\n" +
10130 " }\n" +
10131 "}\n"},
10132 "----------\n" +
10133 "1. ERROR in X.java (at line 10)\n" +
10134 " if (o == null) return;\n" +
10135 " ^\n" +
10136 "Null comparison always yields false: The variable o cannot be null at this location\n" +
10137 "----------\n" +
10138 "2. WARNING in X.java (at line 10)\n" +
10139 " if (o == null) return;\n" +
10140 " ^^^^^^^\n" +
10141 "Dead code\n" +
10142 "----------\n",
10143 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10144 }
10145
10146 // TODO (philippe) reenable once fixed
_test1026()10147 public void _test1026() {
10148 this.runConformTest(
10149 new String[] {
10150 "X.java",
10151 "public class X {\n" +
10152 " \n" +
10153 " public static void main(String[] args) {\n" +
10154 " Object o;\n" +
10155 " try {\n" +
10156 " o = null;\n" +
10157 " } finally {\n" +
10158 " if (args == null) o = new Object();\n" +
10159 " }\n" +
10160 " if (o == null) System.out.println(\"SUCCESS\");\n" +
10161 " }\n" +
10162 "}\n"},
10163 "SUCCESS");
10164 }
10165
test1027()10166 public void test1027() {
10167 runNegativeNullTest(
10168 new String[] {
10169 "X.java",
10170 "public class X {\n" +
10171 " boolean b;\n" +
10172 " void foo() {\n" +
10173 " Object o = null;\n" +
10174 " while (b) {\n" +
10175 " try {\n" +
10176 " o = null;\n" +
10177 " } finally {\n" +
10178 " if (o == null) \n" +
10179 " o = new Object();\n" +
10180 " }\n" +
10181 " }\n" +
10182 " if (o == null) return;\n" +
10183 " }\n" +
10184 "}\n"},
10185 "----------\n" +
10186 "1. ERROR in X.java (at line 9)\n" +
10187 " if (o == null) \n" +
10188 " ^\n" +
10189 "Redundant null check: The variable o can only be null at this location\n" +
10190 "----------\n");
10191 }
10192
10193 // TODO (philippe) reenable once fixed
_test1028()10194 public void _test1028() {
10195 this.runConformTest(
10196 new String[] {
10197 "X.java",
10198 "public class X {\n" +
10199 " boolean b;\n" +
10200 " void foo() {\n" +
10201 " Object o = null;\n" +
10202 " while (b) {\n" +
10203 " try {\n" +
10204 " o = null;\n" +
10205 " break;\n" +
10206 " } finally {\n" +
10207 " if (o == null) \n" +
10208 " o = new Object();\n" +
10209 " }\n" +
10210 " }\n" +
10211 " if (o == null) return;\n" +
10212 " }\n" +
10213 "}\n"},
10214 "");
10215 }
10216
test1029()10217 public void test1029() {
10218 this.runConformTest(
10219 new String[] {
10220 "X.java",
10221 "public class X {\n" +
10222 " public static void main(String[] args) {\n" +
10223 " Object o = null;\n" +
10224 " int i = 0;\n" +
10225 " while (i++ < 2) {\n" +
10226 " try {\n" +
10227 " if (i == 2) return;\n" +
10228 " o = null;\n" +
10229 " } finally {\n" +
10230 " if (i == 2) System.out.println(o);\n" +
10231 " o = \"SUCCESS\";\n" +
10232 " }\n" +
10233 " }\n" +
10234 " if (o == null) return;\n" +
10235 " }\n" +
10236 "}\n"},
10237 "SUCCESS");
10238 }
10239
test1030()10240 public void test1030() {
10241 this.runNegativeTest(
10242 new String[] {
10243 "X.java",
10244 "public class X {\n" +
10245 " \n" +
10246 " void foo() {\n" +
10247 " Object a = null;\n" +
10248 " while (true) {\n" +
10249 " a = null;\n" +
10250 " if (a == null) {\n" +
10251 " System.out.println();\n" +
10252 " }\n" +
10253 " a = new Object();\n" +
10254 " break;\n" +
10255 " }\n" +
10256 " }\n" +
10257 "}\n"},
10258 "----------\n" +
10259 "1. ERROR in X.java (at line 6)\n" +
10260 " a = null;\n" +
10261 " ^\n" +
10262 "Redundant assignment: The variable a can only be null at this location\n" +
10263 "----------\n" +
10264 "2. ERROR in X.java (at line 7)\n" +
10265 " if (a == null) {\n" +
10266 " ^\n" +
10267 "Redundant null check: The variable a can only be null at this location\n" +
10268 "----------\n",
10269 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10270 }
10271
test1031()10272 public void test1031() {
10273 this.runNegativeTest(
10274 new String[] {
10275 "X.java",
10276 "public class X {\n" +
10277 " \n" +
10278 " void foo() {\n" +
10279 " Object a = null;\n" +
10280 " while (true) {\n" +
10281 " a = null;\n" +
10282 " if (a == null) {\n" +
10283 " System.out.println();\n" +
10284 " }\n" +
10285 " a = new Object();\n" +
10286 " break;\n" +
10287 " }\n" +
10288 " if (a == null) {\n" +
10289 " System.out.println();\n" +
10290 " }\n" +
10291 " }\n" +
10292 "}\n"},
10293 "----------\n" +
10294 "1. ERROR in X.java (at line 6)\n" +
10295 " a = null;\n" +
10296 " ^\n" +
10297 "Redundant assignment: The variable a can only be null at this location\n" +
10298 "----------\n" +
10299 "2. ERROR in X.java (at line 7)\n" +
10300 " if (a == null) {\n" +
10301 " ^\n" +
10302 "Redundant null check: The variable a can only be null at this location\n" +
10303 "----------\n" +
10304 "3. ERROR in X.java (at line 13)\n" +
10305 " if (a == null) {\n" +
10306 " ^\n" +
10307 "Null comparison always yields false: The variable a cannot be null at this location\n" +
10308 "----------\n" +
10309 "4. WARNING in X.java (at line 13)\n" +
10310 " if (a == null) {\n" +
10311 " System.out.println();\n" +
10312 " }\n" +
10313 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10314 "Dead code\n" +
10315 "----------\n",
10316 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10317 }
10318
test1032()10319 public void test1032() {
10320 this.runNegativeTest(
10321 new String[] {
10322 "X.java",
10323 "public class X {\n" +
10324 " void foo() {\n" +
10325 " Object o1 = this;\n" +
10326 " Object o3;\n" +
10327 " while (o1 != null && (o3 = o1) != null) {\n" +
10328 " o1 = o3;\n" +
10329 " }\n" +
10330 " }\n" +
10331 "}\n"},
10332 "----------\n" +
10333 "1. ERROR in X.java (at line 5)\n" +
10334 " while (o1 != null && (o3 = o1) != null) {\n" +
10335 " ^^\n" +
10336 "Redundant null check: The variable o1 cannot be null at this location\n" +
10337 "----------\n" +
10338 "2. ERROR in X.java (at line 5)\n" +
10339 " while (o1 != null && (o3 = o1) != null) {\n" +
10340 " ^^^^^^^^^\n" +
10341 "Redundant null check: The variable o3 cannot be null at this location\n" +
10342 "----------\n",
10343 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10344 }
10345
10346 // (simplified to focus on nulls)
test1033()10347 public void test1033() {
10348 this.runNegativeTest(
10349 new String[] {
10350 "X.java",
10351 "public class X {\n" +
10352 " \n" +
10353 " void foo() {\n" +
10354 " String a,b;\n" +
10355 " do{\n" +
10356 " a=\"Hello \";\n" +
10357 " }while(a!=null);\n" +
10358 " if(a!=null)\n" +
10359 " { /* */ }\n" +
10360 " }\n" +
10361 "}\n"},
10362 "----------\n" +
10363 "1. ERROR in X.java (at line 7)\n" +
10364 " }while(a!=null);\n" +
10365 " ^\n" +
10366 "Redundant null check: The variable a cannot be null at this location\n" +
10367 "----------\n" +
10368 "2. ERROR in X.java (at line 8)\n" +
10369 " if(a!=null)\n" +
10370 " ^\n" +
10371 "Null comparison always yields false: The variable a can only be null at this location\n" +
10372 "----------\n" +
10373 "3. WARNING in X.java (at line 9)\n" +
10374 " { /* */ }\n" +
10375 " ^^^^^^^^^\n" +
10376 "Dead code\n" +
10377 "----------\n",
10378 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10379 }
10380
10381 // from AssignmentTest#test034, simplified
test1034()10382 public void test1034() {
10383 runNegativeNullTest(
10384 new String[] {
10385 "X.java",
10386 "public final class X \n" +
10387 "{\n" +
10388 " void foo()\n" +
10389 " {\n" +
10390 " String rs = null;\n" +
10391 " try\n" +
10392 " {\n" +
10393 " rs = \"\";\n" +
10394 " return;\n" +
10395 " }\n" +
10396 " catch (Exception e)\n" +
10397 " {\n" +
10398 " }\n" +
10399 " finally\n" +
10400 " {\n" +
10401 " if (rs != null)\n" +
10402 " {\n" +
10403 " try\n" +
10404 " {\n" +
10405 " rs.toString();\n" +
10406 " }\n" +
10407 " catch (Exception e)\n" +
10408 " {\n" +
10409 " }\n" +
10410 " }\n" +
10411 " }\n" +
10412 " return;\n" +
10413 " }\n" +
10414 "}\n",
10415 },
10416 "----------\n" +
10417 "1. ERROR in X.java (at line 16)\n" +
10418 " if (rs != null)\n" +
10419 " ^^\n" +
10420 "Redundant null check: The variable rs cannot be null at this location\n" +
10421 "----------\n");
10422 }
10423
test1036()10424 public void test1036() {
10425 this.runNegativeTest(
10426 new String[] {
10427 "X.java",
10428 "public class X {\n" +
10429 "\n" +
10430 " void foo() {\n" +
10431 " Object o = new Object();\n" +
10432 " do {\n" +
10433 " o = null;\n" +
10434 " } while (o != null);\n" +
10435 " if (o == null) {\n" +
10436 " // throw new Exception();\n" +
10437 " }\n" +
10438 " }\n" +
10439 "}\n"},
10440 "----------\n" +
10441 "1. ERROR in X.java (at line 7)\n" +
10442 " } while (o != null);\n" +
10443 " ^\n" +
10444 "Null comparison always yields false: The variable o can only be null at this location\n" +
10445 "----------\n" +
10446 "2. ERROR in X.java (at line 8)\n" +
10447 " if (o == null) {\n" +
10448 " ^\n" +
10449 "Redundant null check: The variable o can only be null at this location\n" +
10450 "----------\n",
10451 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10452 }
10453 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10454 // adding distinct options to control null checks in more detail
10455 // default for null options is Ignore
10456 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=192875
10457 // changed default for null access to warning
test1050_options_all_default()10458 public void test1050_options_all_default() {
10459 try {
10460 setNullRelatedOptions = false;
10461 runConformTest(
10462 true, // flush
10463 new String[] {
10464 "X.java",
10465 "public class X {\n" +
10466 " void foo(Object p) {\n" +
10467 " Object o = null;\n" +
10468 " if (o != null) {\n" +
10469 " o = null;\n" +
10470 " }\n" +
10471 " if (p == null) {}\n" + // taint p
10472 " o.toString();\n" +
10473 " p.toString();\n" +
10474 " }\n" +
10475 "}\n"
10476 } /* testFiles */,
10477 "----------\n" +
10478 "1. WARNING in X.java (at line 4)\n" +
10479 " if (o != null) {\n" +
10480 " o = null;\n" +
10481 " }\n" +
10482 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10483 "Dead code\n" +
10484 "----------\n" +
10485 "2. WARNING in X.java (at line 8)\n" +
10486 " o.toString();\n" +
10487 " ^\n" +
10488 "Null pointer access: The variable o can only be null at this location\n" +
10489 "----------\n" /* expectedCompilerLog */,
10490 "" /* expectedOutputString */,
10491 "" /* expectedErrorString */,
10492 JavacTestOptions.Excuse.EclipseHasSomeMoreWarnings);
10493 }
10494 finally {
10495 setNullRelatedOptions = true;
10496 }
10497 }
10498 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10499 // adding distinct options to control null checks in more detail
10500 // all null options set to Ignore
test1051_options_all_ignore()10501 public void test1051_options_all_ignore() {
10502 Map customOptions = getCompilerOptions();
10503 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.IGNORE);
10504 customOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.IGNORE);
10505 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
10506 this.runConformTest(
10507 new String[] {
10508 "X.java",
10509 "public class X {\n" +
10510 " void foo(Object p) {\n" +
10511 " Object o = null;\n" +
10512 " if (o != null) {\n" +
10513 " o = null;\n" +
10514 " }\n" +
10515 " if (p == null) {}\n" + // taint p
10516 " o.toString();\n" +
10517 " p.toString();\n" +
10518 " }\n" +
10519 "}\n"},
10520 null /* no expected output string */,
10521 null /* no extra class libraries */,
10522 true /* flush output directory */,
10523 null /* no vm arguments */,
10524 customOptions,
10525 null /* no custom requestor*/,
10526 false /* do not skip javac for this peculiar test */);
10527 }
10528 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10529 // adding distinct options to control null checks in more detail
10530 // all options set to error
test1052_options_all_error()10531 public void test1052_options_all_error() {
10532 this.runNegativeTest(
10533 new String[] {
10534 "X.java",
10535 "public class X {\n" +
10536 " void foo(Object p) {\n" +
10537 " Object o = null;\n" +
10538 " if (o != null) {\n" +
10539 " o = null;\n" +
10540 " }\n" +
10541 " if (p == null) {}\n" + // taint p
10542 " o.toString();\n" +
10543 " p.toString();\n" +
10544 " }\n" +
10545 "}\n"},
10546 "----------\n" +
10547 "1. ERROR in X.java (at line 4)\n" +
10548 " if (o != null) {\n" +
10549 " ^\n" +
10550 "Null comparison always yields false: The variable o can only be null at this location\n" +
10551 "----------\n" +
10552 "2. WARNING in X.java (at line 4)\n" +
10553 " if (o != null) {\n" +
10554 " o = null;\n" +
10555 " }\n" +
10556 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10557 "Dead code\n" +
10558 "----------\n" +
10559 "3. ERROR in X.java (at line 8)\n" +
10560 " o.toString();\n" +
10561 " ^\n" +
10562 "Null pointer access: The variable o can only be null at this location\n" +
10563 "----------\n" +
10564 "4. ERROR in X.java (at line 9)\n" +
10565 " p.toString();\n" +
10566 " ^\n" +
10567 "Potential null pointer access: The variable p may be null at this location\n" +
10568 "----------\n",
10569 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10570 }
10571 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10572 // adding distinct options to control null checks in more detail
10573 // selectively changing error levels
test1053_options_mix()10574 public void test1053_options_mix() {
10575 Map customOptions = getCompilerOptions();
10576 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
10577 customOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.IGNORE);
10578 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
10579 runNegativeTest(
10580 // test directory preparation
10581 true /* flush output directory */,
10582 new String[] { /* test files */
10583 "X.java",
10584 "public class X {\n" +
10585 " void foo(Object p) {\n" +
10586 " Object o = null;\n" +
10587 " if (o != null) {\n" +
10588 " o = null;\n" +
10589 " }\n" +
10590 " if (p == null) {}\n" + // taint p
10591 " o.toString();\n" +
10592 " p.toString();\n" +
10593 " }\n" +
10594 "}\n"
10595 },
10596 // compiler options
10597 null /* no class libraries */,
10598 customOptions /* custom options */,
10599 // compiler results
10600 "----------\n" + /* expected compiler log */
10601 "1. WARNING in X.java (at line 4)\n" +
10602 " if (o != null) {\n" +
10603 " o = null;\n" +
10604 " }\n" +
10605 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10606 "Dead code\n" +
10607 "----------\n" +
10608 "2. ERROR in X.java (at line 8)\n" +
10609 " o.toString();\n" +
10610 " ^\n" +
10611 "Null pointer access: The variable o can only be null at this location\n" +
10612 "----------\n",
10613 // javac options
10614 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError /* javac test options */);
10615 }
10616 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10617 // adding distinct options to control null checks in more detail
10618 // selectively changing error levels
test1054_options_mix()10619 public void test1054_options_mix() {
10620 Map customOptions = getCompilerOptions();
10621 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
10622 customOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.IGNORE);
10623 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
10624 runNegativeTest(
10625 // test directory preparation
10626 true /* flush output directory */,
10627 new String[] { /* test files */
10628 "X.java",
10629 "public class X {\n" +
10630 " void foo(Object p) {\n" +
10631 " Object o = null;\n" +
10632 " if (o != null) {\n" +
10633 " o = null;\n" +
10634 " }\n" +
10635 " if (p == null) {}\n" + // taint p
10636 " o.toString();\n" +
10637 " p.toString();\n" +
10638 " }\n" +
10639 "}\n"
10640 },
10641 // compiler options
10642 null /* no class libraries */,
10643 customOptions /* custom options */,
10644 // compiler results
10645 "----------\n" + /* expected compiler log */
10646 "1. ERROR in X.java (at line 4)\n" +
10647 " if (o != null) {\n" +
10648 " ^\n" +
10649 "Null comparison always yields false: The variable o can only be null at this location\n" +
10650 "----------\n" +
10651 "2. WARNING in X.java (at line 4)\n" +
10652 " if (o != null) {\n" +
10653 " o = null;\n" +
10654 " }\n" +
10655 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10656 "Dead code\n" +
10657 "----------\n" +
10658 "3. WARNING in X.java (at line 8)\n" +
10659 " o.toString();\n" +
10660 " ^\n" +
10661 "Null pointer access: The variable o can only be null at this location\n" +
10662 "----------\n",
10663 // javac options
10664 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError /* javac test options */);
10665 }
10666 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10667 // adding distinct options to control null checks in more detail
10668 // selectively changing error levels
test1055_options_mix()10669 public void test1055_options_mix() {
10670 Map customOptions = getCompilerOptions();
10671 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.IGNORE);
10672 customOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.ERROR);
10673 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
10674 runNegativeTest(
10675 // test directory preparation
10676 true /* flush output directory */,
10677 new String[] { /* test files */
10678 "X.java",
10679 "public class X {\n" +
10680 " void foo(Object p) {\n" +
10681 " Object o = null;\n" +
10682 " if (o != null) {\n" +
10683 " o = null;\n" +
10684 " }\n" +
10685 " if (p == null) {}\n" + // taint p
10686 " o.toString();\n" +
10687 " p.toString();\n" +
10688 " }\n" +
10689 "}\n"
10690 },
10691 // compiler options
10692 null /* no class libraries */,
10693 customOptions /* custom options */,
10694 // compiler results
10695 "----------\n" + /* expected compiler log */
10696 "1. ERROR in X.java (at line 4)\n" +
10697 " if (o != null) {\n" +
10698 " ^\n" +
10699 "Null comparison always yields false: The variable o can only be null at this location\n" +
10700 "----------\n" +
10701 "2. WARNING in X.java (at line 4)\n" +
10702 " if (o != null) {\n" +
10703 " o = null;\n" +
10704 " }\n" +
10705 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10706 "Dead code\n" +
10707 "----------\n" +
10708 "3. ERROR in X.java (at line 9)\n" +
10709 " p.toString();\n" +
10710 " ^\n" +
10711 "Potential null pointer access: The variable p may be null at this location\n" +
10712 "----------\n",
10713 // javac options
10714 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError /* javac test options */);
10715 }
10716 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10717 // adding distinct options to control null checks in more detail
10718 // selectively changing error levels
test1056_options_mix_with_SuppressWarnings()10719 public void test1056_options_mix_with_SuppressWarnings() {
10720 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
10721 Map customOptions = getCompilerOptions();
10722 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
10723 customOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.WARNING);
10724 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
10725 runNegativeTest(
10726 // test directory preparation
10727 true /* flush output directory */,
10728 new String[] { /* test files */
10729 "X.java",
10730 "public class X {\n" +
10731 "@SuppressWarnings(\"null\")\n" +
10732 " void foo(Object p) {\n" +
10733 " Object o = null;\n" +
10734 " if (o != null) {\n" +
10735 " o = null;\n" +
10736 " }\n" +
10737 " if (p == null) {}\n" + // taint p
10738 " o.toString();\n" +
10739 " p.toString();\n" +
10740 " }\n" +
10741 "}\n"
10742 },
10743 // compiler options
10744 null /* no class libraries */,
10745 customOptions /* custom options */,
10746 // compiler results
10747 "----------\n" + /* expected compiler log */
10748 "1. WARNING in X.java (at line 5)\n" +
10749 " if (o != null) {\n" +
10750 " o = null;\n" +
10751 " }\n" +
10752 " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
10753 "Dead code\n" +
10754 "----------\n" +
10755 "2. ERROR in X.java (at line 9)\n" +
10756 " o.toString();\n" +
10757 " ^\n" +
10758 "Null pointer access: The variable o can only be null at this location\n" +
10759 "----------\n",
10760 // javac options
10761 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError /* javac test options */);
10762 }
10763 }
10764 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10765 // adding distinct options to control null checks in more detail
test1057_options_instanceof_is_check()10766 public void test1057_options_instanceof_is_check() {
10767 this.runNegativeTest(
10768 new String[] {
10769 "X.java",
10770 "public class X {\n" +
10771 " void foo(Object p) {\n" +
10772 " Object o = null;\n" +
10773 " if (p == null) {}\n" + // taint p
10774 " if (o instanceof String) {};\n" +
10775 " if (p instanceof String) {};\n" +
10776 " }\n" +
10777 "}\n"},
10778 "----------\n" +
10779 "1. ERROR in X.java (at line 5)\n" +
10780 " if (o instanceof String) {};\n" +
10781 " ^\n" +
10782 "instanceof always yields false: The variable o can only be null at this location\n" +
10783 "----------\n",
10784 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10785 }
10786 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10787 // adding distinct options to control null checks in more detail
test1058_options_instanceof_is_check()10788 public void test1058_options_instanceof_is_check() {
10789 Map customOptions = getCompilerOptions();
10790 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
10791 this.runConformTest(
10792 new String[] {
10793 "X.java",
10794 "public class X {\n" +
10795 " void foo(Object p) {\n" +
10796 " Object o = null;\n" +
10797 " if (p == null) {}\n" + // taint p
10798 " if (o instanceof String) {};\n" +
10799 " if (p instanceof String) {};\n" +
10800 " }\n" +
10801 "}\n"},
10802 null /* no expected output string */,
10803 null /* no extra class libraries */,
10804 true /* flush output directory */,
10805 null /* no vm arguments */,
10806 customOptions,
10807 null /* no custom requestor*/,
10808 false /* do not skip javac for this peculiar test */);
10809 }
10810 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=170704
10811 // adding distinct options to control null checks in more detail
test1059_options_cannot_be_null_check()10812 public void test1059_options_cannot_be_null_check() {
10813 Map customOptions = getCompilerOptions();
10814 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
10815 this.runConformTest(
10816 new String[] {
10817 "X.java",
10818 "public class X {\n" +
10819 " void foo(Object p) {\n" +
10820 " Object o = new Object();\n" +
10821 " if (o == null) {}\n" +
10822 " }\n" +
10823 "}\n"},
10824 null /* no expected output string */,
10825 null /* no extra class libraries */,
10826 true /* flush output directory */,
10827 null /* no vm arguments */,
10828 customOptions,
10829 null /* no custom requestor*/,
10830 false /* do not skip javac for this peculiar test */);
10831 }
10832 // encoding validation
test1500()10833 public void test1500() {
10834 this.runConformTest(
10835 new String[] {
10836 "X.java",
10837 "public class X {\n" +
10838 " void foo(Object o, int i, boolean b, Object u) {\n" +
10839 " o.toString();\n" +
10840 " switch (i) {\n" +
10841 " case 0:\n" +
10842 " if (b) {\n" +
10843 " o = u;\n" +
10844 " } else {\n" +
10845 " o = new Object();\n" +
10846 " }\n" +
10847 " break;\n" +
10848 " }\n" +
10849 " if (o == null) { /* empty */ }\n" +
10850 " }\n" +
10851 "}\n"},
10852 "");
10853 }
10854
10855 // encoding validation
test1501()10856 public void test1501() {
10857 this.runConformTest(
10858 new String[] {
10859 "X.java",
10860 "public class X {\n" +
10861 " void foo(Object o, int i, boolean b, Object u) {\n" +
10862 " if (b) {\n" +
10863 " o = new Object();\n" +
10864 " }\n" +
10865 " o.toString();\n" +
10866 " switch (i) {\n" +
10867 " case 0:\n" +
10868 " if (b) {\n" +
10869 " o = u;\n" +
10870 " } else {\n" +
10871 " o = new Object();\n" +
10872 " }\n" +
10873 " break;\n" +
10874 " }\n" +
10875 " if (o == null) { /* empty */ }\n" +
10876 " }\n" +
10877 "}\n"},
10878 "");
10879 }
10880
10881 // encoding validation
test1502()10882 public void test1502() {
10883 this.runConformTest(
10884 new String[] {
10885 "X.java",
10886 "public class X {\n" +
10887 " void foo(Object o, int i, boolean b, Object u) {\n" +
10888 " if (b) {\n" +
10889 " o = u;\n" +
10890 " }\n" +
10891 " o.toString();\n" +
10892 " switch (i) {\n" +
10893 " case 0:\n" +
10894 " if (b) {\n" +
10895 " o = u;\n" +
10896 " } else {\n" +
10897 " o = new Object();\n" +
10898 " }\n" +
10899 " break;\n" +
10900 " }\n" +
10901 " if (o == null) { /* empty */ }\n" +
10902 " }\n" +
10903 "}\n"},
10904 "");
10905 }
10906
10907 // encoding validation
test1503()10908 public void test1503() {
10909 this.runConformTest(
10910 new String[] {
10911 "X.java",
10912 "public class X {\n" +
10913 " void foo(Object o, int i, boolean b, Object u) {\n" +
10914 " if (b) {\n" +
10915 " o = u;\n" +
10916 " } else {\n" +
10917 " o = new Object();\n" +
10918 " }\n" +
10919 " o.toString();\n" +
10920 " switch (i) {\n" +
10921 " case 0:\n" +
10922 " if (b) {\n" +
10923 " o = u;\n" +
10924 " } else {\n" +
10925 " o = new Object();\n" +
10926 " }\n" +
10927 " break;\n" +
10928 " }\n" +
10929 " if (o == null) { /* empty */ }\n" +
10930 " }\n" +
10931 "}\n"},
10932 "");
10933 }
10934
10935 // flow info low-level validation
test2000_flow_info()10936 public void test2000_flow_info() {
10937 this.runNegativeTest(
10938 new String[] {
10939 "X.java",
10940 "public class X {\n" +
10941 "\n" +
10942 " void foo() {\n" +
10943 " Object o0 = new Object(), o1 = o0, o2 = o0, o3 = o0, o4 = o0,\n" +
10944 " o5 = o0, o6 = o0, o7 = o0, o8 = o0, o9 = o0,\n" +
10945 " o10 = o0, o11 = o0, o12 = o0, o13 = o0, o14 = o0,\n" +
10946 " o15 = o0, o16 = o0, o17 = o0, o18 = o0, o19 = o0,\n" +
10947 " o20 = o0, o21 = o0, o22 = o0, o23 = o0, o24 = o0,\n" +
10948 " o25 = o0, o26 = o0, o27 = o0, o28 = o0, o29 = o0,\n" +
10949 " o30 = o0, o31 = o0, o32 = o0, o33 = o0, o34 = o0,\n" +
10950 " o35 = o0, o36 = o0, o37 = o0, o38 = o0, o39 = o0,\n" +
10951 " o40 = o0, o41 = o0, o42 = o0, o43 = o0, o44 = o0,\n" +
10952 " o45 = o0, o46 = o0, o47 = o0, o48 = o0, o49 = o0,\n" +
10953 " o50 = o0, o51 = o0, o52 = o0, o53 = o0, o54 = o0,\n" +
10954 " o55 = o0, o56 = o0, o57 = o0, o58 = o0, o59 = o0,\n" +
10955 " o60 = o0, o61 = o0, o62 = o0, o63 = o0, o64 = o0,\n" +
10956 " o65 = o0, o66 = o0, o67 = o0, o68 = o0, o69 = o0;\n" +
10957 " if (o65 == null) { /* */ }\n" + // complain
10958 " if (o65 != null) { /* */ }\n" +
10959 " }\n" +
10960 "}\n"},
10961 "----------\n" +
10962 "1. ERROR in X.java (at line 18)\n" +
10963 " if (o65 == null) { /* */ }\n" +
10964 " ^^^\n" +
10965 "Null comparison always yields false: The variable o65 cannot be null at this location\n" +
10966 "----------\n" +
10967 "2. WARNING in X.java (at line 18)\n" +
10968 " if (o65 == null) { /* */ }\n" +
10969 " ^^^^^^^^^\n" +
10970 "Dead code\n" +
10971 "----------\n" +
10972 "3. ERROR in X.java (at line 19)\n" +
10973 " if (o65 != null) { /* */ }\n" +
10974 " ^^^\n" +
10975 "Redundant null check: The variable o65 cannot be null at this location\n" +
10976 "----------\n",
10977 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
10978 }
10979
test2001_flow_info()10980 public void test2001_flow_info() {
10981 this.runConformTest(
10982 new String[] {
10983 "X.java",
10984 "public class X {\n" +
10985 "\n" +
10986 " void foo(\n" +
10987 " Object o0, Object o1, Object o2, Object o3, Object o4,\n" +
10988 " Object o5, Object o6, Object o7, Object o8, Object o9,\n" +
10989 " Object o10, Object o11, Object o12, Object o13, Object o14,\n" +
10990 " Object o15, Object o16, Object o17, Object o18, Object o19,\n" +
10991 " Object o20, Object o21, Object o22, Object o23, Object o24,\n" +
10992 " Object o25, Object o26, Object o27, Object o28, Object o29,\n" +
10993 " Object o30, Object o31, Object o32, Object o33, Object o34,\n" +
10994 " Object o35, Object o36, Object o37, Object o38, Object o39,\n" +
10995 " Object o40, Object o41, Object o42, Object o43, Object o44,\n" +
10996 " Object o45, Object o46, Object o47, Object o48, Object o49,\n" +
10997 " Object o50, Object o51, Object o52, Object o53, Object o54,\n" +
10998 " Object o55, Object o56, Object o57, Object o58, Object o59,\n" +
10999 " Object o60, Object o61, Object o62, Object o63, Object o64,\n" +
11000 " Object o65, Object o66, Object o67, Object o68, Object o69) {\n" +
11001 " if (o65 == null) { /* */ }\n" +
11002 " if (o65 != null) { /* */ }\n" +
11003 " }\n" +
11004 "}\n"},
11005 "");
11006 }
11007
test2002_flow_info()11008 public void test2002_flow_info() {
11009 this.runConformTest(
11010 new String[] {
11011 "X.java",
11012 "public class X {\n" +
11013 " Object m0, m1, m2, m3, m4,\n" +
11014 " m5, m6, m7, m8, m9,\n" +
11015 " m10, m11, m12, m13, m14,\n" +
11016 " m15, m16, m17, m18, m19,\n" +
11017 " m20, m21, m22, m23, m24,\n" +
11018 " m25, m26, m27, m28, m29,\n" +
11019 " m30, m31, m32, m33, m34,\n" +
11020 " m35, m36, m37, m38, m39,\n" +
11021 " m40, m41, m42, m43, m44,\n" +
11022 " m45, m46, m47, m48, m49,\n" +
11023 " m50, m51, m52, m53, m54,\n" +
11024 " m55, m56, m57, m58, m59,\n" +
11025 " m60, m61, m62, m63;\n" +
11026 " void foo(Object o) {\n" +
11027 " if (o == null) { /* */ }\n" +
11028 " if (o != null) { /* */ }\n" +
11029 " }\n" +
11030 "}\n"},
11031 "");
11032 }
11033
test2003_flow_info()11034 public void test2003_flow_info() {
11035 this.runConformTest(
11036 new String[] {
11037 "X.java",
11038 "public class X {\n" +
11039 " Object m0, m1, m2, m3, m4,\n" +
11040 " m5, m6, m7, m8, m9,\n" +
11041 " m10, m11, m12, m13, m14,\n" +
11042 " m15, m16, m17, m18, m19,\n" +
11043 " m20, m21, m22, m23, m24,\n" +
11044 " m25, m26, m27, m28, m29,\n" +
11045 " m30, m31, m32, m33, m34,\n" +
11046 " m35, m36, m37, m38, m39,\n" +
11047 " m40, m41, m42, m43, m44,\n" +
11048 " m45, m46, m47, m48, m49,\n" +
11049 " m50, m51, m52, m53, m54,\n" +
11050 " m55, m56, m57, m58, m59,\n" +
11051 " m60, m61, m62, m63;\n" +
11052 " void foo(Object o) {\n" +
11053 " o.toString();\n" +
11054 " }\n" +
11055 "}\n"},
11056 "");
11057 }
11058
test2004_flow_info()11059 public void test2004_flow_info() {
11060 this.runNegativeTest(
11061 new String[] {
11062 "X.java",
11063 "public class X {\n" +
11064 " Object m0, m1, m2, m3, m4,\n" +
11065 " m5, m6, m7, m8, m9,\n" +
11066 " m10, m11, m12, m13, m14,\n" +
11067 " m15, m16, m17, m18, m19,\n" +
11068 " m20, m21, m22, m23, m24,\n" +
11069 " m25, m26, m27, m28, m29,\n" +
11070 " m30, m31, m32, m33, m34,\n" +
11071 " m35, m36, m37, m38, m39,\n" +
11072 " m40, m41, m42, m43, m44,\n" +
11073 " m45, m46, m47, m48, m49,\n" +
11074 " m50, m51, m52, m53, m54,\n" +
11075 " m55, m56, m57, m58, m59,\n" +
11076 " m60, m61, m62, m63;\n" +
11077 " void foo() {\n" +
11078 " Object o;\n" +
11079 " if (o == null) { /* */ }\n" +
11080 " }\n" +
11081 "}\n"},
11082 "----------\n" +
11083 "1. ERROR in X.java (at line 17)\n" +
11084 " if (o == null) { /* */ }\n" +
11085 " ^\n" +
11086 "The local variable o may not have been initialized\n" +
11087 "----------\n");
11088 }
11089
test2005_flow_info()11090 public void test2005_flow_info() {
11091 this.runConformTest(
11092 new String[] {
11093 "X.java",
11094 "public class X {\n" +
11095 " Object m0, m1, m2, m3, m4,\n" +
11096 " m5, m6, m7, m8, m9,\n" +
11097 " m10, m11, m12, m13, m14,\n" +
11098 " m15, m16, m17, m18, m19,\n" +
11099 " m20, m21, m22, m23, m24,\n" +
11100 " m25, m26, m27, m28, m29,\n" +
11101 " m30, m31, m32, m33, m34,\n" +
11102 " m35, m36, m37, m38, m39,\n" +
11103 " m40, m41, m42, m43, m44,\n" +
11104 " m45, m46, m47, m48, m49,\n" +
11105 " m50, m51, m52, m53, m54,\n" +
11106 " m55, m56, m57, m58, m59,\n" +
11107 " m60, m61, m62, m63;\n" +
11108 " void foo(Object o) {\n" +
11109 " o = null;\n" +
11110 " }\n" +
11111 "}\n"},
11112 "");
11113 }
11114
test2006_flow_info()11115 public void test2006_flow_info() {
11116 this.runConformTest(
11117 new String[] {
11118 "X.java",
11119 "public class X {\n" +
11120 " Object m0, m1, m2, m3, m4,\n" +
11121 " m5, m6, m7, m8, m9,\n" +
11122 " m10, m11, m12, m13, m14,\n" +
11123 " m15, m16, m17, m18, m19,\n" +
11124 " m20, m21, m22, m23, m24,\n" +
11125 " m25, m26, m27, m28, m29,\n" +
11126 " m30, m31, m32, m33, m34,\n" +
11127 " m35, m36, m37, m38, m39,\n" +
11128 " m40, m41, m42, m43, m44,\n" +
11129 " m45, m46, m47, m48, m49,\n" +
11130 " m50, m51, m52, m53, m54,\n" +
11131 " m55, m56, m57, m58, m59,\n" +
11132 " m60, m61, m62, m63;\n" +
11133 " void foo() {\n" +
11134 " Object o = null;\n" +
11135 " }\n" +
11136 "}\n"},
11137 "");
11138 }
11139
test2007_flow_info()11140 public void test2007_flow_info() {
11141 this.runConformTest(
11142 new String[] {
11143 "X.java",
11144 "public class X {\n" +
11145 " Object m0, m1, m2, m3, m4,\n" +
11146 " m5, m6, m7, m8, m9,\n" +
11147 " m10, m11, m12, m13, m14,\n" +
11148 " m15, m16, m17, m18, m19,\n" +
11149 " m20, m21, m22, m23, m24,\n" +
11150 " m25, m26, m27, m28, m29,\n" +
11151 " m30, m31, m32, m33, m34,\n" +
11152 " m35, m36, m37, m38, m39,\n" +
11153 " m40, m41, m42, m43, m44,\n" +
11154 " m45, m46, m47, m48, m49,\n" +
11155 " m50, m51, m52, m53, m54,\n" +
11156 " m55, m56, m57, m58, m59,\n" +
11157 " m60, m61, m62, m63;\n" +
11158 " void foo() {\n" +
11159 " Object o[] = null;\n" +
11160 " }\n" +
11161 "}\n"},
11162 "");
11163 }
11164
11165 // null analysis -- flow info
test2008_flow_info()11166 public void test2008_flow_info() {
11167 this.runConformTest(
11168 new String[] {
11169 "X.java",
11170 "public class X {\n" +
11171 " Object m0, m1, m2, m3, m4,\n" +
11172 " m5, m6, m7, m8, m9,\n" +
11173 " m10, m11, m12, m13, m14,\n" +
11174 " m15, m16, m17, m18, m19,\n" +
11175 " m20, m21, m22, m23, m24,\n" +
11176 " m25, m26, m27, m28, m29,\n" +
11177 " m30, m31, m32, m33, m34,\n" +
11178 " m35, m36, m37, m38, m39,\n" +
11179 " m40, m41, m42, m43, m44,\n" +
11180 " m45, m46, m47, m48, m49,\n" +
11181 " m50, m51, m52, m53, m54,\n" +
11182 " m55, m56, m57, m58, m59,\n" +
11183 " m60, m61, m62, m63;\n" +
11184 " void foo(boolean b) {\n" +
11185 " Object o = null;\n" +
11186 " while (o == null) {\n" +
11187 // quiet: first iteration is sure to find o null,
11188 // but other iterations may change it
11189 " try { /* */ }\n" +
11190 " finally {\n" +
11191 " if (b) {\n" +
11192 " o = new Object();\n" +
11193 " }\n" +
11194 " }\n" +
11195 " }\n" +
11196 " }\n" +
11197 "}\n"},
11198 "");
11199 }
11200
11201 // null analysis -- flow info
test2009_flow_info()11202 public void test2009_flow_info() {
11203 this.runNegativeTest(
11204 new String[] {
11205 "X.java",
11206 "public class X {\n" +
11207 " Object m0, m1, m2, m3, m4,\n" +
11208 " m5, m6, m7, m8, m9,\n" +
11209 " m10, m11, m12, m13, m14,\n" +
11210 " m15, m16, m17, m18, m19,\n" +
11211 " m20, m21, m22, m23, m24,\n" +
11212 " m25, m26, m27, m28, m29,\n" +
11213 " m30, m31, m32, m33, m34,\n" +
11214 " m35, m36, m37, m38, m39,\n" +
11215 " m40, m41, m42, m43, m44,\n" +
11216 " m45, m46, m47, m48, m49,\n" +
11217 " m50, m51, m52, m53, m54,\n" +
11218 " m55, m56, m57, m58, m59,\n" +
11219 " m60, m61, m62, m63;\n" +
11220 " void foo(Object o) {\n" +
11221 " try { /* */ }\n" +
11222 " finally {\n" +
11223 " o = new Object();\n" +
11224 " }\n" +
11225 " if (o == null) { /* */ }\n" +
11226 " }\n" +
11227 "}\n"},
11228 "----------\n" +
11229 "1. ERROR in X.java (at line 20)\n" +
11230 " if (o == null) { /* */ }\n" +
11231 " ^\n" +
11232 "Null comparison always yields false: The variable o cannot be null at this location\n" +
11233 "----------\n" +
11234 "2. WARNING in X.java (at line 20)\n" +
11235 " if (o == null) { /* */ }\n" +
11236 " ^^^^^^^^^\n" +
11237 "Dead code\n" +
11238 "----------\n",
11239 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11240 }
11241
11242 // null analysis -- flow info
test2010_flow_info()11243 public void test2010_flow_info() {
11244 this.runNegativeTest(
11245 new String[] {
11246 "X.java",
11247 "public class X {\n" +
11248 " Object m00, m01, m02, m03, m04,\n" +
11249 " m05, m06, m07, m08, m09,\n" +
11250 " m10, m11, m12, m13, m14,\n" +
11251 " m15, m16, m17, m18, m19,\n" +
11252 " m20, m21, m22, m23, m24,\n" +
11253 " m25, m26, m27, m28, m29,\n" +
11254 " m30, m31, m32, m33, m34,\n" +
11255 " m35, m36, m37, m38, m39,\n" +
11256 " m40, m41, m42, m43, m44,\n" +
11257 " m45, m46, m47, m48, m49,\n" +
11258 " m50, m51, m52, m53, m54,\n" +
11259 " m55, m56, m57, m58, m59,\n" +
11260 " m60, m61, m62, m63;\n" +
11261 " void foo() {\n" +
11262 " Object o;\n" +
11263 " try { /* */ }\n" +
11264 " finally {\n" +
11265 " o = new Object();\n" +
11266 " }\n" +
11267 " if (o == null) { /* */ }\n" +
11268 " }\n" +
11269 "}\n"},
11270 "----------\n" +
11271 "1. ERROR in X.java (at line 21)\n" +
11272 " if (o == null) { /* */ }\n" +
11273 " ^\n" +
11274 "Null comparison always yields false: The variable o cannot be null at this location\n" +
11275 "----------\n" +
11276 "2. WARNING in X.java (at line 21)\n" +
11277 " if (o == null) { /* */ }\n" +
11278 " ^^^^^^^^^\n" +
11279 "Dead code\n" +
11280 "----------\n",
11281 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11282 }
11283
11284 // null analysis -- flow info
test2011_flow_info()11285 public void test2011_flow_info() {
11286 this.runNegativeTest(
11287 new String[] {
11288 "X.java",
11289 "public class X {\n" +
11290 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11291 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11292 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11293 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11294 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11295 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11296 " m060, m061, m062, m063;\n" +
11297 " void foo() {\n" +
11298 " Object o000, o001, o002, o003, o004, o005, o006, o007, o008, o009,\n" +
11299 " o010, o011, o012, o013, o014, o015, o016, o017, o018, o019,\n" +
11300 " o020, o021, o022, o023, o024, o025, o026, o027, o028, o029,\n" +
11301 " o030, o031, o032, o033, o034, o035, o036, o037, o038, o039,\n" +
11302 " o040, o041, o042, o043, o044, o045, o046, o047, o048, o049,\n" +
11303 " o050, o051, o052, o053, o054, o055, o056, o057, o058, o059,\n" +
11304 " o060, o061, o062, o063;\n" +
11305 " Object o;\n" +
11306 " try {\n" +
11307 " o000 = new Object();\n" +
11308 " }\n" +
11309 " finally {\n" +
11310 " o = new Object();\n" +
11311 " }\n" +
11312 " if (o == null) { /* */ }\n" +
11313 " }\n" +
11314 "}\n"},
11315 "----------\n" +
11316 "1. ERROR in X.java (at line 24)\n" +
11317 " if (o == null) { /* */ }\n" +
11318 " ^\n" +
11319 "Null comparison always yields false: The variable o cannot be null at this location\n" +
11320 "----------\n" +
11321 "2. WARNING in X.java (at line 24)\n" +
11322 " if (o == null) { /* */ }\n" +
11323 " ^^^^^^^^^\n" +
11324 "Dead code\n" +
11325 "----------\n",
11326 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11327 }
11328
11329 // null analysis -- flow info
test2012_flow_info()11330 public void test2012_flow_info() {
11331 this.runNegativeTest(
11332 new String[] {
11333 "X.java",
11334 "public class X {\n" +
11335 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11336 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11337 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11338 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11339 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11340 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11341 " m060, m061, m062, m063;\n" +
11342 " void foo() {\n" +
11343 " Object o000, o001, o002, o003, o004, o005, o006, o007, o008, o009,\n" +
11344 " o010, o011, o012, o013, o014, o015, o016, o017, o018, o019,\n" +
11345 " o020, o021, o022, o023, o024, o025, o026, o027, o028, o029,\n" +
11346 " o030, o031, o032, o033, o034, o035, o036, o037, o038, o039,\n" +
11347 " o040, o041, o042, o043, o044, o045, o046, o047, o048, o049,\n" +
11348 " o050, o051, o052, o053, o054, o055, o056, o057, o058, o059,\n" +
11349 " o060, o061, o062, o063;\n" +
11350 " Object o;\n" +
11351 " try {\n" +
11352 " o = new Object();\n" +
11353 " }\n" +
11354 " finally {\n" +
11355 " o000 = new Object();\n" +
11356 " }\n" +
11357 " if (o == null) { /* */ }\n" +
11358 " }\n" +
11359 "}\n"},
11360 "----------\n" +
11361 "1. ERROR in X.java (at line 24)\n" +
11362 " if (o == null) { /* */ }\n" +
11363 " ^\n" +
11364 "Null comparison always yields false: The variable o cannot be null at this location\n" +
11365 "----------\n" +
11366 "2. WARNING in X.java (at line 24)\n" +
11367 " if (o == null) { /* */ }\n" +
11368 " ^^^^^^^^^\n" +
11369 "Dead code\n" +
11370 "----------\n",
11371 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11372 }
11373
11374 // null analysis -- flow info
test2013_flow_info()11375 public void test2013_flow_info() {
11376 this.runNegativeTest(
11377 new String[] {
11378 "X.java",
11379 "public class X {\n" +
11380 " boolean dummy;\n" +
11381 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11382 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11383 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11384 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11385 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11386 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11387 " m060, m061, m062, m063;\n" +
11388 " void foo(Object u) {\n" +
11389 " Object o = null;\n" +
11390 " while (dummy) {\n" +
11391 " o = u;\n" +
11392 " }\n" +
11393 " o.toString();\n" +
11394 " }\n" +
11395 "}\n"},
11396 "----------\n" +
11397 "1. ERROR in X.java (at line 15)\n" +
11398 " o.toString();\n" +
11399 " ^\n" +
11400 "Potential null pointer access: The variable o may be null at this location\n" +
11401 "----------\n",
11402 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11403 }
11404
11405 // null analysis -- flow info
test2014_flow_info()11406 public void test2014_flow_info() {
11407 this.runNegativeTest(
11408 new String[] {
11409 "X.java",
11410 "class X {\n" +
11411 " int m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11412 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11413 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11414 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11415 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11416 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11417 " m060, m061, m062, m063;\n" +
11418 " final int m064;\n" +
11419 " X() {\n" +
11420 " m064 = 10;\n" +
11421 " class Inner extends X {\n" +
11422 " int m100, m101, m102, m103, m104, m105, m106, m107, m108, m109,\n" +
11423 " m110, m111, m112, m113, m114, m115, m116, m117, m118, m119,\n" +
11424 " m120, m121, m122, m123, m124, m125, m126, m127, m128, m129,\n" +
11425 " m130, m131, m132, m133, m134, m135, m136, m137, m138, m139,\n" +
11426 " m140, m141, m142, m143, m144, m145, m146, m147, m148, m149,\n" +
11427 " m150, m151, m152, m153, m154, m155, m156, m157, m158, m159,\n" +
11428 " m160, m161, m162, m163;\n" +
11429 " final int m164;\n" +
11430 " int bar() {\n" +
11431 " return m100 + m101 + m102 + m103 + m104 +\n" +
11432 " m105 + m106 + m107 + m108 + m109 +\n" +
11433 " m110 + m111 + m112 + m113 + m114 +\n" +
11434 " m115 + m116 + m117 + m118 + m119 +\n" +
11435 " m120 + m121 + m122 + m123 + m124 +\n" +
11436 " m125 + m126 + m127 + m128 + m129 +\n" +
11437 " m130 + m131 + m132 + m133 + m134 +\n" +
11438 " m135 + m136 + m137 + m138 + m139 +\n" +
11439 " m140 + m141 + m142 + m143 + m144 +\n" +
11440 " m145 + m146 + m147 + m148 + m149 +\n" +
11441 " m150 + m151 + m152 + m153 + m154 +\n" +
11442 " m155 + m156 + m157 + m158 + m159 +\n" +
11443 " m160 + m161 + m162 + m163 + m164;\n" +
11444 " }\n" +
11445 " };\n" +
11446 " System.out.println((new Inner()).bar());\n" +
11447 " }\n" +
11448 "}\n"},
11449 "----------\n" +
11450 "1. ERROR in X.java (at line 20)\n" +
11451 " final int m164;\n" +
11452 " ^^^^\n" +
11453 "The blank final field m164 may not have been initialized\n" +
11454 "----------\n");
11455 }
11456
11457 // null analysis -- flow info
test2015_flow_info()11458 public void test2015_flow_info() {
11459 this.runNegativeTest(
11460 new String[] {
11461 "X.java",
11462 "class X {\n" +
11463 " int m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11464 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11465 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11466 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11467 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11468 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11469 " m060, m061, m062, m063;\n" +
11470 " final int m200;\n" +
11471 " int m201, m202, m203, m204, m205, m206, m207, m208, m209,\n" +
11472 " m210, m211, m212, m213, m214, m215, m216, m217, m218, m219,\n" +
11473 " m220, m221, m222, m223, m224, m225, m226, m227, m228, m229,\n" +
11474 " m230, m231, m232, m233, m234, m235, m236, m237, m238, m239,\n" +
11475 " m240, m241, m242, m243, m244, m245, m246, m247, m248, m249,\n" +
11476 " m250, m251, m252, m253, m254, m255, m256, m257, m258, m259,\n" +
11477 " m260, m261, m262, m263;\n" +
11478 " int m301, m302, m303, m304, m305, m306, m307, m308, m309,\n" +
11479 " m310, m311, m312, m313, m314, m315, m316, m317, m318, m319,\n" +
11480 " m320, m321, m322, m323, m324, m325, m326, m327, m328, m329,\n" +
11481 " m330, m331, m332, m333, m334, m335, m336, m337, m338, m339,\n" +
11482 " m340, m341, m342, m343, m344, m345, m346, m347, m348, m349,\n" +
11483 " m350, m351, m352, m353, m354, m355, m356, m357, m358, m359,\n" +
11484 " m360, m361, m362, m363;\n" +
11485 " X() {\n" +
11486 " m200 = 10;\n" +
11487 " class Inner extends X {\n" +
11488 " int m100, m101, m102, m103, m104, m105, m106, m107, m108, m109,\n" +
11489 " m110, m111, m112, m113, m114, m115, m116, m117, m118, m119,\n" +
11490 " m120, m121, m122, m123, m124, m125, m126, m127, m128, m129,\n" +
11491 " m130, m131, m132, m133, m134, m135, m136, m137, m138, m139,\n" +
11492 " m140, m141, m142, m143, m144, m145, m146, m147, m148, m149,\n" +
11493 " m150, m151, m152, m153, m154, m155, m156, m157, m158, m159,\n" +
11494 " m160, m161, m162, m163;\n" +
11495 " final int m164;\n" +
11496 " int bar() {\n" +
11497 " return m100 + m101 + m102 + m103 + m104 +\n" +
11498 " m105 + m106 + m107 + m108 + m109 +\n" +
11499 " m110 + m111 + m112 + m113 + m114 +\n" +
11500 " m115 + m116 + m117 + m118 + m119 +\n" +
11501 " m120 + m121 + m122 + m123 + m124 +\n" +
11502 " m125 + m126 + m127 + m128 + m129 +\n" +
11503 " m130 + m131 + m132 + m133 + m134 +\n" +
11504 " m135 + m136 + m137 + m138 + m139 +\n" +
11505 " m140 + m141 + m142 + m143 + m144 +\n" +
11506 " m145 + m146 + m147 + m148 + m149 +\n" +
11507 " m150 + m151 + m152 + m153 + m154 +\n" +
11508 " m155 + m156 + m157 + m158 + m159 +\n" +
11509 " m160 + m161 + m162 + m163 + m164;\n" +
11510 " }\n" +
11511 " };\n" +
11512 " System.out.println((new Inner()).bar());\n" +
11513 " }\n" +
11514 "}\n"},
11515 "----------\n" +
11516 "1. ERROR in X.java (at line 34)\n" +
11517 " final int m164;\n" +
11518 " ^^^^\n" +
11519 "The blank final field m164 may not have been initialized\n" +
11520 "----------\n");
11521 }
11522
11523 // null analysis -- flow info
test2016_flow_info()11524 public void test2016_flow_info() {
11525 this.runNegativeTest(
11526 new String[] {
11527 "X.java",
11528 "class X {\n" +
11529 " int m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11530 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11531 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11532 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11533 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11534 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11535 " m060, m061;\n" +
11536 " final int m062;\n" +
11537 " {\n" +
11538 " int l063, m201 = 0, m202, m203, m204, m205, m206, m207, m208, m209,\n" +
11539 " m210, m211, m212, m213, m214, m215, m216, m217, m218, m219,\n" +
11540 " m220, m221, m222, m223, m224, m225, m226, m227, m228, m229,\n" +
11541 " m230, m231, m232, m233, m234, m235, m236, m237, m238, m239,\n" +
11542 " m240, m241, m242, m243, m244, m245, m246, m247, m248, m249,\n" +
11543 " m250, m251, m252, m253, m254, m255, m256, m257, m258, m259,\n" +
11544 " m260, m261, m262, m263;\n" +
11545 " int m301, m302, m303, m304, m305, m306, m307, m308, m309,\n" +
11546 " m310, m311, m312, m313, m314, m315, m316, m317, m318, m319,\n" +
11547 " m320, m321, m322, m323, m324, m325, m326, m327, m328, m329,\n" +
11548 " m330, m331, m332, m333, m334, m335, m336, m337, m338, m339,\n" +
11549 " m340, m341, m342, m343, m344, m345, m346, m347, m348, m349,\n" +
11550 " m350, m351, m352, m353, m354, m355, m356, m357, m358, m359,\n" +
11551 " m360 = 0, m361 = 0, m362 = 0, m363 = 0;\n" +
11552 " m062 = m360;\n" +
11553 " }\n" +
11554 " X() {\n" +
11555 " int l0, l1;\n" +
11556 " m000 = l1;\n" +
11557 " class Inner extends X {\n" +
11558 " int bar() {\n" +
11559 " return 0;\n" +
11560 " }\n" +
11561 " };\n" +
11562 " System.out.println((new Inner()).bar());\n" +
11563 " }\n" +
11564 "}\n"},
11565 "----------\n" +
11566 "1. ERROR in X.java (at line 29)\n" +
11567 " m000 = l1;\n" +
11568 " ^^\n" +
11569 "The local variable l1 may not have been initialized\n" +
11570 "----------\n");
11571 }
11572
test2017_flow_info()11573 public void test2017_flow_info() {
11574 this.runConformTest(
11575 new String[] {
11576 "X.java",
11577 "public class X {\n" +
11578 " boolean dummy;\n" +
11579 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11580 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11581 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11582 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11583 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11584 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11585 " m060, m061, m062, m063;\n" +
11586 " void foo(Object u) {\n" +
11587 " Object o = null;\n" +
11588 " while (dummy) {\n" +
11589 " if (dummy) {\n" + // uncorrelated
11590 " o = u;\n" +
11591 " continue;\n" +
11592 " }\n" +
11593 " }\n" +
11594 " if (o != null) { /* */ }\n" +
11595 " }\n" +
11596 "}\n"},
11597 "");
11598 }
11599
test2018_flow_info()11600 public void test2018_flow_info() {
11601 this.runNegativeTest(
11602 new String[] {
11603 "X.java",
11604 "public class X {\n" +
11605 " boolean dummy;\n" +
11606 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11607 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11608 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11609 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11610 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11611 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11612 " m060, m061, m062, m063;\n" +
11613 " void foo() {\n" +
11614 " Object o;\n" +
11615 " while (dummy) {\n" +
11616 " if (dummy) {\n" + // uncorrelated
11617 " o = null;\n" +
11618 " continue;\n" +
11619 " }\n" +
11620 " }\n" +
11621 " o.toString();\n" +
11622 " }\n" +
11623 "}\n"},
11624 "----------\n" +
11625 "1. ERROR in X.java (at line 18)\n" +
11626 " o.toString();\n" +
11627 " ^\n" +
11628 "The local variable o may not have been initialized\n" +
11629 "----------\n" +
11630 "2. ERROR in X.java (at line 18)\n" +
11631 " o.toString();\n" +
11632 " ^\n" +
11633 "Potential null pointer access: The variable o may be null at this location\n" +
11634 "----------\n");
11635 }
11636
test2019_flow_info()11637 public void test2019_flow_info() {
11638 this.runNegativeTest(
11639 new String[] {
11640 "X.java",
11641 "public class X {\n" +
11642 " boolean dummy;\n" +
11643 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11644 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11645 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11646 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11647 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11648 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11649 " m060, m061, m062, m063;\n" +
11650 " void foo() {\n" +
11651 " Object o;\n" +
11652 " while (dummy) {\n" +
11653 " if (dummy) {\n" + // uncorrelated
11654 " continue;\n" +
11655 " }\n" +
11656 " o = null;\n" +
11657 " }\n" +
11658 " o.toString();\n" +
11659 " }\n" +
11660 "}\n"},
11661 "----------\n" +
11662 "1. ERROR in X.java (at line 18)\n" +
11663 " o.toString();\n" +
11664 " ^\n" +
11665 "The local variable o may not have been initialized\n" +
11666 "----------\n" +
11667 "2. ERROR in X.java (at line 18)\n" +
11668 " o.toString();\n" +
11669 " ^\n" +
11670 "Potential null pointer access: The variable o may be null at this location\n" +
11671 "----------\n");
11672 }
11673
test2020_flow_info()11674 public void test2020_flow_info() {
11675 this.runNegativeTest(
11676 new String[] {
11677 "X.java",
11678 "public class X {\n" +
11679 " boolean dummy;\n" +
11680 " Object m000, m001, m002, m003, m004, m005, m006, m007, m008, m009,\n" +
11681 " m010, m011, m012, m013, m014, m015, m016, m017, m018, m019,\n" +
11682 " m020, m021, m022, m023, m024, m025, m026, m027, m028, m029,\n" +
11683 " m030, m031, m032, m033, m034, m035, m036, m037, m038, m039,\n" +
11684 " m040, m041, m042, m043, m044, m045, m046, m047, m048, m049,\n" +
11685 " m050, m051, m052, m053, m054, m055, m056, m057, m058, m059,\n" +
11686 " m060, m061, m062, m063;\n" +
11687 " int m200, m201, m202, m203, m204, m205, m206, m207, m208, m209,\n" +
11688 " m210, m211, m212, m213, m214, m215, m216, m217, m218, m219,\n" +
11689 " m220, m221, m222, m223, m224, m225, m226, m227, m228, m229,\n" +
11690 " m230, m231, m232, m233, m234, m235, m236, m237, m238, m239,\n" +
11691 " m240, m241, m242, m243, m244, m245, m246, m247, m248, m249,\n" +
11692 " m250, m251, m252, m253, m254, m255, m256, m257, m258, m259,\n" +
11693 " m260, m261;\n" +
11694 " void foo() {\n" +
11695 " Object o0, o1;\n" +
11696 " while (dummy) {\n" +
11697 " o0 = new Object();\n" +
11698 " if (dummy) {\n" + // uncorrelated
11699 " o1 = null;\n" +
11700 " continue;\n" +
11701 " }\n" +
11702 " }\n" +
11703 " o1.toString();\n" +
11704 " }\n" +
11705 "}\n"},
11706 "----------\n" +
11707 "1. ERROR in X.java (at line 26)\n" +
11708 " o1.toString();\n" +
11709 " ^^\n" +
11710 "The local variable o1 may not have been initialized\n" +
11711 "----------\n" +
11712 "2. ERROR in X.java (at line 26)\n" +
11713 " o1.toString();\n" +
11714 " ^^\n" +
11715 "Potential null pointer access: The variable o1 may be null at this location\n" +
11716 "----------\n");
11717 }
11718
11719 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
11720 // Test to verify that redundant null checks are properly reported in all loops
testBug291418a()11721 public void testBug291418a() {
11722 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
11723 runNegativeNullTest(
11724 new String[] {
11725 "X.java",
11726 "class X {\n" +
11727 " void foo(int[] argArray) {\n" +
11728 " int[] array = {2};\n" +
11729 " int[] collectionVar = {1,2};\n" +
11730 " if(argArray == null) return;\n" +
11731 " for(int x:collectionVar) {\n" +
11732 " if (collectionVar == null);\n" + // collectionVar cannot be null here
11733 " if (array == null);\n" + // array is not null here
11734 " if (argArray == null);\n" + // argArray cannot be null here
11735 " }\n" +
11736 " int count = 0;\n" +
11737 " do {\n" +
11738 " count++;\n" +
11739 " if (array == null);\n" + // array is not null here
11740 " if (argArray == null);\n" + // argArray cannot be null here
11741 " } while (count<10);\n" +
11742 " array = new int[0];\n" + // reset tainting by null check
11743 " if (argArray == null) return;\n" + // reset tainting by null check
11744 " for (int i=0; i<2; i++) {\n" +
11745 " if (array == null);\n" + // array is not null here
11746 " if (argArray == null);\n" + // argArray cannot be null here
11747 " }\n" +
11748 " while (true) {\n" +
11749 " if (array == null);\n" + // array is not null here
11750 " if (argArray == null);\n" + // argArray cannot be null here
11751 " }\n" +
11752 " }\n" +
11753 "}"},
11754 "----------\n" +
11755 "1. ERROR in X.java (at line 7)\n" +
11756 " if (collectionVar == null);\n" +
11757 " ^^^^^^^^^^^^^\n" +
11758 "Null comparison always yields false: The variable collectionVar cannot be null at this location\n" +
11759 "----------\n" +
11760 "2. ERROR in X.java (at line 8)\n" +
11761 " if (array == null);\n" +
11762 " ^^^^^\n" +
11763 "Null comparison always yields false: The variable array cannot be null at this location\n" +
11764 "----------\n" +
11765 "3. ERROR in X.java (at line 9)\n" +
11766 " if (argArray == null);\n" +
11767 " ^^^^^^^^\n" +
11768 "Null comparison always yields false: The variable argArray cannot be null at this location\n" +
11769 "----------\n" +
11770 "4. ERROR in X.java (at line 14)\n" +
11771 " if (array == null);\n" +
11772 " ^^^^^\n" +
11773 "Null comparison always yields false: The variable array cannot be null at this location\n" +
11774 "----------\n" +
11775 "5. ERROR in X.java (at line 15)\n" +
11776 " if (argArray == null);\n" +
11777 " ^^^^^^^^\n" +
11778 "Null comparison always yields false: The variable argArray cannot be null at this location\n" +
11779 "----------\n" +
11780 "6. ERROR in X.java (at line 20)\n" +
11781 " if (array == null);\n" +
11782 " ^^^^^\n" +
11783 "Null comparison always yields false: The variable array cannot be null at this location\n" +
11784 "----------\n" +
11785 "7. ERROR in X.java (at line 21)\n" +
11786 " if (argArray == null);\n" +
11787 " ^^^^^^^^\n" +
11788 "Null comparison always yields false: The variable argArray cannot be null at this location\n" +
11789 "----------\n" +
11790 "8. ERROR in X.java (at line 24)\n" +
11791 " if (array == null);\n" +
11792 " ^^^^^\n" +
11793 "Null comparison always yields false: The variable array cannot be null at this location\n" +
11794 "----------\n" +
11795 "9. ERROR in X.java (at line 25)\n" +
11796 " if (argArray == null);\n" +
11797 " ^^^^^^^^\n" +
11798 "Null comparison always yields false: The variable argArray cannot be null at this location\n" +
11799 "----------\n");
11800 }
11801 }
11802
11803 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=291418
11804 // Test to verify that redundant null checks are properly reported
11805 // in a loop in case the null status is modified downstream in the loop
testBug291418b()11806 public void testBug291418b() {
11807 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
11808 runNegativeNullTest(
11809 new String[] {
11810 "X.java",
11811 "class X {\n" +
11812 " void foo(int[] argArray) {\n" +
11813 " int[] array = {2};\n" +
11814 " int[] collectionVar = {1,2};\n" +
11815 " if(argArray == null) return;" +
11816 " for(int x:collectionVar) {\n" +
11817 " if (collectionVar == null);\n" + // collectionVar cannot be null here
11818 " if (array == null);\n" + // array is not null in first iteration but assigned null later in the loop. So we keep quiet
11819 " if (argArray == null);\n" + // argArray cannot be null here
11820 " array = null;\n" +
11821 " }\n" +
11822 " }\n" +
11823 "}"},
11824 "----------\n" +
11825 "1. ERROR in X.java (at line 6)\n" +
11826 " if (collectionVar == null);\n" +
11827 " ^^^^^^^^^^^^^\n" +
11828 "Null comparison always yields false: The variable collectionVar cannot be null at this location\n" +
11829 "----------\n" +
11830 "2. ERROR in X.java (at line 8)\n" +
11831 " if (argArray == null);\n" +
11832 " ^^^^^^^^\n" +
11833 "Null comparison always yields false: The variable argArray cannot be null at this location\n" +
11834 "----------\n");
11835 }
11836 }
11837
11838 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=293917
11839 // Test that a redundant null check doesn't affect the null status of
11840 // a variable downstream.
testBug293917a()11841 public void testBug293917a() {
11842 this.runNegativeTest(
11843 new String[] {
11844 "X.java",
11845 "public class X {\n" +
11846 " public void foo(){\n" +
11847 " String x = null, y = null;\n" +
11848 " if (x == null) x = \"foo\";\n" +
11849 " if (x != null) y = \"bar\";\n" +
11850 " x.length();\n" + // shouldn't warn here
11851 " y.length();\n" + // shouldn't warn here
11852 " }\n" +
11853 "}\n"},
11854 "----------\n" +
11855 "1. ERROR in X.java (at line 4)\n" +
11856 " if (x == null) x = \"foo\";\n" +
11857 " ^\n" +
11858 "Redundant null check: The variable x can only be null at this location\n" +
11859 "----------\n" +
11860 "2. ERROR in X.java (at line 5)\n" +
11861 " if (x != null) y = \"bar\";\n" +
11862 " ^\n" +
11863 "Redundant null check: The variable x cannot be null at this location\n" +
11864 "----------\n",
11865 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11866 }
11867
11868 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=293917
11869 // Test that a redundant null check doesn't affect the null status of
11870 // a variable downstream in a loop.
testBug293917b()11871 public void testBug293917b() {
11872 this.runNegativeTest(
11873 new String[] {
11874 "X.java",
11875 "public class X {\n" +
11876 " public void foo(){\n" +
11877 " String x = null, y = null;" +
11878 " while(true) {\n" +
11879 " if (x == null) x = \"foo\";\n" +
11880 " if (x != null) y = \"bar\";\n" +
11881 " x.length();\n" + // shouldn't warn here
11882 " y.length();\n" + // shouldn't warn here
11883 " }\n" +
11884 " }\n" +
11885 "}\n"},
11886 "----------\n" +
11887 "1. ERROR in X.java (at line 5)\n" +
11888 " if (x != null) y = \"bar\";\n" +
11889 " ^\n" +
11890 "Redundant null check: The variable x cannot be null at this location\n" +
11891 "----------\n",
11892 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11893 }
11894
11895 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=293917
11896 // Test that a redundant null check doesn't affect the null status of
11897 // a variable downstream in a finally block.
testBug293917c()11898 public void testBug293917c() {
11899 this.runNegativeTest(
11900 new String[] {
11901 "X.java",
11902 "public class X {\n" +
11903 " public void foo(){\n" +
11904 " String x = null, y = null;" +
11905 " try {}\n" +
11906 " finally {\n" +
11907 " if (x == null) x = \"foo\";\n" +
11908 " if (x != null) y = \"bar\";\n" +
11909 " x.length();\n" + // shouldn't warn here
11910 " y.length();\n" + // shouldn't warn here
11911 " }\n" +
11912 " }\n" +
11913 "}\n"},
11914 "----------\n" +
11915 "1. ERROR in X.java (at line 5)\n" +
11916 " if (x == null) x = \"foo\";\n" +
11917 " ^\n" +
11918 "Redundant null check: The variable x can only be null at this location\n" +
11919 "----------\n" +
11920 "2. ERROR in X.java (at line 6)\n" +
11921 " if (x != null) y = \"bar\";\n" +
11922 " ^\n" +
11923 "Redundant null check: The variable x cannot be null at this location\n" +
11924 "----------\n",
11925 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11926 }
11927
11928 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=190623
11929 // Test that a redundant null check doesn't affect the null status of
11930 // a variable downstream.
testBug190623()11931 public void testBug190623() {
11932 this.runNegativeTest(
11933 new String[] {
11934 "X.java",
11935 "public class X {\n" +
11936 " public static void main(String[] args) {\n" +
11937 " Number n = getNumber();\n" +
11938 " if (n instanceof Double) {\n" +
11939 " Double d= (Double) n;\n" +
11940 " if (d != null && d.isNaN()) {\n" +
11941 " System.out.println(\"outside loop\");\n" +
11942 " }\n" +
11943 " for (int i= 0; i < 10; i++) {\n" +
11944 " if (d != null && d.isNaN()) {\n" +
11945 " System.out.println(\"inside loop\");\n" +
11946 " }\n" +
11947 " }\n" +
11948 " }\n" +
11949 " }\n" +
11950 " private static Number getNumber() {\n" +
11951 " return Double.valueOf(Math.sqrt(-1));\n" +
11952 " }\n" +
11953 "}\n"},
11954 "----------\n" +
11955 "1. ERROR in X.java (at line 6)\n" +
11956 " if (d != null && d.isNaN()) {\n" +
11957 " ^\n" +
11958 "Redundant null check: The variable d cannot be null at this location\n" +
11959 "----------\n" +
11960 "2. ERROR in X.java (at line 10)\n" +
11961 " if (d != null && d.isNaN()) {\n" +
11962 " ^\n" +
11963 "Redundant null check: The variable d cannot be null at this location\n" +
11964 "----------\n",
11965 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
11966 }
11967
11968 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=299900
11969 //Test to verify that null checks are properly reported for the variable(s)
11970 //in the right expression of an OR condition statement.
testBug299900a()11971 public void testBug299900a() {
11972 runNegativeNullTest(
11973 new String[] {
11974 "X.java",
11975 "class X {\n" +
11976 " void foo(Object foo, Object bar) {\n" +
11977 " if(foo == null || bar == null) {\n" +
11978 " System.out.println(foo.toString());\n" +
11979 " System.out.println(bar.toString());\n" +
11980 " }\n" +
11981 " }\n" +
11982 "}"},
11983 "----------\n" +
11984 "1. ERROR in X.java (at line 4)\n" +
11985 " System.out.println(foo.toString());\n" +
11986 " ^^^\n" +
11987 "Potential null pointer access: The variable foo may be null at this location\n" +
11988 "----------\n" +
11989 "2. ERROR in X.java (at line 5)\n" +
11990 " System.out.println(bar.toString());\n" +
11991 " ^^^\n" +
11992 "Potential null pointer access: The variable bar may be null at this location\n" +
11993 "----------\n");
11994 }
11995
11996 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=299900
11997 //Test to verify that null checks are properly reported for the variable(s)
11998 //in the right expression of an OR condition statement.
testBug299900b()11999 public void testBug299900b() {
12000 runNegativeNullTest(
12001 new String[] {
12002 "X.java",
12003 "class X {\n" +
12004 " void foo(Object foo, Object bar) {\n" +
12005 " if(foo == null || bar == null) {\n" +
12006 " }\n" +
12007 " System.out.println(foo.toString());\n" +
12008 " System.out.println(bar.toString());\n" +
12009 " }\n" +
12010 "}"},
12011 "----------\n" +
12012 "1. ERROR in X.java (at line 5)\n" +
12013 " System.out.println(foo.toString());\n" +
12014 " ^^^\n" +
12015 "Potential null pointer access: The variable foo may be null at this location\n" +
12016 "----------\n" +
12017 "2. ERROR in X.java (at line 6)\n" +
12018 " System.out.println(bar.toString());\n" +
12019 " ^^^\n" +
12020 "Potential null pointer access: The variable bar may be null at this location\n" +
12021 "----------\n");
12022 }
12023
12024 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
12025 // Test whether Null pointer access warnings are being reported correctly when auto-unboxing
testBug253896a()12026 public void testBug253896a() {
12027 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
12028 runNegativeNullTest(
12029 new String[] {
12030 "X.java",
12031 "public class X {\n" +
12032 " public void foo() {\n" +
12033 " Integer f1 = null;\n" +
12034 " if(f1 == 1)\n" +
12035 " System.out.println(\"f1 is 1\");\n" +
12036 " Integer f2 = null;\n" +
12037 " int abc = (f2 != 1)? 1 : 0;\n" +
12038 " Float f3 = null;\n" +
12039 " if(f3 == null)\n" +
12040 " System.out.println(\"f3 is null\");\n" +
12041 " Byte f4 = null;\n" +
12042 " if(f4 != null)\n" +
12043 " System.out.println(\"f4 is not null\");\n" +
12044 " }\n" +
12045 "}"},
12046 "----------\n" +
12047 "1. ERROR in X.java (at line 4)\n" +
12048 " if(f1 == 1)\n" +
12049 " ^^\n" +
12050 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12051 "----------\n" +
12052 "2. ERROR in X.java (at line 7)\n" +
12053 " int abc = (f2 != 1)? 1 : 0;\n" +
12054 " ^^\n" +
12055 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12056 "----------\n" +
12057 "3. ERROR in X.java (at line 9)\n" +
12058 " if(f3 == null)\n" +
12059 " ^^\n" +
12060 "Redundant null check: The variable f3 can only be null at this location\n" +
12061 "----------\n" +
12062 "4. ERROR in X.java (at line 12)\n" +
12063 " if(f4 != null)\n" +
12064 " ^^\n" +
12065 "Null comparison always yields false: The variable f4 can only be null at this location\n" +
12066 "----------\n" +
12067 "5. WARNING in X.java (at line 13)\n" +
12068 " System.out.println(\"f4 is not null\");\n" +
12069 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
12070 "Dead code\n" +
12071 "----------\n");
12072 }
12073 }
12074
12075 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
12076 // To test whether null pointer access and potential null pointer access warnings are correctly reported when auto-unboxing
testBug253896b()12077 public void testBug253896b() {
12078 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
12079 runNegativeNullTest(
12080 new String[] {
12081 "X.java",
12082 "public class X {\n" +
12083 " public void foo(Integer i1, Integer i2) {\n" +
12084 " if(i1 == null && i2 == null){\n" +
12085 " if(i1 == 1)\n" +
12086 " System.out.println(i1);}\n" + //i1 is definitely null here
12087 " else {\n" +
12088 " if(i1 == 0) {}\n" + //i1 may be null here.
12089 " }\n" +
12090 " }\n" +
12091 "}"},
12092 "----------\n" +
12093 "1. ERROR in X.java (at line 4)\n" +
12094 " if(i1 == 1)\n" +
12095 " ^^\n" +
12096 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12097 "----------\n" +
12098 "2. ERROR in X.java (at line 7)\n" +
12099 " if(i1 == 0) {}\n" +
12100 " ^^\n" +
12101 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
12102 "----------\n");
12103 }
12104 }
12105
12106 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
12107 // Test whether Null pointer access warnings are being reported correctly when auto-unboxing inside loops
testBug253896c()12108 public void testBug253896c() {
12109 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
12110 runNegativeNullTest(
12111 new String[] {
12112 "X.java",
12113 "public class X {\n" +
12114 " public void foo() {\n" +
12115 " Integer a = null;\n" +
12116 " Integer outer2 = null;\n" +
12117 " while (true) {\n" +
12118 " Integer f1 = null;\n" +
12119 " if(f1 == 1)\n" +
12120 " System.out.println(\"f1 is 1\");\n" +
12121 " Integer f2 = null;\n" +
12122 " int abc = (f2 != 1)? 1 : 0;\n" +
12123 " Float f3 = null;\n" +
12124 " if(f3 == null)\n" +
12125 " System.out.println(\"f3 is null\");\n" +
12126 " Byte f4 = null;\n" +
12127 " if(f4 != null)\n" +
12128 " System.out.println(\"f4 is not null\");\n" +
12129 " if(a == 1) {}\n" + // warn null reference in deferred check case
12130 " if(outer2 == 1) {}\n" + // warn potential null reference in deferred check case
12131 " outer2 = 1;\n" +
12132 " }\n" +
12133 " }\n" +
12134 "}"},
12135 "----------\n" +
12136 "1. ERROR in X.java (at line 7)\n" +
12137 " if(f1 == 1)\n" +
12138 " ^^\n" +
12139 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12140 "----------\n" +
12141 "2. ERROR in X.java (at line 10)\n" +
12142 " int abc = (f2 != 1)? 1 : 0;\n" +
12143 " ^^\n" +
12144 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12145 "----------\n" +
12146 "3. ERROR in X.java (at line 12)\n" +
12147 " if(f3 == null)\n" +
12148 " ^^\n" +
12149 "Redundant null check: The variable f3 can only be null at this location\n" +
12150 "----------\n" +
12151 "4. ERROR in X.java (at line 15)\n" +
12152 " if(f4 != null)\n" +
12153 " ^^\n" +
12154 "Null comparison always yields false: The variable f4 can only be null at this location\n" +
12155 "----------\n" +
12156 "5. WARNING in X.java (at line 16)\n" +
12157 " System.out.println(\"f4 is not null\");\n" +
12158 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
12159 "Dead code\n" +
12160 "----------\n" +
12161 "6. ERROR in X.java (at line 17)\n" +
12162 " if(a == 1) {}\n" +
12163 " ^\n" +
12164 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12165 "----------\n" +
12166 "7. ERROR in X.java (at line 18)\n" +
12167 " if(outer2 == 1) {}\n" +
12168 " ^^^^^^\n" +
12169 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
12170 "----------\n");
12171 }
12172 }
12173
12174 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=253896
12175 // Test whether Null pointer access warnings are being reported correctly when auto-unboxing inside finally contexts
testBug253896d()12176 public void testBug253896d() {
12177 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
12178 runNegativeNullTest(
12179 new String[] {
12180 "X.java",
12181 "public class X {\n" +
12182 " public void foo(Integer param) {\n" +
12183 " Integer outer = null;\n" +
12184 " if (param == null) {}\n" + //tainting param
12185 " try {}\n" +
12186 " finally {\n" +
12187 " Integer f1 = null;\n" +
12188 " if(f1 == 1)\n" +
12189 " System.out.println(\"f1 is 1\");\n" +
12190 " Integer f2 = null;\n" +
12191 " int abc = (f2 != 1)? 1 : 0;\n" +
12192 " Float f3 = null;\n" +
12193 " if(f3 == null)\n" +
12194 " System.out.println(\"f3 is null\");\n" +
12195 " Byte f4 = null;\n" +
12196 " if(f4 != null)\n" +
12197 " System.out.println(\"f4 is not null\");\n" +
12198 " if(outer == 1) {}\n" + // warn null reference in deferred check case
12199 " if(param == 1) {}\n" +
12200 " }\n" +
12201 " }\n" +
12202 "}"},
12203 "----------\n" +
12204 "1. ERROR in X.java (at line 8)\n" +
12205 " if(f1 == 1)\n" +
12206 " ^^\n" +
12207 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12208 "----------\n" +
12209 "2. ERROR in X.java (at line 11)\n" +
12210 " int abc = (f2 != 1)? 1 : 0;\n" +
12211 " ^^\n" +
12212 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12213 "----------\n" +
12214 "3. ERROR in X.java (at line 13)\n" +
12215 " if(f3 == null)\n" +
12216 " ^^\n" +
12217 "Redundant null check: The variable f3 can only be null at this location\n" +
12218 "----------\n" +
12219 "4. ERROR in X.java (at line 16)\n" +
12220 " if(f4 != null)\n" +
12221 " ^^\n" +
12222 "Null comparison always yields false: The variable f4 can only be null at this location\n" +
12223 "----------\n" +
12224 "5. WARNING in X.java (at line 17)\n" +
12225 " System.out.println(\"f4 is not null\");\n" +
12226 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
12227 "Dead code\n" +
12228 "----------\n" +
12229 "6. ERROR in X.java (at line 18)\n" +
12230 " if(outer == 1) {}\n" +
12231 " ^^^^^\n" +
12232 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12233 "----------\n" +
12234 "7. ERROR in X.java (at line 19)\n" +
12235 " if(param == 1) {}\n" +
12236 " ^^^^^\n" +
12237 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
12238 "----------\n");
12239 }
12240 }
12241 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=303448
12242 //To check that code gen is not optimized for an if statement
12243 //where a local variable's definite nullness or otherwise is known because of
12244 //an earlier assert expression (inside finally context)
testBug303448a()12245 public void testBug303448a() throws Exception {
12246 Map options = getCompilerOptions();
12247 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.IGNORE);
12248 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.IGNORE);
12249 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
12250 options.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE);
12251 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
12252 this.runConformTest(
12253 new String[] {
12254 "X.java",
12255 "public class X {\n" +
12256 " public void foo() {\n" +
12257 " Object foo = null;\n" +
12258 " Object foo2 = null;\n" +
12259 " try {} \n" +
12260 " finally {\n" +
12261 " assert (foo != null && foo2 != null);\n" +
12262 " if (foo != null) {\n" +
12263 " System.out.println(\"foo is not null\");\n" +
12264 " } else {\n" +
12265 " System.out.println(\"foo is null\");\n" +
12266 " }\n" +
12267 " if (foo2 != null) {\n" +
12268 " System.out.println(\"foo2 is not null\");\n" +
12269 " } else {\n" +
12270 " System.out.println(\"foo2 is null\");\n" +
12271 " }\n" +
12272 " }\n" +
12273 " }\n" +
12274 "}\n",
12275 },
12276 "",
12277 null,
12278 true,
12279 null,
12280 options,
12281 null); // custom requestor
12282
12283 String expectedOutput = this.complianceLevel < ClassFileConstants.JDK1_5?
12284 " // Method descriptor #11 ()V\n" +
12285 " // Stack: 2, Locals: 3\n" +
12286 " public void foo();\n" +
12287 " 0 aconst_null\n" +
12288 " 1 astore_1 [foo]\n" +
12289 " 2 aconst_null\n" +
12290 " 3 astore_2 [foo2]\n" +
12291 " 4 getstatic X.$assertionsDisabled : boolean [38]\n" +
12292 " 7 ifne 26\n" +
12293 " 10 aload_1 [foo]\n" +
12294 " 11 ifnull 18\n" +
12295 " 14 aload_2 [foo2]\n" +
12296 " 15 ifnonnull 26\n" +
12297 " 18 new java.lang.AssertionError [49]\n" +
12298 " 21 dup\n" +
12299 " 22 invokespecial java.lang.AssertionError() [51]\n" +
12300 " 25 athrow\n" +
12301 " 26 aload_1 [foo]\n" +
12302 " 27 ifnull 41\n" +
12303 " 30 getstatic java.lang.System.out : java.io.PrintStream [52]\n" +
12304 " 33 ldc <String \"foo is not null\"> [58]\n" +
12305 " 35 invokevirtual java.io.PrintStream.println(java.lang.String) : void [60]\n" +
12306 " 38 goto 49\n" +
12307 " 41 getstatic java.lang.System.out : java.io.PrintStream [52]\n" +
12308 " 44 ldc <String \"foo is null\"> [65]\n" +
12309 " 46 invokevirtual java.io.PrintStream.println(java.lang.String) : void [60]\n" +
12310 " 49 aload_2 [foo2]\n" +
12311 " 50 ifnull 64\n" +
12312 " 53 getstatic java.lang.System.out : java.io.PrintStream [52]\n" +
12313 " 56 ldc <String \"foo2 is not null\"> [67]\n" +
12314 " 58 invokevirtual java.io.PrintStream.println(java.lang.String) : void [60]\n" +
12315 " 61 goto 72\n" +
12316 " 64 getstatic java.lang.System.out : java.io.PrintStream [52]\n" +
12317 " 67 ldc <String \"foo2 is null\"> [69]\n" +
12318 " 69 invokevirtual java.io.PrintStream.println(java.lang.String) : void [60]\n" +
12319 " 72 return\n" +
12320 " Line numbers:\n" +
12321 " [pc: 0, line: 3]\n" +
12322 " [pc: 2, line: 4]\n" +
12323 " [pc: 4, line: 7]\n" +
12324 " [pc: 26, line: 8]\n" +
12325 " [pc: 30, line: 9]\n" +
12326 " [pc: 38, line: 10]\n" +
12327 " [pc: 41, line: 11]\n" +
12328 " [pc: 49, line: 13]\n" +
12329 " [pc: 53, line: 14]\n" +
12330 " [pc: 61, line: 15]\n" +
12331 " [pc: 64, line: 16]\n" +
12332 " [pc: 72, line: 19]\n" +
12333 " Local variable table:\n" +
12334 " [pc: 0, pc: 73] local: this index: 0 type: X\n" +
12335 " [pc: 2, pc: 73] local: foo index: 1 type: java.lang.Object\n" +
12336 " [pc: 4, pc: 73] local: foo2 index: 2 type: java.lang.Object\n"
12337 : this.complianceLevel < ClassFileConstants.JDK1_6?
12338 " // Method descriptor #8 ()V\n" +
12339 " // Stack: 2, Locals: 3\n" +
12340 " public void foo();\n" +
12341 " 0 aconst_null\n" +
12342 " 1 astore_1 [foo]\n" +
12343 " 2 aconst_null\n" +
12344 " 3 astore_2 [foo2]\n" +
12345 " 4 getstatic X.$assertionsDisabled : boolean [16]\n" +
12346 " 7 ifne 26\n" +
12347 " 10 aload_1 [foo]\n" +
12348 " 11 ifnull 18\n" +
12349 " 14 aload_2 [foo2]\n" +
12350 " 15 ifnonnull 26\n" +
12351 " 18 new java.lang.AssertionError [26]\n" +
12352 " 21 dup\n" +
12353 " 22 invokespecial java.lang.AssertionError() [28]\n" +
12354 " 25 athrow\n" +
12355 " 26 aload_1 [foo]\n" +
12356 " 27 ifnull 41\n" +
12357 " 30 getstatic java.lang.System.out : java.io.PrintStream [29]\n" +
12358 " 33 ldc <String \"foo is not null\"> [35]\n" +
12359 " 35 invokevirtual java.io.PrintStream.println(java.lang.String) : void [37]\n" +
12360 " 38 goto 49\n" +
12361 " 41 getstatic java.lang.System.out : java.io.PrintStream [29]\n" +
12362 " 44 ldc <String \"foo is null\"> [43]\n" +
12363 " 46 invokevirtual java.io.PrintStream.println(java.lang.String) : void [37]\n" +
12364 " 49 aload_2 [foo2]\n" +
12365 " 50 ifnull 64\n" +
12366 " 53 getstatic java.lang.System.out : java.io.PrintStream [29]\n" +
12367 " 56 ldc <String \"foo2 is not null\"> [45]\n" +
12368 " 58 invokevirtual java.io.PrintStream.println(java.lang.String) : void [37]\n" +
12369 " 61 goto 72\n" +
12370 " 64 getstatic java.lang.System.out : java.io.PrintStream [29]\n" +
12371 " 67 ldc <String \"foo2 is null\"> [47]\n" +
12372 " 69 invokevirtual java.io.PrintStream.println(java.lang.String) : void [37]\n" +
12373 " 72 return\n" +
12374 " Line numbers:\n" +
12375 " [pc: 0, line: 3]\n" +
12376 " [pc: 2, line: 4]\n" +
12377 " [pc: 4, line: 7]\n" +
12378 " [pc: 26, line: 8]\n" +
12379 " [pc: 30, line: 9]\n" +
12380 " [pc: 38, line: 10]\n" +
12381 " [pc: 41, line: 11]\n" +
12382 " [pc: 49, line: 13]\n" +
12383 " [pc: 53, line: 14]\n" +
12384 " [pc: 61, line: 15]\n" +
12385 " [pc: 64, line: 16]\n" +
12386 " [pc: 72, line: 19]\n" +
12387 " Local variable table:\n" +
12388 " [pc: 0, pc: 73] local: this index: 0 type: X\n" +
12389 " [pc: 2, pc: 73] local: foo index: 1 type: java.lang.Object\n" +
12390 " [pc: 4, pc: 73] local: foo2 index: 2 type: java.lang.Object\n"
12391 : " // Method descriptor #8 ()V\n" +
12392 " // Stack: 2, Locals: 3\n" +
12393 " public void foo();\n" +
12394 " 0 aconst_null\n" +
12395 " 1 astore_1 [foo]\n" +
12396 " 2 aconst_null\n" +
12397 " 3 astore_2 [foo2]\n" +
12398 " 4 getstatic X.$assertionsDisabled : boolean [16]\n" +
12399 " 7 ifne 26\n" +
12400 " 10 aload_1 [foo]\n" +
12401 " 11 ifnull 18\n" +
12402 " 14 aload_2 [foo2]\n" +
12403 " 15 ifnonnull 26\n" +
12404 " 18 new java.lang.AssertionError [27]\n" +
12405 " 21 dup\n" +
12406 " 22 invokespecial java.lang.AssertionError() [29]\n" +
12407 " 25 athrow\n" +
12408 " 26 aload_1 [foo]\n" +
12409 " 27 ifnull 41\n" +
12410 " 30 getstatic java.lang.System.out : java.io.PrintStream [30]\n" +
12411 " 33 ldc <String \"foo is not null\"> [36]\n" +
12412 " 35 invokevirtual java.io.PrintStream.println(java.lang.String) : void [38]\n" +
12413 " 38 goto 49\n" +
12414 " 41 getstatic java.lang.System.out : java.io.PrintStream [30]\n" +
12415 " 44 ldc <String \"foo is null\"> [44]\n" +
12416 " 46 invokevirtual java.io.PrintStream.println(java.lang.String) : void [38]\n" +
12417 " 49 aload_2 [foo2]\n" +
12418 " 50 ifnull 64\n" +
12419 " 53 getstatic java.lang.System.out : java.io.PrintStream [30]\n" +
12420 " 56 ldc <String \"foo2 is not null\"> [46]\n" +
12421 " 58 invokevirtual java.io.PrintStream.println(java.lang.String) : void [38]\n" +
12422 " 61 goto 72\n" +
12423 " 64 getstatic java.lang.System.out : java.io.PrintStream [30]\n" +
12424 " 67 ldc <String \"foo2 is null\"> [48]\n" +
12425 " 69 invokevirtual java.io.PrintStream.println(java.lang.String) : void [38]\n" +
12426 " 72 return\n" +
12427 " Line numbers:\n" +
12428 " [pc: 0, line: 3]\n" +
12429 " [pc: 2, line: 4]\n" +
12430 " [pc: 4, line: 7]\n" +
12431 " [pc: 26, line: 8]\n" +
12432 " [pc: 30, line: 9]\n" +
12433 " [pc: 38, line: 10]\n" +
12434 " [pc: 41, line: 11]\n" +
12435 " [pc: 49, line: 13]\n" +
12436 " [pc: 53, line: 14]\n" +
12437 " [pc: 61, line: 15]\n" +
12438 " [pc: 64, line: 16]\n" +
12439 " [pc: 72, line: 19]\n" +
12440 " Local variable table:\n" +
12441 " [pc: 0, pc: 73] local: this index: 0 type: X\n" +
12442 " [pc: 2, pc: 73] local: foo index: 1 type: java.lang.Object\n" +
12443 " [pc: 4, pc: 73] local: foo2 index: 2 type: java.lang.Object\n" +
12444 " Stack map table: number of frames 6\n" +
12445 " [pc: 18, append: {java.lang.Object, java.lang.Object}]\n" +
12446 " [pc: 26, same]\n" +
12447 " [pc: 41, same]\n" +
12448 " [pc: 49, same]\n" +
12449 " [pc: 64, same]\n" +
12450 " [pc: 72, same]\n";
12451
12452 File f = new File(OUTPUT_DIR + File.separator + "X.class");
12453 byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
12454 ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
12455 String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
12456 int index = result.indexOf(expectedOutput);
12457 if (index == -1 || expectedOutput.length() == 0) {
12458 System.out.println(Util.displayString(result, 3));
12459 }
12460 if (index == -1) {
12461 assertEquals("Wrong contents", expectedOutput, result);
12462 }
12463 }
12464 }
12465 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=303448
12466 //To check that code gen is not optimized for an if statement
12467 //where a local variable's definite nullness or otherwise is known because of
12468 //an earlier assert expression (inside finally context)
12469 public void testBug303448b() throws Exception {
12470 Map options = getCompilerOptions();
12471 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.IGNORE);
12472 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.IGNORE);
12473 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
12474 options.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE);
12475 if (this.complianceLevel >= ClassFileConstants.JDK1_4) {
12476 this.runConformTest(
12477 new String[] {
12478 "X.java",
12479 "public class X {\n" +
12480 " public static void main(String[] args) {\n" +
12481 " System.out.print(\"start\");\n" +
12482 " Object foo = null;\n" +
12483 " assert (foo != null);\n" +
12484 " if (foo != null) {\n" +
12485 " System.out.println(\"foo is not null\");\n" +
12486 " }\n" +
12487 " System.out.print(\"end\");\n" +
12488 " }\n" +
12489 "}\n",
12490 },
12491 "startend",
12492 null,
12493 true,
12494 null,
12495 options,
12496 null);
12497 }
12498 }
12499 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=304416
12500 public void testBug304416() throws Exception {
12501 Map options = getCompilerOptions();
12502 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
12503 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.WARNING);
12504 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
12505 options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.OPTIMIZE_OUT);
12506 this.runConformTest(
12507 new String[] {
12508 "X.java",
12509 "public class X {\n" +
12510 " public static void main(String[] args) {\n" +
12511 " String s = null;\n" +
12512 " String s2 = null;\n" +
12513 " if (s != null && s2 != null) {\n" +
12514 " System.out.println(s);\n" +
12515 " System.out.println(s2);\n" +
12516 " }\n" +
12517 " }\n" +
12518 "}",
12519 },
12520 "",
12521 null,
12522 true,
12523 null,
12524 options,
12525 null);
12526 String expectedOutput =
12527 " public static void main(java.lang.String[] args);\n" +
12528 " 0 aconst_null\n" +
12529 " 1 astore_1 [s]\n" +
12530 " 2 aconst_null\n" +
12531 " 3 astore_2 [s2]\n" +
12532 " 4 aload_1 [s]\n" +
12533 " 5 ifnull 26\n" +
12534 " 8 aload_2 [s2]\n" +
12535 " 9 ifnull 26\n" +
12536 " 12 getstatic java.lang.System.out : java.io.PrintStream [16]\n" +
12537 " 15 aload_1 [s]\n" +
12538 " 16 invokevirtual java.io.PrintStream.println(java.lang.String) : void [22]\n" +
12539 " 19 getstatic java.lang.System.out : java.io.PrintStream [16]\n" +
12540 " 22 aload_2 [s2]\n" +
12541 " 23 invokevirtual java.io.PrintStream.println(java.lang.String) : void [22]\n" +
12542 " 26 return\n";
12543 checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
12544 }
12545
12546 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305590
12547 // To verify that a "instanceof always yields false" warning is not elicited in the
12548 // case when the expression has been assigned a non null value in the instanceof check.
12549 public void testBug305590() {
12550 runNegativeTest(
12551 new String[] {
12552 "X.java",
12553 "public class X {\n" +
12554 " public void foo() {\n" +
12555 " Object str = null;\n" +
12556 " if ((str = \"str\") instanceof String) {}\n" + // shouldn't warn
12557 " str = null;\n" +
12558 " if ((str = \"str\") instanceof Number) {}\n" + // shouldn't warn
12559 " str = null;\n" +
12560 " if (str instanceof String) {}\n" + // should warn
12561 " }\n" +
12562 "}"},
12563 "----------\n" +
12564 "1. ERROR in X.java (at line 8)\n" +
12565 " if (str instanceof String) {}\n" +
12566 " ^^^\n" +
12567 "instanceof always yields false: The variable str can only be null at this location\n" +
12568 "----------\n",
12569 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
12570 }
12571 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201
12572 // unboxing raises an NPE
12573 // LocalDeclaration
12574 public void testBug319201() {
12575 if (this.complianceLevel < ClassFileConstants.JDK1_5)
12576 return;
12577 runNegativeTest(
12578 new String[] {
12579 "X.java",
12580 "public class X {\n" +
12581 " public void foo() {\n" +
12582 " Integer i = null;\n" +
12583 " int j = i;\n" + // should warn
12584 " }\n" +
12585 "}"},
12586 "----------\n" +
12587 "1. ERROR in X.java (at line 4)\n" +
12588 " int j = i;\n" +
12589 " ^\n" +
12590 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12591 "----------\n",
12592 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
12593 }
12594 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201
12595 // unboxing could raise an NPE
12596 // Assignment
12597 public void testBug319201a() {
12598 if (this.complianceLevel < ClassFileConstants.JDK1_5)
12599 return;
12600 runNegativeTest(
12601 new String[] {
12602 "X.java",
12603 "public class X {\n" +
12604 " public void foo(Integer i) {\n" +
12605 " if (i == null) {};\n" +
12606 " int j;\n" +
12607 " j = i;\n" + // should warn
12608 " }\n" +
12609 "}"},
12610 "----------\n" +
12611 "1. ERROR in X.java (at line 5)\n" +
12612 " j = i;\n" +
12613 " ^\n" +
12614 "Potential null pointer access: This expression of type Integer may be null but requires auto-unboxing\n" +
12615 "----------\n",
12616 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
12617 }
12618 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201
12619 // unboxing raises an NPE
12620 // MessageSend
12621 public void testBug319201b() {
12622 if (this.complianceLevel < ClassFileConstants.JDK1_5)
12623 return;
12624 runNegativeTest(
12625 new String[] {
12626 "X.java",
12627 "public class X {\n" +
12628 " public void foo() {\n" +
12629 " Boolean bo = null;;\n" +
12630 " bar(bo);\n" + // should warn
12631 " }\n" +
12632 " void bar(boolean b) {}\n" +
12633 "}"},
12634 "----------\n" +
12635 "1. ERROR in X.java (at line 4)\n" +
12636 " bar(bo);\n" +
12637 " ^^\n" +
12638 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12639 "----------\n",
12640 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
12641 }
12642 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201
12643 // unboxing raises an NPE
12644 // Node types covered (in this order):
12645 // ExplicitConstructorCall
12646 // AllocationExpression
12647 // AND_AND_Expression
12648 // OR_OR_Expression
12649 // ArrayAllocationExpression
12650 // ForStatement
12651 // DoStatement
12652 // IfStatement
12653 // QualifiedAllocationExpression
12654 // SwitchStatement
12655 // WhileStatement
12656 // CastExpression
12657 // AssertStatement
12658 // ReturnStatement
12659 public void testBug319201c() {
12660 if (this.complianceLevel < ClassFileConstants.JDK1_5)
12661 return;
12662 runNegativeTest(
12663 new String[] {
12664 "X.java",
12665 "class Y { public Y(boolean b1, boolean b2) {} }\n" +
12666 "public class X extends Y {\n" +
12667 " public X(boolean b, Boolean b2) {\n" +
12668 " super(b2 == null, b2);\n" +
12669 " }\n" +
12670 " class Z {\n" +
12671 " public Z(boolean b) {}\n" +
12672 " }\n" +
12673 " boolean fB = (Boolean)null;\n" +
12674 " public boolean foo(boolean inB) {\n" +
12675 " Boolean b1 = null;\n" +
12676 " X x = new X(b1, null);\n" +
12677 " Boolean b2 = null;\n" +
12678 " boolean dontcare = b2 && inB;\n" +
12679 " Boolean b3 = null;\n" +
12680 " dontcare = inB || b3;\n" +
12681 " Integer dims = null;\n" +
12682 " char[] cs = new char[dims];\n" +
12683 " Boolean b5 = null;\n" +
12684 " do {\n" +
12685 " Boolean b4 = null;\n" +
12686 " for (int i=0;b4; i++);\n" +
12687 " } while (b5);\n" +
12688 " Boolean b6 = null;\n" +
12689 " if (b6) { }\n" +
12690 " Boolean b7 = null;\n" +
12691 " Z z = this.new Z(b7);\n" +
12692 " Integer sel = null;\n" +
12693 " switch(sel) {\n" +
12694 " case 1: break;\n" +
12695 " default: break;\n" +
12696 " }\n" +
12697 " Boolean b8 = null;\n" +
12698 " while (b8) {}\n" +
12699 " Boolean b9 = null;\n" +
12700 " dontcare = (boolean)b9;\n" +
12701 " Boolean b10 = null;\n" +
12702 " assert b10 : \"shouldn't happen, but will\";\n" +
12703 " Boolean b11 = null;\n" +
12704 " return b11;\n" +
12705 " }\n" +
12706 "}"},
12707 "----------\n" +
12708 "1. ERROR in X.java (at line 4)\n" +
12709 " super(b2 == null, b2);\n" +
12710 " ^^\n" +
12711 "Potential null pointer access: This expression of type Boolean may be null but requires auto-unboxing\n" +
12712 "----------\n" +
12713 "2. ERROR in X.java (at line 9)\n" +
12714 " boolean fB = (Boolean)null;\n" +
12715 " ^^^^^^^^^^^^^\n" +
12716 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12717 "----------\n" +
12718 "3. ERROR in X.java (at line 12)\n" +
12719 " X x = new X(b1, null);\n" +
12720 " ^^\n" +
12721 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12722 "----------\n" +
12723 "4. ERROR in X.java (at line 14)\n" +
12724 " boolean dontcare = b2 && inB;\n" +
12725 " ^^\n" +
12726 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12727 "----------\n" +
12728 "5. ERROR in X.java (at line 16)\n" +
12729 " dontcare = inB || b3;\n" +
12730 " ^^\n" +
12731 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12732 "----------\n" +
12733 "6. ERROR in X.java (at line 18)\n" +
12734 " char[] cs = new char[dims];\n" +
12735 " ^^^^\n" +
12736 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12737 "----------\n" +
12738 "7. ERROR in X.java (at line 22)\n" +
12739 " for (int i=0;b4; i++);\n" +
12740 " ^^\n" +
12741 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12742 "----------\n" +
12743 "8. ERROR in X.java (at line 23)\n" +
12744 " } while (b5);\n" +
12745 " ^^\n" +
12746 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12747 "----------\n" +
12748 "9. ERROR in X.java (at line 25)\n" +
12749 " if (b6) { }\n" +
12750 " ^^\n" +
12751 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12752 "----------\n" +
12753 "10. ERROR in X.java (at line 27)\n" +
12754 " Z z = this.new Z(b7);\n" +
12755 " ^^\n" +
12756 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12757 "----------\n" +
12758 "11. ERROR in X.java (at line 29)\n" +
12759 " switch(sel) {\n" +
12760 " ^^^\n" +
12761 "Null pointer access: This expression of type Integer is null but requires auto-unboxing\n" +
12762 "----------\n" +
12763 "12. ERROR in X.java (at line 34)\n" +
12764 " while (b8) {}\n" +
12765 " ^^\n" +
12766 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12767 "----------\n" +
12768 "13. ERROR in X.java (at line 36)\n" +
12769 " dontcare = (boolean)b9;\n" +
12770 " ^^\n" +
12771 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12772 "----------\n" +
12773 "14. ERROR in X.java (at line 38)\n" +
12774 " assert b10 : \"shouldn\'t happen, but will\";\n" +
12775 " ^^^\n" +
12776 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12777 "----------\n" +
12778 "15. ERROR in X.java (at line 40)\n" +
12779 " return b11;\n" +
12780 " ^^^\n" +
12781 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12782 "----------\n",
12783 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
12784 }
12785 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=319201
12786 // unboxing raises an NPE
12787 // DoStatement, variants with assignement and/or continue in the body & empty body
12788 public void testBug319201d() {
12789 if (this.complianceLevel < ClassFileConstants.JDK1_5)
12790 return;
12791 Map customOptions = getCompilerOptions();
12792 customOptions.put(CompilerOptions.OPTION_ReportUnnecessaryElse, CompilerOptions.IGNORE);
12793 runNegativeTest(
12794 new String[] {
12795 "X.java",
12796 "public class X {\n" +
12797 " public void foo(boolean cond, boolean cond2) {\n" +
12798 " Boolean b = null;\n" +
12799 " do {\n" +
12800 " b = false;\n" +
12801 " if (cond) continue;\n" + // shouldn't make a difference
12802 " } while (b);\n" + // don't complain, loop body has already assigned b
12803 " Boolean b2 = null;\n" +
12804 " do {\n" +
12805 " if (cond) continue;\n" +
12806 " b2 = false;\n" +
12807 " } while (b2);\n" + // complain here: potentially null
12808 " Boolean b3 = null;\n" +
12809 " do {\n" +
12810 " } while (b3);\n" + // complain here: definitely null
12811 " Boolean b4 = null;\n" +
12812 " do {\n" +
12813 " if (cond) {\n" +
12814 " b4 = true;\n" +
12815 " if (cond2) continue;\n" +
12816 " }\n" +
12817 " b4 = false;\n" +
12818 " } while (b4);\n" + // don't complain here: definitely non-null
12819 " Boolean b5 = null;\n" +
12820 " do {\n" +
12821 " b5 = true;\n" +
12822 " } while (b5);\n" + // don't complain
12823 " Boolean b6 = null;\n" +
12824 " do {\n" +
12825 " b6 = true;\n" +
12826 " continue;\n" +
12827 " } while (b6); \n" + // don't complain
12828 " Boolean b7 = null;\n" +
12829 " Boolean b8 = null;\n" +
12830 " do {\n" +
12831 " if (cond) {\n" +
12832 " b7 = true;\n" +
12833 " continue;\n" +
12834 " } else {\n" +
12835 " b8 = true;\n" +
12836 " }\n" +
12837 " } while (b7);\n" + // complain here: after else branch b7 can still be null
12838 " }\n" +
12839 "}"},
12840 "----------\n" +
12841 "1. ERROR in X.java (at line 12)\n" +
12842 " } while (b2);\n" +
12843 " ^^\n" +
12844 "Potential null pointer access: This expression of type Boolean may be null but requires auto-unboxing\n" +
12845 "----------\n" +
12846 "2. ERROR in X.java (at line 15)\n" +
12847 " } while (b3);\n" +
12848 " ^^\n" +
12849 "Null pointer access: This expression of type Boolean is null but requires auto-unboxing\n" +
12850 "----------\n" +
12851 "3. ERROR in X.java (at line 42)\n" +
12852 " } while (b7);\n" +
12853 " ^^\n" +
12854 "Potential null pointer access: This expression of type Boolean may be null but requires auto-unboxing\n" +
12855 "----------\n",
12856 null/*classLibraries*/,
12857 true/*shouldFlushOutputDirectory*/,
12858 customOptions,
12859 "",
12860 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
12861 }
12862 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=320414
12863 public void testBug320414() throws Exception {
12864 Map options = getCompilerOptions();
12865 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
12866 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.ERROR);
12867 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
12868 options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.OPTIMIZE_OUT);
12869 this.runConformTest(
12870 new String[] {
12871 "X.java",
12872 "public class X {\n" +
12873 " static class B {\n" +
12874 " public static final int CONST = 16;\n" +
12875 " int i;\n" +
12876 " }\n" +
12877 " B b;\n" +
12878 " public static void main(String[] args) {\n" +
12879 " new X().foo();\n" +
12880 " }\n" +
12881 " void foo() {\n" +
12882 " B localB = b; \n" +
12883 " int i = localB.CONST;\n" +
12884 " if (localB != null) {\n" +
12885 " i = localB.i;\n" +
12886 " }\n" +
12887 " System.out.println(i);\n" +
12888 " }\n" +
12889 "}",
12890 },
12891 "16",
12892 null,
12893 true,
12894 null,
12895 options,
12896 null);
12897 String expectedOutput =
12898 " void foo();\n" +
12899 " 0 aload_0 [this]\n" +
12900 " 1 getfield X.b : X.B [24]\n" +
12901 " 4 astore_1 [localB]\n" +
12902 " 5 bipush 16\n" +
12903 " 7 istore_2 [i]\n" +
12904 " 8 aload_1 [localB]\n" +
12905 " 9 ifnull 17\n" +
12906 " 12 aload_1 [localB]\n" +
12907 " 13 getfield X$B.i : int [26]\n" +
12908 " 16 istore_2 [i]\n" +
12909 " 17 getstatic java.lang.System.out : java.io.PrintStream [32]\n" +
12910 " 20 iload_2 [i]\n" +
12911 " 21 invokevirtual java.io.PrintStream.println(int) : void [38]\n" +
12912 " 24 return\n";
12913 checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
12914 }
12915 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=321926
12916 // To verify that a "redundant null check" warning is NOT elicited for a variable assigned non-null
12917 // in an infinite while loop inside a try catch block and that code generation shows no surprises.
12918 public void testBug321926a() {
12919 this.runConformTest(
12920 new String[] {
12921 "X.java",
12922 "import java.io.IOException;\n" +
12923 "public class X {\n" +
12924 " public static void main(String[] args) {\n" +
12925 " String someVariable = null;\n" +
12926 " int i = 0;\n" +
12927 " try {\n" +
12928 " while (true) {\n" +
12929 " if (i == 0){\n" +
12930 " someVariable = \"not null\";\n" +
12931 " i++;\n" +
12932 " }\n" +
12933 " else\n" +
12934 " throw new IOException();\n" +
12935 " }\n" +
12936 " } catch (IOException e) {\n" +
12937 " // broken from loop, continue on\n" +
12938 " }\n" +
12939 " if (someVariable == null) {\n" +
12940 " System.out.println(\"Compiler buggy\");\n" +
12941 " } else {\n" +
12942 " System.out.println(\"Compiler good\");\n" +
12943 " }\n" +
12944 " }\n" +
12945 "}"},
12946 "Compiler good");
12947 }
12948 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=321926
12949 // need more precise info from the throw location
12950 public void testBug321926a2() {
12951 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses @SW annotation
12952 Map options = getCompilerOptions();
12953 options.put(JavaCore.COMPILER_PB_UNUSED_WARNING_TOKEN, JavaCore.ERROR);
12954 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
12955 this.runConformTest(
12956 new String[] {
12957 "X.java",
12958 "import java.io.IOException;\n" +
12959 "public class X {\n" +
12960 " @SuppressWarnings(\"null\")\n" + // expecting "redundant null check" at "if (someVariable == null)"
12961 " public static void main(String[] args) {\n" +
12962 " String someVariable = null;\n" +
12963 " int i = 0;\n" +
12964 " try {\n" +
12965 " while (true) {\n" +
12966 " if (i == 0){\n" +
12967 " someVariable = \"not null\";\n" +
12968 " i++;\n" +
12969 " }\n" +
12970 " else {\n" +
12971 " someVariable = \"value\";\n" +
12972 " throw new IOException();\n" +
12973 " }\n" +
12974 " }\n" +
12975 " } catch (IOException e) {\n" +
12976 " // broken from loop, continue on\n" +
12977 " }\n" +
12978 " if (someVariable == null) {\n" +
12979 " System.out.println(\"Compiler buggy\");\n" +
12980 " } else {\n" +
12981 " System.out.println(\"Compiler good\");\n" +
12982 " }\n" +
12983 " }\n" +
12984 "}"},
12985 "Compiler good",
12986 options);
12987 }
12988 // Test that dead code warning does show up.
12989 public void testBug321926b() {
12990 this.runNegativeTest(
12991 new String[] {
12992 "X.java",
12993 "import java.io.IOException;\n" +
12994 "public class X {\n" +
12995 " public static void main(String[] args) {\n" +
12996 " String someVariable = null;\n" +
12997 " int i = 0;\n" +
12998 " try {\n" +
12999 " while (true) {\n" +
13000 " if (i == 0){\n" +
13001 " someVariable = \"not null\";\n" +
13002 " i++;\n" +
13003 " }\n" +
13004 " else\n" +
13005 " throw new IOException();\n" +
13006 " }\n" +
13007 " System.out.println(\"This is dead code\");\n" +
13008 " } catch (IOException e) {\n" +
13009 " // broken from loop, continue on\n" +
13010 " }\n" +
13011 " if (someVariable == null) {\n" +
13012 " System.out.println(\"Compiler buggy\");\n" +
13013 " } else {\n" +
13014 " System.out.println(\"Compiler good\");\n" +
13015 " }\n" +
13016 " }\n" +
13017 "}"},
13018 "----------\n" +
13019 "1. ERROR in X.java (at line 15)\n" +
13020 " System.out.println(\"This is dead code\");\n" +
13021 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
13022 "Unreachable code\n" +
13023 "----------\n");
13024 }
13025 // Check nullness in catch block, finally block and downstream code.
13026 public void testBug321926c() {
13027 this.runConformTest(
13028 new String[] {
13029 "X.java",
13030 "import java.io.IOException;\n" +
13031 "public class X {\n" +
13032 " public static void main(String[] args) {\n" +
13033 " String someVariable = null;\n" +
13034 " int i = 0;\n" +
13035 " try {\n" +
13036 " while (true) {\n" +
13037 " if (i == 0){\n" +
13038 " someVariable = \"not null\";\n" +
13039 " i++;\n" +
13040 " }\n" +
13041 " else\n" +
13042 " throw new IOException();\n" +
13043 " }\n" +
13044 " } catch (IOException e) {\n" +
13045 " if (someVariable == null) {\n" +
13046 " System.out.println(\"Compiler buggy\");\n" +
13047 " } else {\n" +
13048 " System.out.print(\"Compiler good \");\n" +
13049 " }\n" +
13050 " } finally {\n" +
13051 " if (someVariable == null) {\n" +
13052 " System.out.println(\"Compiler buggy\");\n" +
13053 " } else {\n" +
13054 " System.out.print(\"Compiler good \");\n" +
13055 " }\n" +
13056 " }\n" +
13057 " if (someVariable == null) {\n" +
13058 " System.out.println(\"Compiler buggy\");\n" +
13059 " } else {\n" +
13060 " System.out.println(\"Compiler good\");\n" +
13061 " }\n" +
13062 " }\n" +
13063 "}"},
13064 "Compiler good Compiler good Compiler good");
13065 }
13066 // Various nested loops.
13067 public void testBug321926d() {
13068 this.runConformTest(
13069 new String[] {
13070 "X.java",
13071 "import java.io.IOException;\n" +
13072 "public class X {\n" +
13073 " public static void main(String[] args) {\n" +
13074 " String someVariable = null;\n" +
13075 " int i = 0;\n" +
13076 " try {\n" +
13077 " while (true) {\n" +
13078 " for(;;) { \n" +
13079 " while (true) {\n" +
13080 " if (i == 0){\n" +
13081 " someVariable = \"not null\";\n" +
13082 " i++;\n" +
13083 " }\n" +
13084 " else\n" +
13085 " throw new IOException();\n" +
13086 " }\n" +
13087 " }\n" +
13088 " }\n" +
13089 " } catch (IOException e) {\n" +
13090 " if (someVariable == null) {\n" +
13091 " System.out.println(\"Compiler buggy\");\n" +
13092 " } else {\n" +
13093 " System.out.print(\"Compiler good \");\n" +
13094 " }\n" +
13095 " } finally {\n" +
13096 " if (someVariable == null) {\n" +
13097 " System.out.println(\"Compiler buggy\");\n" +
13098 " } else {\n" +
13099 " System.out.print(\"Compiler good \");\n" +
13100 " }\n" +
13101 " }\n" +
13102 " if (someVariable == null) {\n" +
13103 " System.out.println(\"Compiler buggy\");\n" +
13104 " } else {\n" +
13105 " System.out.println(\"Compiler good\");\n" +
13106 " }\n" +
13107 " }\n" +
13108 "}"},
13109 "Compiler good Compiler good Compiler good");
13110 }
13111 // Test widening catch.
13112 public void testBug321926e() {
13113 this.runConformTest(
13114 new String[] {
13115 "X.java",
13116 "import java.io.IOException;\n" +
13117 "public class X {\n" +
13118 " public static void main(String[] args) {\n" +
13119 " String someVariable = null;\n" +
13120 " int i = 0;\n" +
13121 " try {\n" +
13122 " while (true) {\n" +
13123 " if (i == 0){\n" +
13124 " someVariable = \"not null\";\n" +
13125 " i++;\n" +
13126 " }\n" +
13127 " else\n" +
13128 " throw new IOException();\n" +
13129 " }\n" +
13130 " } catch (Exception e) {\n" +
13131 " // broken from loop, continue on\n" +
13132 " }\n" +
13133 " if (someVariable == null) {\n" +
13134 " System.out.println(\"Compiler buggy\");\n" +
13135 " } else {\n" +
13136 " System.out.println(\"Compiler good\");\n" +
13137 " }\n" +
13138 " }\n" +
13139 "}"},
13140 "Compiler good");
13141 }
13142 // Tested nested try blocks.
13143 public void testBug321926f() {
13144 this.runConformTest(
13145 new String[] {
13146 "X.java",
13147 "import java.io.IOException;\n" +
13148 "public class X {\n" +
13149 " public static void main(String[] args) {\n" +
13150 " String someVariable = null;\n" +
13151 " int i = 0;\n" +
13152 " try {\n" +
13153 " while (true) {\n" +
13154 " if (i != 0) {\n" +
13155 " try {\n" +
13156 " throw new IOException();\n" +
13157 " } catch (IOException e) {\n" +
13158 " if (someVariable == null) {\n" +
13159 " System.out.println(\"The compiler is buggy\");\n" +
13160 " } else {\n" +
13161 " System.out.print(\"Compiler good \");\n" +
13162 " }\n" +
13163 " throw e;\n" +
13164 " }\n" +
13165 " } else {\n" +
13166 " someVariable = \"not null\";\n" +
13167 " i++;\n" +
13168 " }\n" +
13169 " }\n" +
13170 " } catch (Exception e) {\n" +
13171 " // having broken from loop, continue on\n" +
13172 " }\n" +
13173 " if (someVariable == null) {\n" +
13174 " System.out.println(\"The compiler is buggy\");\n" +
13175 " } else {\n" +
13176 " System.out.println(\"Compiler good\");\n" +
13177 " }\n" +
13178 " }\n" +
13179 "}\n"},
13180 "Compiler good Compiler good");
13181 }
13182 // test for loop
13183 public void testBug321926g() {
13184 this.runConformTest(
13185 new String[] {
13186 "X.java",
13187 "import java.io.IOException;\n" +
13188 "public class X {\n" +
13189 " public static void main(String[] args) {\n" +
13190 " String someVariable = null;\n" +
13191 " int i = 0;\n" +
13192 " try {\n" +
13193 " for (int j = 0; true; j++) {\n" +
13194 " if (i == 0){\n" +
13195 " someVariable = \"not null\";\n" +
13196 " i++;\n" +
13197 " }\n" +
13198 " else\n" +
13199 " throw new IOException();\n" +
13200 " }\n" +
13201 " } catch (IOException e) {\n" +
13202 " // broken from loop, continue on\n" +
13203 " }\n" +
13204 " if (someVariable == null) {\n" +
13205 " System.out.println(\"Compiler buggy\");\n" +
13206 " } else {\n" +
13207 " System.out.println(\"Compiler good\");\n" +
13208 " }\n" +
13209 " }\n" +
13210 "}"},
13211 "Compiler good");
13212 }
13213 // test do while loop
13214 public void testBug321926h() {
13215 this.runConformTest(
13216 new String[] {
13217 "X.java",
13218 "import java.io.IOException;\n" +
13219 "public class X {\n" +
13220 " public static void main(String[] args) {\n" +
13221 " String someVariable = null;\n" +
13222 " int i = 0;\n" +
13223 " try {\n" +
13224 " do {\n" +
13225 " if (i == 0){\n" +
13226 " someVariable = \"not null\";\n" +
13227 " i++;\n" +
13228 " }\n" +
13229 " else\n" +
13230 " throw new IOException();\n" +
13231 " } while(true);\n" +
13232 " } catch (IOException e) {\n" +
13233 " // broken from loop, continue on\n" +
13234 " }\n" +
13235 " if (someVariable == null) {\n" +
13236 " System.out.println(\"Compiler buggy\");\n" +
13237 " } else {\n" +
13238 " System.out.println(\"Compiler good\");\n" +
13239 " }\n" +
13240 " }\n" +
13241 "}"},
13242 "Compiler good");
13243 }
13244 // test with while (true) with a break inside. was working already.
13245 public void testBug321926i() {
13246 this.runConformTest(
13247 new String[] {
13248 "X.java",
13249 "import java.io.IOException;\n" +
13250 "public class X {\n" +
13251 " public static void main(String[] args) {\n" +
13252 " String someVariable = null;\n" +
13253 " int i = 0;\n" +
13254 " try {\n" +
13255 " while (true) {\n" +
13256 " if (i == 0){\n" +
13257 " someVariable = \"not null\";\n" +
13258 " i++;\n" +
13259 " break;\n" +
13260 " }\n" +
13261 " else\n" +
13262 " throw new IOException();\n" +
13263 " }\n" +
13264 " } catch (IOException e) {\n" +
13265 " // broken from loop, continue on\n" +
13266 " }\n" +
13267 " if (someVariable == null) {\n" +
13268 " System.out.println(\"Compiler buggy\");\n" +
13269 " } else {\n" +
13270 " System.out.println(\"Compiler good\");\n" +
13271 " }\n" +
13272 " }\n" +
13273 "}"},
13274 "Compiler good");
13275 }
13276 // Test with non-explicit throws, i.e call method which throws rather than an inline throw statement.
13277 public void testBug321926j() {
13278 this.runConformTest(
13279 new String[] {
13280 "X.java",
13281 "import java.io.IOException;\n" +
13282 "public class X {\n" +
13283 " public static void main(String[] args) {\n" +
13284 " String someVariable = null;\n" +
13285 " int i = 0;\n" +
13286 " try {\n" +
13287 " while (true) {\n" +
13288 " if (i == 0){\n" +
13289 " someVariable = \"not null\";\n" +
13290 " i++;\n" +
13291 " }\n" +
13292 " else\n" +
13293 " invokeSomeMethod();\n" +
13294 " }\n" +
13295 " } catch (IOException e) {\n" +
13296 " // broken from loop, continue on\n" +
13297 " }\n" +
13298 " if (someVariable == null) {\n" +
13299 " System.out.println(\"Compiler buggy\");\n" +
13300 " } else {\n" +
13301 " System.out.println(\"Compiler good\");\n" +
13302 " }\n" +
13303 " }\n" +
13304 " public static void invokeSomeMethod() throws IOException {\n" +
13305 " throw new IOException();\n" +
13306 " }\n" +
13307 "}"},
13308 "Compiler good");
13309 }
13310 // Variation with nested loops
13311 public void testBug321926k() {
13312 this.runConformTest(
13313 new String[] {
13314 "X.java",
13315 "import java.io.IOException;\n" +
13316 "public class X {\n" +
13317 " public static void main(String[] args) {\n" +
13318 " String someVariable = null;\n" +
13319 " int i = 0;\n" +
13320 " try {\n" +
13321 " while (true) {\n" +
13322 " try {\n" +
13323 " while (true) {\n" +
13324 " if (i == 0){\n" +
13325 " someVariable = \"not null\";\n" +
13326 " i++;\n" +
13327 " }\n" +
13328 " else\n" +
13329 " throw new IOException();\n" +
13330 " }\n" +
13331 " } catch (IOException e) {\n" +
13332 " }\n" +
13333 " if (someVariable == null) {\n" +
13334 " System.out.println(\"Compiler buggy\");\n" +
13335 " } else {\n" +
13336 " System.out.print(\"Compiler good \");\n" +
13337 " }\n" +
13338 " throw new IOException();\n" +
13339 " }\n" +
13340 " } catch (IOException e) {\n" +
13341 " // broken from loop, continue on\n" +
13342 " }\n" +
13343 " if (someVariable == null) {\n" +
13344 " System.out.println(\"Compiler buggy\");\n" +
13345 " } else {\n" +
13346 " System.out.println(\"Compiler good\");\n" +
13347 " }\n" +
13348 " }\n" +
13349 "}"},
13350 "Compiler good Compiler good");
13351 }
13352 // variation with nested loops.
13353 public void testBug321926l() {
13354 Map options = getCompilerOptions();
13355 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13356
13357 this.runTest(
13358 new String[] {
13359 "X.java",
13360 "import java.io.IOException;\n" +
13361 "public class X {\n" +
13362 " public static void main(String[] args) {\n" +
13363 " String someVariable = null;\n" +
13364 " int i = 0;\n" +
13365 " try {\n" +
13366 " while (true) {\n" +
13367 " someVariable = null;\n"+
13368 " try {\n" +
13369 " while (true) {\n" +
13370 " if (i == 0){\n" +
13371 " someVariable = \"not null\";\n" +
13372 " i++;\n" +
13373 " }\n" +
13374 " else\n" +
13375 " throw new IOException();\n" +
13376 " }\n" +
13377 " } catch (IOException e) {\n" +
13378 " }\n" +
13379 " if (someVariable == null) {\n" +
13380 " System.out.println(\"Compiler buggy\");\n" +
13381 " } else {\n" +
13382 " System.out.print(\"Compiler good \");\n" +
13383 " }\n" +
13384 " throw new IOException();\n" +
13385 " }\n" +
13386 " } catch (IOException e) {\n" +
13387 " // broken from loop, continue on\n" +
13388 " }\n" +
13389 " if (someVariable == null) {\n" +
13390 " System.out.println(\"Compiler buggy\");\n" +
13391 " } else {\n" +
13392 " System.out.println(\"Compiler good\");\n" +
13393 " }\n" +
13394 " }\n" +
13395 "}"},
13396 false,
13397 "----------\n" +
13398 "1. WARNING in X.java (at line 8)\n" +
13399 " someVariable = null;\n" +
13400 " ^^^^^^^^^^^^\n" +
13401 "Redundant assignment: The variable someVariable can only be null at this location\n" +
13402 "----------\n",
13403 "Compiler good Compiler good",
13404 "",
13405 true, // force execution
13406 null, // classlibs
13407 true, // flush output,
13408 null, // vm args
13409 options,
13410 null,
13411 JavacTestOptions.Excuse.EclipseHasSomeMoreWarnings);
13412 }
13413 public void testBug321926m() {
13414 this.runConformTest(
13415 new String[] {
13416 "X.java",
13417 "import java.io.IOException;\n" +
13418 "public class X {\n" +
13419 " public static void main(String[] args) {\n" +
13420 " String someVariable = null;\n" +
13421 " int i = 0;\n" +
13422 " try {\n" +
13423 " while (true) {\n" +
13424 " if (i == 0){\n" +
13425 " someVariable = \"not null\";\n" +
13426 " i++;\n" +
13427 " }\n" +
13428 " else\n" +
13429 " throw new IOException();\n" +
13430 " if (true) {\n" +
13431 " break;\n" +
13432 " }\n" +
13433 " }\n" +
13434 " } catch (IOException e) {\n" +
13435 " // broken from loop, continue on\n" +
13436 " }\n" +
13437 " if (someVariable == null) {\n" +
13438 " System.out.println(\"Compiler buggy\");\n" +
13439 " } else {\n" +
13440 " System.out.println(\"Compiler good\");\n" +
13441 " }\n" +
13442 " }\n" +
13443 "}"},
13444 "Compiler good");
13445 }
13446 public void testBug321926n() {
13447 Map options = getCompilerOptions();
13448 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13449 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13450 this.runConformTest(
13451 new String[] {
13452 "X.java",
13453 "import java.io.IOException;\n" +
13454 "public class X {\n" +
13455 " public static void main(String[] args) {\n" +
13456 " String someVariable = null;\n" +
13457 " int i = 0;\n" +
13458 " try {\n" +
13459 " someVariable = \"not null\";\n" +
13460 " while (true) {\n" +
13461 " if (i == 0){\n" +
13462 " someVariable = \"not null\";\n" +
13463 " i++;\n" +
13464 " }\n" +
13465 " else\n" +
13466 " throw new IOException();\n" +
13467 " }\n" +
13468 " } catch (IOException e) {\n" +
13469 " // broken from loop, continue on\n" +
13470 " }\n" +
13471 " if (someVariable == null) {\n" +
13472 " System.out.println(\"Compiler buggy\");\n" +
13473 " } else {\n" +
13474 " System.out.println(\"Compiler good\");\n" +
13475 " }\n" +
13476 " }\n" +
13477 "}"},
13478 "Compiler good",
13479 options);
13480 }
13481 public void testBug321926o() {
13482 Map options = getCompilerOptions();
13483 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13484 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13485 this.runConformTest(
13486 new String[] {
13487 "X.java",
13488 "import java.io.IOException;\n" +
13489 "public class X {\n" +
13490 " public static void main(String[] args) {\n" +
13491 " String someVariable = null;\n" +
13492 " int i = 0;\n" +
13493 " try {\n" +
13494 " someVariable = \"not null\";\n" +
13495 " for(;;) {\n" +
13496 " if (i == 0){\n" +
13497 " someVariable = \"not null\";\n" +
13498 " i++;\n" +
13499 " }\n" +
13500 " else\n" +
13501 " throw new IOException();\n" +
13502 " }\n" +
13503 " } catch (IOException e) {\n" +
13504 " // broken from loop, continue on\n" +
13505 " }\n" +
13506 " if (someVariable == null) {\n" +
13507 " System.out.println(\"Compiler buggy\");\n" +
13508 " } else {\n" +
13509 " System.out.println(\"Compiler good\");\n" +
13510 " }\n" +
13511 " }\n" +
13512 "}"},
13513 "Compiler good",
13514 options);
13515 }
13516 public void testBug321926p() {
13517 Map options = getCompilerOptions();
13518 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13519 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13520 this.runConformTest(
13521 new String[] {
13522 "X.java",
13523 "import java.io.IOException;\n" +
13524 "public class X {\n" +
13525 " public static void main(String[] args) {\n" +
13526 " String someVariable = null;\n" +
13527 " int i = 0;\n" +
13528 " try {\n" +
13529 " someVariable = \"not null\";\n" +
13530 " do {\n" +
13531 " if (i == 0){\n" +
13532 " someVariable = \"not null\";\n" +
13533 " i++;\n" +
13534 " }\n" +
13535 " else\n" +
13536 " throw new IOException();\n" +
13537 " } while (true);\n" +
13538 " } catch (IOException e) {\n" +
13539 " // broken from loop, continue on\n" +
13540 " }\n" +
13541 " if (someVariable == null) {\n" +
13542 " System.out.println(\"Compiler buggy\");\n" +
13543 " } else {\n" +
13544 " System.out.println(\"Compiler good\");\n" +
13545 " }\n" +
13546 " }\n" +
13547 "}"},
13548 "Compiler good",
13549 options);
13550 }
13551 public void testBug321926q() {
13552 Map options = getCompilerOptions();
13553 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
13554 this.runConformTest(
13555 new String[] {
13556 "X.java",
13557 "import java.io.IOException;\n" +
13558 "public class X {\n" +
13559 " public static void main(String[] args) {\n" +
13560 " String someVariable = null;\n" +
13561 " int i = 0;\n" +
13562 " try {\n" +
13563 " someVariable = \"not null\";\n" +
13564 " do {\n" +
13565 " if (i == 0){\n" +
13566 " someVariable = \"not null\";\n" +
13567 " i++;\n" +
13568 " }\n" +
13569 " else\n" +
13570 " throw new IOException();\n" +
13571 " } while ((someVariable = \"not null\") != null);\n" +
13572 " } catch (IOException e) {\n" +
13573 " // broken from loop, continue on\n" +
13574 " }\n" +
13575 " if (someVariable == null) {\n" +
13576 " System.out.println(\"Compiler buggy\");\n" +
13577 " } else {\n" +
13578 " System.out.println(\"Compiler good\");\n" +
13579 " }\n" +
13580 " }\n" +
13581 "}"},
13582 "Compiler good", null, true, null, options, null);
13583 }
13584 public void testBug321926r() {
13585 Map options = getCompilerOptions();
13586 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
13587 this.runConformTest(
13588 new String[] {
13589 "X.java",
13590 "import java.io.IOException;\n" +
13591 "public class X {\n" +
13592 " public static void main(String[] args) {\n" +
13593 " String someVariable = null;\n" +
13594 " int i = 0;\n" +
13595 " try {\n" +
13596 " while ((someVariable = \"not null\") != null) {\n" +
13597 " if (i == 0){\n" +
13598 " someVariable = \"not null\";\n" +
13599 " i++;\n" +
13600 " }\n" +
13601 " else\n" +
13602 " throw new IOException();\n" +
13603 " }\n" +
13604 " } catch (IOException e) {\n" +
13605 " // broken from loop, continue on\n" +
13606 " }\n" +
13607 " if (someVariable == null) {\n" +
13608 " System.out.println(\"Compiler buggy\");\n" +
13609 " } else {\n" +
13610 " System.out.println(\"Compiler good\");\n" +
13611 " }\n" +
13612 " }\n" +
13613 "}"},
13614 "Compiler good", null, true, null, options, null
13615 );
13616 }
13617 public void testBug321926s() {
13618 Map options = getCompilerOptions();
13619 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
13620 this.runConformTest(
13621 new String[] {
13622 "X.java",
13623 "import java.io.IOException;\n" +
13624 "public class X {\n" +
13625 " public static void main(String[] args) {\n" +
13626 " String someVariable = null;\n" +
13627 " int i = 0;\n" +
13628 " try {\n" +
13629 " someVariable = \" not null\";\n" +
13630 " while ((someVariable = null) != null) {\n" +
13631 " if (i == 0){\n" +
13632 " someVariable = \"not null\";\n" +
13633 " i++;\n" +
13634 " }\n" +
13635 " else\n" +
13636 " throw new IOException();\n" +
13637 " }\n" +
13638 " } catch (IOException e) {\n" +
13639 " // broken from loop, continue on\n" +
13640 " }\n" +
13641 " if (someVariable == null) {\n" +
13642 " System.out.println(\"Compiler good\");\n" +
13643 " } else {\n" +
13644 " System.out.println(\"Compiler buggy\");\n" +
13645 " }\n" +
13646 " }\n" +
13647 "}"},
13648 "Compiler good", null, true, null, options, null
13649 );
13650 }
13651 public void testBug321926t() {
13652 this.runConformTest(
13653 new String[] {
13654 "X.java",
13655 "import java.io.IOException;\n" +
13656 "public class X {\n" +
13657 " public static void main(String s[]) {\n" +
13658 " String file = \"non null\";\n" +
13659 " int i = 0;\n" +
13660 " try {\n" +
13661 " while (true) {\n" +
13662 " if (i == 0) {\n" +
13663 " file = null;\n" +
13664 " i++;\n"+
13665 " }\n" +
13666 " else \n" +
13667 " throw new IOException();\n" +
13668 " }\n" +
13669 " } catch (IOException e) {\n" +
13670 " }\n" +
13671 " if (file == null)\n" +
13672 " System.out.println(\"Compiler good\");\n" +
13673 " else \n" +
13674 " System.out.println(\"Compiler bad\");\n" +
13675 " }\n" +
13676 "}\n"},
13677 "Compiler good");
13678 }
13679 public void testBug321926u() {
13680 Map options = getCompilerOptions();
13681 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
13682 this.runConformTest(
13683 new String[] {
13684 "X.java",
13685 "import java.io.IOException;\n" +
13686 "public class X {\n" +
13687 " public static void main(String s[]) {\n" +
13688 " String file = \"non null\";\n" +
13689 " int i = 0;\n" +
13690 " try {\n" +
13691 " while (true) {\n" +
13692 " if (i == 0) {\n" +
13693 " file = null;\n" +
13694 " i++;\n"+
13695 " }\n" +
13696 " else {\n" +
13697 " file = null;\n" +
13698 " throw new IOException();\n" +
13699 " }\n" +
13700 " }\n" +
13701 " } catch (IOException e) {\n" +
13702 " }\n" +
13703 " if (file == null)\n" +
13704 " System.out.println(\"Compiler good\");\n" +
13705 " else \n" +
13706 " System.out.println(\"Compiler bad\");\n" +
13707 " }\n" +
13708 "}\n"},
13709 "Compiler good",
13710 options);
13711 }
13712 public void testBug321926v() {
13713 Map options = getCompilerOptions();
13714 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.IGNORE);
13715 this.runConformTest(
13716 new String[] {
13717 "X.java",
13718 "import java.io.IOException;\n" +
13719 "public class X {\n" +
13720 " public static void main(String s[]) {\n" +
13721 " String file = null;\n" +
13722 " int i = 0;\n" +
13723 " try {\n" +
13724 " while (true) {\n" +
13725 " if (i == 0) {\n" +
13726 " file = \"non null\";\n" +
13727 " i++;\n"+
13728 " }\n" +
13729 " else {\n" +
13730 " file = \"non null\";\n" +
13731 " throw new IOException();\n" +
13732 " }\n" +
13733 " }\n" +
13734 " } catch (IOException e) {\n" +
13735 " }\n" +
13736 " if (file == null)\n" +
13737 " System.out.println(\"Compiler bad\");\n" +
13738 " else \n" +
13739 " System.out.println(\"Compiler good\");\n" +
13740 " }\n" +
13741 "}\n"},
13742 "Compiler good",
13743 options);
13744 }
13745 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13746 public void testBug317829a() {
13747 Map options = getCompilerOptions();
13748 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13749 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13750 this.runConformTest(
13751 new String[] {
13752 "X.java",
13753 "import java.io.IOException;\n" +
13754 "public class X {\n" +
13755 " public static void main(String[] args) {\n" +
13756 " String someVariable = null;\n" +
13757 " int i = 0;\n" +
13758 " try {\n" +
13759 " someVariable = \"not null\";\n" +
13760 " while (true) {\n" +
13761 " throw new IOException();\n" +
13762 " }\n" +
13763 " } catch (IOException e) {\n" +
13764 " if (someVariable == null) {\n" +
13765 " System.out.println(\"Compiler bad\");\n" +
13766 " } else {\n" +
13767 " System.out.print(\"Compiler good \");\n" +
13768 " }\n" +
13769 " }\n" +
13770 " if (someVariable == null) {\n" +
13771 " System.out.println(\"Compiler bad\");\n" +
13772 " } else {\n" +
13773 " System.out.println(\"Compiler good\");\n" +
13774 " }\n" +
13775 " }\n" +
13776 "}"},
13777 "Compiler good Compiler good",
13778 options);
13779 }
13780 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13781 // assignment from unknown - not reporting redundant check
13782 public void testBug317829a2() {
13783 this.runConformTest(
13784 new String[] {
13785 "X.java",
13786 "import java.io.IOException;\n" +
13787 "public class X {\n" +
13788 " public static void main(String[] args) {\n" +
13789 " String someVariable = null;\n" +
13790 " int i = 0;\n" +
13791 " try {\n" +
13792 " someVariable = getString();\n" +
13793 " while (true) {\n" +
13794 " throw new IOException();\n" +
13795 " }\n" +
13796 " } catch (IOException e) {\n" +
13797 " if (someVariable == null) {\n" +
13798 " System.out.println(\"Compiler bad\");\n" +
13799 " } else {\n" +
13800 " System.out.print(\"Compiler good \");\n" +
13801 " }\n" +
13802 " }\n" +
13803 " if (someVariable == null) {\n" +
13804 " System.out.println(\"Compiler bad\");\n" +
13805 " } else {\n" +
13806 " System.out.println(\"Compiler good\");\n" +
13807 " }\n" +
13808 " }\n" +
13809 " static String getString() { return \"\"; }\n" +
13810 "}"},
13811 "Compiler good Compiler good");
13812 }
13813 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13814 public void testBug317829b() {
13815 Map options = getCompilerOptions();
13816 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13817 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13818 this.runConformTest(
13819 new String[] {
13820 "X.java",
13821 "import java.io.IOException;\n" +
13822 "public class X {\n" +
13823 " public static void main(String[] args) {\n" +
13824 " String someVariable = null;\n" +
13825 " int i = 0;\n" +
13826 " try {\n" +
13827 " someVariable = \"not null\";\n" +
13828 " while (true) {\n" +
13829 " someMethod();\n" +
13830 " }\n" +
13831 " } catch (IOException e) {\n" +
13832 " if (someVariable == null) {\n" +
13833 " System.out.println(\"Compiler bad\");\n" +
13834 " } else {\n" +
13835 " System.out.print(\"Compiler good \");\n" +
13836 " }\n" +
13837 " }\n" +
13838 " if (someVariable == null) {\n" +
13839 " System.out.println(\"Compiler bad\");\n" +
13840 " } else {\n" +
13841 " System.out.println(\"Compiler good\");\n" +
13842 " }\n" +
13843 " }\n" +
13844 " public static void someMethod() throws IOException {\n" +
13845 " throw new IOException();\n" +
13846 " }\n" +
13847 "}"},
13848 "Compiler good Compiler good",
13849 options);
13850 }
13851 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13852 public void testBug317829c() {
13853 Map options = getCompilerOptions();
13854 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13855 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13856 this.runConformTest(
13857 new String[] {
13858 "X.java",
13859 "import java.io.IOException;\n" +
13860 "public class X {\n" +
13861 " public static void main(String[] args) {\n" +
13862 " String someVariable = null;\n" +
13863 " int i = 0;\n" +
13864 " try {\n" +
13865 " someVariable = \"not null\";\n" +
13866 " for (;;) {\n" +
13867 " throw new IOException();\n" +
13868 " }\n" +
13869 " } catch (IOException e) {\n" +
13870 " if (someVariable == null) {\n" +
13871 " System.out.println(\"Compiler bad\");\n" +
13872 " } else {\n" +
13873 " System.out.print(\"Compiler good \");\n" +
13874 " }\n" +
13875 " }\n" +
13876 " if (someVariable == null) {\n" +
13877 " System.out.println(\"Compiler bad\");\n" +
13878 " } else {\n" +
13879 " System.out.println(\"Compiler good\");\n" +
13880 " }\n" +
13881 " }\n" +
13882 "}"},
13883 "Compiler good Compiler good",
13884 options);
13885 }
13886 //https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13887 public void testBug317829d() {
13888 Map options = getCompilerOptions();
13889 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13890 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13891 this.runConformTest(
13892 new String[] {
13893 "X.java",
13894 "import java.io.IOException;\n" +
13895 "public class X {\n" +
13896 " public static void main(String[] args) {\n" +
13897 " String someVariable = null;\n" +
13898 " int i = 0;\n" +
13899 " try {\n" +
13900 " someVariable = \"not null\";\n" +
13901 " for(;;) {\n" +
13902 " someMethod();\n" +
13903 " }\n" +
13904 " } catch (IOException e) {\n" +
13905 " if (someVariable == null) {\n" +
13906 " System.out.println(\"Compiler bad\");\n" +
13907 " } else {\n" +
13908 " System.out.print(\"Compiler good \");\n" +
13909 " }\n" +
13910 " }\n" +
13911 " if (someVariable == null) {\n" +
13912 " System.out.println(\"Compiler bad\");\n" +
13913 " } else {\n" +
13914 " System.out.println(\"Compiler good\");\n" +
13915 " }\n" +
13916 " }\n" +
13917 " public static void someMethod() throws IOException {\n" +
13918 " throw new IOException();\n" +
13919 " }\n" +
13920 "}"},
13921 "Compiler good Compiler good",
13922 options);
13923 }
13924 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13925 public void testBug317829e() {
13926 Map options = getCompilerOptions();
13927 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13928 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13929 this.runConformTest(
13930 new String[] {
13931 "X.java",
13932 "import java.io.IOException;\n" +
13933 "public class X {\n" +
13934 " public static void main(String[] args) {\n" +
13935 " String someVariable = null;\n" +
13936 " int i = 0;\n" +
13937 " try {\n" +
13938 " someVariable = \"not null\";\n" +
13939 " do {\n" +
13940 " throw new IOException();\n" +
13941 " } while (true);\n" +
13942 " } catch (IOException e) {\n" +
13943 " if (someVariable == null) {\n" +
13944 " System.out.println(\"Compiler bad\");\n" +
13945 " } else {\n" +
13946 " System.out.print(\"Compiler good \");\n" +
13947 " }\n" +
13948 " }\n" +
13949 " if (someVariable == null) {\n" +
13950 " System.out.println(\"Compiler bad\");\n" +
13951 " } else {\n" +
13952 " System.out.println(\"Compiler good\");\n" +
13953 " }\n" +
13954 " }\n" +
13955 "}"},
13956 "Compiler good Compiler good",
13957 options);
13958 }
13959 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317829
13960 public void testBug317829f() {
13961 Map options = getCompilerOptions();
13962 options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
13963 options.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.WARNING);
13964 this.runConformTest(
13965 new String[] {
13966 "X.java",
13967 "import java.io.IOException;\n" +
13968 "public class X {\n" +
13969 " public static void main(String[] args) {\n" +
13970 " String someVariable = null;\n" +
13971 " int i = 0;\n" +
13972 " try {\n" +
13973 " someVariable = \"not null\";\n" +
13974 " do {\n" +
13975 " someMethod();\n" +
13976 " } while (true);\n" +
13977 " } catch (IOException e) {\n" +
13978 " if (someVariable == null) {\n" +
13979 " System.out.println(\"Compiler bad\");\n" +
13980 " } else {\n" +
13981 " System.out.print(\"Compiler good \");\n" +
13982 " }\n" +
13983 " }\n" +
13984 " if (someVariable == null) {\n" +
13985 " System.out.println(\"Compiler bad\");\n" +
13986 " } else {\n" +
13987 " System.out.println(\"Compiler good\");\n" +
13988 " }\n" +
13989 " }\n" +
13990 " public static void someMethod() throws IOException {\n" +
13991 " throw new IOException();\n" +
13992 " }\n" +
13993 "}"},
13994 "Compiler good Compiler good",
13995 options);
13996 }
13997
13998 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
13999 // LocalDeclaration
14000 public void testBug292478() {
14001 this.runNegativeTest(
14002 new String[] {
14003 "X.java",
14004 "public class X {\n" +
14005 " void foo(Object o) {\n" +
14006 " if (o != null) {/* */}\n" +
14007 " Object p = o;\n" +
14008 " p.toString();\n" + // complain here
14009 " }\n" +
14010 "}"},
14011 "----------\n" +
14012 "1. ERROR in X.java (at line 5)\n" +
14013 " p.toString();\n" +
14014 " ^\n" +
14015 "Potential null pointer access: The variable p may be null at this location\n" +
14016 "----------\n",
14017 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
14018 }
14019
14020 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14021 // Assignment
14022 public void testBug292478a() {
14023 this.runNegativeTest(
14024 new String[] {
14025 "X.java",
14026 "public class X {\n" +
14027 " void foo(Object o) {\n" +
14028 " Object p;" +
14029 " if (o != null) {/* */}\n" +
14030 " p = o;\n" +
14031 " p.toString();\n" + // complain here
14032 " }\n" +
14033 "}"},
14034 "----------\n" +
14035 "1. ERROR in X.java (at line 5)\n" +
14036 " p.toString();\n" +
14037 " ^\n" +
14038 "Potential null pointer access: The variable p may be null at this location\n" +
14039 "----------\n",
14040 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
14041 }
14042
14043 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14044 // Assignment after definite null
14045 public void testBug292478b() {
14046 this.runNegativeTest(
14047 new String[] {
14048 "X.java",
14049 "public class X {\n" +
14050 " void foo(Object o) {\n" +
14051 " Object p = null;\n" +
14052 " if (o != null) {/* */}\n" +
14053 " p = o;\n" +
14054 " p.toString();\n" + // complain here
14055 " }\n" +
14056 "}"},
14057 "----------\n" +
14058 "1. ERROR in X.java (at line 6)\n" +
14059 " p.toString();\n" +
14060 " ^\n" +
14061 "Potential null pointer access: The variable p may be null at this location\n" +
14062 "----------\n",
14063 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
14064 }
14065
14066 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14067 // Assignment after definite null - many locals
14068 public void testBug292478c() {
14069 this.runNegativeTest(
14070 new String[] {
14071 "X.java",
14072 "public class X {\n" +
14073 " void foo(Object o) {\n" +
14074 " int i00, i01, i02, i03, i04, i05, i06, i07, i08, i09;\n" +
14075 " int i10, i11, i12, i13, i14, i15, i16, i17, i18, i19;\n" +
14076 " int i20, i21, i22, i23, i24, i25, i26, i27, i28, i29;\n" +
14077 " int i30, i31, i32, i33, i34, i35, i36, i37, i38, i39;\n" +
14078 " int i40, i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" +
14079 " int i50, i51, i52, i53, i54, i55, i56, i57, i58, i59;\n" +
14080 " int i60, i61, i62, i63, i64, i65, i66, i67, i68, i69;\n" +
14081 " Object p = null;\n" +
14082 " if (o != null) {/* */}\n" +
14083 " p = o;\n" +
14084 " p.toString();\n" + // complain here
14085 " }\n" +
14086 "}"},
14087 "----------\n" +
14088 "1. ERROR in X.java (at line 13)\n" +
14089 " p.toString();\n" +
14090 " ^\n" +
14091 "Potential null pointer access: The variable p may be null at this location\n" +
14092 "----------\n",
14093 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
14094 }
14095
14096 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14097 // Assignment affects initsOnFinally
14098 public void testBug292478d() {
14099 runNegativeNullTest(
14100 new String[] {
14101 "X.java",
14102 "public class X {\n" +
14103 " X bar() {\n" +
14104 " return null;\n" +
14105 " }\n" +
14106 " Object foo() {\n" +
14107 " X x = null;\n" +
14108 " X y = new X();\n" +
14109 " X u = null;\n" +
14110 " try {\n" +
14111 " u = bar();\n" +
14112 " x = bar();\n" +
14113 " if (x==null) { }\n" +
14114 " y = x;\n" + // this makes y potentially null
14115 " if (x==null) { y=bar();} else { y=new X(); }\n" +
14116 " return x;\n" +
14117 " } finally {\n" +
14118 " y.toString();\n" + // must complain against potentially null, although normal exist of tryBlock says differently (unknown or non-null)
14119 " }\n" +
14120 " }\n" +
14121 "}\n"},
14122 "----------\n" +
14123 "1. ERROR in X.java (at line 17)\n" +
14124 " y.toString();\n" +
14125 " ^\n" +
14126 "Potential null pointer access: The variable y may be null at this location\n" +
14127 "----------\n");
14128 }
14129
14130 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14131 // test regression reported in comment 8
14132 public void testBug292478e() {
14133 this.runConformTest(
14134 new String[] {
14135 "Test.java",
14136 "public class Test {\n" +
14137 " Object foo(int i, boolean b1, boolean b2) {\n" +
14138 " Object o1 = null;\n" +
14139 " done : while (true) { \n" +
14140 " switch (i) {\n" +
14141 " case 1 :\n" +
14142 " Object o2 = null;\n" +
14143 " if (b2)\n" +
14144 " o2 = new Object();\n" +
14145 " o1 = o2;\n" +
14146 " break;\n" +
14147 " case 2 :\n" +
14148 " break done;\n" +
14149 " }\n" +
14150 " } \n" +
14151 " if (o1 != null)\n" +
14152 " return o1;\n" +
14153 " return null;\n" +
14154 " }\n" +
14155 "}\n"
14156 });
14157 }
14158
14159 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14160 // variant where regression occurred inside the while-switch structure
14161 public void testBug292478f() {
14162 this.runConformTest(
14163 new String[] {
14164 "Test.java",
14165 "public class Test {\n" +
14166 " Object foo(int i, boolean b1, boolean b2) {\n" +
14167 " Object o1 = null;\n" +
14168 " done : while (true) { \n" +
14169 " switch (i) {\n" +
14170 " case 1 :\n" +
14171 " Object o2 = null;\n" +
14172 " if (b2)\n" +
14173 " o2 = new Object();\n" +
14174 " o1 = o2;\n" +
14175 " if (o1 != null)\n" +
14176 " return o1;\n" +
14177 " break;\n" +
14178 " case 2 :\n" +
14179 " break done;\n" +
14180 " }\n" +
14181 " } \n" +
14182 " return null;\n" +
14183 " }\n" +
14184 "}\n"
14185 });
14186 }
14187
14188 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=292478 - Report potentially null across variable assignment
14189 // variant for transfering state potentially unknown
14190 public void testBug292478g() {
14191 this.runConformTest(
14192 new String[] {
14193 "Test.java",
14194 "public class Test {\n" +
14195 " Object foo(int i, boolean b1, boolean b2, Object o2) {\n" +
14196 " Object o1 = null;\n" +
14197 " done : while (true) { \n" +
14198 " switch (i) {\n" +
14199 " case 1 :\n" +
14200 " if (b2)\n" +
14201 " o2 = bar();\n" +
14202 " o1 = o2;\n" +
14203 " if (o1 != null)\n" +
14204 " return o1;\n" +
14205 " break;\n" +
14206 " case 2 :\n" +
14207 " break done;\n" +
14208 " }\n" +
14209 " } \n" +
14210 " return null;\n" +
14211 " }\n" +
14212 " Object bar() { return null; }\n" +
14213 "}\n"
14214 });
14215 }
14216
14217 // Bug 324762 - Compiler thinks there is deadcode and removes it!
14218 // regression caused by the fix for bug 133125
14219 // ternary is non-null or null
14220 public void testBug324762() {
14221 this.runConformTest(
14222 new String[] {
14223 "Test.java",
14224 "public class Test {\n" +
14225 " void zork(boolean b1) {\n" +
14226 " Object satisfied = null;\n" +
14227 " if (b1) {\n" +
14228 " String[] s = new String[] { \"a\", \"b\" };\n" +
14229 " for (int k = 0; k < s.length && satisfied == null; k++)\n" +
14230 " satisfied = s.length > 1 ? new Object() : null;\n" +
14231 " }\n" +
14232 " }\n" +
14233 "}\n"
14234 });
14235 }
14236
14237 // Bug 324762 - Compiler thinks there is deadcode and removes it!
14238 // regression caused by the fix for bug 133125
14239 // ternary is unknown or null
14240 public void testBug324762a() {
14241 this.runConformTest(
14242 new String[] {
14243 "Test.java",
14244 "public class Test {\n" +
14245 " void zork(boolean b1) {\n" +
14246 " Object satisfied = null;\n" +
14247 " if (b1) {\n" +
14248 " String[] s = new String[] { \"a\", \"b\" };\n" +
14249 " for (int k = 0; k < s.length && satisfied == null; k++)\n" +
14250 " satisfied = s.length > 1 ? bar() : null;\n" +
14251 " }\n" +
14252 " }\n" +
14253 " Object bar() { return null; }\n" +
14254 "}\n"
14255 });
14256 }
14257
14258 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325229
14259 // instancof expression
14260 public void testBug325229a() {
14261 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14262 Map compilerOptions = getCompilerOptions();
14263 compilerOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
14264 this.runConformTest(
14265 new String[] {
14266 "Test.java",
14267 "public class Test {\n" +
14268 " void foo(Object a) {\n" +
14269 " assert a instanceof Object;\n " +
14270 " if (a!=null) {\n" +
14271 " System.out.println(\"a is not null\");\n" +
14272 " } else{\n" +
14273 " System.out.println(\"a is null\");\n" +
14274 " }\n" +
14275 " }\n" +
14276 " public static void main(String[] args){\n" +
14277 " Test test = new Test();\n" +
14278 " test.foo(null);\n" +
14279 " }\n" +
14280 "}\n"},
14281 "a is null",
14282 null,
14283 true,
14284 new String[] {"-da"},
14285 compilerOptions,
14286 null);
14287 }
14288 }
14289
14290 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325229
14291 // MessageSend in assert
14292 public void testBug325229b() {
14293 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14294 Map compilerOptions = getCompilerOptions();
14295 compilerOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
14296 this.runConformTest(
14297 new String[] {
14298 "Test.java",
14299 "public class Test {\n" +
14300 " boolean bar() {\n" +
14301 " return false;\n" +
14302 " }" +
14303 " void foo(Test a) {\n" +
14304 " assert a.bar();\n " +
14305 " if (a!=null) {\n" +
14306 " System.out.println(\"a is not null\");\n" +
14307 " } else{\n" +
14308 " System.out.println(\"a is null\");\n" +
14309 " }\n" +
14310 " }\n" +
14311 " public static void main(String[] args){\n" +
14312 " Test test = new Test();\n" +
14313 " test.foo(null);\n" +
14314 " }\n" +
14315 "}\n"},
14316 "a is null",
14317 null,
14318 true,
14319 new String[] {"-da"},
14320 compilerOptions,
14321 null);
14322 }
14323 }
14324
14325 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325229
14326 // QualifiedNameReference in assert
14327 public void testBug325229c() {
14328 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14329 Map compilerOptions = getCompilerOptions();
14330 compilerOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
14331 this.runConformTest(
14332 new String[] {
14333 "Test.java",
14334 "public class Test {\n" +
14335 " boolean bar() {\n" +
14336 " return false;\n" +
14337 " }" +
14338 " Test tfield;\n" +
14339 " void foo(Test a) {\n" +
14340 " assert a.tfield.bar();\n " +
14341 " if (a!=null) {\n" +
14342 " System.out.println(\"a is not null\");\n" +
14343 " } else{\n" +
14344 " System.out.println(\"a is null\");\n" +
14345 " }\n" +
14346 " }\n" +
14347 " public static void main(String[] args){\n" +
14348 " Test test = new Test();\n" +
14349 " test.foo(null);\n" +
14350 " }\n" +
14351 "}\n"},
14352 "a is null",
14353 null,
14354 true,
14355 new String[] {"-da"},
14356 compilerOptions,
14357 null);
14358 }
14359 }
14360
14361 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325229
14362 // EqualExpression in assert, comparison against non null
14363 public void testBug325229d() {
14364 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14365 Map compilerOptions = getCompilerOptions();
14366 compilerOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
14367 this.runConformTest(
14368 new String[] {
14369 "Test.java",
14370 "public class Test {\n" +
14371 " void foo(Object a) {\n" +
14372 " Object b = null;" +
14373 " assert a == b;\n " +
14374 " if (a!=null) {\n" +
14375 " System.out.println(\"a is not null\");\n" +
14376 " } else{\n" +
14377 " System.out.println(\"a is null\");\n" +
14378 " }\n" +
14379 " assert a != b;\n " +
14380 " if (a!=null) {\n" +
14381 " System.out.println(\"a is not null\");\n" +
14382 " } else{\n" +
14383 " System.out.println(\"a is null\");\n" +
14384 " }\n" +
14385 " }\n" +
14386 " public static void main(String[] args){\n" +
14387 " Test test = new Test();\n" +
14388 " test.foo(null);\n" +
14389 " }\n" +
14390 "}\n"},
14391 "a is null\n" +
14392 "a is null",
14393 null,
14394 true,
14395 new String[] {"-da"},
14396 compilerOptions,
14397 null);
14398 }
14399 }
14400
14401 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325342
14402 // Null warnings because of assert statements should be suppressed
14403 // when CompilerOptions.OPTION_IncludeNullInfoFromAsserts is disabled.
14404 public void testBug325342a() {
14405 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14406 Map compilerOptions = getCompilerOptions();
14407 compilerOptions.put(CompilerOptions.OPTION_IncludeNullInfoFromAsserts, CompilerOptions.DISABLED);
14408 this.runNegativeTest(
14409 new String[] {
14410 "Test.java",
14411 "public class Test {\n" +
14412 " void foo(Object a, Object b, Object c) {\n" +
14413 " assert a == null;\n " +
14414 " if (a!=null) {\n" +
14415 " System.out.println(\"a is not null\");\n" +
14416 " } else{\n" +
14417 " System.out.println(\"a is null\");\n" +
14418 " }\n" +
14419 " a = null;\n" +
14420 " if (a== null) {}\n" +
14421 " assert b != null;\n " +
14422 " if (b!=null) {\n" +
14423 " System.out.println(\"b is not null\");\n" +
14424 " } else{\n" +
14425 " System.out.println(\"b is null\");\n" +
14426 " }\n" +
14427 " assert c == null;\n" +
14428 " if (c.equals(a)) {\n" +
14429 " System.out.println(\"\");\n" +
14430 " } else{\n" +
14431 " System.out.println(\"\");\n" +
14432 " }\n" +
14433 " }\n" +
14434 " public static void main(String[] args){\n" +
14435 " Test test = new Test();\n" +
14436 " test.foo(null,null, null);\n" +
14437 " }\n" +
14438 "}\n"},
14439 "----------\n" +
14440 "1. ERROR in Test.java (at line 10)\n" +
14441 " if (a== null) {}\n" +
14442 " ^\n" +
14443 "Redundant null check: The variable a can only be null at this location\n" +
14444 "----------\n",
14445 null,
14446 true,
14447 compilerOptions,
14448 "",
14449 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
14450 }
14451 }
14452
14453 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325342
14454 // Null warnings because of assert statements should not be suppressed
14455 // when CompilerOptions.OPTION_IncludeNullInfoFromAsserts is enabled.
14456 public void testBug325342b() {
14457 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14458 Map compilerOptions = getCompilerOptions();
14459 compilerOptions.put(CompilerOptions.OPTION_IncludeNullInfoFromAsserts, CompilerOptions.ENABLED);
14460 this.runNegativeTest(
14461 new String[] {
14462 "Test.java",
14463 "public class Test {\n" +
14464 " void foo(Object a, Object b, Object c) {\n" +
14465 " assert a == null;\n " +
14466 " if (a!=null) {\n" +
14467 " System.out.println(\"a is not null\");\n" +
14468 " } else{\n" +
14469 " System.out.println(\"a is null\");\n" +
14470 " }\n" +
14471 " assert b != null;\n " +
14472 " if (b!=null) {\n" +
14473 " System.out.println(\"a is not null\");\n" +
14474 " } else{\n" +
14475 " System.out.println(\"a is null\");\n" +
14476 " }\n" +
14477 " assert c == null;\n" +
14478 " if (c.equals(a)) {\n" +
14479 " System.out.println(\"\");\n" +
14480 " } else{\n" +
14481 " System.out.println(\"\");\n" +
14482 " }\n" +
14483 " }\n" +
14484 " public static void main(String[] args){\n" +
14485 " Test test = new Test();\n" +
14486 " test.foo(null,null,null);\n" +
14487 " }\n" +
14488 "}\n"},
14489 "----------\n" +
14490 "1. ERROR in Test.java (at line 4)\n" +
14491 " if (a!=null) {\n" +
14492 " ^\n" +
14493 "Null comparison always yields false: The variable a can only be null at this location\n" +
14494 "----------\n" +
14495 "2. WARNING in Test.java (at line 4)\n" +
14496 " if (a!=null) {\n" +
14497 " System.out.println(\"a is not null\");\n" +
14498 " } else{\n" +
14499 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
14500 "Dead code\n" +
14501 "----------\n" +
14502 "3. ERROR in Test.java (at line 10)\n" +
14503 " if (b!=null) {\n" +
14504 " ^\n" +
14505 "Redundant null check: The variable b cannot be null at this location\n" +
14506 "----------\n" +
14507 "4. WARNING in Test.java (at line 12)\n" +
14508 " } else{\n" +
14509 " System.out.println(\"a is null\");\n" +
14510 " }\n" +
14511 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
14512 "Dead code\n" +
14513 "----------\n" +
14514 "5. ERROR in Test.java (at line 16)\n" +
14515 " if (c.equals(a)) {\n" +
14516 " ^\n" +
14517 "Null pointer access: The variable c can only be null at this location\n" +
14518 "----------\n",
14519 null, true, compilerOptions, "",
14520 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
14521 }
14522 }
14523
14524 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325755
14525 // null analysis -- conditional expression
14526 public void testBug325755a() {
14527 this.runConformTest(
14528 new String[] {
14529 "X.java",
14530 "public class X {\n" +
14531 " public static Object foo(String s1, String s2) {\n" +
14532 " String local1 = s1;\n" +
14533 " String local2 = s2;\n" +
14534 " \n" +
14535 " String local3 = null;\n" +
14536 " if (local1 != null && local2 != null)\n" +
14537 " local3 = \"\"; //$NON-NLS-1$\n" +
14538 " else\n" +
14539 " local3 = local1 != null ? local1 : local2;\n" +
14540 "\n" +
14541 " if (local3 != null)\n" +
14542 " return new Integer(local3.length());\n" +
14543 " return null;\n" +
14544 " }\n" +
14545 " \n" +
14546 " public static void main(String[] args) {\n" +
14547 " System.out.print(foo(null, null));\n" +
14548 " System.out.print(foo(\"p1\", null));\n" +
14549 " System.out.print(foo(null, \"p2\"));\n" +
14550 " System.out.print(foo(\"p1\", \"p2\"));\n" +
14551 " }\n" +
14552 "}"},
14553 "null220");
14554 }
14555
14556 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=325755
14557 // null analysis -- conditional expression, many locals
14558 public void testBug325755b() {
14559 this.runConformTest(
14560 new String[] {
14561 "X.java",
14562 "public class X {\n" +
14563 " public static Object foo(String s1, String s2) {\n" +
14564 " int i00, i01, i02, i03, i04, i05, i06, i07, i08, i09;\n" +
14565 " int i10, i11, i12, i13, i14, i15, i16, i17, i18, i19;\n" +
14566 " int i20, i21, i22, i23, i24, i25, i26, i27, i28, i29;\n" +
14567 " int i30, i31, i32, i33, i34, i35, i36, i37, i38, i39;\n" +
14568 " int i40, i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" +
14569 " int i50, i51, i52, i53, i54, i55, i56, i57, i58, i59;\n" +
14570 " int i60, i61, i62, i63, i64, i65, i66, i67, i68, i69;\n" +
14571
14572 " String local1 = s1;\n" +
14573 " String local2 = s2;\n" +
14574 " \n" +
14575 " String local3 = null;\n" +
14576 " if (local1 != null && local2 != null)\n" +
14577 " local3 = \"\"; //$NON-NLS-1$\n" +
14578 " else\n" +
14579 " local3 = local1 != null ? local1 : local2;\n" +
14580 "\n" +
14581 " if (local3 != null)\n" +
14582 " return new Integer(local3.length());\n" +
14583 " return null;\n" +
14584 " }\n" +
14585 " \n" +
14586 " public static void main(String[] args) {\n" +
14587 " System.out.print(foo(null, null));\n" +
14588 " System.out.print(foo(\"p1\", null));\n" +
14589 " System.out.print(foo(null, \"p2\"));\n" +
14590 " System.out.print(foo(\"p1\", \"p2\"));\n" +
14591 " }\n" +
14592 "}"},
14593 "null220");
14594 }
14595 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332637
14596 // Dead Code detection removing code that isn't dead
14597 public void testBug332637() {
14598 if (this.complianceLevel < ClassFileConstants.JDK1_5)
14599 return;
14600 this.runConformTest(
14601 new String[] {
14602 "DeadCodeExample.java",
14603 "public class DeadCodeExample {\n" +
14604 "\n" +
14605 " private class CanceledException extends Exception {\n" +
14606 " }\n" +
14607 "\n" +
14608 " private interface ProgressMonitor {\n" +
14609 " boolean isCanceled();\n" +
14610 " }\n" +
14611 "\n" +
14612 " private void checkForCancellation(ProgressMonitor monitor)\n" +
14613 " throws CanceledException {\n" +
14614 " if (monitor.isCanceled()) {\n" +
14615 " throw new CanceledException();\n" +
14616 " }\n" +
14617 " }\n" +
14618 "\n" +
14619 " private int run() {\n" +
14620 "\n" +
14621 " ProgressMonitor monitor = new ProgressMonitor() {\n" +
14622 " private int i = 0;\n" +
14623 "\n" +
14624 " public boolean isCanceled() {\n" +
14625 " return (++i == 5);\n" +
14626 " }\n" +
14627 " };\n" +
14628 "\n" +
14629 " Integer number = null;\n" +
14630 "\n" +
14631 " try {\n" +
14632 " checkForCancellation(monitor);\n" +
14633 "\n" +
14634 " number = Integer.valueOf(0);\n" +
14635 "\n" +
14636 " for (String s : new String[10]) {\n" +
14637 " checkForCancellation(monitor);\n" +
14638 " number++;\n" +
14639 " }\n" +
14640 " return 0;\n" +
14641 " } catch (CanceledException e) {\n" +
14642 " System.out.println(\"Canceled after \" + number\n" +
14643 " + \" times through the loop\");\n" +
14644 " if (number != null) {\n" +
14645 " System.out.println(\"number = \" + number);\n" +
14646 " }\n" +
14647 " return -1;\n" +
14648 " }\n" +
14649 " }\n" +
14650 "\n" +
14651 " public static void main(String[] args) {\n" +
14652 " System.out.println(new DeadCodeExample().run());\n" +
14653 " }\n" +
14654 "}\n"
14655 },
14656 "Canceled after 3 times through the loop\n" +
14657 "number = 3\n" +
14658 "-1");
14659 }
14660 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332637
14661 // Dead Code detection removing code that isn't dead
14662 // variant with a finally block
14663 public void testBug332637b() {
14664 if (this.complianceLevel < ClassFileConstants.JDK1_5)
14665 return;
14666 this.runConformTest(
14667 new String[] {
14668 "DeadCodeExample.java",
14669 "public class DeadCodeExample {\n" +
14670 "\n" +
14671 " private class CanceledException extends Exception {\n" +
14672 " }\n" +
14673 "\n" +
14674 " private interface ProgressMonitor {\n" +
14675 " boolean isCanceled();\n" +
14676 " }\n" +
14677 "\n" +
14678 " private void checkForCancellation(ProgressMonitor monitor)\n" +
14679 " throws CanceledException {\n" +
14680 " if (monitor.isCanceled()) {\n" +
14681 " throw new CanceledException();\n" +
14682 " }\n" +
14683 " }\n" +
14684 "\n" +
14685 " private int run() {\n" +
14686 "\n" +
14687 " ProgressMonitor monitor = new ProgressMonitor() {\n" +
14688 " private int i = 0;\n" +
14689 "\n" +
14690 " public boolean isCanceled() {\n" +
14691 " return (++i == 5);\n" +
14692 " }\n" +
14693 " };\n" +
14694 "\n" +
14695 " Integer number = null;\n" +
14696 "\n" +
14697 " try {\n" +
14698 " checkForCancellation(monitor);\n" +
14699 "\n" +
14700 " number = Integer.valueOf(0);\n" +
14701 "\n" +
14702 " for (String s : new String[10]) {\n" +
14703 " checkForCancellation(monitor);\n" +
14704 " number++;\n" +
14705 " }\n" +
14706 " return 0;\n" +
14707 " } catch (CanceledException e) {\n" +
14708 " System.out.println(\"Canceled after \" + number\n" +
14709 " + \" times through the loop\");\n" +
14710 " if (number != null) {\n" +
14711 " System.out.println(\"number = \" + number);\n" +
14712 " }\n" +
14713 " return -1;\n" +
14714 " } finally {\n" +
14715 " System.out.println(\"Done\");\n" +
14716 " }\n" +
14717 " }\n" +
14718 "\n" +
14719 " public static void main(String[] args) {\n" +
14720 " System.out.println(new DeadCodeExample().run());\n" +
14721 " }\n" +
14722 "}\n"
14723 },
14724 "Canceled after 3 times through the loop\n" +
14725 "number = 3\n" +
14726 "Done\n" +
14727 "-1");
14728 }
14729
14730 public void testBug406160a() {
14731 if (this.complianceLevel < ClassFileConstants.JDK1_5)
14732 return;
14733 this.runConformTest(
14734 new String[] {
14735 "DeadCodeExample.java",
14736 "public class DeadCodeExample {\n" +
14737 "\n" +
14738 " class CanceledException extends Exception {\n" +
14739 " }\n" +
14740 "\n" +
14741 " private interface ProgressMonitor {\n" +
14742 " boolean isCanceled();\n" +
14743 " }\n" +
14744 "\n" +
14745 " private void checkForCancellation(ProgressMonitor monitor)\n" +
14746 " throws CanceledException {\n" +
14747 " if (monitor.isCanceled()) {\n" +
14748 " throw new CanceledException();\n" +
14749 " }\n" +
14750 " }\n" +
14751 "\n" +
14752 " private int run() {\n" +
14753 "\n" +
14754 " ProgressMonitor monitor = new ProgressMonitor() {\n" +
14755 " private int i = 0;\n" +
14756 "\n" +
14757 " public boolean isCanceled() {\n" +
14758 " return (++i == 5);\n" +
14759 " }\n" +
14760 " };\n" +
14761 "\n" +
14762 " Integer number = null;\n" +
14763 "\n" +
14764 " for (int j = 0; j < 1; ) {\n" +
14765 "\n" +
14766 " try {\n" +
14767 " checkForCancellation(monitor);\n" +
14768 "\n" +
14769 " number = Integer.valueOf(0);\n" +
14770 "\n" +
14771 " for (String s : new String[10]) {\n" +
14772 " checkForCancellation(monitor);\n" +
14773 " number++;\n" +
14774 " }\n" +
14775 " return 0;\n" +
14776 " } catch (CanceledException e) {\n" +
14777 " System.out.println(\"Canceled after \" + number\n" +
14778 " + \" times through the loop\");\n" +
14779 " if (number != null) {\n" +
14780 " System.out.println(\"number = \" + number);\n" +
14781 " }\n" +
14782 " return -1;\n" +
14783 " }\n" +
14784 " }\n" +
14785 " return 13;\n" +
14786 " }\n" +
14787 "\n" +
14788 " public static void main(String[] args) {\n" +
14789 " System.out.println(new DeadCodeExample().run());\n" +
14790 " }\n" +
14791 "}\n"
14792 },
14793 "Canceled after 3 times through the loop\n" +
14794 "number = 3\n" +
14795 "-1");
14796 }
14797
14798 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=333089
14799 // null analysis -- to make sure no AIOOBE or NPE is thrown while calling UnconditionalFlowInfo.markNullStatus(..)
14800 public void testBug333089() {
14801 this.runConformTest(
14802 new String[] {
14803 "X.java",
14804 "public class X {\n" +
14805 " public static void foo(Object s1) {\n" +
14806 " int i00, i01, i02, i03, i04, i05, i06, i07, i08, i09;\n" +
14807 " int i10, i11, i12, i13, i14, i15, i16, i17, i18, i19;\n" +
14808 " int i20, i21, i22, i23, i24, i25, i26, i27, i28, i29;\n" +
14809 " int i30, i31, i32, i33, i34, i35, i36, i37, i38, i39;\n" +
14810 " int i40, i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" +
14811 " int i50, i51, i52, i53, i54, i55, i56, i57, i58, i59;\n" +
14812 " int i60, i61, i62, i63, i64, i65, i66, i67, i68, i69;\n" +
14813 " Object local1;\n" +
14814 " if (s1 == null){}\n" +
14815 " try {" +
14816 " local1 = s1;\n" +
14817 " } finally {\n" +
14818 " }\n" +
14819 " }\n" +
14820 "}"},
14821 "");
14822 }
14823
14824 //Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14825 //original issue
14826 public void testBug336428() {
14827 this.runConformTest(
14828 new String[] {
14829 "DoWhileBug.java",
14830 "public class DoWhileBug {\n" +
14831 " void test(boolean b1, Object o1) {\n" +
14832 " Object o2 = new Object();\n" +
14833 " do {\n" +
14834 " if (b1)\n" +
14835 " o1 = null;\n" +
14836 " } while ((o2 = o1) != null);\n" +
14837 " }\n" +
14838 "}"
14839 },
14840 "");
14841 }
14842 //Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14843 //hitting the same implementation branch from within the loop
14844 //information from unknown o1 is not propagated into the loop, analysis currently believes o2 is def null.
14845 public void _testBug336428a() {
14846 this.runConformTest(
14847 new String[] {
14848 "DoWhileBug.java",
14849 "public class DoWhileBug {\n" +
14850 " void test(boolean b1, Object o1) {\n" +
14851 " Object o2 = null;\n" +
14852 " do {\n" +
14853 " if (b1)\n" +
14854 " o1 = null;\n" +
14855 " if ((o2 = o1) != null)\n" +
14856 " break;\n" +
14857 " } while (true);\n" +
14858 " }\n" +
14859 "}"
14860 },
14861 "");
14862 }
14863
14864 //Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14865 //in this variant the analysis believes o2 is def unknown and doesn't even consider raising a warning.
14866 public void _testBug336428b() {
14867 runNegativeNullTest(
14868 new String[] {
14869 "DoWhileBug.java",
14870 "public class DoWhileBug {\n" +
14871 " void test(boolean b1) {\n" +
14872 " Object o1 = null;\n" +
14873 " Object o2 = null;\n" +
14874 " do {\n" +
14875 " if ((o2 = o1) == null) break;\n" +
14876 " } while (true);\n" +
14877 " }\n" +
14878 "}"
14879 },
14880 "----------\n" +
14881 "1. ERROR in DoWhileBug.java (at line 6)\n" +
14882 " if ((o2 = o1) == null) break;\n" +
14883 " ^^^^^^^^^\n" +
14884 "Redundant null check: The variable o2 can only be null at this location\n" +
14885 "----------\n");
14886 }
14887
14888 //Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14889 //in this case considering o1 as unknown is correct
14890 public void testBug336428c() {
14891 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14892 this.runConformTest(
14893 new String[] {
14894 "DoWhileBug.java",
14895 "public class DoWhileBug {\n" +
14896 " void test(boolean b1, Object o1) {\n" +
14897 " Object o2 = null;\n" +
14898 " do {\n" +
14899 " if ((o2 = o1) == null) break;\n" +
14900 " } while (true);\n" +
14901 " }\n" +
14902 "}"
14903 },
14904 "");
14905 }
14906 }
14907
14908 //Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14909 //one more if-statement triggers the expected warnings
14910 public void testBug336428d() {
14911 runNegativeNullTest(
14912 new String[] {
14913 "DoWhileBug.java",
14914 "public class DoWhileBug {\n" +
14915 " void test(boolean b1) {\n" +
14916 " Object o1 = null;\n" +
14917 " Object o2 = null;\n" +
14918 " do {\n" +
14919 " if (b1)\n" +
14920 " o1 = null;\n" +
14921 " if ((o2 = o1) == null) break;\n" +
14922 " } while (true);\n" +
14923 " }\n" +
14924 "}"
14925 },
14926 "----------\n" +
14927 "1. ERROR in DoWhileBug.java (at line 7)\n" +
14928 " o1 = null;\n" +
14929 " ^^\n" +
14930 "Redundant assignment: The variable o1 can only be null at this location\n" +
14931 /* In general it's safer *not* to assume that o1 is null on every iteration (see also testBug336428d2):
14932 "----------\n" +
14933 "2. ERROR in DoWhileBug.java (at line 8)\n" +
14934 " if ((o2 = o1) == null) break;\n" +
14935 " ^^^^^^^^^\n" +
14936 "Redundant null check: The variable o2 can only be null at this location\n" +
14937 */
14938 "----------\n"
14939 );
14940 }
14941
14942 // Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14943 // variant after Bug 454031 to demonstrate:
14944 // - previously we would believe that o1 is always null in the assignment to o2 -> bogus warning re redundant null check
14945 // - with improved analysis we don't claim to know the value of o1 in this assignment -> no warning
14946 public void testBug336428d2() {
14947 this.runConformTest(
14948 new String[] {
14949 "DoWhileBug.java",
14950 "public class DoWhileBug {\n" +
14951 " void test(boolean b1) {\n" +
14952 " Object o1 = null;\n" +
14953 " Object o2 = null;\n" +
14954 " do {\n" +
14955 " if (b1)\n" +
14956 " o1 = null;\n" +
14957 " if ((o2 = o1) == null) System.out.println(\"null\");\n" +
14958 " o1 = new Object();\n" +
14959 " } while (true);\n" +
14960 " }\n" +
14961 "}"
14962 });
14963 }
14964
14965 //Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
14966 //same analysis, but assert instead of if suppresses the warning
14967 public void testBug336428e() {
14968 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
14969 runNegativeNullTest(
14970 new String[] {
14971 "DoWhileBug.java",
14972 "public class DoWhileBug {\n" +
14973 " void test(boolean b1) {\n" +
14974 " Object o1 = null;\n" +
14975 " Object o2 = null;\n" +
14976 " do {\n" +
14977 " if (b1)\n" +
14978 " o1 = null;\n" +
14979 " assert (o2 = o1) != null : \"bug\";\n" +
14980 " } while (true);\n" +
14981 " }\n" +
14982 "}"
14983 },
14984 "----------\n" +
14985 "1. ERROR in DoWhileBug.java (at line 7)\n" +
14986 " o1 = null;\n" +
14987 " ^^\n" +
14988 "Redundant assignment: The variable o1 can only be null at this location\n" +
14989 /* In general it's safer *not* to assume that o1 is null on every iteration:
14990 "----------\n" +
14991 "2. ERROR in DoWhileBug.java (at line 8)\n" +
14992 " assert (o2 = o1) != null : \"bug\";\n" +
14993 " ^^^^^^^^^\n" +
14994 "Null comparison always yields false: The variable o2 can only be null at this location\n" +
14995 */
14996 "----------\n");
14997 }
14998 }
14999
15000 // Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
15001 // same analysis, but assert instead of if suppresses the warning
15002 // condition inside assert is redundant null check and hence should not be warned against
15003 public void testBug336428f() {
15004 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
15005 runNegativeNullTest(
15006 new String[] {
15007 "DoWhileBug.java",
15008 "public class DoWhileBug {\n" +
15009 " void test(boolean b1) {\n" +
15010 " Object o1 = null;\n" +
15011 " Object o2 = null;\n" +
15012 " do {\n" +
15013 " if (b1)\n" +
15014 " o1 = null;\n" +
15015 " assert (o2 = o1) == null : \"bug\";\n" +
15016 " } while (true);\n" +
15017 " }\n" +
15018 "}"
15019 },
15020 "----------\n" +
15021 "1. ERROR in DoWhileBug.java (at line 7)\n" +
15022 " o1 = null;\n" +
15023 " ^^\n" +
15024 "Redundant assignment: The variable o1 can only be null at this location\n" +
15025 "----------\n");
15026 }
15027 }
15028
15029 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332838
15030 // Null info of assert statements should not affect flow info
15031 // when CompilerOptions.OPTION_IncludeNullInfoFromAsserts is disabled.
15032 public void testBug332838() {
15033 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
15034 Map compilerOptions = getCompilerOptions();
15035 compilerOptions.put(CompilerOptions.OPTION_IncludeNullInfoFromAsserts, CompilerOptions.DISABLED);
15036 this.runNegativeTest(
15037 new String[] {
15038 "Info.java",
15039 "public class Info {\n" +
15040 " public void test(Info[] infos) {\n" +
15041 " for (final Info info : infos) {\n " +
15042 " if (info != null) {\n" +
15043 " assert info.checkSomething();\n" +
15044 " info.doSomething();\n" + // no warning
15045 " }\n" +
15046 " }\n" +
15047 " for (final Info info : infos) {\n " +
15048 " if (info == null) {\n" +
15049 " assert info.checkSomething();\n" +
15050 " info.doSomething();\n" + // warn NPE, not pot. NPE
15051 " }\n" +
15052 " }\n" +
15053 " }\n" +
15054 " void doSomething() {}\n" +
15055 " boolean checkSomething() {return true;}\n" +
15056 "}\n"},
15057 "----------\n" +
15058 "1. ERROR in Info.java (at line 11)\n" +
15059 " assert info.checkSomething();\n" +
15060 " ^^^^\n" +
15061 "Null pointer access: The variable info can only be null at this location\n" +
15062 "----------\n" +
15063 "2. ERROR in Info.java (at line 12)\n" +
15064 " info.doSomething();\n" +
15065 " ^^^^\n" +
15066 "Null pointer access: The variable info can only be null at this location\n" +
15067 "----------\n",
15068 null,
15069 true,
15070 compilerOptions,
15071 "",
15072 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
15073 }
15074 }
15075
15076 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=336544
15077 public void testBug336544() {
15078 this.runConformTest(
15079 new String[] {
15080 "X.java",
15081 "public class X {\n" +
15082 " public static void main(String[] args) {\n" +
15083 " Integer i1 = getInt();\n" +
15084 " Integer i2 = i1 == null ? null : i1;\n" +
15085 " if (i2 != null) {\n" +
15086 " System.out.println(\"SUCCESS\");\n" +
15087 " return;\n" +
15088 " }\n" +
15089 " System.out.println(\"FAILURE\");\n" +
15090 " }\n" +
15091 " private static Integer getInt() {\n" +
15092 " return new Integer(0);\n" +
15093 " }\n" +
15094 "}"
15095 },
15096 "SUCCESS");
15097 }
15098
15099 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=336544
15100 public void testBug336544_2() {
15101 this.runConformTest(
15102 new String[] {
15103 "X.java",
15104 "public class X {\n" +
15105 " public static void main(String[] args) {\n" +
15106 " Integer i1 = null;\n" +
15107 " Integer i2 = (i1 = getInt()) == null ? null : i1;\n" +
15108 " if (i2 != null) {\n" +
15109 " System.out.println(\"SUCCESS\");\n" +
15110 " return;\n" +
15111 " }\n" +
15112 " System.out.println(\"FAILURE\");\n" +
15113 " }\n" +
15114 " private static Integer getInt() {\n" +
15115 " return new Integer(0);\n" +
15116 " }\n" +
15117 "}"
15118 },
15119 "SUCCESS");
15120 }
15121
15122 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=336544
15123 public void testBug336544_3() {
15124 this.runConformTest(
15125 new String[] {
15126 "X.java",
15127 "public class X {\n" +
15128 " public static void main(String[] args) {\n" +
15129 " Integer i1 = null;\n" +
15130 " Integer i2;\n" +
15131 " i2 = (i1 = getInt()) == null ? null : i1;\n" +
15132 " if (i2 != null) {\n" +
15133 " System.out.println(\"SUCCESS\");\n" +
15134 " return;\n" +
15135 " }\n" +
15136 " System.out.println(\"FAILURE\");\n" +
15137 " }\n" +
15138 " private static Integer getInt() {\n" +
15139 " return new Integer(0);\n" +
15140 " }\n" +
15141 "}"
15142 },
15143 "SUCCESS");
15144 }
15145
15146 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=313870
15147 public void testBug313870() {
15148 this.runConformTest(
15149 new String[] {
15150 "X.java",
15151 "public class X {\n" +
15152 " public static void main(String[] args) {\n" +
15153 " String s = \"\";\n" +
15154 " for (int i = 0; i < 2; i++) {\n" +
15155 " if (i != 0) { \n" +
15156 " s = test();\n" +
15157 " }\n" +
15158 " if (s == null) {\n" +
15159 " System.out.println(\"null\");\n" +
15160 " }\n" +
15161 " }\n" +
15162 " }\n" +
15163 " public static String test() {\n" +
15164 " return null;\n" +
15165 " }\n" +
15166 "}"
15167 },
15168 "null");
15169 }
15170
15171 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=313870
15172 public void testBug313870b() {
15173 this.runConformTest(
15174 new String[] {
15175 "X.java",
15176 "import java.io.BufferedReader;\n" +
15177 "import java.io.IOException;\n" +
15178 "public class X {\n" +
15179 " public void main(BufferedReader bufReader) throws IOException {\n" +
15180 " String line = \"\";\n" +
15181 " boolean doRead = false;\n" +
15182 " while (true) {\n" +
15183 " if (doRead) { \n" +
15184 " line = bufReader.readLine();\n" +
15185 " }\n" +
15186 " if (line == null) {\n" +
15187 " return;\n" +
15188 " }\n" +
15189 " doRead = true;\n" +
15190 " }\n" +
15191 " }\n" +
15192 "}"
15193 },
15194 "");
15195 }
15196
15197 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=313870
15198 public void testBug313870c() {
15199 this.runConformTest(
15200 new String[] {
15201 "X.java",
15202 "import java.io.File;\n" +
15203 "public class X {\n" +
15204 " public static void main(String[] args) {\n" +
15205 " boolean sometimes = (System.currentTimeMillis() & 1L) != 0L;\n" +
15206 " File file = new File(\"myfile\");\n" +
15207 " for (int i = 0; i < 2; i++) {\n" +
15208 " if (sometimes) { \n" +
15209 " file = getNewFile();\n" +
15210 " }\n" +
15211 " if (file == null) { \n" +
15212 " System.out.println(\"\");\n" +
15213 " }\n" +
15214 " }\n" +
15215 " }\n" +
15216 " private static File getNewFile() {\n" +
15217 " return null;\n" +
15218 " }\n" +
15219 "}"
15220 },
15221 "");
15222 }
15223 // https://bugs.eclipse.org/338303 - Warning about Redundant assignment conflicts with definite assignment
15224 public void testBug338303() {
15225 this.runConformTest(
15226 new String[] {
15227 "Bug338303.java",
15228 "import java.io.File;\n" +
15229 "import java.io.IOException;\n" +
15230 "\n" +
15231 "public class Bug338303 {\n" +
15232 " Object test(Object in, final File f) {\n" +
15233 " Object local;\n" +
15234 " try {\n" +
15235 " local = in;\n" +
15236 " if (local == null)\n" +
15237 " local = loadEntry(f, false);\n" +
15238 " } catch (final IOException e) {\n" +
15239 " e.printStackTrace();\n" +
15240 " local = null;\n" +
15241 " }\n" +
15242 " return local;\n" +
15243 " }\n" +
15244 "\n" +
15245 " private Object loadEntry(File f, boolean b) throws IOException {\n" +
15246 " throw new IOException();\n" +
15247 " }\n" +
15248 "}\n"
15249 },
15250 "");
15251 }
15252
15253 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=338234
15254 public void testBug338234() {
15255 runNegativeNullTest(
15256 new String[] {
15257 "X.java",
15258 "public class X {\n" +
15259 " static int foo() {\n" +
15260 " Object o = null;\n" +
15261 " int i = 0;\n" +
15262 " label: {\n" +
15263 " if (o == null)\n" +
15264 " break label;\n" +
15265 " i++;" +
15266 " }\n" +
15267 " if (i != 0) {\n" +
15268 " System.out.println(i);\n" +
15269 " }\n" +
15270 " return 0;\n" +
15271 " }\n" +
15272 "}\n"
15273 },
15274 "----------\n" +
15275 "1. ERROR in X.java (at line 6)\n" +
15276 " if (o == null)\n" +
15277 " ^\n" +
15278 "Redundant null check: The variable o can only be null at this location\n" +
15279 "----------\n" +
15280 "2. WARNING in X.java (at line 8)\n" +
15281 " i++; }\n" +
15282 " ^^^\n" +
15283 "Dead code\n" +
15284 "----------\n");
15285 }
15286 // Bug 324178 - [null] ConditionalExpression.nullStatus(..) doesn't take into account the analysis of condition itself
15287 public void testBug324178() {
15288 this.runConformTest(
15289 new String[] {
15290 "Bug324178.java",
15291 "public class Bug324178 {\n" +
15292 " boolean b;\n" +
15293 " void foo(Object u) {\n" +
15294 " if (u == null) {}\n" +
15295 " Object o = (u == null) ? new Object() : u;\n" +
15296 " o.toString(); // Incorrect potential NPE\n" +
15297 " }\n" +
15298 "}\n"
15299 },
15300 "");
15301 }
15302
15303 // Bug 324178 - [null] ConditionalExpression.nullStatus(..) doesn't take into account the analysis of condition itself
15304 public void testBug324178a() {
15305 this.runConformTest(
15306 new String[] {
15307 "Bug324178.java",
15308 "public class Bug324178 {\n" +
15309 " boolean b;\n" +
15310 " void foo(Boolean u) {\n" +
15311 " if (u == null) {}\n" +
15312 " Boolean o;\n" +
15313 " o = (u == null) ? Boolean.TRUE : u;\n" +
15314 " o.toString(); // Incorrect potential NPE\n" +
15315 " }\n" +
15316 "}\n"
15317 },
15318 "");
15319 }
15320 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=326950
15321 public void testBug326950a() throws Exception {
15322 Map options = getCompilerOptions();
15323 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
15324 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.WARNING);
15325 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
15326 this.runConformTest(
15327 new String[] {
15328 "X.java",
15329 "public class X {\n" +
15330 " public static void main(String[] args) {\n" +
15331 " String s = null;\n" +
15332 " if (s == null) {\n" +
15333 " System.out.println(\"SUCCESS\");\n" +
15334 " } else {\n" +
15335 " System.out.println(\"Dead code, but don't optimize me out\");\n" +
15336 " }\n" +
15337 " }\n" +
15338 "}",
15339 },
15340 "SUCCESS",
15341 null,
15342 true,
15343 null,
15344 options,
15345 null);
15346 String expectedOutput =
15347 " public static void main(java.lang.String[] args);\n" +
15348 " 0 aconst_null\n" +
15349 " 1 astore_1 [s]\n" +
15350 " 2 aload_1 [s]\n" +
15351 " 3 ifnonnull 17\n" +
15352 " 6 getstatic java.lang.System.out : java.io.PrintStream [16]\n" +
15353 " 9 ldc <String \"SUCCESS\"> [22]\n" +
15354 " 11 invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]\n" +
15355 " 14 goto 25\n" +
15356 " 17 getstatic java.lang.System.out : java.io.PrintStream [16]\n" +
15357 " 20 ldc <String \"Dead code, but don\'t optimize me out\"> [30]\n" +
15358 " 22 invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]\n" +
15359 " 25 return\n";
15360 checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
15361 }
15362 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=326950
15363 // Code marked dead due to if(false), etc. can be optimized out
15364 public void testBug326950b() throws Exception {
15365 Map options = getCompilerOptions();
15366 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
15367 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.WARNING);
15368 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
15369 this.runConformTest(
15370 new String[] {
15371 "X.java",
15372 "public class X {\n" +
15373 " public static void main(String[] args) {\n" +
15374 " int i = 0;\n" +
15375 " if (false) {\n" +
15376 " System.out.println(\"Deadcode and you can optimize me out\");\n" +
15377 " }\n" +
15378 " if (true) {\n" +
15379 " i++;\n" +
15380 " } else {\n" +
15381 " System.out.println(\"Deadcode and you can optimize me out\");\n" +
15382 " }\n" +
15383 " }\n" +
15384 "}",
15385 },
15386 "",
15387 null,
15388 true,
15389 null,
15390 options,
15391 null);
15392 String expectedOutput =
15393 " public static void main(java.lang.String[] args);\n" +
15394 " 0 iconst_0\n" +
15395 " 1 istore_1 [i]\n" +
15396 " 2 iinc 1 1 [i]\n" +
15397 " 5 return\n";
15398 checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
15399 }
15400 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=326950
15401 // Free return should be generated for a method even if it ends with dead code
15402 public void testBug326950c() throws Exception {
15403 Map options = getCompilerOptions();
15404 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
15405 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.WARNING);
15406 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
15407 this.runConformTest(
15408 new String[] {
15409 "X.java",
15410 "public class X {\n" +
15411 " public void foo(String[] args) {\n" +
15412 " String s = \"\";\n" +
15413 " int i = 0;\n" +
15414 " if (s != null) {\n" +
15415 " return;\n" +
15416 " }\n" +
15417 " i++;\n" +
15418 " }\n" +
15419 "}",
15420 },
15421 "",
15422 null,
15423 true,
15424 null,
15425 options,
15426 null);
15427 String expectedOutput =
15428 " public void foo(java.lang.String[] args);\n" +
15429 " 0 ldc <String \"\"> [16]\n" +
15430 " 2 astore_2 [s]\n" +
15431 " 3 iconst_0\n" +
15432 " 4 istore_3 [i]\n" +
15433 " 5 aload_2 [s]\n" +
15434 " 6 ifnull 10\n" +
15435 " 9 return\n" +
15436 " 10 iinc 3 1 [i]\n" +
15437 " 13 return\n";
15438 checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
15439 }
15440 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=326950
15441 // Free return should be generated for a constructor even if it ends with dead code
15442 public void testBug326950d() throws Exception {
15443 Map options = getCompilerOptions();
15444 options.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.WARNING);
15445 options.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.WARNING);
15446 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
15447 this.runConformTest(
15448 new String[] {
15449 "X.java",
15450 "public class X {\n" +
15451 " X() {\n" +
15452 " String s = \"\";\n" +
15453 " int i = 0;\n" +
15454 " if (s != null) {\n" +
15455 " return;\n" +
15456 " }\n" +
15457 " i++;\n" +
15458 " }\n" +
15459 "}",
15460 },
15461 "",
15462 null,
15463 true,
15464 null,
15465 options,
15466 null);
15467 String expectedOutput =
15468 " X();\n" +
15469 " 0 aload_0 [this]\n" +
15470 " 1 invokespecial java.lang.Object() [8]\n" +
15471 " 4 ldc <String \"\"> [10]\n" +
15472 " 6 astore_1 [s]\n" +
15473 " 7 iconst_0\n" +
15474 " 8 istore_2 [i]\n" +
15475 " 9 aload_1 [s]\n" +
15476 " 10 ifnull 14\n" +
15477 " 13 return\n" +
15478 " 14 iinc 2 1 [i]\n" +
15479 " 17 return\n";
15480 checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput);
15481 }
15482 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
15483 // Check code gen
15484 public void testBug339250() throws Exception {
15485 Map options = getCompilerOptions();
15486 options.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
15487 this.runConformTest(
15488 new String[] {
15489 "X.java",
15490 "public class X {\n" +
15491 " public static void main(String[] args) {\n" +
15492 " String s = null;\n" +
15493 " s += \"correctly\";\n" +
15494 " if (s != null) {\n" + // s cannot be null
15495 " System.out.println(\"It works \" + s);\n" +
15496 " }\n" +
15497 " }\n" +
15498 "}",
15499 },
15500 "It works nullcorrectly",
15501 null,
15502 true,
15503 null,
15504 options,
15505 null);
15506 }
15507 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
15508 // Check that the redundant null check warning is correctly produced
15509 public void testBug339250a() throws Exception {
15510 runNegativeNullTest(
15511 new String[] {
15512 "X.java",
15513 "public class X {\n" +
15514 " public static void main(String[] args) {\n" +
15515 " String s = null;\n" +
15516 " s += \"correctly\";\n" +
15517 " if (s != null) {\n" + // s cannot be null
15518 " System.out.println(\"It works \" + s);\n" +
15519 " }\n" +
15520 " }\n" +
15521 "}",
15522 },
15523 "----------\n" +
15524 "1. ERROR in X.java (at line 5)\n" +
15525 " if (s != null) {\n" +
15526 " ^\n" +
15527 "Redundant null check: The variable s cannot be null at this location\n" +
15528 "----------\n");
15529 }
15530 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=339250
15531 // Check that the redundant null check warning is correctly produced
15532 public void testBug339250b() throws Exception {
15533 runNegativeNullTest(
15534 new String[] {
15535 "X.java",
15536 "public class X {\n" +
15537 " public static void main(String[] args) {\n" +
15538 " String s = null;\n" +
15539 " s += null;\n" +
15540 " if (s != null) {\n" + // s is definitely not null
15541 " System.out.println(\"It works \" + s);\n" +
15542 " }\n" +
15543 " s = null;\n" +
15544 " if (s != null) {\n" + // s is definitely null
15545 " System.out.println(\"Fails \" + s);\n" +
15546 " } else {\n" +
15547 " System.out.println(\"Works second time too \" + s);\n" +
15548 " }\n" +
15549 " }\n" +
15550 "}",
15551 },
15552 "----------\n" +
15553 "1. ERROR in X.java (at line 5)\n" +
15554 " if (s != null) {\n" +
15555 " ^\n" +
15556 "Redundant null check: The variable s cannot be null at this location\n" +
15557 "----------\n" +
15558 "2. ERROR in X.java (at line 9)\n" +
15559 " if (s != null) {\n" +
15560 " ^\n" +
15561 "Null comparison always yields false: The variable s can only be null at this location\n" +
15562 "----------\n" +
15563 "3. WARNING in X.java (at line 9)\n" +
15564 " if (s != null) {\n" +
15565 " System.out.println(\"Fails \" + s);\n" +
15566 " } else {\n" +
15567 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
15568 "Dead code\n" +
15569 "----------\n");
15570 }
15571 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=342300
15572 public void testBug342300() throws Exception {
15573 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
15574 this.runConformTest(
15575 new String[] {
15576 "X.java",
15577 "public class X {\n" +
15578 " public static void initPattern(String p, Character escapeChar) {\n" +
15579 " int len = p.length();\n" +
15580 " for (int i = 0; i < len; i++) {\n" +
15581 " char c = p.charAt(i);\n" +
15582 " if (escapeChar != null && escapeChar == c) {\n" + // quiet
15583 " c = p.charAt(++i);\n" +
15584 " }\n" +
15585 " }\n" +
15586 " }\n" +
15587 "}",
15588 },
15589 "");
15590 }
15591 }
15592 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=342300
15593 // To make sure only the redundant null check is given and not a potential NPE
15594 public void testBug342300b() throws Exception {
15595 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
15596 runNegativeNullTest(
15597 new String[] {
15598 "X.java",
15599 "public class X {\n" +
15600 " public static void initPattern(String p, Character escapeChar) {\n" +
15601 " int len = p.length();\n" +
15602 " for (int i = 0; i < len; i++) {\n" +
15603 " char c = p.charAt(i);\n" +
15604 " if (escapeChar != null && escapeChar != null) {\n" + // look here
15605 " c = p.charAt(++i);\n" +
15606 " }\n" +
15607 " }\n" +
15608 " }\n" +
15609 "}",
15610 },
15611 "----------\n" +
15612 "1. ERROR in X.java (at line 6)\n" +
15613 " if (escapeChar != null && escapeChar != null) {\n" +
15614 " ^^^^^^^^^^\n" +
15615 "Redundant null check: The variable escapeChar cannot be null at this location\n" +
15616 "----------\n");
15617 }
15618 }
15619 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348379
15620 public void testBug348379a() throws Exception {
15621 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15622 runNegativeNullTest(
15623 new String[] {
15624 "X.java",
15625 "public class X {\n" +
15626 " public void foo() {\n" +
15627 " String s = null;\n" +
15628 " switch(s) {\n" +
15629 " case \"abcd\":\n" +
15630 " System.out.println(\"abcd\");\n" +
15631 " break;\n" +
15632 " default:\n" +
15633 " System.out.println(\"oops\");\n" +
15634 " break;\n" +
15635 " }\n" +
15636 " }\n" +
15637 "}",
15638 },
15639 "----------\n" +
15640 "1. ERROR in X.java (at line 4)\n" +
15641 " switch(s) {\n" +
15642 " ^\n" +
15643 "Null pointer access: The variable s can only be null at this location\n" +
15644 "----------\n");
15645 }
15646 }
15647 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348379
15648 public void testBug348379b() throws Exception {
15649 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15650 this.runConformTest(
15651 new String[] {
15652 "X.java",
15653 "public class X {\n" +
15654 " public static void main(String[] args) {\n" +
15655 " String s = \"abcd\";\n" +
15656 " switch(s) {\n" + // no warning since s is not null
15657 " case \"abcd\":\n" +
15658 " System.out.println(\"abcd\");\n" +
15659 " break;\n" +
15660 " default:\n" +
15661 " System.out.println(\"oops\");\n" +
15662 " break;\n" +
15663 " }\n" +
15664 " }\n" +
15665 "}",
15666 },
15667 "abcd");
15668 }
15669 }
15670 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348379
15671 public void testBug348379c() throws Exception {
15672 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15673 runNegativeNullTest(
15674 new String[] {
15675 "X.java",
15676 "public class X {\n" +
15677 " public void foo(String s) {\n" +
15678 " if (s == null) {}\n" + // tainting s
15679 " switch(s) {\n" +
15680 " case \"abcd\":\n" +
15681 " System.out.println(\"abcd\");\n" +
15682 " break;\n" +
15683 " default:\n" +
15684 " System.out.println(\"oops\");\n" +
15685 " break;\n" +
15686 " }\n" +
15687 " }\n" +
15688 "}",
15689 },
15690 "----------\n" +
15691 "1. ERROR in X.java (at line 4)\n" +
15692 " switch(s) {\n" +
15693 " ^\n" +
15694 "Potential null pointer access: The variable s may be null at this location\n" +
15695 "----------\n");
15696 }
15697 }
15698 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348379
15699 public void testBug348379d() throws Exception {
15700 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15701 runNegativeNullTest(
15702 new String[] {
15703 "X.java",
15704 "public class X {\n" +
15705 " public void foo(String s) {\n" +
15706 " if (s != null) {}\n" + // tainting s
15707 " switch(s) {\n" +
15708 " case \"abcd\":\n" +
15709 " System.out.println(\"abcd\");\n" +
15710 " break;\n" +
15711 " default:\n" +
15712 " System.out.println(\"oops\");\n" +
15713 " break;\n" +
15714 " }\n" +
15715 " }\n" +
15716 "}",
15717 },
15718 "----------\n" +
15719 "1. ERROR in X.java (at line 4)\n" +
15720 " switch(s) {\n" +
15721 " ^\n" +
15722 "Potential null pointer access: The variable s may be null at this location\n" +
15723 "----------\n");
15724 }
15725 }
15726 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348379
15727 public void testBug348379e() throws Exception {
15728 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15729 this.runNegativeTest(
15730 new String[] {
15731 "X.java",
15732 "public class X {\n" +
15733 " public void foo(String s) {\n" +
15734 " if (s == null) {}\n" + // tainting s
15735 " else\n" +
15736 " switch(s) {\n" + // no warning because we're inside else
15737 " case \"abcd\":\n" +
15738 " System.out.println(\"abcd\");\n" +
15739 " break;\n" +
15740 " default:\n" +
15741 " System.out.println(\"oops\");\n" +
15742 " break;\n" +
15743 " }\n" +
15744 " }\n" +
15745 "}",
15746 },
15747 "");
15748 }
15749 }
15750 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348379
15751 public void testBug348379f() throws Exception {
15752 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15753 runNegativeNullTest(
15754 new String[] {
15755 "X.java",
15756 "public class X {\n" +
15757 " public void foo(String s) {\n" +
15758 " s = null;\n" +
15759 " switch(s) {\n" +
15760 " case \"abcd\":\n" +
15761 " System.out.println(\"abcd\");\n" +
15762 " break;\n" +
15763 " default:\n" +
15764 " switch(s) {\n" + // do not warn again
15765 " case \"abcd\":\n" +
15766 " System.out.println(\"abcd\");\n" +
15767 " break;\n" +
15768 " default:\n" +
15769 " break;\n" +
15770 " }\n" +
15771 " }\n" +
15772 " }\n" +
15773 "}",
15774 },
15775 "----------\n" +
15776 "1. ERROR in X.java (at line 4)\n" +
15777 " switch(s) {\n" +
15778 " ^\n" +
15779 "Null pointer access: The variable s can only be null at this location\n" +
15780 "----------\n");
15781 }
15782 }
15783 // Bug 354554 - [null] conditional with redundant condition yields weak error message
15784 public void testBug354554() {
15785 runNegativeNullTest(
15786 new String[] {
15787 "Bug354554.java",
15788 "public class Bug354554{\n" +
15789 " void foo() {\n" +
15790 " Object u = new Object();\n" +
15791 " Object r = (u == null ? u : null);\n" + // condition is always false - should not spoil subsequent null-analysis
15792 " System.out.println(r.toString());\n" + // should strongly complain: r is definitely null
15793 " }\n" +
15794 "}\n"
15795 },
15796 "----------\n" +
15797 "1. ERROR in Bug354554.java (at line 4)\n" +
15798 " Object r = (u == null ? u : null);\n" +
15799 " ^\n" +
15800 "Null comparison always yields false: The variable u cannot be null at this location\n" +
15801 "----------\n" +
15802 "2. ERROR in Bug354554.java (at line 5)\n" +
15803 " System.out.println(r.toString());\n" +
15804 " ^\n" +
15805 "Null pointer access: The variable r can only be null at this location\n" +
15806 "----------\n");
15807 }
15808 //Bug 354554 - [null] conditional with redundant condition yields weak error message
15809 public void testBug354554b() {
15810 runNegativeNullTest(
15811 new String[] {
15812 "Bug354554.java",
15813 "public class Bug354554{\n" +
15814 " void foo() {\n" +
15815 " Object u = new Object();\n" +
15816 " Object r = (u != null ? u : null);\n" + // condition is always true - should not spoil subsequent null-analysis
15817 " System.out.println(r.toString());\n" + // don't complain: r is definitely non-null
15818 " }\n" +
15819 "}\n"
15820 },
15821 "----------\n" +
15822 "1. ERROR in Bug354554.java (at line 4)\n" +
15823 " Object r = (u != null ? u : null);\n" +
15824 " ^\n" +
15825 "Redundant null check: The variable u cannot be null at this location\n" +
15826 "----------\n");
15827 }
15828 // Bug 358827 - [1.7] exception analysis for t-w-r spoils null analysis
15829 public void test358827() {
15830 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15831 runNegativeNullTest(
15832 new String[] {
15833 "Bug358827.java",
15834 "import java.io.FileReader;\n" +
15835 "public class Bug358827 {\n" +
15836 " Object foo2() throws Exception {\n" +
15837 " String o = null;\n" +
15838 " try (FileReader rf = new FileReader(\"file\")){\n" +
15839 " o = o.toUpperCase();\n" +
15840 " } finally {\n" +
15841 " o = \"OK\";\n" +
15842 " }\n" +
15843 " return o;\n" +
15844 " }\n" +
15845 "}\n"
15846 },
15847 "----------\n" +
15848 "1. ERROR in Bug358827.java (at line 6)\n" +
15849 " o = o.toUpperCase();\n" +
15850 " ^\n" +
15851 "Null pointer access: The variable o can only be null at this location\n" +
15852 "----------\n");
15853 }
15854 }
15855 // Bug 367879 - Incorrect "Potential null pointer access" warning on statement after try-with-resources within try-finally
15856 public void test367879() {
15857 if (this.complianceLevel >= ClassFileConstants.JDK1_7) {
15858 this.runConformTest(
15859 new String[] {
15860 "Bug367879.java",
15861 "import java.io.IOException;\n" +
15862 "import java.io.InputStream;\n" +
15863 "import java.net.HttpURLConnection;\n" +
15864 "import java.net.URL;\n" +
15865 "public class Bug367879 {\n" +
15866 " public void test() throws IOException {\n" +
15867 " HttpURLConnection http = null;\n" +
15868 " try {\n" +
15869 " http = (HttpURLConnection) new URL(\"http://example.com/\").openConnection();\n" +
15870 " try (InputStream in = http.getInputStream()) { /* get input */ }\n" +
15871 " http.getURL();\n" + // shouldn't *not* flag as Potential null pointer access
15872 " } finally {\n" +
15873 " if (http != null) { http.disconnect(); }\n" +
15874 " }\n" +
15875 " }\n" +
15876 "}\n"
15877 },
15878 "");
15879 }
15880 }
15881 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=256796
15882 public void testBug256796() {
15883 Map compilerOptions = getCompilerOptions();
15884 compilerOptions.put(CompilerOptions.OPTION_ReportUnnecessaryElse, CompilerOptions.IGNORE);
15885 compilerOptions.put(CompilerOptions.OPTION_ReportDeadCode, CompilerOptions.WARNING);
15886 compilerOptions.put(CompilerOptions.OPTION_ReportDeadCodeInTrivialIfStatement, CompilerOptions.DISABLED);
15887 this.runNegativeTest(
15888 new String[] {
15889 "Bug.java",
15890 "public class Bug {\n" +
15891 " private static final boolean TRUE = true;\n" +
15892 " private static final boolean FALSE = false;\n" +
15893 " void foo() throws Exception {\n" +
15894 " if (TRUE) return;\n" +
15895 " else System.out.println(\"\");\n" +
15896 " System.out.println(\"\");\n" + // not dead code
15897 " if (TRUE) throw new Exception();\n" +
15898 " else System.out.println(\"\");\n" +
15899 " System.out.println(\"\");\n" + // not dead code
15900 " if (TRUE) return;\n" +
15901 " System.out.println(\"\");\n" + // not dead code
15902 " if (FALSE) System.out.println(\"\");\n" +
15903 " else return;\n" +
15904 " System.out.println(\"\");\n" + // not dead code
15905 " if (FALSE) return;\n" +
15906 " System.out.println(\"\");\n" + // not dead code
15907 " if (false) return;\n" + // dead code
15908 " System.out.println(\"\");\n" +
15909 " if (true) return;\n" +
15910 " System.out.println(\"\");\n" + // dead code
15911 " }\n" +
15912 "}\n"
15913 },
15914 "----------\n" +
15915 "1. WARNING in Bug.java (at line 18)\n" +
15916 " if (false) return;\n" +
15917 " ^^^^^^^\n" +
15918 "Dead code\n" +
15919 "----------\n" +
15920 "2. WARNING in Bug.java (at line 21)\n" +
15921 " System.out.println(\"\");\n" +
15922 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
15923 "Dead code\n" +
15924 "----------\n",
15925 null,
15926 true,
15927 compilerOptions,
15928 null,
15929 JavacTestOptions.Excuse.EclipseHasSomeMoreWarnings);
15930 }
15931 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=256796
15932 public void testBug256796a() {
15933 Map compilerOptions = getCompilerOptions();
15934 compilerOptions.put(CompilerOptions.OPTION_ReportUnnecessaryElse, CompilerOptions.IGNORE);
15935 compilerOptions.put(CompilerOptions.OPTION_ReportDeadCode, CompilerOptions.WARNING);
15936 compilerOptions.put(CompilerOptions.OPTION_ReportDeadCodeInTrivialIfStatement, CompilerOptions.ENABLED);
15937 this.runNegativeTest(
15938 new String[] {
15939 "Bug.java",
15940 "public class Bug {\n" +
15941 " private static final boolean TRUE = true;\n" +
15942 " private static final boolean FALSE = false;\n" +
15943 " void foo() throws Exception {\n" +
15944 " if (TRUE) return;\n" +
15945 " else System.out.println(\"\");\n" +
15946 " System.out.println(\"\");\n" + // dead code
15947 " }\n" +
15948 " void foo2() {\n" +
15949 " if (TRUE) return;\n" +
15950 " System.out.println(\"\");\n" + // dead code
15951 " }\n" +
15952 " void foo3() throws Exception {\n" +
15953 " if (TRUE) throw new Exception();\n" +
15954 " else System.out.println(\"\");\n" + // dead code
15955 " System.out.println(\"\");\n" + // dead code
15956 " }\n" +
15957 " void foo4() throws Exception {\n" +
15958 " if (FALSE) System.out.println(\"\");\n" +
15959 " else return;\n" +
15960 " System.out.println(\"\");\n" + // dead code
15961 " }\n" +
15962 " void foo5() throws Exception {\n" +
15963 " if (FALSE) return;\n" + // dead code
15964 " System.out.println(\"\");\n" +
15965 " }\n" +
15966 " void foo6() throws Exception {\n" +
15967 " if (false) return;\n" + // dead code
15968 " System.out.println(\"\");\n" +
15969 " if (true) return;\n" +
15970 " System.out.println(\"\");\n" + // dead code
15971 " }\n" +
15972 "}\n"
15973 },
15974 "----------\n" +
15975 "1. WARNING in Bug.java (at line 6)\n" +
15976 " else System.out.println(\"\");\n" +
15977 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
15978 "Dead code\n" +
15979 "----------\n" +
15980 "2. WARNING in Bug.java (at line 7)\n" +
15981 " System.out.println(\"\");\n" +
15982 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
15983 "Dead code\n" +
15984 "----------\n" +
15985 "3. WARNING in Bug.java (at line 11)\n" +
15986 " System.out.println(\"\");\n" +
15987 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
15988 "Dead code\n" +
15989 "----------\n" +
15990 "4. WARNING in Bug.java (at line 15)\n" +
15991 " else System.out.println(\"\");\n" +
15992 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
15993 "Dead code\n" +
15994 "----------\n" +
15995 "5. WARNING in Bug.java (at line 16)\n" +
15996 " System.out.println(\"\");\n" +
15997 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
15998 "Dead code\n" +
15999 "----------\n" +
16000 "6. WARNING in Bug.java (at line 19)\n" +
16001 " if (FALSE) System.out.println(\"\");\n" +
16002 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
16003 "Dead code\n" +
16004 "----------\n" +
16005 "7. WARNING in Bug.java (at line 21)\n" +
16006 " System.out.println(\"\");\n" +
16007 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
16008 "Dead code\n" +
16009 "----------\n" +
16010 "8. WARNING in Bug.java (at line 24)\n" +
16011 " if (FALSE) return;\n" +
16012 " ^^^^^^^\n" +
16013 "Dead code\n" +
16014 "----------\n" +
16015 "9. WARNING in Bug.java (at line 28)\n" +
16016 " if (false) return;\n" +
16017 " ^^^^^^^\n" +
16018 "Dead code\n" +
16019 "----------\n" +
16020 "10. WARNING in Bug.java (at line 31)\n" +
16021 " System.out.println(\"\");\n" +
16022 " ^^^^^^^^^^^^^^^^^^^^^^\n" +
16023 "Dead code\n" +
16024 "----------\n",
16025 null,
16026 true,
16027 compilerOptions,
16028 "",
16029 JavacTestOptions.Excuse.EclipseHasSomeMoreWarnings);
16030 }
16031 // Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop)
16032 public void testBug360328() {
16033 Map customOptions = getCompilerOptions();
16034 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
16035 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
16036 runNegativeTest(
16037 true, /* flushOutputDir */
16038 new String[] {
16039 "X.java",
16040 "public class X {\n" +
16041 " void print4() {\n" +
16042 " final String s1 = \"\";\n" +
16043 " for (int i=0; i<4; i++)\n" +
16044 " new Runnable() {\n" +
16045 " public void run() {\n" +
16046 " if (s1 != null)\n" +
16047 " s1.toString();\n" +
16048 " }\n" +
16049 " }.run();\n" +
16050 " }\n" +
16051 " void print16(boolean b) {\n" +
16052 " final String s3 = b ? null : \"\";\n" +
16053 " for (int i=0; i<16; i++)\n" +
16054 " new Runnable() {\n" +
16055 " public void run() {\n" +
16056 " s3.toString();\n" +
16057 " }\n" +
16058 " }.run();\n" +
16059 " }\n" +
16060 " void print23() {\n" +
16061 " final String s23 = null;\n" +
16062 " for (int i=0; i<23; i++)\n" +
16063 " new Runnable() {\n" +
16064 " public void run() {\n" +
16065 " s23.toString();\n" +
16066 " }\n" +
16067 " }.run();\n" +
16068 " }\n" +
16069 "}\n",
16070
16071 },
16072 null, /* classLibs */
16073 customOptions,
16074 "----------\n" +
16075 "1. ERROR in X.java (at line 7)\n" +
16076 " if (s1 != null)\n" +
16077 " ^^\n" +
16078 "Redundant null check: The variable s1 cannot be null at this location\n" +
16079 "----------\n" +
16080 "2. ERROR in X.java (at line 17)\n" +
16081 " s3.toString();\n" +
16082 " ^^\n" +
16083 "Potential null pointer access: The variable s3 may be null at this location\n" +
16084 "----------\n" +
16085 "3. ERROR in X.java (at line 26)\n" +
16086 " s23.toString();\n" +
16087 " ^^^\n" +
16088 "Null pointer access: The variable s23 can only be null at this location\n" +
16089 "----------\n",
16090 "",/* expected output */
16091 "",/* expected error */
16092 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
16093 }
16094 // Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop)
16095 // constructors
16096 public void testBug360328b() {
16097 Map customOptions = getCompilerOptions();
16098 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
16099 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
16100 runNegativeTest(
16101 true, /* flushOutputDir */
16102 new String[] {
16103 "X.java",
16104 "public class X {\n" +
16105 " void print4() {\n" +
16106 " final String s1 = \"\";\n" +
16107 " for (int i=0; i<4; i++) {\n" +
16108 " class R {\n" +
16109 " public R() {\n" +
16110 " if (s1 != null)\n" +
16111 " s1.toString();\n" +
16112 " }\n" +
16113 " };\n" +
16114 " new R();\n" +
16115 " }\n" +
16116 " }\n" +
16117 " void print16(boolean b) {\n" +
16118 " final String s3 = b ? null : \"\";\n" +
16119 " int i=0; while (i++<16) {\n" +
16120 " class R {\n" +
16121 " public R() {\n" +
16122 " s3.toString();\n" +
16123 " }\n" +
16124 " };\n" +
16125 " new R();\n" +
16126 " };\n" +
16127 " }\n" +
16128 " void print23() {\n" +
16129 " final String s23 = null;\n" +
16130 " for (int i=0; i<23; i++) {\n" +
16131 " class R {\n" +
16132 " public R() {\n" +
16133 " s23.toString();\n" +
16134 " }\n" +
16135 " };\n" +
16136 " new R();\n" +
16137 " };\n" +
16138 " }\n" +
16139 "}\n",
16140
16141 },
16142 null, /* classLibs */
16143 customOptions,
16144 "----------\n" +
16145 "1. ERROR in X.java (at line 7)\n" +
16146 " if (s1 != null)\n" +
16147 " ^^\n" +
16148 "Redundant null check: The variable s1 cannot be null at this location\n" +
16149 "----------\n" +
16150 "2. ERROR in X.java (at line 19)\n" +
16151 " s3.toString();\n" +
16152 " ^^\n" +
16153 "Potential null pointer access: The variable s3 may be null at this location\n" +
16154 "----------\n" +
16155 "3. ERROR in X.java (at line 30)\n" +
16156 " s23.toString();\n" +
16157 " ^^^\n" +
16158 "Null pointer access: The variable s23 can only be null at this location\n" +
16159 "----------\n",
16160 "",/* expected output */
16161 "",/* expected error */
16162 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
16163 }
16164 // Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop)
16165 // initializers
16166 public void testBug360328c() {
16167 Map customOptions = getCompilerOptions();
16168 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
16169 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
16170 customOptions.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, CompilerOptions.IGNORE);
16171 runNegativeTest(
16172 true, /* flushOutputDir */
16173 new String[] {
16174 "X.java",
16175 "public class X {\n" +
16176 " void print4() {\n" +
16177 " final String s1 = \"\";\n" +
16178 " for (int i=0; i<4; i++) {\n" +
16179 " class R {\n" +
16180 " String s1R;\n" +
16181 " {\n" +
16182 " if (s1 != null)\n" +
16183 " s1R = s1;\n" +
16184 " }\n" +
16185 " };\n" +
16186 " new R();\n" +
16187 " }\n" +
16188 " }\n" +
16189 " void print16(boolean b) {\n" +
16190 " final String s3 = b ? null : \"\";\n" +
16191 " for (int i=0; i<16; i++) {\n" +
16192 " class R {\n" +
16193 " String s3R = s3.toString();\n" +
16194 " };\n" +
16195 " new R();\n" +
16196 " };\n" +
16197 " }\n" +
16198 " void print23() {\n" +
16199 " final String s23 = null;\n" +
16200 " for (int i=0; i<23; i++) {\n" +
16201 " class R {\n" +
16202 " String s23R;\n" +
16203 " {\n" +
16204 " s23R = s23.toString();\n" +
16205 " }\n" +
16206 " };\n" +
16207 " new R();\n" +
16208 " };\n" +
16209 " }\n" +
16210 "}\n",
16211
16212 },
16213 null, /* classLibs */
16214 customOptions,
16215 "----------\n" +
16216 "1. ERROR in X.java (at line 8)\n" +
16217 " if (s1 != null)\n" +
16218 " ^^\n" +
16219 "Redundant null check: The variable s1 cannot be null at this location\n" +
16220 "----------\n" +
16221 "2. ERROR in X.java (at line 19)\n" +
16222 " String s3R = s3.toString();\n" +
16223 " ^^\n" +
16224 "Potential null pointer access: The variable s3 may be null at this location\n" +
16225 "----------\n" +
16226 "3. ERROR in X.java (at line 30)\n" +
16227 " s23R = s23.toString();\n" +
16228 " ^^^\n" +
16229 "Null pointer access: The variable s23 can only be null at this location\n" +
16230 "----------\n",
16231 "",/* expected output */
16232 "",/* expected error */
16233 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
16234 }
16235 // Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop)
16236 // try-finally instead of loop
16237 public void testBug360328d() {
16238 Map customOptions = getCompilerOptions();
16239 customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR);
16240 customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
16241 runNegativeTest(
16242 true, /* flushOutputDir */
16243 new String[] {
16244 "X.java",
16245 "public class X {\n" +
16246 " void print4() {\n" +
16247 " final String s1 = \"\";\n" +
16248 " try { } finally {\n" +
16249 " new Runnable() {\n" +
16250 " public void run() {\n" +
16251 " if (s1 != null)\n" +
16252 " s1.toString();\n" +
16253 " }\n" +
16254 " }.run();\n" +
16255 " }\n" +
16256 " }\n" +
16257 " void print16(boolean b) {\n" +
16258 " final String s3 = b ? null : \"\";\n" +
16259 " try { } finally {\n" +
16260 " new Runnable() {\n" +
16261 " public void run() {\n" +
16262 " s3.toString();\n" +
16263 " }\n" +
16264 " }.run();\n" +
16265 " }\n" +
16266 " }\n" +
16267 " void print23() {\n" +
16268 " final String s23 = null;\n" +
16269 " try { } finally {\n" +
16270 " new Runnable() {\n" +
16271 " public void run() {\n" +
16272 " s23.toString();\n" +
16273 " }\n" +
16274 " }.run();\n" +
16275 " }\n" +
16276 " }\n" +
16277 "}\n",
16278
16279 },
16280 null, /* classLibs */
16281 customOptions,
16282 "----------\n" +
16283 "1. ERROR in X.java (at line 7)\n" +
16284 " if (s1 != null)\n" +
16285 " ^^\n" +
16286 "Redundant null check: The variable s1 cannot be null at this location\n" +
16287 "----------\n" +
16288 "2. ERROR in X.java (at line 18)\n" +
16289 " s3.toString();\n" +
16290 " ^^\n" +
16291 "Potential null pointer access: The variable s3 may be null at this location\n" +
16292 "----------\n" +
16293 "3. ERROR in X.java (at line 28)\n" +
16294 " s23.toString();\n" +
16295 " ^^^\n" +
16296 "Null pointer access: The variable s23 can only be null at this location\n" +
16297 "----------\n",
16298 "",/* expected output */
16299 "",/* expected error */
16300 JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
16301 }
16302 // Bug 384380 - False positive on a "Potential null pointer access" after a continue
16303 // original test case
16304 public void testBug384380() {
16305 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
16306 this.runConformTest(
16307 new String[] {
16308 "Test.java",
16309 "public class Test {\n" +
16310 " public static class Container{\n" +
16311 " public int property;\n" +
16312 " }\n" +
16313 " public static class CustomException extends Exception {\n" +
16314 " private static final long serialVersionUID = 1L;\n" +
16315 " }\n" +
16316 " public static void anotherMethod() throws CustomException {}\n" +
16317 "\n" +
16318 " public static void method(final java.util.List<Container> list) {\n" +
16319 " for (final Container c : list) {\n" +
16320 " if(c == null)\n" +
16321 " continue; // return or break, are fine though\n" +
16322 "\n" +
16323 " // without this try-catch+for+exception block it does not fails\n" +
16324 " try {\n" +
16325 " for(int i = 0; i < 10 ; i++) // needs a loop here (a 'while' or a 'for') to fail\n" +
16326 " anotherMethod(); // throwing directly CustomException make it fails too\n" +
16327 " } catch (final CustomException e) {\n" +
16328 " // it fails even if catch is empty\n" +
16329 " }\n" +
16330 " c.property += 1; // \"Potential null pointer access: The variable c may be null at this location\"\n" +
16331 " }\n" +
16332 "\n" +
16333 " }\n" +
16334 "}\n"
16335 },
16336 "");
16337 }
16338 }
16339 // Bug 384380 - False positive on a "Potential null pointer access" after a continue
16340 // variant with a finally block
16341 public void testBug384380_a() {
16342 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
16343 this.runConformTest(
16344 new String[] {
16345 "Test.java",
16346 "public class Test {\n" +
16347 " public static class Container{\n" +
16348 " public int property;\n" +
16349 " }\n" +
16350 " public static class CustomException extends Exception {\n" +
16351 " private static final long serialVersionUID = 1L;\n" +
16352 " }\n" +
16353 " public static void anotherMethod() throws CustomException {}\n" +
16354 "\n" +
16355 " public static void method(final java.util.List<Container> list) {\n" +
16356 " for (final Container c : list) {\n" +
16357 " if(c == null)\n" +
16358 " continue; // return or break, are fine though\n" +
16359 "\n" +
16360 " // without this try-catch+for+exception block it does not fails\n" +
16361 " try {\n" +
16362 " for(int i = 0; i < 10 ; i++) // needs a loop here (a 'while' or a 'for') to fail\n" +
16363 " anotherMethod(); // throwing directly CustomException make it fails too\n" +
16364 " } catch (final CustomException e) {\n" +
16365 " // it fails even if catch is empty\n" +
16366 " } finally {\n" +
16367 " System.out.print(3);\n" +
16368 " }\n" +
16369 " c.property += 1; // \"Potential null pointer access: The variable c may be null at this location\"\n" +
16370 " }\n" +
16371 "\n" +
16372 " }\n" +
16373 "}\n"
16374 },
16375 "");
16376 }
16377 }
16378 // Bug 384380 - False positive on a "Potential null pointer access" after a continue
16379 // while & foreach loops
16380 public void testBug384380_b() {
16381 if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
16382 this.runConformTest(
16383 new String[] {
16384 "Test.java",
16385 "public class Test {\n" +
16386 " public static class Container{\n" +
16387 " public int property;\n" +
16388 " }\n" +
16389 " public static class CustomException extends Exception {\n" +
16390 " private static final long serialVersionUID = 1L;\n" +
16391 " }\n" +
16392 " public static void anotherMethod() throws CustomException {}\n" +
16393 "\n" +
16394 " public static void method(final java.util.List<Container> list) {\n" +
16395 " java.util.Iterator<Container> it = list.iterator();\n" +
16396 " while (it.hasNext()) {\n" +
16397 " final Container c = it.next();\n" +
16398 " if(c == null)\n" +
16399 " continue; // return or break, are fine though\n" +
16400 "\n" +
16401 " // without this try-catch+for+exception block it does not fails\n" +
16402 " try {\n" +
16403 " for(Container c1 : list) // needs a loop here (a 'while' or a 'for') to fail\n" +
16404 " anotherMethod(); // throwing directly CustomException make it fails too\n" +
16405 " } catch (final CustomException e) {\n" +
16406 " // it fails even if catch is empty\n" +
16407 " }\n" +
16408 " c.property += 1; // \"Potential null pointer access: The variable c may be null at this location\"\n" +
16409 " }\n" +
16410 "\n" +
16411 " }\n" +
16412 "}\n"
16413 },
16414 "");
16415 }
16416 }
16417 public void testBug376263() {
16418 Map customOptions = getCompilerOptions();
16419 customOptions.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR);
16420 runConformTest(
16421 new String[] {
16422 "Test.java",
16423 "public class Test {\n" +
16424 " private int x;\n" +
16425 "\n" +
16426 " static void test(Test[] array) {\n" +
16427 " Test elem = null;\n" +
16428 " int i = 0;\n" +
16429 " while (i < array.length) {\n" +
16430 " if (i == 0) {\n" +
16431 " elem = array[0];\n" +
16432 " }\n" +
16433 " if (elem != null) {\n" +
16434 " while (true) {\n" +
16435 " if (elem.x >= 0 || i >= array.length) { // should not warn here\n" +
16436 " break;\n" +
16437 " }\n" +
16438 " elem = array[i++];\n" +
16439 " }\n" +
16440 " }\n" +
16441 " }\n" +
16442 " }\n" +
16443 "}"
16444 },
16445 "",
16446 null/*classLibraries*/,
16447 true/*shouldFlush*/,
16448 null/*vmArgs*/,
16449 customOptions,
16450 null/*requestor*/);
16451 }
16452 //object/array allocation
16453 public void testExpressions01() {
16454 runNegativeNullTest(
16455 new String[] {
16456 "X.java",
16457 "public class X {\n" +
16458 " void foo() {\n" +
16459 " if (new Object() == null)\n" +
16460 " System.out.println(\"null\");\n" +
16461 " }\n" +
16462 " void goo() {\n" +
16463 " if (null != this.new I())\n" +
16464 " System.out.println(\"nonnull\");\n" +
16465 " }\n" +
16466 " void hoo() {\n" +
16467 " if (null != new Object[3])\n" +
16468 " System.out.println(\"nonnull\");\n" +
16469 " }\n" +
16470 " class I {}\n" +
16471 "}\n"
16472 },
16473 "----------\n" +
16474 "1. ERROR in X.java (at line 3)\n" +
16475 " if (new Object() == null)\n" +
16476 " ^^^^^^^^^^^^\n" +
16477 "Null comparison always yields false: this expression cannot be null\n" +
16478 "----------\n" +
16479 "2. WARNING in X.java (at line 4)\n" +
16480 " System.out.println(\"null\");\n" +
16481 " ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
16482 "Dead code\n" +
16483 "----------\n" +
16484 "3. ERROR in X.java (at line 7)\n" +
16485 " if (null != this.new I())\n" +
16486 " ^^^^^^^^^^^^\n" +
16487 "Redundant null check: this expression cannot be null\n" +
16488 "----------\n" +
16489 "4. ERROR in X.java (at line 11)\n" +
16490 " if (null != new Object[3])\n" +
16491 " ^^^^^^^^^^^^^\n" +
16492 "Redundant null check: this expression cannot be null\n" +
16493 "----------\n"
16494 );
16495 }
16496 //'this' expressions (incl. qualif.)
16497 public void testExpressions02() {
16498 runNegativeNullTest(
16499 new String[] {
16500 "X.java",
16501 "public class X {\n" +
16502 " void foo() {\n" +
16503 " if (this == null)\n" +
16504 " System.out.println(\"null\");\n" +
16505 " }\n" +
16506 " class I {\n" +
16507 " void goo() {\n" +
16508 " if (null != X.this)\n" +
16509 " System.out.println(\"nonnull\");\n" +
16510 " }\n" +
16511 " }\n" +
16512 "}\n"
16513 },
16514 "----------\n" +
16515 "1. ERROR in X.java (at line 3)\n" +
16516 " if (this == null)\n" +
16517 " ^^^^\n" +
16518 "Null comparison always yields false: this expression cannot be null\n" +
16519 "----------\n" +
16520 "2. WARNING in X.java (at line 4)\n" +
16521 " System.out.println(\"null\");\n" +
16522 " ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
16523 "Dead code\n" +
16524 "----------\n" +
16525 "3. ERROR in X.java (at line 8)\n" +
16526 " if (null != X.this)\n" +
16527 " ^^^^^^\n" +
16528 "Redundant null check: this expression cannot be null\n" +
16529 "----------\n"
16530 );
16531 }
16532 //various non-null expressions: class-literal, string-literal, casted 'this'
16533 public void testExpressions03() {
16534 runNegativeNullTest(
16535 new String[] {
16536 "X.java",
16537 "public class X {\n" +
16538 " void foo() {\n" +
16539 " if (X.class == null)\n" +
16540 " System.out.println(\"null\");\n" +
16541 " }\n" +
16542 " void goo() {\n" +
16543 " if (null != \"STRING\")\n" +
16544 " System.out.println(\"nonnull\");\n" +
16545 " if (null == (Object)this)\n" +
16546 " System.out.println(\"I'm null\");\n" +
16547 " }\n" +
16548 "}\n"
16549 },
16550 "----------\n" +
16551 "1. ERROR in X.java (at line 3)\n" +
16552 " if (X.class == null)\n" +
16553 " ^^^^^^^\n" +
16554 "Null comparison always yields false: this expression cannot be null\n" +
16555 "----------\n" +
16556 "2. WARNING in X.java (at line 4)\n" +
16557 " System.out.println(\"null\");\n" +
16558 " ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
16559 "Dead code\n" +
16560 "----------\n" +
16561 "3. ERROR in X.java (at line 7)\n" +
16562 " if (null != \"STRING\")\n" +
16563 " ^^^^^^^^\n" +
16564 "Redundant null check: this expression cannot be null\n" +
16565 "----------\n" +
16566 "4. ERROR in X.java (at line 9)\n" +
16567 " if (null == (Object)this)\n" +
16568 " ^^^^^^^^^^^^\n" +
16569 "Null comparison always yields false: this expression cannot be null\n" +
16570 "----------\n" +
16571 "5. WARNING in X.java (at line 10)\n" +
16572 " System.out.println(\"I\'m null\");\n" +
16573 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
16574 "Dead code\n" +
16575 "----------\n"
16576 );
16577 }
16578
16579 //a non-null ternary expression
16580 public void testExpressions04() {
16581 runNegativeNullTest(
16582 new String[] {
16583 "X.java",
16584 "public class X {\n" +
16585 " void foo(boolean b) {\n" +
16586 " Object o1 = new Object();\n" +
16587 " Object o2 = new Object();\n" +
16588 " if ((b ? o1 : o2) != null)\n" +
16589 " System.out.println(\"null\");\n" +
16590 " }\n" +
16591 "}\n"
16592 },
16593 "----------\n" +
16594 "1. ERROR in X.java (at line 5)\n" +
16595 " if ((b ? o1 : o2) != null)\n" +
16596 " ^^^^^^^^^^^^^\n" +
16597 "Redundant null check: this expression cannot be null\n" +
16598 "----------\n"
16599 );
16600 }
16601
16602 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16603 // simplified: only try-finally involved
16604 public void testBug345305_1() {
16605 runConformTest(
16606 new String[] {
16607 "X.java",
16608 "public class X {\n" +
16609 " void foo() {\n" +
16610 " String s = null;\n" +
16611 " try {\n" +
16612 " s = \"hi\";\n" +
16613 " } finally {\n" +
16614 " s.length();\n" +
16615 " s = null;\n" +
16616 " }\n" +
16617 " }\n" +
16618 "}\n"
16619 });
16620 }
16621
16622 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16623 // original test case
16624 public void testBug345305_2() {
16625 runConformTest(
16626 new String[] {
16627 "X.java",
16628 "public class X {\n" +
16629 " void foo() {\n" +
16630 " String s = null;\n" +
16631 " while (true) {\n" +
16632 " try {\n" +
16633 " s = \"hi\";\n" +
16634 " }\n" +
16635 " finally {\n" +
16636 " s.length();\n" +
16637 " s = null;\n" +
16638 " }\n" +
16639 " }\n" +
16640 " }\n" +
16641 "}\n"
16642 });
16643 }
16644
16645 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16646 // assignment in method argument position
16647 public void testBug345305_3() {
16648 runConformTest(
16649 new String[] {
16650 "X.java",
16651 "public class X {\n" +
16652 " void foo() {\n" +
16653 " String s = null;\n" +
16654 " while (true) {\n" +
16655 " try {\n" +
16656 " check(s = \"hi\");\n" +
16657 " }\n" +
16658 " finally {\n" +
16659 " s.length();\n" +
16660 " s = null;\n" +
16661 " }\n" +
16662 " }\n" +
16663 " }\n" +
16664 " void check(String s) {}\n" +
16665 "}\n"
16666 });
16667 }
16668
16669 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16670 // analysis of second local variable must not interfere
16671 public void testBug345305_4() {
16672 runNegativeNullTest(
16673 new String[] {
16674 "X.java",
16675 "public class X {\n" +
16676 " void foo() {\n" +
16677 " String s = \"\";\n" +
16678 " String s2 = null;\n" +
16679 " while (true) {\n" +
16680 " try {\n" +
16681 " s = null;\n" +
16682 " bar();\n" +
16683 " s2 = \"world\";\n" +
16684 " }\n" +
16685 " finally {\n" +
16686 " s.length();\n" +
16687 " s = null;\n" +
16688 " }\n" +
16689 " }\n" +
16690 " }\n" +
16691 " void bar() {}\n" +
16692 "}\n"
16693 },
16694 "----------\n" +
16695 "1. ERROR in X.java (at line 12)\n" +
16696 " s.length();\n" +
16697 " ^\n" +
16698 "Null pointer access: The variable s can only be null at this location\n" +
16699 "----------\n");
16700 }
16701
16702 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16703 // block-less if involved - info about pot.nn. was lost when checking against loop's info (deferred check)
16704 public void testBug345305_6() {
16705 runNegativeNullTest(
16706 new String[] {
16707 "X.java",
16708 "public class X {\n" +
16709 " void foo(boolean b) {\n" +
16710 " String s = null;\n" +
16711 " while (true) {\n" +
16712 " try {\n" +
16713 " if (b)\n" +
16714 " s = \"hi\";\n" +
16715 " }\n" +
16716 " finally {\n" +
16717 " s.length();\n" +
16718 " s = null;\n" +
16719 " }\n" +
16720 " }\n" +
16721 " }\n" +
16722 "}\n"
16723 },
16724 "----------\n" +
16725 "1. ERROR in X.java (at line 10)\n" +
16726 " s.length();\n" +
16727 " ^\n" +
16728 "Potential null pointer access: The variable s may be null at this location\n" +
16729 "----------\n");
16730 }
16731
16732 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16733 // block-less if involved
16734 public void testBug345305_7() {
16735 runNegativeNullTest(
16736 new String[] {
16737 "X.java",
16738 "public class X {\n" +
16739 " void foo(boolean b) {\n" +
16740 " while (true) {\n" +
16741 " String s = null;\n" +
16742 " try {\n" +
16743 " if (b)\n" +
16744 " s = \"hi\";\n" +
16745 " }\n" +
16746 " finally {\n" +
16747 " s.length();\n" +
16748 " s = null;\n" +
16749 " }\n" +
16750 " }\n" +
16751 " }\n" +
16752 "}\n"
16753 },
16754 "----------\n" +
16755 "1. ERROR in X.java (at line 10)\n" +
16756 " s.length();\n" +
16757 " ^\n" +
16758 "Potential null pointer access: The variable s may be null at this location\n" +
16759 "----------\n");
16760 }
16761
16762 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16763 // consider exception thrown from cast expression
16764 public void testBug345305_8() {
16765 runNegativeNullTest(
16766 new String[] {
16767 "X.java",
16768 "public class X {\n" +
16769 " void foo(Object o) {\n" +
16770 " while (true) {\n" +
16771 " String s = null;\n" +
16772 " try {\n" +
16773 " s = (String) o;\n" +
16774 " }\n" +
16775 " finally {\n" +
16776 " s.length();\n" +
16777 " s = null;\n" +
16778 " }\n" +
16779 " }\n" +
16780 " }\n" +
16781 "}\n"
16782 },
16783 "----------\n" +
16784 "1. ERROR in X.java (at line 9)\n" +
16785 " s.length();\n" +
16786 " ^\n" +
16787 "Potential null pointer access: The variable s may be null at this location\n" +
16788 "----------\n");
16789 }
16790
16791 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16792 // consider exception thrown from binary expression
16793 public void testBug345305_9() {
16794 runNegativeNullTest(
16795 new String[] {
16796 "X.java",
16797 "public class X {\n" +
16798 " void foo(int i, int j) {\n" +
16799 " while (true) {\n" +
16800 " String s = null;\n" +
16801 " try {\n" +
16802 " s = ((i / j) == 3) ? \"3\" : \"not-3\";\n" +
16803 " }\n" +
16804 " finally {\n" +
16805 " s.length();\n" +
16806 " s = null;\n" +
16807 " }\n" +
16808 " }\n" +
16809 " }\n" +
16810 "}\n"
16811 },
16812 "----------\n" +
16813 "1. ERROR in X.java (at line 9)\n" +
16814 " s.length();\n" +
16815 " ^\n" +
16816 "Potential null pointer access: The variable s may be null at this location\n" +
16817 "----------\n");
16818 }
16819
16820 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16821 // inner labeled block with break
16822 public void testBug345305_10() {
16823 runNegativeNullTest(
16824 new String[] {
16825 "X.java",
16826 "public class X {\n" +
16827 " void foo(int j) {\n" +
16828 " while (true) {\n" +
16829 " String s = null;\n" +
16830 " try {\n" +
16831 " int i=0;\n" +
16832 " block: {\n" +
16833 " if (i++ == j)\n" +
16834 " break block;\n" +
16835 " s = \"\";\n" +
16836 " return;\n" +
16837 " }\n" +
16838 " }\n" +
16839 " finally {\n" +
16840 " s.length();\n" +
16841 " s = null;\n" +
16842 " }\n" +
16843 " }\n" +
16844 " }\n" +
16845 "}\n"
16846 },
16847 "----------\n" +
16848 "1. ERROR in X.java (at line 15)\n" +
16849 " s.length();\n" +
16850 " ^\n" +
16851 "Potential null pointer access: The variable s may be null at this location\n" +
16852 "----------\n");
16853 }
16854
16855 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16856 // switch statement
16857 public void testBug345305_11() {
16858 runNegativeNullTest(
16859 new String[] {
16860 "X.java",
16861 "public class X {\n" +
16862 " void foo(int j) {\n" +
16863 " while (true) {\n" +
16864 " String s = null;\n" +
16865 " try {\n" +
16866 " switch (j) {\n" +
16867 " case 3:\n" +
16868 " s = \"\";\n" +
16869 " return;\n" +
16870 " default: return;\n" +
16871 " }\n" +
16872 " }\n" +
16873 " finally {\n" +
16874 " s.length();\n" +
16875 " s = null;\n" +
16876 " }\n" +
16877 " }\n" +
16878 " }\n" +
16879 "}\n"
16880 },
16881 "----------\n" +
16882 "1. ERROR in X.java (at line 14)\n" +
16883 " s.length();\n" +
16884 " ^\n" +
16885 "Potential null pointer access: The variable s may be null at this location\n" +
16886 "----------\n");
16887 }
16888
16889 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16890 // assignment inside conditional expression
16891 public void testBug345305_12() {
16892 runNegativeNullTest(
16893 new String[] {
16894 "X.java",
16895 "public class X {\n" +
16896 " String foo(boolean b) {\n" +
16897 " while (true) {\n" +
16898 " String s = null;\n" +
16899 " try {\n" +
16900 " return b ? (s = \"be\") : \"be not\";\n" +
16901 " }\n" +
16902 " finally {\n" +
16903 " s.length();\n" +
16904 " s = null;\n" +
16905 " }\n" +
16906 " }\n" +
16907 " }\n" +
16908 "}\n"
16909 },
16910 "----------\n" +
16911 "1. ERROR in X.java (at line 9)\n" +
16912 " s.length();\n" +
16913 " ^\n" +
16914 "Potential null pointer access: The variable s may be null at this location\n" +
16915 "----------\n");
16916 }
16917
16918 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16919 // explicit throw
16920 public void testBug345305_13() {
16921 runNegativeNullTest(
16922 new String[] {
16923 "X.java",
16924 "public class X {\n" +
16925 " String foo(boolean b) {\n" +
16926 " while (true) {\n" +
16927 " String s = null;\n" +
16928 " RuntimeException ex = new RuntimeException();\n" +
16929 " try {\n" +
16930 " if (b)\n" +
16931 " throw ex;\n" +
16932 " s = \"be\";\n" +
16933 " }\n" +
16934 " finally {\n" +
16935 " s.length();\n" +
16936 " s = null;\n" +
16937 " }\n" +
16938 " }\n" +
16939 " }\n" +
16940 "}\n"
16941 },
16942 "----------\n" +
16943 "1. ERROR in X.java (at line 12)\n" +
16944 " s.length();\n" +
16945 " ^\n" +
16946 "Potential null pointer access: The variable s may be null at this location\n" +
16947 "----------\n");
16948 }
16949
16950 // Bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
16951 // do-while
16952 public void testBug345305_14() {
16953 runNegativeNullTest(
16954 new String[] {
16955 "X.java",
16956 "public class X {\n" +
16957 " void foo1(boolean b) {\n" +
16958 " while (true) {\n" +
16959 " String s = null;\n" +
16960 " try {\n" +
16961 " do {\n" +
16962 " s = \"be\";\n" +
16963 " if (b)\n" +
16964 " return;\n" +
16965 " } while (true);\n" +
16966 " }\n" +
16967 " finally {\n" +
16968 " s.length(); // don't complain here\n" +
16969 " s = null;\n" +
16970 " }\n" +
16971 " }\n" +
16972 " }\n" +
16973 " void foo2(boolean b) {\n" +
16974 " while (true) {\n" +
16975 " String s = null;\n" +
16976 " try {\n" +
16977 " do {\n" +
16978 " if (b)\n" +
16979 " continue;\n" +
16980 " s = \"be\";\n" +
16981 " b = !b;\n" +
16982 " } while (b);\n" +
16983 " }\n" +
16984 " finally {\n" +
16985 " s.length();\n" +
16986 " s = null;\n" +
16987 " }\n" +
16988 " }\n" +
16989 " }\n" +
16990 "}\n"
16991 },
16992 "----------\n" +
16993 "1. ERROR in X.java (at line 30)\n" +
16994 " s.length();\n" +
16995 " ^\n" +
16996 "Potential null pointer access: The variable s may be null at this location\n" +
16997 "----------\n");
16998 }
16999
17000 // Bug 364326 - [compiler][null] NullPointerException is not found by compiler. FindBugs finds that one
17001 public void testBug364326() {
17002 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // employs auto-unboxing
17003 runNegativeNullTest(
17004 new String[] {
17005 "NPE_OnBoxing.java",
17006 "\n" +
17007 "public class NPE_OnBoxing\n" +
17008 "{\n" +
17009 " private interface IValue\n" +
17010 " {\n" +
17011 " boolean isSomething();\n" +
17012 " }\n" +
17013 "\n" +
17014 " private final IValue m_Value;\n" +
17015 "\n" +
17016 " public NPE_OnBoxing()\n" +
17017 " {\n" +
17018 " m_Value = null;\n" +
17019 " }\n" +
17020 "\n" +
17021 " public boolean isSomething()\n" +
17022 " {\n" +
17023 " return m_Value != null ? m_Value.isSomething() : null;\n" +
17024 " }\n" +
17025 "\n" +
17026 " public static void main(final String [] args)\n" +
17027 " {\n" +
17028 " new NPE_OnBoxing().isSomething();\n" +
17029 " }\n" +
17030 "}\n"
17031 },
17032 "----------\n" +
17033 "1. ERROR in NPE_OnBoxing.java (at line 18)\n" +
17034 " return m_Value != null ? m_Value.isSomething() : null;\n" +
17035 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
17036 "Potential null pointer access: This expression of type Boolean may be null but requires auto-unboxing\n" +
17037 "----------\n");
17038 }
17039
17040 // Bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
17041 public void testBug401088() {
17042 runConformTest(
17043 new String[] {
17044 "X.java",
17045 "public class X {\n" +
17046 "\n" +
17047 " private static void occasionallyThrowException() throws Exception {\n" +
17048 " throw new Exception();\n" +
17049 " }\n" +
17050 "\n" +
17051 " private static void open() throws Exception {\n" +
17052 " occasionallyThrowException();\n" +
17053 " }\n" +
17054 "\n" +
17055 " private static void close() throws Exception {\n" +
17056 " occasionallyThrowException();\n" +
17057 " }\n" +
17058 "\n" +
17059 " public static void main(String s[]) {\n" +
17060 " Exception exc = null;\n" +
17061 " try {\n" +
17062 " open();\n" +
17063 " // do more things\n" +
17064 " }\n" +
17065 " catch (Exception e) {\n" +
17066 " exc = e;\n" +
17067 " }\n" +
17068 " finally {\n" +
17069 " try {\n" +
17070 " close();\n" +
17071 " }\n" +
17072 " catch (Exception e) {\n" +
17073 " if (exc == null) // should not warn on this line\n" +
17074 " exc = e;\n" +
17075 " }\n" +
17076 " }\n" +
17077 " if (exc != null)\n" +
17078 " System.out.println(exc);\n" +
17079 " }\n" +
17080 "}\n"
17081 },
17082 "java.lang.Exception");
17083 }
17084 // Bug 401088 - [compiler][null] Wrong warning "Redundant null check" inside nested try statement
17085 public void testBug401088a() {
17086 runConformTest(
17087 new String[] {
17088 "X.java",
17089 "public class X {\n" +
17090 "\n" +
17091 " private static void occasionallyThrowException() throws Exception {\n" +
17092 " throw new Exception();\n" +
17093 " }\n" +
17094 "\n" +
17095 " private static void open() throws Exception {\n" +
17096 " occasionallyThrowException();\n" +
17097 " }\n" +
17098 "\n" +
17099 " private static void close() throws Exception {\n" +
17100 " occasionallyThrowException();\n" +
17101 " }\n" +
17102 "\n" +
17103 " public static void main(String s[]) {\n" +
17104 " Exception exc = null;\n" +
17105 " try {\n" +
17106 " open();\n" +
17107 " // do more things\n" +
17108 " }\n" +
17109 " catch (Exception e) {\n" +
17110 " exc = e;\n" +
17111 " }\n" +
17112 " finally {\n" +
17113 " try {\n" +
17114 " close();\n" +
17115 " }\n" +
17116 " catch (Exception e) {\n" +
17117 " if (exc == null) // should not warn on this line\n" +
17118 " exc = e;\n" +
17119 " }\n" +
17120 " finally { System.out.print(1); }\n" +
17121 " }\n" +
17122 " if (exc != null)\n" +
17123 " System.out.println(exc);\n" +
17124 " }\n" +
17125 "}\n"
17126 },
17127 "1java.lang.Exception");
17128 }
17129 // Bug 401092 - [compiler][null] Wrong warning "Redundant null check" in outer catch of nested try
17130 public void test401092() {
17131 runConformTest(
17132 new String[] {
17133 "X.java",
17134 "import java.util.Date;\n" +
17135 "\n" +
17136 "public class X {\n" +
17137 "\n" +
17138 " private static void occasionallyThrowException() throws Exception {\n" +
17139 " throw new Exception();\n" +
17140 " }\n" +
17141 "\n" +
17142 " private static Date createDate() throws Exception {\n" +
17143 " occasionallyThrowException();\n" +
17144 " return new Date();\n" +
17145 " }\n" +
17146 "\n" +
17147 " public static void main(String s[]) {\n" +
17148 " Date d = null;\n" +
17149 " try {\n" +
17150 " d = createDate();\n" +
17151 " System.out.println(d.toString());\n" +
17152 " try {\n" +
17153 " occasionallyThrowException();\n" +
17154 " }\n" +
17155 " catch (Exception exc) {\n" +
17156 " }\n" +
17157 " }\n" +
17158 " catch (Exception exc) {\n" +
17159 " if (d != null) // should not warn in this line\n" +
17160 " System.out.println(d.toString());\n" +
17161 " }\n" +
17162 " }\n" +
17163 "}\n"
17164 });
17165 }
17166 // Bug 401092 - [compiler][null] Wrong warning "Redundant null check" in outer catch of nested try
17167 public void test401092a() {
17168 runConformTest(
17169 new String[] {
17170 "X.java",
17171 "import java.util.Date;\n" +
17172 "\n" +
17173 "public class X {\n" +
17174 "\n" +
17175 " private static void occasionallyThrowException() throws Exception {\n" +
17176 " throw new Exception();\n" +
17177 " }\n" +
17178 "\n" +
17179 " private static Date createDate() throws Exception {\n" +
17180 " occasionallyThrowException();\n" +
17181 " return new Date();\n" +
17182 " }\n" +
17183 "\n" +
17184 " public static void main(String s[]) {\n" +
17185 " Date d = null;\n" +
17186 " try {\n" +
17187 " d = createDate();\n" +
17188 " System.out.println(d.toString());\n" +
17189 " try {\n" +
17190 " occasionallyThrowException();\n" +
17191 " }\n" +
17192 " catch (Exception exc) {\n" +
17193 " }\n" +
17194 " finally { System.out.println(1); }\n" +
17195 " }\n" +
17196 " catch (Exception exc) {\n" +
17197 " if (d != null) // should not warn in this line\n" +
17198 " System.out.println(d.toString());\n" +
17199 " }\n" +
17200 " finally { System.out.println(2); }\n" +
17201 " }\n" +
17202 "}\n"
17203 });
17204 }
17205 // Bug 402993 - [null] Follow up of bug 401088: Missing warning about redundant null check
17206 public void testBug402993() {
17207 runNegativeNullTest(
17208 new String[] {
17209 "Test.java",
17210 "public class Test {\n" +
17211 "\n" +
17212 " private static void occasionallyThrowException() throws Exception {\n" +
17213 " if ((System.currentTimeMillis() & 1L) != 0L)\n" +
17214 " throw new Exception();\n" +
17215 " }\n" +
17216 "\n" +
17217 " private static void open() throws Exception {\n" +
17218 " occasionallyThrowException();\n" +
17219 " }\n" +
17220 "\n" +
17221 " private static void close() throws Exception {\n" +
17222 " occasionallyThrowException();\n" +
17223 " }\n" +
17224 "\n" +
17225 " public static void main(String s[]) {\n" +
17226 " Exception exc = null;\n" +
17227 " try {\n" +
17228 " open();\n" +
17229 " // do more things\n" +
17230 " }\n" +
17231 " catch (Exception e) {\n" +
17232 " if (exc == null) // no warning here ??\n" +
17233 " ;\n" +
17234 " }\n" +
17235 " finally {\n" +
17236 " try {\n" +
17237 " close();\n" +
17238 " }\n" +
17239 " catch (Exception e) {\n" +
17240 " if (exc == null) // No warning here ??\n" +
17241 " exc = e;\n" +
17242 " }\n" +
17243 " }\n" +
17244 " }\n" +
17245 "}\n"
17246 },
17247 "----------\n" +
17248 "1. ERROR in Test.java (at line 23)\n" +
17249 " if (exc == null) // no warning here ??\n" +
17250 " ^^^\n" +
17251 "Redundant null check: The variable exc can only be null at this location\n" +
17252 "----------\n" +
17253 "2. ERROR in Test.java (at line 31)\n" +
17254 " if (exc == null) // No warning here ??\n" +
17255 " ^^^\n" +
17256 "Redundant null check: The variable exc can only be null at this location\n" +
17257 "----------\n");
17258 }
17259 // Bug 402993 - [null] Follow up of bug 401088: Missing warning about redundant null check
17260 // variant with finally block in inner try
17261 public void testBug402993a() {
17262 runNegativeNullTest(
17263 new String[] {
17264 "Test.java",
17265 "public class Test {\n" +
17266 "\n" +
17267 " private static void occasionallyThrowException() throws Exception {\n" +
17268 " if ((System.currentTimeMillis() & 1L) != 0L)\n" +
17269 " throw new Exception();\n" +
17270 " }\n" +
17271 "\n" +
17272 " private static void open() throws Exception {\n" +
17273 " occasionallyThrowException();\n" +
17274 " }\n" +
17275 "\n" +
17276 " private static void close() throws Exception {\n" +
17277 " occasionallyThrowException();\n" +
17278 " }\n" +
17279 "\n" +
17280 " public static void main(String s[]) {\n" +
17281 " Exception exc = null;\n" +
17282 " try {\n" +
17283 " open();\n" +
17284 " // do more things\n" +
17285 " }\n" +
17286 " catch (Exception e) {\n" +
17287 " if (exc == null) // no warning here ??\n" +
17288 " ;\n" +
17289 " }\n" +
17290 " finally {\n" +
17291 " try {\n" +
17292 " close();\n" +
17293 " }\n" +
17294 " catch (Exception e) {\n" +
17295 " if (exc == null) // No warning here ??\n" +
17296 " exc = e;\n" +
17297 " } finally {\n" +
17298 " System.out.print(1);\n" +
17299 " }\n" +
17300 " }\n" +
17301 " }\n" +
17302 "}\n"
17303 },
17304 "----------\n" +
17305 "1. ERROR in Test.java (at line 23)\n" +
17306 " if (exc == null) // no warning here ??\n" +
17307 " ^^^\n" +
17308 "Redundant null check: The variable exc can only be null at this location\n" +
17309 "----------\n" +
17310 "2. ERROR in Test.java (at line 31)\n" +
17311 " if (exc == null) // No warning here ??\n" +
17312 " ^^^\n" +
17313 "Redundant null check: The variable exc can only be null at this location\n" +
17314 "----------\n");
17315 }
17316 public void testBug453305() {
17317 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses foreach loop
17318 runConformTest(
17319 new String[] {
17320 "NullTest.java",
17321 "import java.util.*;\n" +
17322 "public class NullTest {\n" +
17323 " class SomeOtherClass {\n" +
17324 "\n" +
17325 " public SomeOtherClass m() {\n" +
17326 " return new SomeOtherClass();\n" +
17327 " }\n" +
17328 "\n" +
17329 " public void doSomething() {\n" +
17330 " }\n" +
17331 " }\n" +
17332 "\n" +
17333 " public Object m1() {\n" +
17334 " SomeOtherClass result = null;\n" +
17335 " List<Object> list = new ArrayList<Object>();\n" +
17336 " for (Object next : list) {\n" +
17337 " System.out.println(next);\n" +
17338 " boolean bool = false;\n" +
17339 " if (bool) {\n" +
17340 " SomeOtherClass something = new SomeOtherClass();\n" +
17341 " result = something.m();\n" +
17342 " } else {\n" +
17343 " result = new SomeOtherClass();\n" +
17344 " }\n" +
17345 " result.doSomething(); // warning is here\n" +
17346 " break;\n" +
17347 " }\n" +
17348 " return null;\n" +
17349 " }\n" +
17350 "}\n"
17351 });
17352 }
17353 public void testBug431016() {
17354 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses foreach loop
17355 runConformTest(
17356 new String[] {
17357 "Test.java",
17358 "public class Test {\n" +
17359 " void test(Object[] values) {\n" +
17360 " Object first = null;\n" +
17361 " for (Object current : values) {\n" +
17362 " if (first == null) {\n" +
17363 " first = current;\n" +
17364 " }\n" +
17365 "\n" +
17366 " if (current.hashCode() > 0) {\n" +
17367 " System.out.println(first.hashCode());\n" +
17368 " }\n" +
17369 "\n" +
17370 " System.out.println(first.hashCode());\n" +
17371 " }\n" +
17372 " }\n" +
17373 "}\n"
17374 });
17375 }
17376 // originally created for documentation purpose, see https://bugs.eclipse.org/453483#c9
17377 public void testBug431016_simplified() {
17378 runConformTest(
17379 new String[] {
17380 "Test.java",
17381 "public class Test {\n" +
17382 " void test(Object input, boolean b) {\n" +
17383 " Object o = null;\n" +
17384 " while (true) {\n" +
17385 " if (o == null)\n" +
17386 " o = input;\n" +
17387 " if (b)\n" +
17388 " o.toString();\n" +
17389 " o.toString();\n" +
17390 " }\n" +
17391 " }\n" +
17392 "}\n"
17393 });
17394 }
17395 public void testBug432109() {
17396 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses generics & foreach loop
17397 runConformTest(
17398 new String[] {
17399 "Test.java",
17400 "import java.util.Collection;\n" +
17401 "public class Test {\n" +
17402 " public void test(Collection <Object> values)\n" +
17403 " {\n" +
17404 " boolean condition = false;\n" +
17405 " \n" +
17406 " for(Object value : values)\n" +
17407 " {\n" +
17408 " \n" +
17409 " if(value == null)\n" +
17410 " {\n" +
17411 " if( condition )\n" +
17412 " {\n" +
17413 " // without this continue statement, \n" +
17414 " // there is no warning below\n" +
17415 " continue; \n" +
17416 " }\n" +
17417 " \n" +
17418 " value = getDefaultValue();\n" +
17419 " }\n" +
17420 " \n" +
17421 " // IDE complains here about potential null pointer access\n" +
17422 " value.toString();\n" +
17423 " }\n" +
17424 " }\n" +
17425 "\n" +
17426 " public String getDefaultValue() { return \"<empty>\"; }\n" +
17427 "}\n"
17428 });
17429 }
17430 public void testBug435528_orig() {
17431 Runner runner = new Runner();
17432 runner.testFiles =
17433 new String[] {
17434 "Test.java",
17435 "public class Test\n" +
17436 "{\n" +
17437 " static final String a = \"A\";\n" +
17438 "\n" +
17439 " static void main(String args[])\n" +
17440 " {\n" +
17441 " String x = null;\n" +
17442 " while (true) {\n" +
17443 " x = Math.random() < 0.5 ? a : \"BB\";\n" +
17444 " if (a != null) {\n" +
17445 " System.out.println(\"s2 value: \" + x);\n" +
17446 " }\n" +
17447 " if (x.equals(\"A\")) {\n" +
17448 " break;\n" +
17449 " } else {\n" +
17450 " x = null;\n" +
17451 " }\n" +
17452 " }\n" +
17453 " }\n" +
17454 "}\n"
17455 };
17456 runner.expectedCompilerLog =
17457 "----------\n" +
17458 "1. ERROR in Test.java (at line 10)\n" +
17459 " if (a != null) {\n" +
17460 " ^\n" +
17461 "Redundant null check: The field a is a nonnull constant\n" +
17462 "----------\n" +
17463 "2. WARNING in Test.java (at line 15)\n" +
17464 " } else {\n" +
17465 " x = null;\n" +
17466 " }\n" +
17467 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
17468 "Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally\n" +
17469 "----------\n";
17470 runner.customOptions = getCompilerOptions();
17471 runner.javacTestOptions = JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
17472 runner.runNegativeTest();
17473 }
17474 public void testBug435528_notaconstant() {
17475 runConformTest(
17476 true/*flush*/,
17477 new String[] {
17478 "Test.java",
17479 "public class Test\n" +
17480 "{\n" +
17481 " static String a ;\n" +
17482 "\n" +
17483 " static void main(String args[])\n" +
17484 " {\n" +
17485 " String x = null;\n" +
17486 " while (true) {\n" +
17487 " x = Math.random() < 0.5 ? a : \"BB\";\n" +
17488 " if (a != null) {\n" +
17489 " System.out.println(\"s2 value: \" + x);\n" +
17490 " }\n" +
17491 " if (x.equals(\"A\")) {\n" +
17492 " break;\n" +
17493 " } else {\n" +
17494 " x = null;\n" +
17495 " }\n" +
17496 " }\n" +
17497 " }\n" +
17498 "}\n"
17499 },
17500 "----------\n" +
17501 "1. WARNING in Test.java (at line 15)\n" +
17502 " } else {\n" +
17503 " x = null;\n" +
17504 " }\n" +
17505 " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
17506 "Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally\n" +
17507 "----------\n",
17508 "",
17509 "",
17510 JavacTestOptions.Excuse.EclipseHasSomeMoreWarnings);
17511 }
17512 public void testBug418500() {
17513 runConformTest(
17514 new String[] {
17515 "Test.java",
17516 "import java.util.*;\n" +
17517 "public class Test {\n" +
17518 (this.complianceLevel < ClassFileConstants.JDK1_5 ? "\n" : " @SuppressWarnings(\"unchecked\")\n" ) +
17519 " void method() {\n" +
17520 " Map topMap = new HashMap();\n" +
17521 " List targets = null;\n" +
17522 " \n" +
17523 " for (int idx = 1; idx < 100; idx++) {\n" +
17524 " String[] targetArray = (String[]) topMap.get(\"a\");\n" +
17525 " if (targetArray != null) {\n" +
17526 " targets = Arrays.asList(targetArray);\n" +
17527 " } else {\n" +
17528 " targets = new ArrayList(64);\n" +
17529 " }\n" +
17530 " if (targets.size() > 0) {\n" +
17531 " topMap.put(\"b\", targets.toArray(new String[1]));\n" +
17532 " } else {\n" +
17533 " topMap.remove(\"b\");\n" +
17534 " }\n" +
17535 "\n" +
17536 " // BUG - this statement causes null analysis to\n" +
17537 " // report that at the targets.size() statement above\n" +
17538 " // targets must be null. Commenting this line eliminates the error.\n" +
17539 " targets = null;\n" +
17540 " }\n" +
17541 " }\n" +
17542 "}\n"
17543 });
17544 }
17545 public void testBug441737() {
17546 runConformTest(
17547 new String[] {
17548 "Bogus.java",
17549 "public class Bogus {\n" +
17550 " static boolean ok = true;\n" +
17551 " static int count = 0;\n" +
17552 " public static void main(String[] args) {\n" +
17553 " Thing x = new Thing();\n" +
17554 " // if y is left uninitialized here, the warning below disappears\n" +
17555 " Thing y = null;\n" +
17556 " do {\n" +
17557 " y = x;\n" +
17558 " if (ok) {\n" +
17559 " // if this assignment is moved out of the if statement\n" +
17560 " // or commented out, the warning below disappears\n" +
17561 " x = y.resolve();\n" +
17562 " }\n" +
17563 " // a warning about y being potentially null occurs here:\n" +
17564 " x = y.resolve();\n" +
17565 " } while (x != y);\n" +
17566 " }\n" +
17567 "\n" +
17568 " static class Thing {\n" +
17569 " public Thing resolve() {\n" +
17570 " return count++ > 2 ? this : new Thing();\n" +
17571 " }\n" +
17572 " }\n" +
17573 "}\n"
17574 });
17575 }
17576 // fixed in 3.6.2, likely via bug 332637.
17577 public void testBug195638_comment3() {
17578 runConformTest(
17579 new String[] {
17580 "Test.java",
17581 "import java.sql.Connection;\n" +
17582 "import java.sql.SQLException;\n" +
17583 "public class Test {\n" +
17584 " void m() throws SQLException\n" +
17585 " {\n" +
17586 " Connection conn = null;\n" +
17587 " try\n" +
17588 " {\n" +
17589 " conn = createConnection();\n" +
17590 "\n" +
17591 " for (; ; )\n" +
17592 " {\n" +
17593 " throwSomething();\n" +
17594 " }\n" +
17595 " }\n" +
17596 " catch (MyException e)\n" +
17597 " {\n" +
17598 " conn.rollback(); //The variable can never be null here...\n" +
17599 " }\n" +
17600 " }\n" +
17601 "\n" +
17602 " private void throwSomething() throws MyException\n" +
17603 " {\n" +
17604 " throw new MyException();\n" +
17605 " }\n" +
17606 "\n" +
17607 " class MyException extends Exception\n" +
17608 " {\n" +
17609 "\n" +
17610 " }\n" +
17611 "\n" +
17612 " private Connection createConnection()\n" +
17613 " {\n" +
17614 " return null;\n" +
17615 " }\n" +
17616 "}\n"
17617 });
17618 }
17619 public void testBug195638_comment6() {
17620 runNegativeNullTest(
17621 new String[] {
17622 "CanOnlyBeNullShouldBeMayBeNull.java",
17623 "public class CanOnlyBeNullShouldBeMayBeNull {\n" +
17624 "\n" +
17625 " private void method() {\n" +
17626 " String tblVarRpl = null;\n" +
17627 " while (true) {\n" +
17628 " boolean isOpenVariableMortageRateProduct = true;\n" +
17629 " boolean tblVarRplAllElementAddedIndicator = false;\n" +
17630 " if (isOpenVariableMortageRateProduct) {\n" +
17631 " if (tblVarRplAllElementAddedIndicator == false)\n" +
17632 " tblVarRpl = \"\";\n" +
17633 " tblVarRpl.substring(1); //Can only be null???\n" +
17634 " return; \n" +
17635 " }\n" +
17636 " }\n" +
17637 " }\n" +
17638 "}\n"
17639 },
17640 "----------\n" +
17641 "1. WARNING in CanOnlyBeNullShouldBeMayBeNull.java (at line 3)\n" +
17642 " private void method() {\n" +
17643 " ^^^^^^^^\n" +
17644 "The method method() from the type CanOnlyBeNullShouldBeMayBeNull is never used locally\n" +
17645 "----------\n" +
17646 "2. ERROR in CanOnlyBeNullShouldBeMayBeNull.java (at line 11)\n" +
17647 " tblVarRpl.substring(1); //Can only be null???\n" +
17648 " ^^^^^^^^^\n" +
17649 "Potential null pointer access: The variable tblVarRpl may be null at this location\n" +
17650 "----------\n");
17651 }
17652 public void testBug195638_comment14() {
17653 runNegativeNullTest(
17654 new String[] {
17655 "Test.java",
17656 "public class Test {\n" +
17657 " private void test() {\n" +
17658 " boolean x = true;\n" +
17659 " Object o = null;\n" +
17660 " \n" +
17661 " for (;;) {\n" +
17662 " if (x) o = new Object();\n" +
17663 " \n" +
17664 " o.toString(); // warning here\n" + // bug was: Null pointer access: The variable o can only be null at this location
17665 " \n" +
17666 " o = null;\n" +
17667 " }\n" +
17668 " }\n" +
17669 "}\n"
17670 },
17671 "----------\n" +
17672 "1. WARNING in Test.java (at line 2)\n" +
17673 " private void test() {\n" +
17674 " ^^^^^^\n" +
17675 "The method test() from the type Test is never used locally\n" +
17676 "----------\n" +
17677 "2. ERROR in Test.java (at line 9)\n" +
17678 " o.toString(); // warning here\n" +
17679 " ^\n" +
17680 "Potential null pointer access: The variable o may be null at this location\n" +
17681 "----------\n");
17682 }
17683 public void testBug195638_comment19() {
17684 runConformTest(
17685 new String[] {
17686 "Test.java",
17687 "public class Test {\n" +
17688 " public void testIt() {\n" +
17689 " Object aRole = null;\n" +
17690 " for (;;) {\n" +
17691 " aRole = new Object();\n" +
17692 " if (aRole.toString() == null) {\n" +
17693 " aRole = getObject(); // changing to \"new Object()\" makes warning disappear.\n" +
17694 " }\n" +
17695 " aRole.toString();\n" +
17696 " // above line gets: \"Null pointer access: The variable aRole can only be null at this location\"\n" +
17697 " break;\n" +
17698 " }\n" +
17699 " }\n" +
17700 " private Object getObject() {\n" +
17701 " return new Object();\n" +
17702 " }\n" +
17703 "}\n"
17704 });
17705 }
17706 public void testBug454031() {
17707 runNegativeNullTest(
17708 new String[] {
17709 "xy/Try.java",
17710 "package xy;\n" +
17711 "\n" +
17712 "public class Try {\n" +
17713 " public static void main(String[] args) {\n" +
17714 " foo(new Node());\n" +
17715 " }\n" +
17716 " static void foo(Node n) {\n" +
17717 " Node selectedNode= n;\n" +
17718 " if (selectedNode == null) {\n" +
17719 " return;\n" +
17720 " }\n" +
17721 " while (selectedNode != null && !(selectedNode instanceof Cloneable)) {\n" +
17722 " selectedNode= selectedNode.getParent();\n" +
17723 " }\n" +
17724 " if (selectedNode == null) { //wrong problem: Null comparison always yields false: The variable selectedNode cannot be null at this location\n" +
17725 " // wrong problem: dead code\n" +
17726 " System.out.println(selectedNode.hashCode());\n" +
17727 " }\n" +
17728 " }\n" +
17729 "}\n" +
17730 "\n" +
17731 "class Node {\n" +
17732 " Node getParent() {\n" +
17733 " return null;\n" +
17734 " }\n" +
17735 "}\n"
17736 },
17737 "----------\n" +
17738 "1. ERROR in xy\\Try.java (at line 17)\n" +
17739 " System.out.println(selectedNode.hashCode());\n" +
17740 " ^^^^^^^^^^^^\n" +
17741 "Null pointer access: The variable selectedNode can only be null at this location\n" +
17742 "----------\n");
17743 }
17744 // switch with fall-through nested in for:
17745 public void testBug451660() {
17746 runNegativeNullTest(
17747 new String[] {
17748 "X.java",
17749 "public class X {\n" +
17750 " public static void main(String[] args)\n" +
17751 " {\n" +
17752 " String s = null;\n" +
17753 " for(; true;) // ok with \"while(true)\"\n" +
17754 " {\n" +
17755 " switch(0)\n" +
17756 " {\n" +
17757 " default:\n" +
17758 " s = \"Hello!\";\n" +
17759 " case 1:\n" +
17760 " System.out.println(s.toString());\n" +
17761 " }\n" +
17762 " return;\n" +
17763 " }\n" +
17764 " }\n" +
17765 "\n" +
17766 "}\n"
17767 },
17768 "----------\n" +
17769 "1. ERROR in X.java (at line 12)\n" +
17770 " System.out.println(s.toString());\n" +
17771 " ^\n" +
17772 "Potential null pointer access: The variable s may be null at this location\n" +
17773 "----------\n");
17774 }
17775 public void testBug486912KnownNullInLoop() {
17776 runNegativeNullTest(
17777 new String[] {
17778 "test/KnownNullInLoop.java",
17779 "package test;\n" +
17780 "\n" +
17781 "public class KnownNullInLoop {\n" +
17782 " public void testDoWhile() {\n" +
17783 " Object o1 = null;\n" +
17784 " do {\n" +
17785 " o1.hashCode(); // ERROR1: known null, but no problem reported.\n" +
17786 " } while (false);\n" +
17787 " }\n" +
17788 "\n" +
17789 " public void testWhileWithBreak() {\n" +
17790 " Object o1 = null;\n" +
17791 " while (true) {\n" +
17792 " o1.hashCode(); // ERROR2: known null, but no problem reported.\n" +
17793 " break;\n" +
17794 " }\n" +
17795 " }\n" +
17796 "}\n" +
17797 "",
17798 },
17799 "----------\n" +
17800 "1. ERROR in test\\KnownNullInLoop.java (at line 7)\n" +
17801 " o1.hashCode(); // ERROR1: known null, but no problem reported.\n" +
17802 " ^^\n" +
17803 "Null pointer access: The variable o1 can only be null at this location\n" +
17804 "----------\n" +
17805 "2. ERROR in test\\KnownNullInLoop.java (at line 14)\n" +
17806 " o1.hashCode(); // ERROR2: known null, but no problem reported.\n" +
17807 " ^^\n" +
17808 "Null pointer access: The variable o1 can only be null at this location\n" +
17809 "----------\n"
17810 );
17811 }
17812 public void testBug486912PotNullInLoop_orig() {
17813 runNegativeNullTest(
17814 new String[] {
17815 "test/PotNullInLoop.java",
17816 "package test;\n" +
17817 "\n" +
17818 "public class PotNullInLoop {\n" +
17819 " boolean b;\n" +
17820 "\n" +
17821 " public void testDoWhile1() {\n" +
17822 " Object o1 = null;\n" +
17823 " Object o2 = new Object();\n" +
17824 " Object potNonNull = b ? o2 : o2;\n" + // actually: def nn
17825 " Object potNull = b ? o1 : o1;\n" + // actually: def n
17826 " Object ponNullOrNonNull = b ? potNull : potNonNull;\n" +
17827 " do {\n" +
17828 " potNonNull.hashCode(); // OK\n" +
17829 " potNull.hashCode(); // ERROR 1: pot null, but nothing reported\n" +
17830 " ponNullOrNonNull.hashCode(); // ERROR 2: pot null, but nothing reported\n" +
17831 " } while (false);\n" +
17832 " }\n" +
17833 "\n" +
17834 " public void testWhileWithBreak() {\n" +
17835 " Object o1 = null;\n" +
17836 " Object o2 = new Object();\n" +
17837 " Object potNonNull = b ? o2 : o2;\n" +
17838 " Object potNull = b ? o1 : o1;\n" +
17839 " Object ponNullOrNonNull = b ? potNull : potNonNull;\n" +
17840 " while (b) {\n" +
17841 " potNonNull.hashCode(); // OK\n" +
17842 " potNull.hashCode(); // ERROR 3 : pot null, but nothing reported\n" +
17843 " ponNullOrNonNull.hashCode(); // ERROR 4: pot null, but nothing reported\n" +
17844 " break;\n" +
17845 " }\n" +
17846 " }\n" +
17847 "\n" +
17848 " public void testWhile() {\n" +
17849 " Object o1 = null;\n" +
17850 " Object o2 = new Object();\n" +
17851 " Object potNonNull = b ? o2 : o2;\n" +
17852 " Object potNull = b ? o1 : o1;\n" +
17853 " Object ponNullOrNonNull = b ? potNull : potNonNull;\n" +
17854 " while (b) {\n" +
17855 " potNonNull.hashCode(); // OK\n" +
17856 " potNull.hashCode(); // OK: pot null, is reported\n" +
17857 " ponNullOrNonNull.hashCode(); // OK: pot null, is reported\n" +
17858 " }\n" +
17859 " }\n" +
17860 "\n" +
17861 " public void testFor() {\n" +
17862 " Object o1 = null;\n" +
17863 " Object o2 = new Object();\n" +
17864 " Object potNonNull = b ? o2 : o2;\n" +
17865 " Object potNull = b ? o1 : o1;\n" +
17866 " Object ponNullOrNonNull = b ? potNull : potNonNull;\n" +
17867 " for (int i = 0; i < 1; i++) {\n" +
17868 " potNonNull.hashCode(); // OK\n" +
17869 " potNull.hashCode(); // OK: pot null, is reported\n" +
17870 " ponNullOrNonNull.hashCode(); // OK: pot null, is reported\n" +
17871 " }\n" +
17872 " }\n" +
17873 "\n" +
17874 " public void testForEach() {\n" +
17875 " Object o1 = null;\n" +
17876 " Object o2 = new Object();\n" +
17877 " Object potNonNull = b ? o2 : o2;\n" +
17878 " Object potNull = b ? o1 : o1;\n" +
17879 " Object ponNullOrNonNull = b ? potNull : potNonNull;\n" +
17880 " for (int i = 0; i < 1; i++) {\n" +
17881 " potNonNull.hashCode(); // OK\n" +
17882 " potNull.hashCode(); // OK: pot null, is reported\n" +
17883 " ponNullOrNonNull.hashCode(); // OK: pot null, is reported\n" +
17884 " }\n" +
17885 " }\n" +
17886 "\n" +
17887 "}\n" +
17888 "",
17889 },
17890 "----------\n" +
17891 "1. ERROR in test\\PotNullInLoop.java (at line 14)\n" +
17892 " potNull.hashCode(); // ERROR 1: pot null, but nothing reported\n" +
17893 " ^^^^^^^\n" +
17894 "Null pointer access: The variable potNull can only be null at this location\n" +
17895 "----------\n" +
17896 "2. ERROR in test\\PotNullInLoop.java (at line 15)\n" +
17897 " ponNullOrNonNull.hashCode(); // ERROR 2: pot null, but nothing reported\n" +
17898 " ^^^^^^^^^^^^^^^^\n" +
17899 "Potential null pointer access: The variable ponNullOrNonNull may be null at this location\n" +
17900 "----------\n" +
17901 "3. ERROR in test\\PotNullInLoop.java (at line 27)\n" +
17902 " potNull.hashCode(); // ERROR 3 : pot null, but nothing reported\n" +
17903 " ^^^^^^^\n" +
17904 "Null pointer access: The variable potNull can only be null at this location\n" +
17905 "----------\n" +
17906 "4. ERROR in test\\PotNullInLoop.java (at line 28)\n" +
17907 " ponNullOrNonNull.hashCode(); // ERROR 4: pot null, but nothing reported\n" +
17908 " ^^^^^^^^^^^^^^^^\n" +
17909 "Potential null pointer access: The variable ponNullOrNonNull may be null at this location\n" +
17910 "----------\n" +
17911 "5. ERROR in test\\PotNullInLoop.java (at line 41)\n" +
17912 " potNull.hashCode(); // OK: pot null, is reported\n" +
17913 " ^^^^^^^\n" +
17914 "Null pointer access: The variable potNull can only be null at this location\n" +
17915 "----------\n" +
17916 "6. ERROR in test\\PotNullInLoop.java (at line 42)\n" +
17917 " ponNullOrNonNull.hashCode(); // OK: pot null, is reported\n" +
17918 " ^^^^^^^^^^^^^^^^\n" +
17919 "Potential null pointer access: The variable ponNullOrNonNull may be null at this location\n" +
17920 "----------\n" +
17921 "7. ERROR in test\\PotNullInLoop.java (at line 54)\n" +
17922 " potNull.hashCode(); // OK: pot null, is reported\n" +
17923 " ^^^^^^^\n" +
17924 "Null pointer access: The variable potNull can only be null at this location\n" +
17925 "----------\n" +
17926 "8. ERROR in test\\PotNullInLoop.java (at line 55)\n" +
17927 " ponNullOrNonNull.hashCode(); // OK: pot null, is reported\n" +
17928 " ^^^^^^^^^^^^^^^^\n" +
17929 "Potential null pointer access: The variable ponNullOrNonNull may be null at this location\n" +
17930 "----------\n" +
17931 "9. ERROR in test\\PotNullInLoop.java (at line 67)\n" +
17932 " potNull.hashCode(); // OK: pot null, is reported\n" +
17933 " ^^^^^^^\n" +
17934 "Null pointer access: The variable potNull can only be null at this location\n" +
17935 "----------\n" +
17936 "10. ERROR in test\\PotNullInLoop.java (at line 68)\n" +
17937 " ponNullOrNonNull.hashCode(); // OK: pot null, is reported\n" +
17938 " ^^^^^^^^^^^^^^^^\n" +
17939 "Potential null pointer access: The variable ponNullOrNonNull may be null at this location\n" +
17940 "----------\n"
17941 );
17942 }
17943 // variant of testBug486912PotNullInLoop_orig spiced up with potentiality from an 'unknown' o0:
17944 public void testBug486912PotNullInLoop() {
17945 runNegativeNullTest(
17946 new String[] {
17947 "test/PotNullInLoop.java",
17948 "package test;\n" +
17949 "\n" +
17950 "public class PotNullInLoop {\n" +
17951 " boolean b;\n" +
17952 "\n" +
17953 " public void testDoWhile1(Object o0) {\n" +
17954 " Object o1 = null;\n" +
17955 " Object o2 = new Object();\n" +
17956 " Object potNonNull = b ? o2 : o0;\n" +
17957 " Object potNull = b ? o1 : o0;\n" +
17958 " do {\n" +
17959 " potNonNull.hashCode(); // OK\n" +
17960 " potNull.hashCode(); // ERROR 1: pot null, but nothing reported\n" +
17961 " } while (false);\n" +
17962 " }\n" +
17963 "\n" +
17964 " public void testWhileWithBreak(Object o0) {\n" +
17965 " Object o1 = null;\n" +
17966 " Object o2 = new Object();\n" +
17967 " Object potNonNull = b ? o2 : o0;\n" +
17968 " Object potNull = b ? o1 : o0;\n" +
17969 " while (b) {\n" +
17970 " potNonNull.hashCode(); // OK\n" +
17971 " potNull.hashCode(); // ERROR 3 : pot null, but nothing reported\n" +
17972 " break;\n" +
17973 " }\n" +
17974 " }\n" +
17975 "\n" +
17976 " public void testWhile(Object o0) {\n" +
17977 " Object o1 = null;\n" +
17978 " Object o2 = new Object();\n" +
17979 " Object potNonNull = b ? o2 : o0;\n" +
17980 " Object potNull = b ? o1 : o0;\n" +
17981 " while (b) {\n" +
17982 " potNonNull.hashCode(); // OK\n" +
17983 " potNull.hashCode(); // OK: pot null, is reported\n" +
17984 " }\n" +
17985 " }\n" +
17986 "\n" +
17987 " public void testFor(Object o0) {\n" +
17988 " Object o1 = null;\n" +
17989 " Object o2 = new Object();\n" +
17990 " Object potNonNull = b ? o2 : o0;\n" +
17991 " Object potNull = b ? o1 : o0;\n" +
17992 " for (int i = 0; i < 1; i++) {\n" +
17993 " potNonNull.hashCode(); // OK\n" +
17994 " potNull.hashCode(); // OK: pot null, is reported\n" +
17995 " }\n" +
17996 " }\n" +
17997 "\n" +
17998 " public void testForEach(Object o0) {\n" +
17999 " Object o1 = null;\n" +
18000 " Object o2 = new Object();\n" +
18001 " Object potNonNull = b ? o2 : o0;\n" +
18002 " Object potNull = b ? o1 : o0;\n" +
18003 " for (int i = 0; i < 1; i++) {\n" +
18004 " potNonNull.hashCode(); // OK\n" +
18005 " potNull.hashCode(); // OK: pot null, is reported\n" +
18006 " }\n" +
18007 " }\n" +
18008 "\n" +
18009 "}\n" +
18010 "",
18011 },
18012 "----------\n" +
18013 "1. ERROR in test\\PotNullInLoop.java (at line 13)\n" +
18014 " potNull.hashCode(); // ERROR 1: pot null, but nothing reported\n" +
18015 " ^^^^^^^\n" +
18016 "Potential null pointer access: The variable potNull may be null at this location\n" +
18017 "----------\n" +
18018 "2. ERROR in test\\PotNullInLoop.java (at line 24)\n" +
18019 " potNull.hashCode(); // ERROR 3 : pot null, but nothing reported\n" +
18020 " ^^^^^^^\n" +
18021 "Potential null pointer access: The variable potNull may be null at this location\n" +
18022 "----------\n" +
18023 "3. ERROR in test\\PotNullInLoop.java (at line 36)\n" +
18024 " potNull.hashCode(); // OK: pot null, is reported\n" +
18025 " ^^^^^^^\n" +
18026 "Potential null pointer access: The variable potNull may be null at this location\n" +
18027 "----------\n" +
18028 "4. ERROR in test\\PotNullInLoop.java (at line 47)\n" +
18029 " potNull.hashCode(); // OK: pot null, is reported\n" +
18030 " ^^^^^^^\n" +
18031 "Potential null pointer access: The variable potNull may be null at this location\n" +
18032 "----------\n" +
18033 "5. ERROR in test\\PotNullInLoop.java (at line 58)\n" +
18034 " potNull.hashCode(); // OK: pot null, is reported\n" +
18035 " ^^^^^^^\n" +
18036 "Potential null pointer access: The variable potNull may be null at this location\n" +
18037 "----------\n"
18038 );
18039 }
18040 public void testBug447695() {
18041 runConformTest(
18042 new String[] {
18043 "test/Test447695.java",
18044 "package test;\n" +
18045 "\n" +
18046 "public class Test447695 {\n" +
18047 " public static void f() {\n" +
18048 " int[] array = null;\n" +
18049 " (array = new int[1])[0] = 42;\n" +
18050 " }\n" +
18051 " public static int g() {\n" +
18052 " int[] array = null;\n" +
18053 " return (array = new int[1])[0];\n" +
18054 " }\n" +
18055 "}\n"
18056 }
18057 );
18058 }
18059 public void testBug447695b() {
18060 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses foreach
18061 runConformTest(
18062 new String[] {
18063 "X.java",
18064 "import java.util.*;\n" +
18065 "public class X {\n" +
18066 " void test(String[] ss) {\n" +
18067 " List<String> strings = null;\n" +
18068 " for (String s : (strings = Arrays.asList(ss)))\n" +
18069 " System.out.println(s);\n" +
18070 " }\n" +
18071 "}\n"
18072 });
18073 }
18074 public void testBug447695c() {
18075 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses autoboxing
18076 runConformTest(
18077 new String[] {
18078 "test/Test447695.java",
18079 "package test;\n" +
18080 "\n" +
18081 "public class Test447695 {\n" +
18082 " void f() {\n" +
18083 " Integer l1 = null;\n" +
18084 " Integer l2 = null;\n" +
18085 " int b = (l1 = new Integer(2)) + (l2 = new Integer(1));\n" +
18086 " }\n" +
18087 "}\n"
18088 }
18089 );
18090 }
18091 public void testBug447695d() {
18092 if (this.complianceLevel < ClassFileConstants.JDK1_8) return; // uses reference expression
18093 runConformTest(
18094 new String[] {
18095 "test/Test447695.java",
18096 "package test;\n" +
18097 "\n" +
18098 "import java.util.function.Supplier;\n" +
18099 "\n" +
18100 "public class Test447695 {\n" +
18101 " void f() {\n" +
18102 " String s = null;\n" +
18103 " Supplier<String> l = (s = \"\")::toString;\n" +
18104 " }\n" +
18105 "}\n"
18106 }
18107 );
18108 }
18109 public void testBug447695e() {
18110 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses autoboxing
18111 runConformTest(
18112 new String[] {
18113 "test/Test447695.java",
18114 "package test;\n" +
18115 "\n" +
18116 "public class Test447695 {\n" +
18117 " void f() {\n" +
18118 " Integer i = null;\n" +
18119 " int j = -(i = new Integer(1));\n" +
18120 " Boolean b1 = null;\n" +
18121 " boolean b = !(b1 = new Boolean(false));\n" +
18122 " }\n" +
18123 "}\n"
18124 }
18125 );
18126 }
18127 public void testBug447695f() {
18128 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses autoboxing
18129 runConformTest(
18130 new String[] {
18131 "test/Test447695.java",
18132 "package test;\n" +
18133 "\n" +
18134 "public class Test447695 {\n" +
18135 " void f() {\n" +
18136 " int i = 0;\n" +
18137 " Integer i1 = null;\n" +
18138 " Integer i2 = null;\n" +
18139 " Integer i3 = null;\n" +
18140 " int j = (i1 = new Integer(1)) \n" +
18141 " + (i2 = new Integer(1)) \n" +
18142 " + i + i + i + i + i + i + i + i + i + i + i + i + i + i + i + i + i + i + i \n" +
18143 " + (i3 = new Integer(2)) + i;\n" +
18144 " }\n" +
18145 "}\n"
18146 }
18147 );
18148 }
18149 public void testBug447695g() {
18150 runNegativeNullTest(
18151 new String[] {
18152 "test/Test447695.java",
18153 "package test;\n" +
18154 "\n" +
18155 "class X {\n" +
18156 " int i;\n" +
18157 "}\n" +
18158 "\n" +
18159 "public class Test447695 {\n" +
18160 " void f() {\n" +
18161 " X x1 = null;\n" +
18162 " X x2 = null;\n" +
18163 " X x3 = null;\n" +
18164 " X x4 = null;\n" +
18165 " X x5 = null;\n" +
18166 " X x6 = null;\n" +
18167 " X x7 = null;\n" +
18168 " X x8 = null;\n" +
18169 " X x9 = null;\n" +
18170 " X x10 = null;\n" +
18171 " X x11 = null;\n" +
18172 " x1.i = 1; // error 1 expected\n" +
18173 " x2.i += 1; // error 2 expected\n" +
18174 " (x3).i = 1; // error 3 expected\n" +
18175 " (x4).i += 1; // error 4 expected\n" +
18176 " (x5 = new X()).i = (x6 = new X()).i;\n" +
18177 " (x7 = new X()).i += (x8 = new X()).i;\n" +
18178 " int i1 = x9.i; // error 5 expected\n" +
18179 " int i2 = (x10).i; // error 6 expected\n" +
18180 " int i3 = (x11 = new X()).i;\n" +
18181 " }\n" +
18182 "}\n"
18183 },
18184 "----------\n" +
18185 "1. ERROR in test\\Test447695.java (at line 20)\n" +
18186 " x1.i = 1; // error 1 expected\n" +
18187 " ^^\n" +
18188 "Null pointer access: The variable x1 can only be null at this location\n" +
18189 "----------\n" +
18190 "2. ERROR in test\\Test447695.java (at line 21)\n" +
18191 " x2.i += 1; // error 2 expected\n" +
18192 " ^^\n" +
18193 "Null pointer access: The variable x2 can only be null at this location\n" +
18194 "----------\n" +
18195 "3. ERROR in test\\Test447695.java (at line 22)\n" +
18196 " (x3).i = 1; // error 3 expected\n" +
18197 " ^^^^\n" +
18198 "Null pointer access: The variable x3 can only be null at this location\n" +
18199 "----------\n" +
18200 "4. ERROR in test\\Test447695.java (at line 23)\n" +
18201 " (x4).i += 1; // error 4 expected\n" +
18202 " ^^^^\n" +
18203 "Null pointer access: The variable x4 can only be null at this location\n" +
18204 "----------\n" +
18205 "5. ERROR in test\\Test447695.java (at line 26)\n" +
18206 " int i1 = x9.i; // error 5 expected\n" +
18207 " ^^\n" +
18208 "Null pointer access: The variable x9 can only be null at this location\n" +
18209 "----------\n" +
18210 "6. ERROR in test\\Test447695.java (at line 27)\n" +
18211 " int i2 = (x10).i; // error 6 expected\n" +
18212 " ^^^^^\n" +
18213 "Null pointer access: The variable x10 can only be null at this location\n" +
18214 "----------\n"
18215 );
18216 }
18217 public void testBug509188() {
18218 runConformTest(
18219 new String[] {
18220 "test/Bug509188.java",
18221 "package test;\n" +
18222 "\n" +
18223 "public class Bug509188 {\n" +
18224 " public static class QuinamidCell {\n" +
18225 " public QuinamidCell next;\n" +
18226 " }\n" +
18227 "\n" +
18228 " public static void drawBoardElements() {\n" +
18229 " Object _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z,\n" +
18230 " _a1, _b1, _c1, _d1, _e1, _f1, _g1, _h1, _i1, _j1, _k1, _l1, _m1, _n1, _o1, _p1, _q1, _r1, _s1, _t1, _u1,\n" +
18231 " _v1, _w1, _x1, _y1, _z_1, _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i2, _j2, _k2;\n" +
18232 "\n" +
18233 " QuinamidCell hitCell = null;\n" +
18234 "\n" +
18235 " int level = 0; while (level < 1) {\n" +
18236 " for (QuinamidCell c = new QuinamidCell(); c != null; c = c.next) {\n" +
18237 " hitCell = c;\n" +
18238 " } level++;\n" +
18239 " }\n" +
18240 " if (hitCell != null) {\n" +
18241 " System.out.println(\"not dead\");\n" +
18242 " }\n" +
18243 " }\n" +
18244 "\n" +
18245 " public static void main(String[] args) {\n" +
18246 " drawBoardElements();\n" +
18247 " }\n" +
18248 "}\n" +
18249 "",
18250 },
18251 "not dead"
18252 );
18253 }
18254 public void testBug536408() {
18255 if (this.complianceLevel < ClassFileConstants.JDK1_5)
18256 return; // uses auto unboxing
18257 Runner runner = new Runner();
18258 runner.testFiles =
18259 new String[] {
18260 "X.java",
18261 "public class X {\n" +
18262 " public static void main(String[] args) {\n" +
18263 " Long s1 = null;\n" +
18264 " long t = 0;\n" +
18265 " t += s1;\n" +
18266 " Long s2 = t > 0 ? 1l : null;\n" +
18267 " t += s2;\n" +
18268 " }\n" +
18269 "}\n"
18270 };
18271 runner.expectedCompilerLog =
18272 "----------\n" +
18273 "1. ERROR in X.java (at line 5)\n" +
18274 " t += s1;\n" +
18275 " ^^\n" +
18276 "Null pointer access: This expression of type Long is null but requires auto-unboxing\n" +
18277 "----------\n" +
18278 "2. ERROR in X.java (at line 7)\n" +
18279 " t += s2;\n" +
18280 " ^^\n" +
18281 "Potential null pointer access: This expression of type Long may be null but requires auto-unboxing\n" +
18282 "----------\n";
18283 runner.javacTestOptions = JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
18284 runner.runNegativeTest();
18285 }
18286 public void testBug542707_1() {
18287 if (!checkPreviewAllowed()) return; // switch expression
18288 Runner runner = new Runner();
18289 runner.customOptions = new HashMap<>();
18290 runner.customOptions.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
18291 runner.customOptions.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
18292 runner.testFiles = new String[] {
18293 "X.java",
18294 "public class X {\n" +
18295 " void test(int i) {\n" +
18296 " String s = switch (i) {\n" +
18297 " case 1 -> \"one\";\n" +
18298 " default -> null;\n" +
18299 " };\n" +
18300 " System.out.println(s.toLowerCase());\n" +
18301 " }\n" +
18302 "}\n"
18303 };
18304 runner.expectedCompilerLog =
18305 "----------\n" +
18306 "1. ERROR in X.java (at line 7)\n" +
18307 " System.out.println(s.toLowerCase());\n" +
18308 " ^\n" +
18309 "Potential null pointer access: The variable s may be null at this location\n" +
18310 "----------\n";
18311 runner.runNegativeTest();
18312 }
18313 public void testBug544872() {
18314 runNegativeNullTest(
18315 new String[] {
18316 "Test.java",
18317 "public class Test {\n" +
18318 " static void f(String string) {\n" +
18319 " if (string != null)\n" +
18320 " string.hashCode();\n" +
18321 " synchronized (string) {\n" +
18322 " string.hashCode();\n" +
18323 " }\n" +
18324 " }\n" +
18325 "}\n" +
18326 ""
18327 },
18328 "----------\n" +
18329 "1. ERROR in Test.java (at line 5)\n" +
18330 " synchronized (string) {\n" +
18331 " ^^^^^^\n" +
18332 "Potential null pointer access: The variable string may be null at this location\n" +
18333 "----------\n"
18334 );
18335 }
18336 public void testBug551012() {
18337 runNegativeNullTest(
18338 new String[] {
18339 "NullConstants.java",
18340 "public class NullConstants {\n" +
18341 " protected static final String FOO = null;\n" +
18342 "\n" +
18343 " protected String foo = FOO;\n" +
18344 "\n" +
18345 " protected static final String BAR = \"\";\n" +
18346 "\n" +
18347 " protected String bar = BAR;\n" +
18348 "\n" +
18349 " public boolean notAProblemButWhyNot() {\n" +
18350 " return FOO == null ? foo != null : !FOO.equals(foo);\n" +
18351 " }\n" +
18352 "\n" +
18353 " public boolean alsoNotAProblemButThisWillAlwaysNPE() {\n" +
18354 " return FOO != null ? foo != null : !FOO.equals(foo);\n" +
18355 " }\n" +
18356 "\n" +
18357 " public boolean aProblemButHowToAvoid() {\n" +
18358 " return BAR == null ? bar != null : !BAR.equals(bar);\n" +
18359 " }\n" +
18360 "\n" +
18361 " public boolean wrongpProblemMessage() {\n" +
18362 " return BAR != null ? !BAR.equals(bar) : bar != null;\n" +
18363 " }\n" +
18364 "\n" +
18365 " public boolean howAboutThis() {\n" +
18366 " return bar == null ? BAR != null : bar.equals(BAR);\n" +
18367 " }\n" +
18368 "}\n"
18369 },
18370 "----------\n" +
18371 "1. ERROR in NullConstants.java (at line 19)\n" +
18372 " return BAR == null ? bar != null : !BAR.equals(bar);\n" +
18373 " ^^^\n" +
18374 "Null comparison always yields false: The field BAR is a nonnull constant\n" +
18375 "----------\n" +
18376 "2. ERROR in NullConstants.java (at line 23)\n" +
18377 " return BAR != null ? !BAR.equals(bar) : bar != null;\n" +
18378 " ^^^\n" +
18379 "Redundant null check: The field BAR is a nonnull constant\n" +
18380 "----------\n" +
18381 "3. ERROR in NullConstants.java (at line 27)\n" +
18382 " return bar == null ? BAR != null : bar.equals(BAR);\n" +
18383 " ^^^\n" +
18384 "Redundant null check: The field BAR is a nonnull constant\n" +
18385 "----------\n");
18386 }
18387 public void testBug561280() {
18388 if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // uses generics
18389 runConformTest(
18390 new String[] {
18391 "test/Test.java",
18392 "package test;\n" +
18393 "\n" +
18394 "import java.util.List;\n" +
18395 "import java.util.Set;\n" +
18396 "\n" +
18397 "public class Test\n" +
18398 "{\n" +
18399 " protected static final String ERROR_TYPE = \"error\";\n" +
18400 " protected static final String OBJECT_TYPE = \"object\";\n" +
18401 " protected static final String UNKNOWN_FEATURE_TYPE = \"unknownFeature\";\n" +
18402 " protected static final String DOCUMENT_ROOT_TYPE = \"documentRoot\";\n" +
18403 "\n" +
18404 " protected final static String TYPE_ATTRIB = \"\";\n" +
18405 " protected final static String NIL_ATTRIB = \"\";\n" +
18406 " protected final static String SCHEMA_LOCATION_ATTRIB = \"\";\n" +
18407 " protected final static String NO_NAMESPACE_SCHEMA_LOCATION_ATTRIB = \"\";\n" +
18408 "\n" +
18409 " protected final static boolean DEBUG_DEMANDED_PACKAGES = false;\n" +
18410 "\n" +
18411 "\n" +
18412 " protected Object xmlResource;\n" +
18413 " protected Object helper;\n" +
18414 " protected Object elements;\n" +
18415 " protected Object objects;\n" +
18416 " protected Object types;\n" +
18417 " protected Object mixedTargets;\n" +
18418 " protected Object prefixesToFactories;\n" +
18419 " protected Object urisToLocations;\n" +
18420 " protected Object externalURIToLocations;\n" +
18421 " protected boolean processSchemaLocations;\n" +
18422 " protected Object extent;\n" +
18423 " protected Object deferredExtent;\n" +
18424 " protected Object resourceSet;\n" +
18425 " protected Object packageRegistry;\n" +
18426 " protected Object resourceURI;\n" +
18427 " protected boolean resolve;\n" +
18428 " protected boolean oldStyleProxyURIs;\n" +
18429 " protected boolean disableNotify;\n" +
18430 " protected StringBuffer text;\n" +
18431 " protected boolean isIDREF;\n" +
18432 " protected boolean isSimpleFeature;\n" +
18433 " protected Object sameDocumentProxies;\n" +
18434 " protected Object[] identifiers;\n" +
18435 " protected int[] positions;\n" +
18436 " protected static final int ARRAY_SIZE = 64;\n" +
18437 " protected static final int REFERENCE_THRESHOLD = 5;\n" +
18438 " protected int capacity;\n" +
18439 " protected Set<String> notFeatures;\n" +
18440 " protected String idAttribute;\n" +
18441 " protected String hrefAttribute;\n" +
18442 " protected Object xmlMap;\n" +
18443 " protected Object extendedMetaData;\n" +
18444 " protected Object anyType;\n" +
18445 " protected Object anySimpleType;\n" +
18446 " protected boolean recordUnknownFeature;\n" +
18447 " protected boolean useNewMethods;\n" +
18448 " protected boolean recordAnyTypeNSDecls;\n" +
18449 " protected Object eObjectToExtensionMap;\n" +
18450 " protected Object contextFeature;\n" +
18451 " protected Object xmlSchemaTypePackage = null;\n" +
18452 " protected boolean deferIDREFResolution;\n" +
18453 " protected boolean processAnyXML;\n" +
18454 " protected Object ecoreBuilder;\n" +
18455 " protected boolean isRoot;\n" +
18456 " protected Object locator;\n" +
18457 " protected Object attribs;\n" +
18458 " protected boolean useConfigurationCache;\n" +
18459 " protected boolean needsPushContext;\n" +
18460 " protected Object resourceEntityHandler;\n" +
18461 " protected Object uriHandler;\n" +
18462 " protected Object documentRoot;\n" +
18463 " protected boolean usedNullNamespacePackage;\n" +
18464 " protected boolean isNamespaceAware;\n" +
18465 " protected boolean suppressDocumentRoot;\n" +
18466 " protected boolean laxWildcardProcessing;\n" +
18467 "\n" +
18468 " protected static void processObjectx(Object object)\n" +
18469 " {\n" +
18470 " if (object instanceof List)\n" +
18471 " {\n" +
18472 " List<?> list = ((List<?>)object);\n" +
18473 " list.size();\n" +
18474 " }\n" +
18475 "\n" +
18476 " if (object != null)\n" +
18477 " {\n" +
18478 " object.hashCode();\n" +
18479 " }\n" +
18480 " else\n" +
18481 " {\n" +
18482 " System.err.println(\"#\");\n" +
18483 " }\n" +
18484 " }\n" +
18485 "}\n"
18486 });
18487 }
18488 }