Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010, 2011 GK Software AG and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Stephan Herrmann - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.compiler.regression; |
12 |
|
13 |
|
14 |
import java.io.File; |
15 |
import java.util.Map; |
16 |
|
17 |
import junit.framework.Test; |
18 |
|
19 |
import org.eclipse.core.runtime.FileLocator; |
20 |
import org.eclipse.core.runtime.Platform; |
21 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
22 |
|
23 |
public class NullAnnotationTest extends AbstractComparableTest { |
24 |
|
25 |
// class libraries including our default null annotation types: |
26 |
String[] LIBS; |
27 |
|
28 |
// names and content of custom annotations used in a few tests: |
29 |
private static final String CUSTOM_NONNULL_NAME = "org/foo/NonNull.java"; |
30 |
private static final String CUSTOM_NONNULL_CONTENT = |
31 |
"package org.foo;\n" + |
32 |
"import static java.lang.annotation.ElementType.*;\n" + |
33 |
"import java.lang.annotation.*;\n" + |
34 |
"@Retention(RetentionPolicy.CLASS)\n" + |
35 |
"@Target({METHOD,PARAMETER,LOCAL_VARIABLE})\n" + |
36 |
"public @interface NonNull {\n" + |
37 |
"}\n"; |
38 |
private static final String CUSTOM_NULLABLE_NAME = "org/foo/Nullable.java"; |
39 |
private static final String CUSTOM_NULLABLE_CONTENT = "package org.foo;\n" + |
40 |
"import static java.lang.annotation.ElementType.*;\n" + |
41 |
"import java.lang.annotation.*;\n" + |
42 |
"@Retention(RetentionPolicy.CLASS)\n" + |
43 |
"@Target({METHOD,PARAMETER,LOCAL_VARIABLE})\n" + |
44 |
"public @interface Nullable {\n" + |
45 |
"}\n"; |
46 |
|
47 |
public NullAnnotationTest(String name) { |
48 |
super(name); |
49 |
} |
50 |
|
51 |
// Static initializer to specify tests subset using TESTS_* static variables |
52 |
// All specified tests which do not belong to the class are skipped... |
53 |
static { |
54 |
// TESTS_NAMES = new String[] { "test_nonnull_var_in_constrol_structure_3" }; |
55 |
// TESTS_NUMBERS = new int[] { 561 }; |
56 |
// TESTS_RANGE = new int[] { 1, 2049 }; |
57 |
} |
58 |
|
59 |
public static Test suite() { |
60 |
return buildComparableTestSuite(testClass()); |
61 |
} |
62 |
|
63 |
public static Class testClass() { |
64 |
return NullAnnotationTest.class; |
65 |
} |
66 |
|
67 |
protected void setUp() throws Exception { |
68 |
super.setUp(); |
69 |
if (this.LIBS == null) { |
70 |
String[] defaultLibs = getDefaultClassPaths(); |
71 |
int len = defaultLibs.length; |
72 |
this.LIBS = new String[len+1]; |
73 |
System.arraycopy(defaultLibs, 0, this.LIBS, 0, len); |
74 |
File bundleFile = FileLocator.getBundleFile(Platform.getBundle("org.eclipse.jdt.annotation.null")); |
75 |
if (bundleFile.isDirectory()) |
76 |
this.LIBS[len] = bundleFile.getPath()+"/bin"; |
77 |
else |
78 |
this.LIBS[len] = bundleFile.getPath(); |
79 |
} |
80 |
} |
81 |
// Conditionally augment problem detection settings |
82 |
static boolean setNullRelatedOptions = true; |
83 |
protected Map getCompilerOptions() { |
84 |
Map defaultOptions = super.getCompilerOptions(); |
85 |
if (setNullRelatedOptions) { |
86 |
defaultOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
87 |
defaultOptions.put(CompilerOptions.OPTION_ReportPotentialNullReference, CompilerOptions.ERROR); |
88 |
defaultOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR); |
89 |
defaultOptions.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE); |
90 |
defaultOptions.put(CompilerOptions.OPTION_IncludeNullInfoFromAsserts, CompilerOptions.ENABLED); |
91 |
|
92 |
defaultOptions.put(CompilerOptions.OPTION_ReportMissingOverrideAnnotationForInterfaceMethodImplementation, CompilerOptions.DISABLED); |
93 |
|
94 |
// enable null annotations: |
95 |
defaultOptions.put(CompilerOptions.OPTION_AnnotationBasedNullAnalysis, CompilerOptions.ENABLED); |
96 |
// leave other new options at these defaults: |
97 |
// defaultOptions.put(CompilerOptions.OPTION_ReportNullContractViolation, CompilerOptions.ERROR); |
98 |
// defaultOptions.put(CompilerOptions.OPTION_ReportPotentialNullContractViolation, CompilerOptions.ERROR); |
99 |
// defaultOptions.put(CompilerOptions.OPTION_ReportNullContractInsufficientInfo, CompilerOptions.WARNING); |
100 |
|
101 |
// defaultOptions.put(CompilerOptions.OPTION_NullableAnnotationName, "org.eclipse.jdt.annotation.Nullable"); |
102 |
// defaultOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.eclipse.jdt.annotation.NonNull"); |
103 |
} |
104 |
return defaultOptions; |
105 |
} |
106 |
void runNegativeTestWithLibs(String[] testFiles, String expectedErrorLog) { |
107 |
runNegativeTest( |
108 |
testFiles, |
109 |
expectedErrorLog, |
110 |
this.LIBS, |
111 |
false /*shouldFlush*/); |
112 |
} |
113 |
void runNegativeTestWithLibs(boolean shouldFlushOutputDirectory, String[] testFiles, Map customOptions, String expectedErrorLog) { |
114 |
runNegativeTest( |
115 |
shouldFlushOutputDirectory, |
116 |
testFiles, |
117 |
this.LIBS, |
118 |
customOptions, |
119 |
expectedErrorLog, |
120 |
// runtime options |
121 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
122 |
} |
123 |
void runNegativeTestWithLibs(String[] testFiles, Map customOptions, String expectedErrorLog) { |
124 |
runNegativeTestWithLibs(false /* flush output directory */, testFiles, customOptions, expectedErrorLog); |
125 |
} |
126 |
void runConformTestWithLibs(String[] testFiles, Map customOptions, String expectedCompilerLog) { |
127 |
runConformTestWithLibs(false /* flush output directory */, testFiles, customOptions, expectedCompilerLog); |
128 |
} |
129 |
void runConformTestWithLibs(boolean shouldFlushOutputDirectory, String[] testFiles, Map customOptions, String expectedCompilerLog) { |
130 |
runConformTest( |
131 |
shouldFlushOutputDirectory, |
132 |
testFiles, |
133 |
this.LIBS, |
134 |
customOptions, |
135 |
expectedCompilerLog, |
136 |
"",/* expected output */ |
137 |
"",/* expected error */ |
138 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
139 |
} |
140 |
void runConformTest(String[] testFiles, Map customOptions, String expectedOutputString) { |
141 |
runConformTest( |
142 |
testFiles, |
143 |
expectedOutputString, |
144 |
null /*classLibraries*/, |
145 |
true /*shouldFlushOutputDirectory*/, |
146 |
null /*vmArguments*/, |
147 |
customOptions, |
148 |
null /*customRequestor*/); |
149 |
|
150 |
} |
151 |
// a nullable argument is dereferenced without a check |
152 |
public void test_nullable_paramter_001() { |
153 |
runNegativeTest( |
154 |
new String[] { |
155 |
"X.java", |
156 |
"import org.eclipse.jdt.annotation.*;\n" + |
157 |
"public class X {\n" + |
158 |
" void foo(@Nullable Object o) {\n" + |
159 |
" System.out.print(o.toString());\n" + |
160 |
" }\n" + |
161 |
"}\n"}, |
162 |
"----------\n" + |
163 |
"1. ERROR in X.java (at line 4)\n" + |
164 |
" System.out.print(o.toString());\n" + |
165 |
" ^\n" + |
166 |
"Potential null pointer access: The variable o may be null at this location\n" + |
167 |
"----------\n", |
168 |
this.LIBS, |
169 |
true /* shouldFlush*/); |
170 |
} |
171 |
|
172 |
// a null value is passed to a nullable argument |
173 |
public void test_nullable_paramter_002() { |
174 |
runConformTest( |
175 |
new String[] { |
176 |
"X.java", |
177 |
"import org.eclipse.jdt.annotation.*;\n" + |
178 |
"public class X {\n" + |
179 |
" void foo(@Nullable Object o) {\n" + |
180 |
" // nop\n" + |
181 |
" }\n" + |
182 |
" void bar() {\n" + |
183 |
" foo(null);\n" + |
184 |
" }\n" + |
185 |
"}\n"}, |
186 |
"", |
187 |
this.LIBS, |
188 |
false/*shouldFlush*/, |
189 |
null/*vmArgs*/); |
190 |
} |
191 |
|
192 |
// a non-null argument is checked for null |
193 |
public void test_nonnull_parameter_001() { |
194 |
runNegativeTest( |
195 |
new String[] { |
196 |
"X.java", |
197 |
"import org.eclipse.jdt.annotation.*;\n" + |
198 |
"public class X {\n" + |
199 |
" void foo(@NonNull Object o) {\n" + |
200 |
" if (o != null)\n" + |
201 |
" System.out.print(o.toString());\n" + |
202 |
" }\n" + |
203 |
"}\n"}, |
204 |
"----------\n" + |
205 |
"1. ERROR in X.java (at line 4)\n" + |
206 |
" if (o != null)\n" + |
207 |
" ^\n" + |
208 |
"Redundant null check: The variable o cannot be null at this location\n" + |
209 |
"----------\n", |
210 |
this.LIBS, |
211 |
true /* shouldFlush*/); |
212 |
} |
213 |
// a non-null argument is dereferenced without a check |
214 |
public void test_nonnull_parameter_002() { |
215 |
runConformTest( |
216 |
new String[] { |
217 |
"X.java", |
218 |
"import org.eclipse.jdt.annotation.*;\n" + |
219 |
"public class X {\n" + |
220 |
" void foo(@NonNull Object o) {\n" + |
221 |
" System.out.print(o.toString());\n" + |
222 |
" }\n" + |
223 |
" public static void main(String... args) {\n" + |
224 |
" new X().foo(\"OK\");\n" + |
225 |
" }\n" + |
226 |
"}\n"}, |
227 |
"OK", |
228 |
this.LIBS, |
229 |
false/*shouldFlush*/, |
230 |
null/*vmArgs*/); |
231 |
} |
232 |
// passing null to nonnull parameter - many fields in enclosing class |
233 |
public void test_nonnull_parameter_003() { |
234 |
runNegativeTest( |
235 |
new String[] { |
236 |
"X.java", |
237 |
"import org.eclipse.jdt.annotation.*;\n" + |
238 |
"public class X {\n" + |
239 |
" int i00, i01, i02, i03, i04, i05, i06, i07, i08, i09;" + |
240 |
" int i10, i11, i12, i13, i14, i15, i16, i17, i18, i19;" + |
241 |
" int i20, i21, i22, i23, i24, i25, i26, i27, i28, i29;" + |
242 |
" int i30, i31, i32, i33, i34, i35, i36, i37, i38, i39;" + |
243 |
" int i40, i41, i42, i43, i44, i45, i46, i47, i48, i49;" + |
244 |
" int i50, i51, i52, i53, i54, i55, i56, i57, i58, i59;" + |
245 |
" int i60, i61, i62, i63, i64, i65, i66, i67, i68, i69;" + |
246 |
" void foo(@NonNull Object o) {\n" + |
247 |
" System.out.print(o.toString());\n" + |
248 |
" }\n" + |
249 |
" void bar() {\n" + |
250 |
" foo(null);\n" + |
251 |
" }\n" + |
252 |
"}\n"}, |
253 |
"----------\n" + |
254 |
"1. ERROR in X.java (at line 7)\n" + |
255 |
" foo(null);\n" + |
256 |
" ^^^^\n" + |
257 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
258 |
"----------\n", |
259 |
this.LIBS, |
260 |
true /* shouldFlush*/); |
261 |
} |
262 |
// passing potential null to nonnull parameter - target method is consumed from .class |
263 |
public void test_nonnull_parameter_004() { |
264 |
runConformTestWithLibs( |
265 |
new String[] { |
266 |
"Lib.java", |
267 |
"import org.eclipse.jdt.annotation.*;\n" + |
268 |
"public class Lib {\n" + |
269 |
" void setObject(@NonNull Object o) { }\n" + |
270 |
"}\n" |
271 |
}, |
272 |
null /*customOptions*/, |
273 |
""); |
274 |
runNegativeTestWithLibs( |
275 |
false, // don't flush |
276 |
new String[] { |
277 |
"X.java", |
278 |
"public class X {\n" + |
279 |
" void bar(Lib l, boolean b) {\n" + |
280 |
" Object o = null;\n" + |
281 |
" if (b) o = new Object();\n" + |
282 |
" l.setObject(o);\n" + |
283 |
" }\n" + |
284 |
"}\n"}, |
285 |
null /*customOptions*/, |
286 |
"----------\n" + |
287 |
"1. ERROR in X.java (at line 5)\n" + |
288 |
" l.setObject(o);\n" + |
289 |
" ^\n" + |
290 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
291 |
"----------\n"); |
292 |
} |
293 |
// passing unknown value to nonnull parameter - target method is consumed from .class |
294 |
public void test_nonnull_parameter_005() { |
295 |
runConformTestWithLibs( |
296 |
new String[] { |
297 |
"Lib.java", |
298 |
"import org.eclipse.jdt.annotation.*;\n" + |
299 |
"public class Lib {\n" + |
300 |
" void setObject(@NonNull Object o) { }\n" + |
301 |
"}\n" |
302 |
}, |
303 |
null /*customOptions*/, |
304 |
""); |
305 |
runConformTestWithLibs( |
306 |
false, // don't flush |
307 |
new String[] { |
308 |
"X.java", |
309 |
"public class X {\n" + |
310 |
" void bar(Lib l, Object o) {\n" + |
311 |
" l.setObject(o);\n" + |
312 |
" }\n" + |
313 |
"}\n"}, |
314 |
null /* options */, |
315 |
"----------\n" + |
316 |
"1. WARNING in X.java (at line 3)\n" + |
317 |
" l.setObject(o);\n" + |
318 |
" ^\n" + |
319 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
320 |
"----------\n"); |
321 |
} |
322 |
// a ternary non-null expression is passed to a nonnull parameter |
323 |
public void test_nonnull_parameter_006() { |
324 |
Map customOptions = getCompilerOptions(); |
325 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
326 |
runConformTestWithLibs( |
327 |
new String[] { |
328 |
"X.java", |
329 |
"import org.eclipse.jdt.annotation.*;\n" + |
330 |
"public class X {\n" + |
331 |
" void m1(@NonNull String a) {}\n" + |
332 |
" void m2(@Nullable String b) {\n" + |
333 |
" m1(b == null ? \"\" : b);\n" + |
334 |
" }\n" + |
335 |
"}\n"}, |
336 |
customOptions, |
337 |
"" /* compiler output */); |
338 |
} |
339 |
// nullable value passed to a non-null parameter in a super-call |
340 |
public void test_nonnull_parameter_007() { |
341 |
Map customOptions = getCompilerOptions(); |
342 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
343 |
runNegativeTestWithLibs( |
344 |
new String[] { |
345 |
"XSub.java", |
346 |
"import org.eclipse.jdt.annotation.*;\n" + |
347 |
"public class XSub extends XSuper {\n" + |
348 |
" XSub(@Nullable String b) {\n" + |
349 |
" super(b);\n" + |
350 |
" }\n" + |
351 |
"}\n", |
352 |
"XSuper.java", |
353 |
"import org.eclipse.jdt.annotation.*;\n" + |
354 |
"public class XSuper {\n" + |
355 |
" XSuper(@NonNull String b) {\n" + |
356 |
" }\n" + |
357 |
"}\n" |
358 |
}, |
359 |
customOptions, |
360 |
"----------\n" + |
361 |
"1. ERROR in XSub.java (at line 4)\n" + |
362 |
" super(b);\n" + |
363 |
" ^\n" + |
364 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
365 |
"----------\n"); |
366 |
} |
367 |
// a nullable value is passed to a non-null parameter in an allocation expression |
368 |
public void test_nonnull_parameter_008() { |
369 |
Map customOptions = getCompilerOptions(); |
370 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
371 |
runNegativeTestWithLibs( |
372 |
new String[] { |
373 |
"X.java", |
374 |
"import org.eclipse.jdt.annotation.*;\n" + |
375 |
"public class X {\n" + |
376 |
" X(@NonNull String a) {}\n" + |
377 |
" static X create(@Nullable String b) {\n" + |
378 |
" return new X(b);\n" + |
379 |
" }\n" + |
380 |
"}\n"}, |
381 |
customOptions, |
382 |
"----------\n" + |
383 |
"1. ERROR in X.java (at line 5)\n" + |
384 |
" return new X(b);\n" + |
385 |
" ^\n" + |
386 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
387 |
"----------\n" /* compiler output */); |
388 |
} |
389 |
// a nullable value is passed to a non-null parameter in a qualified allocation expression |
390 |
public void test_nonnull_parameter_009() { |
391 |
Map customOptions = getCompilerOptions(); |
392 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
393 |
runNegativeTestWithLibs( |
394 |
new String[] { |
395 |
"X.java", |
396 |
"import org.eclipse.jdt.annotation.*;\n" + |
397 |
"public class X {\n" + |
398 |
" class Local {\n" + |
399 |
" Local(@NonNull String a) {}\n" + |
400 |
" }\n" + |
401 |
" Local create(@Nullable String b) {\n" + |
402 |
" return this.new Local(b);\n" + |
403 |
" }\n" + |
404 |
"}\n"}, |
405 |
customOptions, |
406 |
"----------\n" + |
407 |
"1. ERROR in X.java (at line 7)\n" + |
408 |
" return this.new Local(b);\n" + |
409 |
" ^\n" + |
410 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
411 |
"----------\n" /* compiler output */); |
412 |
} |
413 |
// assigning potential null to a nonnull local variable |
414 |
public void test_nonnull_local_001() { |
415 |
runNegativeTest( |
416 |
new String[] { |
417 |
"X.java", |
418 |
"import org.eclipse.jdt.annotation.*;\n" + |
419 |
"public class X {\n" + |
420 |
" void foo(boolean b, Object p) {\n" + |
421 |
" @NonNull Object o1 = b ? null : new Object();\n" + |
422 |
" @NonNull String o2 = \"\";\n" + |
423 |
" o2 = null;\n" + |
424 |
" @NonNull Object o3 = p;\n" + |
425 |
" }\n" + |
426 |
"}\n"}, |
427 |
"----------\n" + |
428 |
"1. ERROR in X.java (at line 4)\n" + |
429 |
" @NonNull Object o1 = b ? null : new Object();\n" + |
430 |
" ^^^^^^^^^^^^^^^^^^^^^^^\n" + |
431 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
432 |
"----------\n" + |
433 |
"2. ERROR in X.java (at line 6)\n" + |
434 |
" o2 = null;\n" + |
435 |
" ^^^^\n" + |
436 |
"Type mismatch: required \'@NonNull String\' but the provided value is null\n" + |
437 |
"----------\n" + |
438 |
"3. WARNING in X.java (at line 7)\n" + |
439 |
" @NonNull Object o3 = p;\n" + |
440 |
" ^\n" + |
441 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
442 |
"----------\n", |
443 |
this.LIBS, |
444 |
true /* shouldFlush*/); |
445 |
} |
446 |
|
447 |
// a method tries to tighten the type specification, super declares parameter o as @Nullable |
448 |
// other parameters: s is redefined from not constrained to @Nullable which is OK |
449 |
// third is redefined from not constrained to @NonNull which is bad, too |
450 |
public void test_parameter_specification_inheritance_001() { |
451 |
runConformTestWithLibs( |
452 |
new String[] { |
453 |
"Lib.java", |
454 |
"import org.eclipse.jdt.annotation.*;\n" + |
455 |
"public class Lib {\n" + |
456 |
" void foo(String s, @Nullable Object o, Object third) { }\n" + |
457 |
"}\n" |
458 |
}, |
459 |
null /*customOptions*/, |
460 |
""); |
461 |
runNegativeTestWithLibs( |
462 |
false, // don't flush |
463 |
new String[] { |
464 |
"X.java", |
465 |
"import org.eclipse.jdt.annotation.*;\n" + |
466 |
"public class X extends Lib {\n" + |
467 |
" @Override\n" + |
468 |
" void foo(@Nullable String s, @NonNull Object o, @NonNull Object third) { System.out.print(o.toString()); }\n" + |
469 |
"}\n" |
470 |
}, |
471 |
null /*customOptions*/, |
472 |
"----------\n" + |
473 |
"1. ERROR in X.java (at line 4)\n" + |
474 |
" void foo(@Nullable String s, @NonNull Object o, @NonNull Object third) { System.out.print(o.toString()); }\n" + |
475 |
" ^^^^^^^^^^^^^^^\n" + |
476 |
"Illegal redefinition of parameter o, inherited method from Lib declares this parameter as @Nullable\n" + |
477 |
"----------\n" + |
478 |
"2. ERROR in X.java (at line 4)\n" + |
479 |
" void foo(@Nullable String s, @NonNull Object o, @NonNull Object third) { System.out.print(o.toString()); }\n" + |
480 |
" ^^^^^^^^^^^^^^^\n" + |
481 |
"Illegal redefinition of parameter third, inherited method from Lib does not constrain this parameter\n" + |
482 |
"----------\n"); |
483 |
} |
484 |
// a method body fails to redeclare the inherited null annotation, super declares parameter as @Nullable |
485 |
public void test_parameter_specification_inheritance_002() { |
486 |
runConformTest( |
487 |
new String[] { |
488 |
"Lib.java", |
489 |
"import org.eclipse.jdt.annotation.*;\n" + |
490 |
"public class Lib {\n" + |
491 |
" void foo(@Nullable Object o) { }\n" + |
492 |
"}\n" |
493 |
}, |
494 |
"", |
495 |
this.LIBS, |
496 |
false/*shouldFlush*/, |
497 |
null/*vmArgs*/); |
498 |
runNegativeTestWithLibs( |
499 |
false, // don't flush |
500 |
new String[] { |
501 |
"X.java", |
502 |
"public class X extends Lib {\n" + |
503 |
" @Override\n" + |
504 |
" void foo(Object o) {\n" + |
505 |
" System.out.print(o.toString());\n" + |
506 |
" }\n" + |
507 |
"}\n" |
508 |
}, |
509 |
null /*customOptions*/, |
510 |
"----------\n" + |
511 |
"1. ERROR in X.java (at line 3)\n" + |
512 |
" void foo(Object o) {\n" + |
513 |
" ^^^^^^\n" + |
514 |
"Missing null annotation: inherited method from Lib declares this parameter as @Nullable\n" + |
515 |
"----------\n"); |
516 |
} |
517 |
// a method relaxes the parameter null specification, super interface declares parameter o as @NonNull |
518 |
// other (first) parameter just repeats the inherited @NonNull |
519 |
public void test_parameter_specification_inheritance_003() { |
520 |
runConformTest( |
521 |
new String[] { |
522 |
"IX.java", |
523 |
"import org.eclipse.jdt.annotation.*;\n" + |
524 |
"public interface IX {\n" + |
525 |
" void foo(@NonNull String s, @NonNull Object o);\n" + |
526 |
"}\n", |
527 |
"X.java", |
528 |
"import org.eclipse.jdt.annotation.*;\n" + |
529 |
"public class X implements IX {\n" + |
530 |
" public void foo(@NonNull String s, @Nullable Object o) { ; }\n" + |
531 |
" void bar() { foo(\"OK\", null); }\n" + |
532 |
"}\n" |
533 |
}, |
534 |
"", |
535 |
this.LIBS, |
536 |
false/*shouldFlush*/, |
537 |
null/*vmArgs*/); |
538 |
} |
539 |
// a method adds a @NonNull annotation, super interface has no null annotation |
540 |
// changing other from unconstrained to @Nullable is OK |
541 |
public void test_parameter_specification_inheritance_004() { |
542 |
runConformTest( |
543 |
new String[] { |
544 |
"IX.java", |
545 |
"public interface IX {\n" + |
546 |
" void foo(Object o, Object other);\n" + |
547 |
"}\n" |
548 |
}); |
549 |
runNegativeTestWithLibs( |
550 |
false, // don't flush |
551 |
new String[] { |
552 |
"X.java", |
553 |
"import org.eclipse.jdt.annotation.*;\n" + |
554 |
"public class X implements IX {\n" + |
555 |
" public void foo(@NonNull Object o, @Nullable Object other) { System.out.print(o.toString()); }\n" + |
556 |
"}\n" |
557 |
}, |
558 |
null /*customOptions*/, |
559 |
"----------\n" + |
560 |
"1. ERROR in X.java (at line 3)\n" + |
561 |
" public void foo(@NonNull Object o, @Nullable Object other) { System.out.print(o.toString()); }\n" + |
562 |
" ^^^^^^^^^^^^^^^\n" + |
563 |
"Illegal redefinition of parameter o, inherited method from IX does not constrain this parameter\n" + |
564 |
"----------\n"); |
565 |
} |
566 |
// a method tries to relax the null contract, super declares @NonNull return |
567 |
public void test_parameter_specification_inheritance_005() { |
568 |
runConformTestWithLibs( |
569 |
new String[] { |
570 |
"Lib.java", |
571 |
"import org.eclipse.jdt.annotation.*;\n" + |
572 |
"public class Lib {\n" + |
573 |
" @NonNull Object getObject() { return new Object(); }\n" + |
574 |
"}\n" |
575 |
}, |
576 |
null /*customOptions*/, |
577 |
""); |
578 |
runNegativeTestWithLibs( |
579 |
false, //dont' flush |
580 |
new String[] { |
581 |
"X.java", |
582 |
"import org.eclipse.jdt.annotation.*;\n" + |
583 |
"public class X extends Lib {\n" + |
584 |
" @Override\n" + |
585 |
" @Nullable Object getObject() { return null; }\n" + |
586 |
"}\n" |
587 |
}, |
588 |
null /*customOptions*/, |
589 |
"----------\n" + |
590 |
"1. ERROR in X.java (at line 4)\n" + |
591 |
" @Nullable Object getObject() { return null; }\n" + |
592 |
" ^^^^^^^^^^^^^^^^\n" + |
593 |
"The return type is incompatible with the @NonNull return from Lib.getObject()\n" + |
594 |
"----------\n"); |
595 |
} |
596 |
|
597 |
// super has no constraint for return, sub method confirms the null contract as @Nullable |
598 |
public void test_parameter_specification_inheritance_006() { |
599 |
runConformTest( |
600 |
new String[] { |
601 |
"Lib.java", |
602 |
"public class Lib {\n" + |
603 |
" Object getObject() { return null; }\n" + |
604 |
"}\n" |
605 |
}); |
606 |
runConformTestWithLibs( |
607 |
false, // don't flush |
608 |
new String[] { |
609 |
"X.java", |
610 |
"import org.eclipse.jdt.annotation.*;\n" + |
611 |
"public class X extends Lib {\n" + |
612 |
" @Override\n" + |
613 |
" @Nullable Object getObject() { return null; }\n" + |
614 |
"}\n" |
615 |
}, |
616 |
null /*customOptions*/, |
617 |
""); |
618 |
} |
619 |
// a method body violates the inherited null specification, super declares @NonNull return, missing redeclaration |
620 |
public void test_parameter_specification_inheritance_007() { |
621 |
runConformTestWithLibs( |
622 |
new String[] { |
623 |
"Lib.java", |
624 |
"import org.eclipse.jdt.annotation.*;\n" + |
625 |
"public class Lib {\n" + |
626 |
" @NonNull Object getObject() { return new Object(); }\n" + |
627 |
"}\n" |
628 |
}, |
629 |
null /*customOptions*/, |
630 |
""); |
631 |
runNegativeTestWithLibs( |
632 |
false, // don't flush |
633 |
new String[] { |
634 |
"X.java", |
635 |
"public class X extends Lib {\n" + |
636 |
" @Override\n" + |
637 |
" Object getObject() { return null; }\n" + |
638 |
"}\n" |
639 |
}, |
640 |
null /*customOptions*/, |
641 |
"----------\n" + |
642 |
"1. ERROR in X.java (at line 3)\n" + |
643 |
" Object getObject() { return null; }\n" + |
644 |
" ^^^^^^\n" + |
645 |
"The return type is incompatible with the @NonNull return from Lib.getObject()\n" + |
646 |
"----------\n"); |
647 |
} |
648 |
//a method body violates the @NonNull return specification (repeated from super) |
649 |
public void test_parameter_specification_inheritance_007a() { |
650 |
runConformTestWithLibs( |
651 |
new String[] { |
652 |
"Lib.java", |
653 |
"import org.eclipse.jdt.annotation.*;\n" + |
654 |
"public class Lib {\n" + |
655 |
" @NonNull Object getObject() { return new Object(); }\n" + |
656 |
"}\n" |
657 |
}, |
658 |
null /*customOptions*/, |
659 |
""); |
660 |
runNegativeTestWithLibs( |
661 |
false, // don't flush |
662 |
new String[] { |
663 |
"X.java", |
664 |
"import org.eclipse.jdt.annotation.*;\n" + |
665 |
"public class X extends Lib {\n" + |
666 |
" @Override\n" + |
667 |
" @NonNull Object getObject() { return null; }\n" + |
668 |
"}\n" |
669 |
}, |
670 |
null /*customOptions*/, |
671 |
"----------\n" + |
672 |
"1. ERROR in X.java (at line 4)\n" + |
673 |
" @NonNull Object getObject() { return null; }\n" + |
674 |
" ^^^^\n" + |
675 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
676 |
"----------\n"); |
677 |
} |
678 |
// a client potentially violates the inherited null specification, super interface declares @NonNull parameter |
679 |
public void test_parameter_specification_inheritance_008() { |
680 |
Map options = getCompilerOptions(); |
681 |
options.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
682 |
runConformTestWithLibs( |
683 |
new String[] { |
684 |
"IX.java", |
685 |
"import org.eclipse.jdt.annotation.*;\n" + |
686 |
"public interface IX {\n" + |
687 |
" void printObject(@NonNull Object o);\n" + |
688 |
"}\n" |
689 |
}, |
690 |
null /*customOptions*/, |
691 |
""); |
692 |
runNegativeTestWithLibs( |
693 |
false, // don't flush |
694 |
new String[] { |
695 |
"X.java", |
696 |
"public class X implements IX {\n" + |
697 |
" public void printObject(Object o) { System.out.print(o.toString()); }\n" + |
698 |
"}\n", |
699 |
"M.java", |
700 |
"public class M{\n" + |
701 |
" void foo(IX x, Object o) {\n" + |
702 |
" x.printObject(o);\n" + |
703 |
" }\n" + |
704 |
"}\n" |
705 |
}, |
706 |
options, |
707 |
"----------\n" + |
708 |
// additional error: |
709 |
"1. ERROR in X.java (at line 2)\n" + |
710 |
" public void printObject(Object o) { System.out.print(o.toString()); }\n" + |
711 |
" ^^^^^^\n" + |
712 |
"Missing null annotation: inherited method from IX declares this parameter as @NonNull\n" + |
713 |
"----------\n" + |
714 |
// main error: |
715 |
"----------\n" + |
716 |
"1. ERROR in M.java (at line 3)\n" + |
717 |
" x.printObject(o);\n" + |
718 |
" ^\n" + |
719 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
720 |
"----------\n"); |
721 |
} |
722 |
// a static method has a more relaxed null contract than a like method in the super class, but no overriding. |
723 |
public void test_parameter_specification_inheritance_009() { |
724 |
runConformTestWithLibs( |
725 |
new String[] { |
726 |
"Lib.java", |
727 |
"import org.eclipse.jdt.annotation.*;\n" + |
728 |
"public class Lib {\n" + |
729 |
" @NonNull static Object getObject() { return new Object(); }\n" + |
730 |
"}\n", |
731 |
"X.java", |
732 |
"import org.eclipse.jdt.annotation.*;\n" + |
733 |
"public class X extends Lib {\n" + |
734 |
" @Nullable static Object getObject() { return null; }\n" + |
735 |
"}\n" |
736 |
}, |
737 |
null /*customOptions*/, |
738 |
""); |
739 |
} |
740 |
// class default is nonnull, method and its super both use the default |
741 |
public void test_parameter_specification_inheritance_010() { |
742 |
Map customOptions = getCompilerOptions(); |
743 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
744 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
745 |
runConformTestWithLibs( |
746 |
new String[] { |
747 |
"p1/X.java", |
748 |
"package p1;\n" + |
749 |
"import org.eclipse.jdt.annotation.*;\n" + |
750 |
"@NonNullByDefault\n" + |
751 |
"public class X {\n" + |
752 |
" protected String getString(String s) {\n" + |
753 |
" if (Character.isLowerCase(s.charAt(0)))\n" + |
754 |
" return getString(s);\n" + |
755 |
" return s;\n" + |
756 |
" }\n" + |
757 |
"}\n", |
758 |
"p1/Y.java", |
759 |
"package p1;\n" + |
760 |
"import org.eclipse.jdt.annotation.*;\n" + |
761 |
"@NonNullByDefault\n" + |
762 |
"public class Y extends X {\n" + |
763 |
" @Override\n" + |
764 |
" protected String getString(String s) {\n" + |
765 |
" return super.getString(s);\n" + |
766 |
" }\n" + |
767 |
"}\n", |
768 |
}, |
769 |
customOptions, |
770 |
""); |
771 |
} |
772 |
// class default is nonnull, method and its super both use the default, super-call passes null |
773 |
public void test_parameter_specification_inheritance_011() { |
774 |
Map customOptions = getCompilerOptions(); |
775 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
776 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
777 |
runNegativeTestWithLibs( |
778 |
new String[] { |
779 |
"p1/X.java", |
780 |
"package p1;\n" + |
781 |
"import org.eclipse.jdt.annotation.*;\n" + |
782 |
"@NonNullByDefault\n" + |
783 |
"public class X {\n" + |
784 |
" protected String getString(String s) {\n" + |
785 |
" if (Character.isLowerCase(s.charAt(0)))\n" + |
786 |
" return getString(s);\n" + |
787 |
" return s;\n" + |
788 |
" }\n" + |
789 |
"}\n", |
790 |
"p1/Y.java", |
791 |
"package p1;\n" + |
792 |
"import org.eclipse.jdt.annotation.*;\n" + |
793 |
"@NonNullByDefault\n" + |
794 |
"public class Y extends X {\n" + |
795 |
" @Override\n" + |
796 |
" protected String getString(String s) {\n" + |
797 |
" return super.getString(null);\n" + |
798 |
" }\n" + |
799 |
"}\n", |
800 |
}, |
801 |
customOptions, |
802 |
"----------\n" + |
803 |
"1. ERROR in p1\\Y.java (at line 7)\n" + |
804 |
" return super.getString(null);\n" + |
805 |
" ^^^^\n" + |
806 |
"Type mismatch: required \'@NonNull String\' but the provided value is null\n" + |
807 |
"----------\n"); |
808 |
} |
809 |
// methods from two super types have different null contracts. |
810 |
// sub-class merges both using the weakest common contract |
811 |
public void test_parameter_specification_inheritance_012() { |
812 |
Map customOptions = getCompilerOptions(); |
813 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
814 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
815 |
runConformTestWithLibs( |
816 |
new String[] { |
817 |
"p1/X.java", |
818 |
"package p1;\n" + |
819 |
"import org.eclipse.jdt.annotation.*;\n" + |
820 |
"public class X {\n" + |
821 |
" public @Nullable String getString(String s1, @Nullable String s2, @NonNull String s3) {\n" + |
822 |
" return s1;\n" + |
823 |
" }\n" + |
824 |
"}\n", |
825 |
"p1/IY.java", |
826 |
"package p1;\n" + |
827 |
"import org.eclipse.jdt.annotation.*;\n" + |
828 |
"public interface IY {\n" + |
829 |
" @NonNull String getString(@NonNull String s1, @NonNull String s2, @Nullable String s3);\n" + |
830 |
"}\n", |
831 |
"p1/Y.java", |
832 |
"package p1;\n" + |
833 |
"import org.eclipse.jdt.annotation.*;\n" + |
834 |
"public class Y extends X implements IY {\n" + |
835 |
" @Override\n" + |
836 |
" public @NonNull String getString(@Nullable String s1, @Nullable String s2, @Nullable String s3) {\n" + |
837 |
" return \"\";\n" + |
838 |
" }\n" + |
839 |
"}\n", |
840 |
}, |
841 |
customOptions, |
842 |
""); |
843 |
} |
844 |
// methods from two super types have different null contracts. |
845 |
// sub-class overrides this method in non-conforming ways |
846 |
public void test_parameter_specification_inheritance_013() { |
847 |
Map customOptions = getCompilerOptions(); |
848 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
849 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
850 |
runNegativeTestWithLibs( |
851 |
new String[] { |
852 |
"p1/X.java", |
853 |
"package p1;\n" + |
854 |
"import org.eclipse.jdt.annotation.*;\n" + |
855 |
"public class X {\n" + |
856 |
" public @Nullable String getString(String s1, @Nullable String s2, @NonNull String s3) {\n" + |
857 |
" return s1;\n" + |
858 |
" }\n" + |
859 |
"}\n", |
860 |
"p1/IY.java", |
861 |
"package p1;\n" + |
862 |
"import org.eclipse.jdt.annotation.*;\n" + |
863 |
"public interface IY {\n" + |
864 |
" @NonNull String getString(@NonNull String s1, @NonNull String s2, @Nullable String s3);\n" + |
865 |
"}\n", |
866 |
"p1/Y.java", |
867 |
"package p1;\n" + |
868 |
"import org.eclipse.jdt.annotation.*;\n" + |
869 |
"public class Y extends X implements IY {\n" + |
870 |
" @Override\n" + |
871 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
872 |
" return \"\";\n" + |
873 |
" }\n" + |
874 |
"}\n", |
875 |
}, |
876 |
customOptions, |
877 |
"----------\n" + |
878 |
"1. ERROR in p1\\Y.java (at line 5)\n" + |
879 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
880 |
" ^^^^^^^^^^^^^^^^\n" + |
881 |
"The return type is incompatible with the @NonNull return from IY.getString(String, String, String)\n" + |
882 |
"----------\n" + |
883 |
"2. ERROR in p1\\Y.java (at line 5)\n" + |
884 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
885 |
" ^^^^^^\n" + |
886 |
"Missing null annotation: inherited method from IY declares this parameter as @NonNull\n" + |
887 |
"----------\n" + |
888 |
"3. ERROR in p1\\Y.java (at line 5)\n" + |
889 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
890 |
" ^^^^^^^^^^^^^^^\n" + |
891 |
"Illegal redefinition of parameter s2, inherited method from X declares this parameter as @Nullable\n" + |
892 |
"----------\n" + |
893 |
"4. ERROR in p1\\Y.java (at line 5)\n" + |
894 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
895 |
" ^^^^^^^^^^^^^^^\n" + |
896 |
"Illegal redefinition of parameter s3, inherited method from IY declares this parameter as @Nullable\n" + |
897 |
"----------\n"); |
898 |
} |
899 |
// methods from two super types have different null contracts. |
900 |
// sub-class does not override, but should to bridge the incompatibility |
901 |
public void test_parameter_specification_inheritance_014() { |
902 |
Map customOptions = getCompilerOptions(); |
903 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
904 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
905 |
runNegativeTestWithLibs( |
906 |
new String[] { |
907 |
"p1/IY.java", |
908 |
"package p1;\n" + |
909 |
"import org.eclipse.jdt.annotation.*;\n" + |
910 |
"public interface IY {\n" + |
911 |
" public @NonNull String getString1(String s);\n" + |
912 |
" public @NonNull String getString2(String s);\n" + |
913 |
" public String getString3(@Nullable String s);\n" + |
914 |
" public @NonNull String getString4(@Nullable String s);\n" + |
915 |
" public @NonNull String getString5(@Nullable String s);\n" + |
916 |
" public @Nullable String getString6(@NonNull String s);\n" + |
917 |
"}\n", |
918 |
"p1/X.java", |
919 |
"package p1;\n" + |
920 |
"import org.eclipse.jdt.annotation.*;\n" + |
921 |
"public class X {\n" + |
922 |
" public @Nullable String getString1(String s) {\n" + // incomp. return |
923 |
" return s;\n" + |
924 |
" }\n" + |
925 |
" public String getString2(String s) {\n" + // incomp. return |
926 |
" return s;\n" + |
927 |
" }\n" + |
928 |
" public String getString3(String s) {\n" + // incomp. arg |
929 |
" return \"\";\n" + |
930 |
" }\n" + |
931 |
" public @NonNull String getString4(@Nullable String s) {\n" + |
932 |
" return \"\";\n" + |
933 |
" }\n" + |
934 |
" public @NonNull String getString5(@NonNull String s) {\n" + // incomp. arg |
935 |
" return s;\n" + |
936 |
" }\n" + |
937 |
" public @NonNull String getString6(@Nullable String s) {\n" + |
938 |
" return \"\";\n" + |
939 |
" }\n" + |
940 |
"}\n", |
941 |
"p1/Y.java", |
942 |
"package p1;\n" + |
943 |
"public class Y extends X implements IY {\n" + |
944 |
"}\n", |
945 |
}, |
946 |
customOptions, |
947 |
"----------\n" + |
948 |
"1. ERROR in p1\\Y.java (at line 2)\n" + |
949 |
" public class Y extends X implements IY {\n" + |
950 |
" ^\n" + |
951 |
"The method getString1(String) from class X cannot implement the corresponding method from type IY due to incompatible nullness constraints\n" + |
952 |
"----------\n" + |
953 |
"2. ERROR in p1\\Y.java (at line 2)\n" + |
954 |
" public class Y extends X implements IY {\n" + |
955 |
" ^\n" + |
956 |
"The method getString2(String) from class X cannot implement the corresponding method from type IY due to incompatible nullness constraints\n" + |
957 |
"----------\n" + |
958 |
"3. ERROR in p1\\Y.java (at line 2)\n" + |
959 |
" public class Y extends X implements IY {\n" + |
960 |
" ^\n" + |
961 |
"The method getString5(String) from class X cannot implement the corresponding method from type IY due to incompatible nullness constraints\n" + |
962 |
"----------\n" + |
963 |
"4. ERROR in p1\\Y.java (at line 2)\n" + |
964 |
" public class Y extends X implements IY {\n" + |
965 |
" ^\n" + |
966 |
"The method getString3(String) from class X cannot implement the corresponding method from type IY due to incompatible nullness constraints\n" + |
967 |
"----------\n"); |
968 |
} |
969 |
// a nullable return value is dereferenced without a check |
970 |
public void test_nullable_return_001() { |
971 |
runNegativeTestWithLibs( |
972 |
new String[] { |
973 |
"X.java", |
974 |
"import org.eclipse.jdt.annotation.*;\n" + |
975 |
"public class X {\n" + |
976 |
" @Nullable Object getObject() { return null; }\n" + |
977 |
" void foo() {\n" + |
978 |
" Object o = getObject();\n" + |
979 |
" System.out.print(o.toString());\n" + |
980 |
" }\n" + |
981 |
"}\n" |
982 |
}, |
983 |
"----------\n" + |
984 |
"1. ERROR in X.java (at line 6)\n" + |
985 |
" System.out.print(o.toString());\n" + |
986 |
" ^\n" + |
987 |
"Potential null pointer access: The variable o may be null at this location\n" + |
988 |
"----------\n"); |
989 |
} |
990 |
// a nullable return value is dereferenced without a check, method is read from .class file |
991 |
public void test_nullable_return_002() { |
992 |
runConformTestWithLibs( |
993 |
new String[] { |
994 |
"Lib.java", |
995 |
"import org.eclipse.jdt.annotation.*;\n" + |
996 |
"public class Lib {\n" + |
997 |
" @Nullable Object getObject() { return null; }\n" + |
998 |
"}\n" |
999 |
}, |
1000 |
null /*customOptions*/, |
1001 |
""); |
1002 |
runNegativeTestWithLibs( |
1003 |
false, // don't flush |
1004 |
new String[] { |
1005 |
"X.java", |
1006 |
"public class X {\n" + |
1007 |
" void foo(Lib l) {\n" + |
1008 |
" Object o = l.getObject();\n" + |
1009 |
" System.out.print(o.toString());\n" + |
1010 |
" }\n" + |
1011 |
"}\n" |
1012 |
}, |
1013 |
null /*customOptions*/, |
1014 |
"----------\n" + |
1015 |
"1. ERROR in X.java (at line 4)\n" + |
1016 |
" System.out.print(o.toString());\n" + |
1017 |
" ^\n" + |
1018 |
"Potential null pointer access: The variable o may be null at this location\n" + |
1019 |
"----------\n"); |
1020 |
} |
1021 |
// a non-null return value is checked for null, method is read from .class file |
1022 |
public void test_nonnull_return_001() { |
1023 |
runConformTestWithLibs( |
1024 |
new String[] { |
1025 |
"Lib.java", |
1026 |
"import org.eclipse.jdt.annotation.*;\n" + |
1027 |
"public class Lib {\n" + |
1028 |
" @NonNull Object getObject() { return new Object(); }\n" + |
1029 |
"}\n" |
1030 |
}, |
1031 |
null /*customOptions*/, |
1032 |
""); |
1033 |
runNegativeTestWithLibs( |
1034 |
false, // don't flush |
1035 |
new String[] { |
1036 |
"X.java", |
1037 |
"public class X {\n" + |
1038 |
" void foo(Lib l) {\n" + |
1039 |
" Object o = l.getObject();\n" + |
1040 |
" if (o != null)\n" + |
1041 |
" System.out.print(o.toString());\n" + |
1042 |
" }\n" + |
1043 |
"}\n" |
1044 |
}, |
1045 |
null /*customOptions*/, |
1046 |
"----------\n" + |
1047 |
"1. ERROR in X.java (at line 4)\n" + |
1048 |
" if (o != null)\n" + |
1049 |
" ^\n" + |
1050 |
"Redundant null check: The variable o cannot be null at this location\n" + |
1051 |
"----------\n"); |
1052 |
} |
1053 |
// a non-null method returns null |
1054 |
public void test_nonnull_return_003() { |
1055 |
runNegativeTestWithLibs( |
1056 |
new String[] { |
1057 |
"X.java", |
1058 |
"import org.eclipse.jdt.annotation.*;\n" + |
1059 |
"public class X {\n" + |
1060 |
" @NonNull Object getObject(boolean b) {\n" + |
1061 |
" if (b)\n" + |
1062 |
" return null;\n" + // definite specification violation despite enclosing "if" |
1063 |
" return new Object();\n" + |
1064 |
" }\n" + |
1065 |
"}\n" |
1066 |
}, |
1067 |
"----------\n" + |
1068 |
"1. ERROR in X.java (at line 5)\n" + |
1069 |
" return null;\n" + |
1070 |
" ^^^^\n" + |
1071 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1072 |
"----------\n"); |
1073 |
} |
1074 |
// a non-null method potentially returns null |
1075 |
public void test_nonnull_return_004() { |
1076 |
runNegativeTestWithLibs( |
1077 |
new String[] { |
1078 |
"X.java", |
1079 |
"import org.eclipse.jdt.annotation.*;\n" + |
1080 |
"public class X {\n" + |
1081 |
" @NonNull Object getObject(@Nullable Object o) {\n" + |
1082 |
" return o;\n" + // 'o' is only potentially null |
1083 |
" }\n" + |
1084 |
"}\n" |
1085 |
}, |
1086 |
null /*customOptions*/, |
1087 |
"----------\n" + |
1088 |
"1. ERROR in X.java (at line 4)\n" + |
1089 |
" return o;\n" + |
1090 |
" ^\n" + |
1091 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1092 |
"----------\n"); |
1093 |
} |
1094 |
// a non-null method returns its non-null argument |
1095 |
public void test_nonnull_return_005() { |
1096 |
Map customOptions = getCompilerOptions(); |
1097 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1098 |
runConformTestWithLibs( |
1099 |
new String[] { |
1100 |
"X.java", |
1101 |
"import org.eclipse.jdt.annotation.*;\n" + |
1102 |
"public class X {\n" + |
1103 |
" @NonNull Object getObject(@NonNull Object o) {\n" + |
1104 |
" return o;\n" + |
1105 |
" }\n" + |
1106 |
"}\n" |
1107 |
}, |
1108 |
customOptions, |
1109 |
""); |
1110 |
} |
1111 |
//a non-null method has insufficient nullness info for its return value |
1112 |
public void test_nonnull_return_006() { |
1113 |
runNegativeTestWithLibs( |
1114 |
new String[] { |
1115 |
"X.java", |
1116 |
"import org.eclipse.jdt.annotation.*;\n" + |
1117 |
"public class X {\n" + |
1118 |
" @NonNull Object getObject(Object o) {\n" + |
1119 |
" return o;\n" + |
1120 |
" }\n" + |
1121 |
"}\n" |
1122 |
}, |
1123 |
"----------\n" + |
1124 |
"1. WARNING in X.java (at line 4)\n" + |
1125 |
" return o;\n" + |
1126 |
" ^\n" + |
1127 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
1128 |
"----------\n"); |
1129 |
} |
1130 |
// a result from a nullable method is directly dereferenced |
1131 |
public void test_nonnull_return_007() { |
1132 |
runNegativeTestWithLibs( |
1133 |
new String[] { |
1134 |
"X.java", |
1135 |
"import org.eclipse.jdt.annotation.*;\n" + |
1136 |
"public class X {\n" + |
1137 |
" @Nullable Object getObject() {\n" + |
1138 |
" return null;\n" + |
1139 |
" }\n" + |
1140 |
" void test() {\n" + |
1141 |
" getObject().toString();\n" + |
1142 |
" }\n" + |
1143 |
"}\n" |
1144 |
}, |
1145 |
"----------\n" + |
1146 |
"1. ERROR in X.java (at line 7)\n" + |
1147 |
" getObject().toString();\n" + |
1148 |
" ^^^^^^^^^^^\n" + |
1149 |
"Potential null pointer access: The method getObject() may return null\n" + |
1150 |
"----------\n"); |
1151 |
} |
1152 |
// a result from a nonnull method is directly checked for null: redundant |
1153 |
public void test_nonnull_return_008() { |
1154 |
Map customOptions = getCompilerOptions(); |
1155 |
customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR); |
1156 |
runNegativeTestWithLibs( |
1157 |
new String[] { |
1158 |
"X.java", |
1159 |
"import org.eclipse.jdt.annotation.*;\n" + |
1160 |
"public class X {\n" + |
1161 |
" @NonNull Object getObject() {\n" + |
1162 |
" return new Object();\n" + |
1163 |
" }\n" + |
1164 |
" void test() {\n" + |
1165 |
" if (getObject() == null)\n" + |
1166 |
" throw new RuntimeException();\n" + |
1167 |
" }\n" + |
1168 |
"}\n" |
1169 |
}, |
1170 |
customOptions, |
1171 |
"----------\n" + |
1172 |
"1. ERROR in X.java (at line 7)\n" + |
1173 |
" if (getObject() == null)\n" + |
1174 |
" ^^^^^^^^^^^\n" + |
1175 |
"Redundant null check: The method getObject() cannot return null\n" + |
1176 |
"----------\n"); |
1177 |
} |
1178 |
// a result from a nonnull method is directly checked for null (from local): redundant |
1179 |
public void test_nonnull_return_009() { |
1180 |
Map customOptions = getCompilerOptions(); |
1181 |
customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR); |
1182 |
runNegativeTestWithLibs( |
1183 |
new String[] { |
1184 |
"X.java", |
1185 |
"import org.eclipse.jdt.annotation.*;\n" + |
1186 |
"public class X {\n" + |
1187 |
" @NonNull Object getObject() {\n" + |
1188 |
" return new Object();\n" + |
1189 |
" }\n" + |
1190 |
" void test() {\n" + |
1191 |
" Object left = null;\n" + |
1192 |
" if (left != getObject())\n" + |
1193 |
" throw new RuntimeException();\n" + |
1194 |
" }\n" + |
1195 |
"}\n" |
1196 |
}, |
1197 |
customOptions, |
1198 |
"----------\n" + |
1199 |
"1. ERROR in X.java (at line 8)\n" + |
1200 |
" if (left != getObject())\n" + |
1201 |
" ^^^^\n" + |
1202 |
"Redundant null check: The variable left can only be null at this location\n" + |
1203 |
"----------\n" + |
1204 |
"2. ERROR in X.java (at line 8)\n" + |
1205 |
" if (left != getObject())\n" + |
1206 |
" ^^^^^^^^^^^\n" + |
1207 |
"Redundant null check: The method getObject() cannot return null\n" + |
1208 |
"----------\n"); |
1209 |
} |
1210 |
// a result from a nullable method is assigned and checked for null (from local): not redundant |
1211 |
// see also Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop |
1212 |
public void test_nonnull_return_010() { |
1213 |
Map customOptions = getCompilerOptions(); |
1214 |
customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR); |
1215 |
runNegativeTestWithLibs( |
1216 |
new String[] { |
1217 |
"X.java", |
1218 |
"import org.eclipse.jdt.annotation.*;\n" + |
1219 |
"public class X {\n" + |
1220 |
" @Nullable X getX() {\n" + |
1221 |
" return new X();\n" + |
1222 |
" }\n" + |
1223 |
" void test() {\n" + |
1224 |
" X left = this;\n" + |
1225 |
" do {\n" + |
1226 |
" if (left == null) \n" + |
1227 |
" throw new RuntimeException();\n" + |
1228 |
" } while ((left = left.getX()) != null);\n" + // no warning/error here! |
1229 |
" }\n" + |
1230 |
"}\n" |
1231 |
}, |
1232 |
customOptions, |
1233 |
"----------\n" + |
1234 |
"1. ERROR in X.java (at line 9)\n" + |
1235 |
" if (left == null) \n" + |
1236 |
" ^^^^\n" + |
1237 |
"Null comparison always yields false: The variable left cannot be null at this location\n" + |
1238 |
"----------\n"); |
1239 |
} |
1240 |
// a non-null method returns a checked-for null value, but that branch is dead code |
1241 |
public void test_nonnull_return_011() { |
1242 |
Map customOptions = getCompilerOptions(); |
1243 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
1244 |
runNegativeTestWithLibs( |
1245 |
new String[] { |
1246 |
"X.java", |
1247 |
"import org.eclipse.jdt.annotation.*;\n" + |
1248 |
"@NonNullByDefault\n" + |
1249 |
"public class X {\n" + |
1250 |
" Object getObject(Object dubious) {\n" + |
1251 |
" if (dubious == null)\n" + // redundant |
1252 |
" return dubious;\n" + // definitely null, but not reported inside dead code |
1253 |
" return new Object();\n" + |
1254 |
" }\n" + |
1255 |
"}\n" |
1256 |
}, |
1257 |
customOptions, |
1258 |
"----------\n" + |
1259 |
"1. ERROR in X.java (at line 5)\n" + |
1260 |
" if (dubious == null)\n" + |
1261 |
" ^^^^^^^\n" + |
1262 |
"Null comparison always yields false: The variable dubious cannot be null at this location\n" + |
1263 |
"----------\n" + |
1264 |
"2. WARNING in X.java (at line 6)\n" + |
1265 |
" return dubious;\n" + |
1266 |
" ^^^^^^^^^^^^^^^\n" + |
1267 |
"Dead code\n" + |
1268 |
"----------\n"); |
1269 |
} |
1270 |
// a non-null method returns a definite null from a conditional expression |
1271 |
// requires the fix for Bug 354554 - [null] conditional with redundant condition yields weak error message |
1272 |
// TODO(SH): ENABLE! |
1273 |
public void _test_nonnull_return_012() { |
1274 |
Map customOptions = getCompilerOptions(); |
1275 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
1276 |
runNegativeTestWithLibs( |
1277 |
new String[] { |
1278 |
"X.java", |
1279 |
"import org.eclipse.jdt.annotation.*;\n" + |
1280 |
"@NonNullByDefault\n" + |
1281 |
"public class X {\n" + |
1282 |
" Object getObject(Object dubious) {\n" + |
1283 |
" return dubious == null ? dubious : null;\n" + |
1284 |
" }\n" + |
1285 |
"}\n" |
1286 |
}, |
1287 |
customOptions, |
1288 |
"----------\n" + |
1289 |
"1. ERROR in X.java (at line 5)\n" + |
1290 |
" return dubious == null ? dubious : null;\n" + |
1291 |
" ^^^^^^^\n" + |
1292 |
"Null comparison always yields false: The variable dubious cannot be null at this location\n" + |
1293 |
"----------\n" + |
1294 |
"2. ERROR in X.java (at line 5)\n" + |
1295 |
" return dubious == null ? dubious : null;\n" + |
1296 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
1297 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1298 |
"----------\n"); |
1299 |
} |
1300 |
// don't apply any default annotations to return void |
1301 |
public void test_nonnull_return_013() { |
1302 |
Map customOptions = getCompilerOptions(); |
1303 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
1304 |
runConformTestWithLibs( |
1305 |
new String[] { |
1306 |
"X.java", |
1307 |
"import org.eclipse.jdt.annotation.*;\n" + |
1308 |
"@NonNullByDefault\n" + |
1309 |
"public class X {\n" + |
1310 |
" void getObject() {}\n" + |
1311 |
"}\n", |
1312 |
"Y.java", |
1313 |
"public class Y extends X {\n" + |
1314 |
" @Override\n" + |
1315 |
" void getObject() {}\n" + // don't complain, void takes no (default) annotation |
1316 |
"}\n" |
1317 |
}, |
1318 |
customOptions, |
1319 |
""); |
1320 |
} |
1321 |
//suppress an error regarding null-spec violation |
1322 |
public void test_suppress_001() { |
1323 |
Map customOptions = getCompilerOptions(); |
1324 |
customOptions.put(CompilerOptions.OPTION_SuppressOptionalErrors, CompilerOptions.ENABLED); |
1325 |
runConformTestWithLibs( |
1326 |
new String[] { |
1327 |
"X.java", |
1328 |
"import org.eclipse.jdt.annotation.*;\n" + |
1329 |
"public class X {\n" + |
1330 |
" @SuppressWarnings(\"null\")\n" + |
1331 |
" @NonNull Object getObject(@Nullable Object o) {\n" + |
1332 |
" return o;\n" + // 'o' is only potentially null |
1333 |
" }\n" + |
1334 |
"}\n" |
1335 |
}, |
1336 |
customOptions, |
1337 |
""); |
1338 |
} |
1339 |
// mixed use of fully qualified name / explicit import |
1340 |
public void test_annotation_import_001() { |
1341 |
Map customOptions = getCompilerOptions(); |
1342 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1343 |
customOptions.put(CompilerOptions.OPTION_NullableAnnotationName, "org.foo.Nullable"); |
1344 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.NonNull"); |
1345 |
runConformTestWithLibs( |
1346 |
new String[] { |
1347 |
CUSTOM_NULLABLE_NAME, |
1348 |
CUSTOM_NULLABLE_CONTENT, |
1349 |
CUSTOM_NONNULL_NAME, |
1350 |
CUSTOM_NONNULL_CONTENT, |
1351 |
"Lib.java", |
1352 |
"public class Lib {\n" + |
1353 |
" @org.foo.NonNull Object getObject() { return new Object(); }\n" + // FQN |
1354 |
"}\n", |
1355 |
"X.java", |
1356 |
"import org.foo.NonNull;\n" + // explicit import |
1357 |
"public class X {\n" + |
1358 |
" @NonNull Object getObject(@NonNull Lib l) {\n" + |
1359 |
" return l.getObject();\n" + |
1360 |
" }\n" + |
1361 |
"}\n" |
1362 |
}, |
1363 |
customOptions, |
1364 |
""); |
1365 |
} |
1366 |
|
1367 |
// use of explicit imports throughout |
1368 |
public void test_annotation_import_002() { |
1369 |
Map customOptions = getCompilerOptions(); |
1370 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1371 |
customOptions.put(CompilerOptions.OPTION_NullableAnnotationName, "org.foo.Nullable"); |
1372 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.NonNull"); |
1373 |
runConformTest( |
1374 |
new String[] { |
1375 |
CUSTOM_NULLABLE_NAME, |
1376 |
CUSTOM_NULLABLE_CONTENT, |
1377 |
CUSTOM_NONNULL_NAME, |
1378 |
CUSTOM_NONNULL_CONTENT, |
1379 |
"Lib.java", |
1380 |
"import org.foo.NonNull;\n" + |
1381 |
"public class Lib {\n" + |
1382 |
" @NonNull Object getObject() { return new Object(); }\n" + |
1383 |
"}\n", |
1384 |
"X.java", |
1385 |
"import org.foo.NonNull;\n" + |
1386 |
"public class X {\n" + |
1387 |
" @NonNull Object getObject(@org.foo.Nullable String dummy, @NonNull Lib l) {\n" + |
1388 |
" Object o = l.getObject();" + |
1389 |
" return o;\n" + |
1390 |
" }\n" + |
1391 |
"}\n" |
1392 |
}, |
1393 |
customOptions, |
1394 |
""); |
1395 |
} |
1396 |
// explicit import of existing annotation types |
1397 |
// using a Lib without null specifications |
1398 |
public void test_annotation_import_005() { |
1399 |
Map customOptions = getCompilerOptions(); |
1400 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1401 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
1402 |
customOptions.put(CompilerOptions.OPTION_NullableAnnotationName, "org.foo.MayBeNull"); |
1403 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.MustNotBeNull"); |
1404 |
runNegativeTest( |
1405 |
true/*shouldFlushOutputDirectory*/, |
1406 |
new String[] { |
1407 |
"org/foo/MayBeNull.java", |
1408 |
"package org.foo;\n" + |
1409 |
"import java.lang.annotation.*;\n" + |
1410 |
"@Retention(RetentionPolicy.CLASS)\n" + |
1411 |
"public @interface MayBeNull {}\n", |
1412 |
|
1413 |
"org/foo/MustNotBeNull.java", |
1414 |
"package org.foo;\n" + |
1415 |
"import java.lang.annotation.*;\n" + |
1416 |
"@Retention(RetentionPolicy.CLASS)\n" + |
1417 |
"public @interface MustNotBeNull {}\n", |
1418 |
|
1419 |
"Lib.java", |
1420 |
"public class Lib {\n" + |
1421 |
" Object getObject() { return new Object(); }\n" + |
1422 |
"}\n", |
1423 |
"X.java", |
1424 |
"import org.foo.*;\n" + |
1425 |
"public class X {\n" + |
1426 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1427 |
" return l.getObject();\n" + |
1428 |
" }\n" + |
1429 |
"}\n", |
1430 |
|
1431 |
}, |
1432 |
null /*no libs*/, |
1433 |
customOptions, |
1434 |
"----------\n" + |
1435 |
"1. ERROR in X.java (at line 4)\n" + |
1436 |
" return l.getObject();\n" + |
1437 |
" ^^^^^^^^^^^^^\n" + |
1438 |
"Potential type mismatch: required \'@MustNotBeNull Object\' but nullness of the provided value is unknown\n" + |
1439 |
"----------\n", |
1440 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1441 |
} |
1442 |
// a non-null method returns a value obtained from an unannotated method, missing annotation types |
1443 |
public void test_annotation_import_006() { |
1444 |
Map customOptions = getCompilerOptions(); |
1445 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1446 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
1447 |
customOptions.put(CompilerOptions.OPTION_NullableAnnotationName, "org.foo.MayBeNull"); |
1448 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.MustNotBeNull"); |
1449 |
runNegativeTest( |
1450 |
true/*shouldFlushOutputDirectory*/, |
1451 |
new String[] { |
1452 |
"Lib.java", |
1453 |
"public class Lib {\n" + |
1454 |
" Object getObject() { return new Object(); }\n" + |
1455 |
"}\n", |
1456 |
"X.java", |
1457 |
"public class X {\n" + |
1458 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1459 |
" return l.getObject();\n" + |
1460 |
" }\n" + |
1461 |
"}\n" |
1462 |
}, |
1463 |
null /* no libs */, |
1464 |
customOptions, |
1465 |
"----------\n" + |
1466 |
"1. ERROR in X.java (at line 2)\n" + |
1467 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1468 |
" ^^^^^^^^^^^^^\n" + |
1469 |
"MustNotBeNull cannot be resolved to a type\n" + |
1470 |
"----------\n" + |
1471 |
"2. ERROR in X.java (at line 2)\n" + |
1472 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1473 |
" ^^^^^^^^^^^^^\n" + |
1474 |
"MustNotBeNull cannot be resolved to a type\n" + |
1475 |
"----------\n", |
1476 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1477 |
} |
1478 |
// using nullness defaulting to nonnull, missing annotation types |
1479 |
public void test_annotation_import_007() { |
1480 |
Map customOptions = getCompilerOptions(); |
1481 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1482 |
customOptions.put(CompilerOptions.OPTION_ReportNullSpecInsufficientInfo, CompilerOptions.ERROR); |
1483 |
customOptions.put(CompilerOptions.OPTION_NullableAnnotationName, "org.foo.MayBeNull"); |
1484 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.MustNotBeNull"); |
1485 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
1486 |
runNegativeTest( |
1487 |
true/*shouldFlushOutputDirectory*/, |
1488 |
new String[] { |
1489 |
"Lib.java", |
1490 |
"public class Lib {\n" + |
1491 |
" Object getObject() { return new Object(); }\n" + |
1492 |
"}\n", |
1493 |
"X.java", |
1494 |
"public class X {\n" + |
1495 |
" Object getObject(Lib l) {\n" + |
1496 |
" return l.getObject();\n" + |
1497 |
" }\n" + |
1498 |
"}\n" |
1499 |
}, |
1500 |
this.LIBS, |
1501 |
customOptions, |
1502 |
"----------\n" + |
1503 |
"1. ERROR in Lib.java (at line 1)\n" + |
1504 |
" public class Lib {\n" + |
1505 |
" ^\n" + |
1506 |
"Buildpath problem: the type org.foo.MustNotBeNull which is configured as a null annotation type cannot be resolved\n" + |
1507 |
"----------\n", |
1508 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1509 |
} |
1510 |
|
1511 |
// a null annotation is illegally used on a class: |
1512 |
public void test_illegal_annotation_001() { |
1513 |
runNegativeTest( |
1514 |
new String[] { |
1515 |
"X.java", |
1516 |
"import org.eclipse.jdt.annotation.*;\n" + |
1517 |
"@NonNull public class X {\n" + |
1518 |
"}\n" |
1519 |
}, |
1520 |
"----------\n" + |
1521 |
"1. ERROR in X.java (at line 2)\n" + |
1522 |
" @NonNull public class X {\n" + |
1523 |
" ^^^^^^^^\n" + |
1524 |
"The annotation @NonNull is disallowed for this location\n" + |
1525 |
"----------\n", |
1526 |
this.LIBS, |
1527 |
false/*shouldFlush*/); |
1528 |
} |
1529 |
// this test has been removed: |
1530 |
// setting default to nullable, default applies to a parameter |
1531 |
// public void test_default_nullness_001() |
1532 |
|
1533 |
// a null annotation is illegally defined by its simple name |
1534 |
public void test_illegal_annotation_002() { |
1535 |
Map customOptions = getCompilerOptions(); |
1536 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "NichtNull"); |
1537 |
runNegativeTestWithLibs( |
1538 |
new String[] { |
1539 |
"X.java", |
1540 |
"public class X {\n" + |
1541 |
"}\n" |
1542 |
}, |
1543 |
customOptions, |
1544 |
"----------\n" + |
1545 |
"1. ERROR in X.java (at line 0)\n" + |
1546 |
" public class X {\n" + |
1547 |
" ^\n" + |
1548 |
"Cannot use the unqualified name \'NichtNull\' as an annotation name for null specification\n" + |
1549 |
"----------\n"); |
1550 |
} |
1551 |
public void test_default_nullness_002() { |
1552 |
Map customOptions = getCompilerOptions(); |
1553 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1554 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1555 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
1556 |
runNegativeTestWithLibs( |
1557 |
new String[] { |
1558 |
"X.java", |
1559 |
"import org.eclipse.jdt.annotation.*;\n" + |
1560 |
"public class X {\n" + |
1561 |
" Object getObject(@Nullable Object o) {\n" + |
1562 |
" return new Object();\n" + |
1563 |
" }\n" + |
1564 |
"}\n", |
1565 |
"Y.java", |
1566 |
"import org.eclipse.jdt.annotation.*;\n" + |
1567 |
"public class Y extends X {\n" + |
1568 |
" @Override\n" + |
1569 |
" @Nullable Object getObject(Object o) {\n" + // complain illegal return redef and inherited annot is not repeated |
1570 |
" return o;\n" + |
1571 |
" }\n" + |
1572 |
"}\n", |
1573 |
}, |
1574 |
customOptions, |
1575 |
// main error: |
1576 |
"----------\n" + |
1577 |
"1. ERROR in Y.java (at line 4)\n" + |
1578 |
" @Nullable Object getObject(Object o) {\n" + |
1579 |
" ^^^^^^^^^^^^^^^^\n" + |
1580 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1581 |
"----------\n" + |
1582 |
// additional error: |
1583 |
"2. ERROR in Y.java (at line 4)\n" + |
1584 |
" @Nullable Object getObject(Object o) {\n" + |
1585 |
" ^^^^^^\n" + |
1586 |
"Illegal redefinition of parameter o, inherited method from X declares this parameter as @Nullable\n" + |
1587 |
"----------\n"); |
1588 |
} |
1589 |
// package default is non-null |
1590 |
public void test_default_nullness_003() { |
1591 |
Map customOptions = getCompilerOptions(); |
1592 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1593 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1594 |
runNegativeTestWithLibs( |
1595 |
new String[] { |
1596 |
"p1/X.java", |
1597 |
"package p1;\n" + |
1598 |
"import org.eclipse.jdt.annotation.*;\n" + |
1599 |
"@NonNullByDefault\n" + |
1600 |
"public class X {\n" + |
1601 |
" protected Object getObject(@Nullable Object o) {\n" + |
1602 |
" return new Object();\n" + |
1603 |
" }\n" + |
1604 |
"}\n", |
1605 |
"p2/package-info.java", |
1606 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1607 |
"package p2;\n", |
1608 |
"p2/Y.java", |
1609 |
"package p2;\n" + |
1610 |
"import org.eclipse.jdt.annotation.*;\n" + |
1611 |
"public class Y extends p1.X {\n" + |
1612 |
" @Override\n" + |
1613 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1614 |
" bar(o);\n" + |
1615 |
" return o;\n" + |
1616 |
" }\n" + |
1617 |
" void bar(Object o2) { }\n" + // parameter is nonnull per package default |
1618 |
"}\n" |
1619 |
}, |
1620 |
customOptions, |
1621 |
"----------\n" + |
1622 |
"1. ERROR in p2\\Y.java (at line 5)\n" + |
1623 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1624 |
" ^^^^^^^^^^^^^^^^\n" + |
1625 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1626 |
"----------\n" + |
1627 |
"2. ERROR in p2\\Y.java (at line 6)\n" + |
1628 |
" bar(o);\n" + |
1629 |
" ^\n" + |
1630 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1631 |
"----------\n"); |
1632 |
} |
1633 |
// package level default is consumed from package-info.class |
1634 |
public void test_default_nullness_003a() { |
1635 |
Map customOptions = getCompilerOptions(); |
1636 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1637 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1638 |
runConformTestWithLibs( |
1639 |
new String[] { |
1640 |
"p1/X.java", |
1641 |
"package p1;\n" + |
1642 |
"import org.eclipse.jdt.annotation.*;\n" + |
1643 |
"@NonNullByDefault\n" + |
1644 |
"public class X {\n" + |
1645 |
" protected Object getObject(@Nullable Object o) {\n" + |
1646 |
" return new Object();\n" + |
1647 |
" }\n" + |
1648 |
" protected void bar(Object o2) { }\n" + // parameter is nonnull per type default |
1649 |
"}\n", |
1650 |
"p2/package-info.java", |
1651 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1652 |
"package p2;\n", |
1653 |
}, |
1654 |
customOptions, |
1655 |
""); |
1656 |
// check if default is visible from package-info.class. |
1657 |
runNegativeTestWithLibs( |
1658 |
false, // don't flush |
1659 |
new String[] { |
1660 |
"p2/Y.java", |
1661 |
"package p2;\n" + |
1662 |
"import org.eclipse.jdt.annotation.*;\n" + |
1663 |
"public class Y extends p1.X {\n" + |
1664 |
" @Override\n" + |
1665 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + // can't override inherited default nonnull |
1666 |
" bar(o);\n" + // parameter is nonnull in super class's .class file |
1667 |
" accept(o);\n" + |
1668 |
" return o;\n" + |
1669 |
" }\n" + |
1670 |
" void accept(Object a) {}\n" + // governed by package level default |
1671 |
"}\n" |
1672 |
}, |
1673 |
customOptions, |
1674 |
"----------\n" + |
1675 |
"1. ERROR in p2\\Y.java (at line 5)\n" + |
1676 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1677 |
" ^^^^^^^^^^^^^^^^\n" + |
1678 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1679 |
"----------\n" + |
1680 |
"2. ERROR in p2\\Y.java (at line 6)\n" + |
1681 |
" bar(o);\n" + |
1682 |
" ^\n" + |
1683 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1684 |
"----------\n" + |
1685 |
"3. ERROR in p2\\Y.java (at line 7)\n" + |
1686 |
" accept(o);\n" + |
1687 |
" ^\n" + |
1688 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1689 |
"----------\n"); |
1690 |
} |
1691 |
// don't apply type-level default to non-reference type |
1692 |
public void test_default_nullness_004() { |
1693 |
Map customOptions = getCompilerOptions(); |
1694 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1695 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1696 |
runConformTestWithLibs( |
1697 |
new String[] { |
1698 |
"p1/X.java", |
1699 |
"package p1;\n" + |
1700 |
"import org.eclipse.jdt.annotation.*;\n" + |
1701 |
"@NonNullByDefault\n" + |
1702 |
"public class X {\n" + |
1703 |
" protected Object getObject(boolean o) {\n" + |
1704 |
" return new Object();\n" + |
1705 |
" }\n" + |
1706 |
"}\n", |
1707 |
"p2/Y.java", |
1708 |
"package p2;\n" + |
1709 |
"import org.eclipse.jdt.annotation.*;\n" + |
1710 |
"public class Y extends p1.X {\n" + |
1711 |
" @Override\n" + |
1712 |
" protected @NonNull Object getObject(boolean o) {\n" + |
1713 |
" return o ? this : new Object();\n" + |
1714 |
" }\n" + |
1715 |
"}\n" |
1716 |
}, |
1717 |
customOptions, |
1718 |
""); |
1719 |
} |
1720 |
// package default is non-null |
1721 |
// see also Bug 354536 - compiling package-info.java still depends on the order of compilation units |
1722 |
public void test_default_nullness_005() { |
1723 |
Map customOptions = getCompilerOptions(); |
1724 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1725 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1726 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.NonNull"); |
1727 |
runNegativeTestWithLibs( |
1728 |
new String[] { |
1729 |
"p1/X.java", |
1730 |
"package p1;\n" + |
1731 |
"public class X {\n" + |
1732 |
" class Inner {" + |
1733 |
" protected Object getObject(String s) {\n" + |
1734 |
" return null;\n" + |
1735 |
" }\n" + |
1736 |
" }\n" + |
1737 |
"}\n", |
1738 |
"p1/package-info.java", |
1739 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1740 |
"package p1;\n", |
1741 |
CUSTOM_NONNULL_NAME, |
1742 |
CUSTOM_NONNULL_CONTENT |
1743 |
}, |
1744 |
customOptions, |
1745 |
"----------\n" + |
1746 |
"1. ERROR in p1\\X.java (at line 4)\n" + |
1747 |
" return null;\n" + |
1748 |
" ^^^^\n" + |
1749 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1750 |
"----------\n"); |
1751 |
} |
1752 |
// package default is non-null, package-info.java read before the annotation type |
1753 |
// compile order: beginToCompile(X.Inner) triggers reading of package-info.java before the annotation type was read |
1754 |
public void test_default_nullness_006() { |
1755 |
Map customOptions = getCompilerOptions(); |
1756 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1757 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1758 |
customOptions.put(CompilerOptions.OPTION_NonNullAnnotationName, "org.foo.NonNull"); |
1759 |
runNegativeTestWithLibs( |
1760 |
new String[] { |
1761 |
"p1/package-info.java", |
1762 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1763 |
"package p1;\n", |
1764 |
"p1/X.java", |
1765 |
"package p1;\n" + |
1766 |
"public class X {\n" + |
1767 |
" class Inner {" + |
1768 |
" protected Object getObject(String s) {\n" + |
1769 |
" return null;\n" + |
1770 |
" }\n" + |
1771 |
" }\n" + |
1772 |
"}\n", |
1773 |
CUSTOM_NONNULL_NAME, |
1774 |
CUSTOM_NONNULL_CONTENT |
1775 |
}, |
1776 |
customOptions, |
1777 |
"----------\n" + |
1778 |
"1. ERROR in p1\\X.java (at line 4)\n" + |
1779 |
" return null;\n" + |
1780 |
" ^^^^\n" + |
1781 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1782 |
"----------\n"); |
1783 |
} |
1784 |
// global default nonnull, but return may be null |
1785 |
public void test_default_nullness_007() { |
1786 |
Map customOptions = getCompilerOptions(); |
1787 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1788 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1789 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
1790 |
runNegativeTestWithLibs( |
1791 |
new String[] { |
1792 |
"X.java", |
1793 |
"import org.eclipse.jdt.annotation.*;\n" + |
1794 |
"public class X {\n" + |
1795 |
" @Nullable Object dangerous() {\n" + |
1796 |
" return null;\n" + |
1797 |
" }\n" + |
1798 |
" Object broken() {\n" + |
1799 |
" return dangerous();\n" + |
1800 |
" }\n" + |
1801 |
"}\n", |
1802 |
|
1803 |
}, |
1804 |
customOptions, |
1805 |
"----------\n" + |
1806 |
"1. ERROR in X.java (at line 7)\n" + |
1807 |
" return dangerous();\n" + |
1808 |
" ^^^^^^^^^^^\n" + |
1809 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1810 |
"----------\n"); |
1811 |
} |
1812 |
|
1813 |
// cancel type level default to comply with super specification |
1814 |
public void test_default_nullness_008() { |
1815 |
Map customOptions = getCompilerOptions(); |
1816 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1817 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1818 |
runConformTestWithLibs( |
1819 |
new String[] { |
1820 |
"p1/X.java", |
1821 |
"package p1;\n" + |
1822 |
"public class X {\n" + |
1823 |
" protected Object getObject(Object o) {\n" + |
1824 |
" return new Object();\n" + |
1825 |
" }\n" + |
1826 |
"}\n", |
1827 |
"p2/Y.java", |
1828 |
"package p2;\n" + |
1829 |
"import org.eclipse.jdt.annotation.*;\n" + |
1830 |
"@NonNullByDefault\n" + |
1831 |
"public class Y extends p1.X {\n" + |
1832 |
" @Override\n" + |
1833 |
" @NonNullByDefault(false)\n" + |
1834 |
" protected Object getObject(Object o) {\n" + |
1835 |
" if (o.toString().length() == 0)\n" + // dereference without a warning |
1836 |
" return null;\n" + // return null without a warning |
1837 |
" return o.toString();\n" + |
1838 |
" }\n" + |
1839 |
"}\n" |
1840 |
}, |
1841 |
customOptions, |
1842 |
""); |
1843 |
} |
1844 |
|
1845 |
// cancel outer type level default to comply with super specification |
1846 |
public void test_default_nullness_009() { |
1847 |
Map customOptions = getCompilerOptions(); |
1848 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1849 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1850 |
runNegativeTestWithLibs( |
1851 |
new String[] { |
1852 |
"p1/X.java", |
1853 |
"package p1;\n" + |
1854 |
"public class X {\n" + |
1855 |
" protected Object getObject(Object o) {\n" + |
1856 |
" return new Object();\n" + |
1857 |
" }\n" + |
1858 |
"}\n", |
1859 |
"p2/Y.java", |
1860 |
"package p2;\n" + |
1861 |
"import org.eclipse.jdt.annotation.*;\n" + |
1862 |
"@NonNullByDefault\n" + |
1863 |
"public class Y { \n" + |
1864 |
" @NonNullByDefault(false)\n" + |
1865 |
" static class Z extends p1.X {\n" + |
1866 |
" @Override\n" + |
1867 |
" protected Object getObject(Object o) {\n" + |
1868 |
" if (o.toString().length() == 0) {\n" + |
1869 |
" o = null;\n" + // assign null without a warning |
1870 |
" bar(o); // error: arg is declared @NonNull\n" + |
1871 |
" return null;\n" + |
1872 |
" }\n" + |
1873 |
" return o.toString();\n" + |
1874 |
" }\n" + |
1875 |
" String bar(@NonNull Object o) {\n" + |
1876 |
" return getObject(o).toString();" + |
1877 |
" }\n" + |
1878 |
" }\n" + |
1879 |
"}\n" |
1880 |
}, |
1881 |
customOptions, |
1882 |
"----------\n" + |
1883 |
"1. ERROR in p2\\Y.java (at line 11)\n" + |
1884 |
" bar(o); // error: arg is declared @NonNull\n" + |
1885 |
" ^\n" + |
1886 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1887 |
"----------\n"); |
1888 |
} |
1889 |
// non-null declarations are redundant within a default scope. |
1890 |
public void test_default_nullness_010() { |
1891 |
Map customOptions = getCompilerOptions(); |
1892 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1893 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1894 |
runConformTestWithLibs( |
1895 |
new String[] { |
1896 |
"p2/Y.java", |
1897 |
"package p2;\n" + |
1898 |
"import org.eclipse.jdt.annotation.*;\n" + |
1899 |
"@NonNullByDefault\n" + |
1900 |
"public class Y {\n" + |
1901 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
1902 |
" return o;\n" + |
1903 |
" }\n" + |
1904 |
"}\n" |
1905 |
}, |
1906 |
customOptions, |
1907 |
"----------\n" + |
1908 |
"1. WARNING in p2\\Y.java (at line 5)\n" + |
1909 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
1910 |
" ^^^^^^^^^^^^^^^\n" + |
1911 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
1912 |
"----------\n" + |
1913 |
"2. WARNING in p2\\Y.java (at line 5)\n" + |
1914 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
1915 |
" ^^^^^^^^^^^^^^^^^\n" + |
1916 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
1917 |
"----------\n"); |
1918 |
} |
1919 |
// a nonnull variable is dereferenced in a loop |
1920 |
public void test_nonnull_var_in_constrol_structure_1() { |
1921 |
Map customOptions = getCompilerOptions(); |
1922 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1923 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1924 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
1925 |
runNegativeTestWithLibs( |
1926 |
new String[] { |
1927 |
"X.java", |
1928 |
"import org.eclipse.jdt.annotation.*;\n" + |
1929 |
"public class X {\n" + |
1930 |
" void print4(@NonNull String s) {\n" + |
1931 |
" for (int i=0; i<4; i++)\n" + |
1932 |
" print(s);\n" + |
1933 |
" }\n" + |
1934 |
" void print5(@Nullable String s) {\n" + |
1935 |
" for (int i=0; i<5; i++)\n" + |
1936 |
" print(s);\n" + |
1937 |
" }\n" + |
1938 |
" void print6(boolean b) {\n" + |
1939 |
" String s = b ? null : \"\";\n" + |
1940 |
" for (int i=0; i<5; i++)\n" + |
1941 |
" print(s);\n" + |
1942 |
" }\n" + |
1943 |
" void print(@NonNull String s) {\n" + |
1944 |
" System.out.print(s);\n" + |
1945 |
" }\n" + |
1946 |
"}\n", |
1947 |
|
1948 |
}, |
1949 |
customOptions, |
1950 |
"----------\n" + |
1951 |
"1. WARNING in X.java (at line 3)\n" + |
1952 |
" void print4(@NonNull String s) {\n" + |
1953 |
" ^^^^^^^^^^^^^^^^^\n" + |
1954 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
1955 |
"----------\n" + |
1956 |
"2. ERROR in X.java (at line 9)\n" + |
1957 |
" print(s);\n" + |
1958 |
" ^\n" + |
1959 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
1960 |
"----------\n" + |
1961 |
"3. ERROR in X.java (at line 14)\n" + |
1962 |
" print(s);\n" + |
1963 |
" ^\n" + |
1964 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
1965 |
"----------\n" + |
1966 |
"4. WARNING in X.java (at line 16)\n" + |
1967 |
" void print(@NonNull String s) {\n" + |
1968 |
" ^^^^^^^^^^^^^^^^^\n" + |
1969 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
1970 |
"----------\n"); |
1971 |
} |
1972 |
// a nonnull variable is dereferenced in a finally block |
1973 |
public void test_nonnull_var_in_constrol_structure_2() { |
1974 |
Map customOptions = getCompilerOptions(); |
1975 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
1976 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
1977 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
1978 |
runNegativeTestWithLibs( |
1979 |
new String[] { |
1980 |
"X.java", |
1981 |
"import org.eclipse.jdt.annotation.*;\n" + |
1982 |
"public class X {\n" + |
1983 |
" void print4(String s) {\n" + |
1984 |
" try { /*empty*/ } finally {\n" + |
1985 |
" print(s);\n" + |
1986 |
" }\n" + |
1987 |
" }\n" + |
1988 |
" void print5(@Nullable String s) {\n" + |
1989 |
" try { /*empty*/ } finally {\n" + |
1990 |
" print(s);\n" + |
1991 |
" }\n" + |
1992 |
" }\n" + |
1993 |
" void print6(boolean b) {\n" + |
1994 |
" String s = b ? null : \"\";\n" + |
1995 |
" try { /*empty*/ } finally {\n" + |
1996 |
" print(s);\n" + |
1997 |
" }\n" + |
1998 |
" }\n" + |
1999 |
" void print(String s) {\n" + |
2000 |
" System.out.print(s);\n" + |
2001 |
" }\n" + |
2002 |
"}\n", |
2003 |
|
2004 |
}, |
2005 |
customOptions, |
2006 |
"----------\n" + |
2007 |
"1. ERROR in X.java (at line 10)\n" + |
2008 |
" print(s);\n" + |
2009 |
" ^\n" + |
2010 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2011 |
"----------\n" + |
2012 |
"2. ERROR in X.java (at line 16)\n" + |
2013 |
" print(s);\n" + |
2014 |
" ^\n" + |
2015 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2016 |
"----------\n"); |
2017 |
} |
2018 |
// a nonnull variable is dereferenced in a finally block inside a loop |
2019 |
public void test_nonnull_var_in_constrol_structure_3() { |
2020 |
Map customOptions = getCompilerOptions(); |
2021 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
2022 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
2023 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
2024 |
customOptions.put(CompilerOptions.OPTION_ReportRedundantNullAnnotation, CompilerOptions.IGNORE); |
2025 |
runNegativeTestWithLibs( |
2026 |
new String[] { |
2027 |
"X.java", |
2028 |
"import org.eclipse.jdt.annotation.*;\n" + |
2029 |
"public class X {\n" + |
2030 |
" void print4(@NonNull String s) {\n" + |
2031 |
" for (int i=0; i<4; i++)\n" + |
2032 |
" try { /*empty*/ } finally {\n" + |
2033 |
" print(s);\n" + |
2034 |
" }\n" + |
2035 |
" }\n" + |
2036 |
" void print5(@Nullable String s) {\n" + |
2037 |
" for (int i=0; i<5; i++)\n" + |
2038 |
" try { /*empty*/ } finally {\n" + |
2039 |
" print(s);\n" + |
2040 |
" }\n" + |
2041 |
" }\n" + |
2042 |
" void print6(boolean b) {\n" + |
2043 |
" String s = b ? null : \"\";\n" + |
2044 |
" for (int i=0; i<4; i++)\n" + |
2045 |
" try { /*empty*/ } finally {\n" + |
2046 |
" print(s);\n" + |
2047 |
" }\n" + |
2048 |
" }\n" + |
2049 |
" void print(@NonNull String s) {\n" + |
2050 |
" System.out.print(s);\n" + |
2051 |
" }\n" + |
2052 |
"}\n", |
2053 |
|
2054 |
}, |
2055 |
customOptions, |
2056 |
"----------\n" + |
2057 |
"1. ERROR in X.java (at line 12)\n" + |
2058 |
" print(s);\n" + |
2059 |
" ^\n" + |
2060 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2061 |
"----------\n" + |
2062 |
"2. ERROR in X.java (at line 19)\n" + |
2063 |
" print(s);\n" + |
2064 |
" ^\n" + |
2065 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2066 |
"----------\n"); |
2067 |
} |
2068 |
// a nonnull variable is dereferenced method of a nested type |
2069 |
public void test_nesting_1() { |
2070 |
Map customOptions = getCompilerOptions(); |
2071 |
customOptions.put(CompilerOptions.OPTION_ReportNullReference, CompilerOptions.ERROR); |
2072 |
customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, CompilerOptions.ERROR); |
2073 |
customOptions.put(CompilerOptions.OPTION_NonNullIsDefault, CompilerOptions.ENABLED); |
2074 |
runNegativeTestWithLibs( |
2075 |
new String[] { |
2076 |
"X.java", |
2077 |
"import org.eclipse.jdt.annotation.*;\n" + |
2078 |
"@NonNullByDefault\n" + |
2079 |
"public class X {\n" + |
2080 |
" void print4(final String s1) {\n" + |
2081 |
" for (int i=0; i<3; i++)\n" + |
2082 |
" new Runnable() {\n" + |
2083 |
" public void run() {\n" + |
2084 |
" print(s1);\n" + |
2085 |
" }\n" + |
2086 |
" }.run();\n" + |
2087 |
" }\n" + |
2088 |
" void print8(final @Nullable String s2) {\n" + |
2089 |
" for (int i=0; i<3; i++)\n" + |
2090 |
" new Runnable() {\n" + |
2091 |
" public void run() {\n" + |
2092 |
" print(s2);\n" + |
2093 |
" }\n" + |
2094 |
" }.run();\n" + |
2095 |
" }\n" + |
2096 |
" void print16(boolean b) {\n" + |
2097 |
" final String s3 = b ? null : \"\";\n" + |
2098 |
" for (int i=0; i<3; i++)\n" + |
2099 |
" new Runnable() {\n" + |
2100 |
" public void run() {\n" + |
2101 |
" @NonNull String s3R = s3;\n" + |
2102 |
" }\n" + |
2103 |
" }.run();\n" + |
2104 |
" }\n" + |
2105 |
" void print(String s) {\n" + |
2106 |
" System.out.print(s);\n" + |
2107 |
" }\n" + |
2108 |
"}\n", |
2109 |
|
2110 |
}, |
2111 |
customOptions, |
2112 |
"----------\n" + |
2113 |
"1. ERROR in X.java (at line 16)\n" + |
2114 |
" print(s2);\n" + |
2115 |
" ^^\n" + |
2116 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2117 |
"----------\n" + |
2118 |
"2. ERROR in X.java (at line 25)\n" + |
2119 |
" @NonNull String s3R = s3;\n" + |
2120 |
" ^^\n" + |
2121 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2122 |
"----------\n"); |
2123 |
} |
2124 |
// Test a regression incurred to the OT/J based implementation |
2125 |
// by the fix in Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop) |
2126 |
public void test_constructor_with_nested_class() { |
2127 |
runConformTest( |
2128 |
new String[] { |
2129 |
"X.java", |
2130 |
"public class X {\n" + |
2131 |
" final Object o1;\n" + |
2132 |
" final Object o2;\n" + |
2133 |
" public X() {\n" + |
2134 |
" this.o1 = new Object() {\n" + |
2135 |
" public String toString() { return \"O1\"; }\n" + |
2136 |
" };\n" + |
2137 |
" this.o2 = new Object();" + |
2138 |
" }\n" + |
2139 |
"}\n" |
2140 |
}, |
2141 |
""); |
2142 |
} |
2143 |
} |