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.core.JavaCore; |
22 |
|
23 |
// see bug 186342 - [compiler][null] Using annotations for null checking |
24 |
public class NullAnnotationTest extends AbstractComparableTest { |
25 |
|
26 |
// class libraries including our default null annotation types: |
27 |
String[] LIBS; |
28 |
|
29 |
// names and content of custom annotations used in a few tests: |
30 |
private static final String CUSTOM_NONNULL_NAME = "org/foo/NonNull.java"; |
31 |
private static final String CUSTOM_NONNULL_CONTENT = |
32 |
"package org.foo;\n" + |
33 |
"import static java.lang.annotation.ElementType.*;\n" + |
34 |
"import java.lang.annotation.*;\n" + |
35 |
"@Retention(RetentionPolicy.CLASS)\n" + |
36 |
"@Target({METHOD,PARAMETER,LOCAL_VARIABLE})\n" + |
37 |
"public @interface NonNull {\n" + |
38 |
"}\n"; |
39 |
private static final String CUSTOM_NULLABLE_NAME = "org/foo/Nullable.java"; |
40 |
private static final String CUSTOM_NULLABLE_CONTENT = "package org.foo;\n" + |
41 |
"import static java.lang.annotation.ElementType.*;\n" + |
42 |
"import java.lang.annotation.*;\n" + |
43 |
"@Retention(RetentionPolicy.CLASS)\n" + |
44 |
"@Target({METHOD,PARAMETER,LOCAL_VARIABLE})\n" + |
45 |
"public @interface Nullable {\n" + |
46 |
"}\n"; |
47 |
|
48 |
public NullAnnotationTest(String name) { |
49 |
super(name); |
50 |
} |
51 |
|
52 |
// Static initializer to specify tests subset using TESTS_* static variables |
53 |
// All specified tests which do not belong to the class are skipped... |
54 |
static { |
55 |
// TESTS_NAMES = new String[] { "test_illegal_annotation_00" }; |
56 |
// TESTS_NUMBERS = new int[] { 561 }; |
57 |
// TESTS_RANGE = new int[] { 1, 2049 }; |
58 |
} |
59 |
|
60 |
public static Test suite() { |
61 |
return buildComparableTestSuite(testClass()); |
62 |
} |
63 |
|
64 |
public static Class testClass() { |
65 |
return NullAnnotationTest.class; |
66 |
} |
67 |
|
68 |
protected void setUp() throws Exception { |
69 |
super.setUp(); |
70 |
if (this.LIBS == null) { |
71 |
String[] defaultLibs = getDefaultClassPaths(); |
72 |
int len = defaultLibs.length; |
73 |
this.LIBS = new String[len+1]; |
74 |
System.arraycopy(defaultLibs, 0, this.LIBS, 0, len); |
75 |
File bundleFile = FileLocator.getBundleFile(Platform.getBundle("org.eclipse.jdt.annotation.null")); |
76 |
if (bundleFile.isDirectory()) |
77 |
this.LIBS[len] = bundleFile.getPath()+"/bin"; |
78 |
else |
79 |
this.LIBS[len] = bundleFile.getPath(); |
80 |
} |
81 |
} |
82 |
// Conditionally augment problem detection settings |
83 |
static boolean setNullRelatedOptions = true; |
84 |
protected Map getCompilerOptions() { |
85 |
Map defaultOptions = super.getCompilerOptions(); |
86 |
if (setNullRelatedOptions) { |
87 |
defaultOptions.put(JavaCore.COMPILER_PB_NULL_REFERENCE, JavaCore.ERROR); |
88 |
defaultOptions.put(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, JavaCore.ERROR); |
89 |
defaultOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
90 |
defaultOptions.put(JavaCore.COMPILER_PB_INCLUDE_ASSERTS_IN_NULL_ANALYSIS, JavaCore.ENABLED); |
91 |
|
92 |
defaultOptions.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION_FOR_INTERFACE_METHOD_IMPLEMENTATION, JavaCore.DISABLED); |
93 |
|
94 |
// enable null annotations: |
95 |
defaultOptions.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED); |
96 |
// leave other new options at these defaults: |
97 |
// defaultOptions.put(CompilerOptions.OPTION_ReportNullContractViolation, JavaCore.ERROR); |
98 |
// defaultOptions.put(CompilerOptions.OPTION_ReportPotentialNullContractViolation, JavaCore.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(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.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(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.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(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.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(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.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 nullable 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(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.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 non-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(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
744 |
runConformTestWithLibs( |
745 |
new String[] { |
746 |
"p1/X.java", |
747 |
"package p1;\n" + |
748 |
"import org.eclipse.jdt.annotation.*;\n" + |
749 |
"@NonNullByDefault\n" + |
750 |
"public class X {\n" + |
751 |
" protected String getString(String s) {\n" + |
752 |
" if (Character.isLowerCase(s.charAt(0)))\n" + |
753 |
" return getString(s);\n" + |
754 |
" return s;\n" + |
755 |
" }\n" + |
756 |
"}\n", |
757 |
"p1/Y.java", |
758 |
"package p1;\n" + |
759 |
"import org.eclipse.jdt.annotation.*;\n" + |
760 |
"@NonNullByDefault\n" + |
761 |
"public class Y extends X {\n" + |
762 |
" @Override\n" + |
763 |
" protected String getString(String s) {\n" + |
764 |
" return super.getString(s);\n" + |
765 |
" }\n" + |
766 |
"}\n", |
767 |
}, |
768 |
customOptions, |
769 |
""); |
770 |
} |
771 |
// class default is nonnull, method and its super both use the default, super-call passes null |
772 |
public void test_parameter_specification_inheritance_011() { |
773 |
Map customOptions = getCompilerOptions(); |
774 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
775 |
runNegativeTestWithLibs( |
776 |
new String[] { |
777 |
"p1/X.java", |
778 |
"package p1;\n" + |
779 |
"import org.eclipse.jdt.annotation.*;\n" + |
780 |
"@NonNullByDefault\n" + |
781 |
"public class X {\n" + |
782 |
" protected String getString(String s) {\n" + |
783 |
" if (Character.isLowerCase(s.charAt(0)))\n" + |
784 |
" return getString(s);\n" + |
785 |
" return s;\n" + |
786 |
" }\n" + |
787 |
"}\n", |
788 |
"p1/Y.java", |
789 |
"package p1;\n" + |
790 |
"import org.eclipse.jdt.annotation.*;\n" + |
791 |
"@NonNullByDefault\n" + |
792 |
"public class Y extends X {\n" + |
793 |
" @Override\n" + |
794 |
" protected String getString(String s) {\n" + |
795 |
" return super.getString(null);\n" + |
796 |
" }\n" + |
797 |
"}\n", |
798 |
}, |
799 |
customOptions, |
800 |
"----------\n" + |
801 |
"1. ERROR in p1\\Y.java (at line 7)\n" + |
802 |
" return super.getString(null);\n" + |
803 |
" ^^^^\n" + |
804 |
"Type mismatch: required \'@NonNull String\' but the provided value is null\n" + |
805 |
"----------\n"); |
806 |
} |
807 |
// methods from two super types have different null contracts. |
808 |
// sub-class merges both using the weakest common contract |
809 |
public void test_parameter_specification_inheritance_012() { |
810 |
Map customOptions = getCompilerOptions(); |
811 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
812 |
runConformTestWithLibs( |
813 |
new String[] { |
814 |
"p1/X.java", |
815 |
"package p1;\n" + |
816 |
"import org.eclipse.jdt.annotation.*;\n" + |
817 |
"public class X {\n" + |
818 |
" public @Nullable String getString(String s1, @Nullable String s2, @NonNull String s3) {\n" + |
819 |
" return s1;\n" + |
820 |
" }\n" + |
821 |
"}\n", |
822 |
"p1/IY.java", |
823 |
"package p1;\n" + |
824 |
"import org.eclipse.jdt.annotation.*;\n" + |
825 |
"public interface IY {\n" + |
826 |
" @NonNull String getString(@NonNull String s1, @NonNull String s2, @Nullable String s3);\n" + |
827 |
"}\n", |
828 |
"p1/Y.java", |
829 |
"package p1;\n" + |
830 |
"import org.eclipse.jdt.annotation.*;\n" + |
831 |
"public class Y extends X implements IY {\n" + |
832 |
" @Override\n" + |
833 |
" public @NonNull String getString(@Nullable String s1, @Nullable String s2, @Nullable String s3) {\n" + |
834 |
" return \"\";\n" + |
835 |
" }\n" + |
836 |
"}\n", |
837 |
}, |
838 |
customOptions, |
839 |
""); |
840 |
} |
841 |
// methods from two super types have different null contracts. |
842 |
// sub-class overrides this method in non-conforming ways |
843 |
public void test_parameter_specification_inheritance_013() { |
844 |
Map customOptions = getCompilerOptions(); |
845 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
846 |
runNegativeTestWithLibs( |
847 |
new String[] { |
848 |
"p1/X.java", |
849 |
"package p1;\n" + |
850 |
"import org.eclipse.jdt.annotation.*;\n" + |
851 |
"public class X {\n" + |
852 |
" public @Nullable String getString(String s1, @Nullable String s2, @NonNull String s3) {\n" + |
853 |
" return s1;\n" + |
854 |
" }\n" + |
855 |
"}\n", |
856 |
"p1/IY.java", |
857 |
"package p1;\n" + |
858 |
"import org.eclipse.jdt.annotation.*;\n" + |
859 |
"public interface IY {\n" + |
860 |
" @NonNull String getString(@NonNull String s1, @NonNull String s2, @Nullable String s3);\n" + |
861 |
"}\n", |
862 |
"p1/Y.java", |
863 |
"package p1;\n" + |
864 |
"import org.eclipse.jdt.annotation.*;\n" + |
865 |
"public class Y extends X implements IY {\n" + |
866 |
" @Override\n" + |
867 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
868 |
" return \"\";\n" + |
869 |
" }\n" + |
870 |
"}\n", |
871 |
}, |
872 |
customOptions, |
873 |
"----------\n" + |
874 |
"1. ERROR in p1\\Y.java (at line 5)\n" + |
875 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
876 |
" ^^^^^^^^^^^^^^^^\n" + |
877 |
"The return type is incompatible with the @NonNull return from IY.getString(String, String, String)\n" + |
878 |
"----------\n" + |
879 |
"2. ERROR in p1\\Y.java (at line 5)\n" + |
880 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
881 |
" ^^^^^^\n" + |
882 |
"Missing non-null annotation: inherited method from IY declares this parameter as @NonNull\n" + |
883 |
"----------\n" + |
884 |
"3. ERROR in p1\\Y.java (at line 5)\n" + |
885 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
886 |
" ^^^^^^^^^^^^^^^\n" + |
887 |
"Illegal redefinition of parameter s2, inherited method from X declares this parameter as @Nullable\n" + |
888 |
"----------\n" + |
889 |
"4. ERROR in p1\\Y.java (at line 5)\n" + |
890 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
891 |
" ^^^^^^^^^^^^^^^\n" + |
892 |
"Illegal redefinition of parameter s3, inherited method from IY declares this parameter as @Nullable\n" + |
893 |
"----------\n"); |
894 |
} |
895 |
// methods from two super types have different null contracts. |
896 |
// sub-class does not override, but should to bridge the incompatibility |
897 |
public void test_parameter_specification_inheritance_014() { |
898 |
Map customOptions = getCompilerOptions(); |
899 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
900 |
runNegativeTestWithLibs( |
901 |
new String[] { |
902 |
"p1/IY.java", |
903 |
"package p1;\n" + |
904 |
"import org.eclipse.jdt.annotation.*;\n" + |
905 |
"public interface IY {\n" + |
906 |
" public @NonNull String getString1(String s);\n" + |
907 |
" public @NonNull String getString2(String s);\n" + |
908 |
" public String getString3(@Nullable String s);\n" + |
909 |
" public @NonNull String getString4(@Nullable String s);\n" + |
910 |
" public @NonNull String getString5(@Nullable String s);\n" + |
911 |
" public @Nullable String getString6(@NonNull String s);\n" + |
912 |
"}\n", |
913 |
"p1/X.java", |
914 |
"package p1;\n" + |
915 |
"import org.eclipse.jdt.annotation.*;\n" + |
916 |
"public class X {\n" + |
917 |
" public @Nullable String getString1(String s) {\n" + // incomp. return |
918 |
" return s;\n" + |
919 |
" }\n" + |
920 |
" public String getString2(String s) {\n" + // incomp. return |
921 |
" return s;\n" + |
922 |
" }\n" + |
923 |
" public String getString3(String s) {\n" + // incomp. arg |
924 |
" return \"\";\n" + |
925 |
" }\n" + |
926 |
" public @NonNull String getString4(@Nullable String s) {\n" + |
927 |
" return \"\";\n" + |
928 |
" }\n" + |
929 |
" public @NonNull String getString5(@NonNull String s) {\n" + // incomp. arg |
930 |
" return s;\n" + |
931 |
" }\n" + |
932 |
" public @NonNull String getString6(@Nullable String s) {\n" + |
933 |
" return \"\";\n" + |
934 |
" }\n" + |
935 |
"}\n", |
936 |
"p1/Y.java", |
937 |
"package p1;\n" + |
938 |
"public class Y extends X implements IY {\n" + |
939 |
"}\n", |
940 |
}, |
941 |
customOptions, |
942 |
"----------\n" + |
943 |
"1. ERROR in p1\\Y.java (at line 2)\n" + |
944 |
" public class Y extends X implements IY {\n" + |
945 |
" ^\n" + |
946 |
"The method getString1(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
947 |
"----------\n" + |
948 |
"2. ERROR in p1\\Y.java (at line 2)\n" + |
949 |
" public class Y extends X implements IY {\n" + |
950 |
" ^\n" + |
951 |
"The method getString2(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
952 |
"----------\n" + |
953 |
"3. ERROR in p1\\Y.java (at line 2)\n" + |
954 |
" public class Y extends X implements IY {\n" + |
955 |
" ^\n" + |
956 |
"The method getString5(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
957 |
"----------\n" + |
958 |
"4. ERROR in p1\\Y.java (at line 2)\n" + |
959 |
" public class Y extends X implements IY {\n" + |
960 |
" ^\n" + |
961 |
"The method getString3(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
962 |
"----------\n"); |
963 |
} |
964 |
// a nullable return value is dereferenced without a check |
965 |
public void test_nullable_return_001() { |
966 |
runNegativeTestWithLibs( |
967 |
new String[] { |
968 |
"X.java", |
969 |
"import org.eclipse.jdt.annotation.*;\n" + |
970 |
"public class X {\n" + |
971 |
" @Nullable Object getObject() { return null; }\n" + |
972 |
" void foo() {\n" + |
973 |
" Object o = getObject();\n" + |
974 |
" System.out.print(o.toString());\n" + |
975 |
" }\n" + |
976 |
"}\n" |
977 |
}, |
978 |
"----------\n" + |
979 |
"1. ERROR in X.java (at line 6)\n" + |
980 |
" System.out.print(o.toString());\n" + |
981 |
" ^\n" + |
982 |
"Potential null pointer access: The variable o may be null at this location\n" + |
983 |
"----------\n"); |
984 |
} |
985 |
// a nullable return value is dereferenced without a check, method is read from .class file |
986 |
public void test_nullable_return_002() { |
987 |
runConformTestWithLibs( |
988 |
new String[] { |
989 |
"Lib.java", |
990 |
"import org.eclipse.jdt.annotation.*;\n" + |
991 |
"public class Lib {\n" + |
992 |
" @Nullable Object getObject() { return null; }\n" + |
993 |
"}\n" |
994 |
}, |
995 |
null /*customOptions*/, |
996 |
""); |
997 |
runNegativeTestWithLibs( |
998 |
false, // don't flush |
999 |
new String[] { |
1000 |
"X.java", |
1001 |
"public class X {\n" + |
1002 |
" void foo(Lib l) {\n" + |
1003 |
" Object o = l.getObject();\n" + |
1004 |
" System.out.print(o.toString());\n" + |
1005 |
" }\n" + |
1006 |
"}\n" |
1007 |
}, |
1008 |
null /*customOptions*/, |
1009 |
"----------\n" + |
1010 |
"1. ERROR in X.java (at line 4)\n" + |
1011 |
" System.out.print(o.toString());\n" + |
1012 |
" ^\n" + |
1013 |
"Potential null pointer access: The variable o may be null at this location\n" + |
1014 |
"----------\n"); |
1015 |
} |
1016 |
// a non-null return value is checked for null, method is read from .class file |
1017 |
public void test_nonnull_return_001() { |
1018 |
runConformTestWithLibs( |
1019 |
new String[] { |
1020 |
"Lib.java", |
1021 |
"import org.eclipse.jdt.annotation.*;\n" + |
1022 |
"public class Lib {\n" + |
1023 |
" @NonNull Object getObject() { return new Object(); }\n" + |
1024 |
"}\n" |
1025 |
}, |
1026 |
null /*customOptions*/, |
1027 |
""); |
1028 |
runNegativeTestWithLibs( |
1029 |
false, // don't flush |
1030 |
new String[] { |
1031 |
"X.java", |
1032 |
"public class X {\n" + |
1033 |
" void foo(Lib l) {\n" + |
1034 |
" Object o = l.getObject();\n" + |
1035 |
" if (o != null)\n" + |
1036 |
" System.out.print(o.toString());\n" + |
1037 |
" }\n" + |
1038 |
"}\n" |
1039 |
}, |
1040 |
null /*customOptions*/, |
1041 |
"----------\n" + |
1042 |
"1. ERROR in X.java (at line 4)\n" + |
1043 |
" if (o != null)\n" + |
1044 |
" ^\n" + |
1045 |
"Redundant null check: The variable o cannot be null at this location\n" + |
1046 |
"----------\n"); |
1047 |
} |
1048 |
// a non-null method returns null |
1049 |
public void test_nonnull_return_003() { |
1050 |
runNegativeTestWithLibs( |
1051 |
new String[] { |
1052 |
"X.java", |
1053 |
"import org.eclipse.jdt.annotation.*;\n" + |
1054 |
"public class X {\n" + |
1055 |
" @NonNull Object getObject(boolean b) {\n" + |
1056 |
" if (b)\n" + |
1057 |
" return null;\n" + // definite specification violation despite enclosing "if" |
1058 |
" return new Object();\n" + |
1059 |
" }\n" + |
1060 |
"}\n" |
1061 |
}, |
1062 |
"----------\n" + |
1063 |
"1. ERROR in X.java (at line 5)\n" + |
1064 |
" return null;\n" + |
1065 |
" ^^^^\n" + |
1066 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1067 |
"----------\n"); |
1068 |
} |
1069 |
// a non-null method potentially returns null |
1070 |
public void test_nonnull_return_004() { |
1071 |
runNegativeTestWithLibs( |
1072 |
new String[] { |
1073 |
"X.java", |
1074 |
"import org.eclipse.jdt.annotation.*;\n" + |
1075 |
"public class X {\n" + |
1076 |
" @NonNull Object getObject(@Nullable Object o) {\n" + |
1077 |
" return o;\n" + // 'o' is only potentially null |
1078 |
" }\n" + |
1079 |
"}\n" |
1080 |
}, |
1081 |
null /*customOptions*/, |
1082 |
"----------\n" + |
1083 |
"1. ERROR in X.java (at line 4)\n" + |
1084 |
" return o;\n" + |
1085 |
" ^\n" + |
1086 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1087 |
"----------\n"); |
1088 |
} |
1089 |
// a non-null method returns its non-null argument |
1090 |
public void test_nonnull_return_005() { |
1091 |
runConformTestWithLibs( |
1092 |
new String[] { |
1093 |
"X.java", |
1094 |
"import org.eclipse.jdt.annotation.*;\n" + |
1095 |
"public class X {\n" + |
1096 |
" @NonNull Object getObject(@NonNull Object o) {\n" + |
1097 |
" return o;\n" + |
1098 |
" }\n" + |
1099 |
"}\n" |
1100 |
}, |
1101 |
null, // options |
1102 |
""); |
1103 |
} |
1104 |
//a non-null method has insufficient nullness info for its return value |
1105 |
public void test_nonnull_return_006() { |
1106 |
runNegativeTestWithLibs( |
1107 |
new String[] { |
1108 |
"X.java", |
1109 |
"import org.eclipse.jdt.annotation.*;\n" + |
1110 |
"public class X {\n" + |
1111 |
" @NonNull Object getObject(Object o) {\n" + |
1112 |
" return o;\n" + |
1113 |
" }\n" + |
1114 |
"}\n" |
1115 |
}, |
1116 |
"----------\n" + |
1117 |
"1. WARNING in X.java (at line 4)\n" + |
1118 |
" return o;\n" + |
1119 |
" ^\n" + |
1120 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
1121 |
"----------\n"); |
1122 |
} |
1123 |
// a result from a nullable method is directly dereferenced |
1124 |
public void test_nonnull_return_007() { |
1125 |
runNegativeTestWithLibs( |
1126 |
new String[] { |
1127 |
"X.java", |
1128 |
"import org.eclipse.jdt.annotation.*;\n" + |
1129 |
"public class X {\n" + |
1130 |
" @Nullable Object getObject() {\n" + |
1131 |
" return null;\n" + |
1132 |
" }\n" + |
1133 |
" void test() {\n" + |
1134 |
" getObject().toString();\n" + |
1135 |
" }\n" + |
1136 |
"}\n" |
1137 |
}, |
1138 |
"----------\n" + |
1139 |
"1. ERROR in X.java (at line 7)\n" + |
1140 |
" getObject().toString();\n" + |
1141 |
" ^^^^^^^^^^^\n" + |
1142 |
"Potential null pointer access: The method getObject() may return null\n" + |
1143 |
"----------\n"); |
1144 |
} |
1145 |
// a result from a nonnull method is directly checked for null: redundant |
1146 |
public void test_nonnull_return_008() { |
1147 |
Map customOptions = getCompilerOptions(); |
1148 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1149 |
runNegativeTestWithLibs( |
1150 |
new String[] { |
1151 |
"X.java", |
1152 |
"import org.eclipse.jdt.annotation.*;\n" + |
1153 |
"public class X {\n" + |
1154 |
" @NonNull Object getObject() {\n" + |
1155 |
" return new Object();\n" + |
1156 |
" }\n" + |
1157 |
" void test() {\n" + |
1158 |
" if (getObject() == null)\n" + |
1159 |
" throw new RuntimeException();\n" + |
1160 |
" }\n" + |
1161 |
"}\n" |
1162 |
}, |
1163 |
customOptions, |
1164 |
"----------\n" + |
1165 |
"1. ERROR in X.java (at line 7)\n" + |
1166 |
" if (getObject() == null)\n" + |
1167 |
" ^^^^^^^^^^^\n" + |
1168 |
"Redundant null check: The method getObject() cannot return null\n" + |
1169 |
"----------\n"); |
1170 |
} |
1171 |
// a result from a nonnull method is directly checked for null (from local): redundant |
1172 |
public void test_nonnull_return_009() { |
1173 |
Map customOptions = getCompilerOptions(); |
1174 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1175 |
runNegativeTestWithLibs( |
1176 |
new String[] { |
1177 |
"X.java", |
1178 |
"import org.eclipse.jdt.annotation.*;\n" + |
1179 |
"public class X {\n" + |
1180 |
" @NonNull Object getObject() {\n" + |
1181 |
" return new Object();\n" + |
1182 |
" }\n" + |
1183 |
" void test() {\n" + |
1184 |
" Object left = null;\n" + |
1185 |
" if (left != getObject())\n" + |
1186 |
" throw new RuntimeException();\n" + |
1187 |
" }\n" + |
1188 |
"}\n" |
1189 |
}, |
1190 |
customOptions, |
1191 |
"----------\n" + |
1192 |
"1. ERROR in X.java (at line 8)\n" + |
1193 |
" if (left != getObject())\n" + |
1194 |
" ^^^^\n" + |
1195 |
"Redundant null check: The variable left can only be null at this location\n" + |
1196 |
"----------\n" + |
1197 |
"2. ERROR in X.java (at line 8)\n" + |
1198 |
" if (left != getObject())\n" + |
1199 |
" ^^^^^^^^^^^\n" + |
1200 |
"Redundant null check: The method getObject() cannot return null\n" + |
1201 |
"----------\n"); |
1202 |
} |
1203 |
// a result from a nullable method is assigned and checked for null (from local): not redundant |
1204 |
// see also Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop |
1205 |
public void test_nonnull_return_010() { |
1206 |
Map customOptions = getCompilerOptions(); |
1207 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1208 |
runNegativeTestWithLibs( |
1209 |
new String[] { |
1210 |
"X.java", |
1211 |
"import org.eclipse.jdt.annotation.*;\n" + |
1212 |
"public class X {\n" + |
1213 |
" @Nullable X getX() {\n" + |
1214 |
" return new X();\n" + |
1215 |
" }\n" + |
1216 |
" void test() {\n" + |
1217 |
" X left = this;\n" + |
1218 |
" do {\n" + |
1219 |
" if (left == null) \n" + |
1220 |
" throw new RuntimeException();\n" + |
1221 |
" } while ((left = left.getX()) != null);\n" + // no warning/error here! |
1222 |
" }\n" + |
1223 |
"}\n" |
1224 |
}, |
1225 |
customOptions, |
1226 |
"----------\n" + |
1227 |
"1. ERROR in X.java (at line 9)\n" + |
1228 |
" if (left == null) \n" + |
1229 |
" ^^^^\n" + |
1230 |
"Null comparison always yields false: The variable left cannot be null at this location\n" + |
1231 |
"----------\n"); |
1232 |
} |
1233 |
// a non-null method returns a checked-for null value, but that branch is dead code |
1234 |
public void test_nonnull_return_011() { |
1235 |
Map customOptions = getCompilerOptions(); |
1236 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1237 |
runNegativeTestWithLibs( |
1238 |
new String[] { |
1239 |
"X.java", |
1240 |
"import org.eclipse.jdt.annotation.*;\n" + |
1241 |
"@NonNullByDefault\n" + |
1242 |
"public class X {\n" + |
1243 |
" Object getObject(Object dubious) {\n" + |
1244 |
" if (dubious == null)\n" + // redundant |
1245 |
" return dubious;\n" + // definitely null, but not reported inside dead code |
1246 |
" return new Object();\n" + |
1247 |
" }\n" + |
1248 |
"}\n" |
1249 |
}, |
1250 |
customOptions, |
1251 |
"----------\n" + |
1252 |
"1. ERROR in X.java (at line 5)\n" + |
1253 |
" if (dubious == null)\n" + |
1254 |
" ^^^^^^^\n" + |
1255 |
"Null comparison always yields false: The variable dubious cannot be null at this location\n" + |
1256 |
"----------\n" + |
1257 |
"2. WARNING in X.java (at line 6)\n" + |
1258 |
" return dubious;\n" + |
1259 |
" ^^^^^^^^^^^^^^^\n" + |
1260 |
"Dead code\n" + |
1261 |
"----------\n"); |
1262 |
} |
1263 |
// a non-null method returns a definite null from a conditional expression |
1264 |
// requires the fix for Bug 354554 - [null] conditional with redundant condition yields weak error message |
1265 |
// TODO(SH): ENABLE! |
1266 |
public void _test_nonnull_return_012() { |
1267 |
Map customOptions = getCompilerOptions(); |
1268 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1269 |
runNegativeTestWithLibs( |
1270 |
new String[] { |
1271 |
"X.java", |
1272 |
"import org.eclipse.jdt.annotation.*;\n" + |
1273 |
"@NonNullByDefault\n" + |
1274 |
"public class X {\n" + |
1275 |
" Object getObject(Object dubious) {\n" + |
1276 |
" return dubious == null ? dubious : null;\n" + |
1277 |
" }\n" + |
1278 |
"}\n" |
1279 |
}, |
1280 |
customOptions, |
1281 |
"----------\n" + |
1282 |
"1. ERROR in X.java (at line 5)\n" + |
1283 |
" return dubious == null ? dubious : null;\n" + |
1284 |
" ^^^^^^^\n" + |
1285 |
"Null comparison always yields false: The variable dubious cannot be null at this location\n" + |
1286 |
"----------\n" + |
1287 |
"2. ERROR in X.java (at line 5)\n" + |
1288 |
" return dubious == null ? dubious : null;\n" + |
1289 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
1290 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1291 |
"----------\n"); |
1292 |
} |
1293 |
// don't apply any default annotations to return void |
1294 |
public void test_nonnull_return_013() { |
1295 |
Map customOptions = getCompilerOptions(); |
1296 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1297 |
runConformTestWithLibs( |
1298 |
new String[] { |
1299 |
"X.java", |
1300 |
"import org.eclipse.jdt.annotation.*;\n" + |
1301 |
"@NonNullByDefault\n" + |
1302 |
"public class X {\n" + |
1303 |
" void getObject() {}\n" + |
1304 |
"}\n", |
1305 |
"Y.java", |
1306 |
"public class Y extends X {\n" + |
1307 |
" @Override\n" + |
1308 |
" void getObject() {}\n" + // don't complain, void takes no (default) annotation |
1309 |
"}\n" |
1310 |
}, |
1311 |
customOptions, |
1312 |
""); |
1313 |
} |
1314 |
//suppress an error regarding null-spec violation |
1315 |
public void test_suppress_001() { |
1316 |
Map customOptions = getCompilerOptions(); |
1317 |
customOptions.put(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, JavaCore.ENABLED); |
1318 |
runConformTestWithLibs( |
1319 |
new String[] { |
1320 |
"X.java", |
1321 |
"import org.eclipse.jdt.annotation.*;\n" + |
1322 |
"public class X {\n" + |
1323 |
" @SuppressWarnings(\"null\")\n" + |
1324 |
" @NonNull Object getObject(@Nullable Object o) {\n" + |
1325 |
" return o;\n" + // 'o' is only potentially null |
1326 |
" }\n" + |
1327 |
"}\n" |
1328 |
}, |
1329 |
customOptions, |
1330 |
""); |
1331 |
} |
1332 |
// mixed use of fully qualified name / explicit import |
1333 |
public void test_annotation_import_001() { |
1334 |
Map customOptions = getCompilerOptions(); |
1335 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.Nullable"); |
1336 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1337 |
runConformTestWithLibs( |
1338 |
new String[] { |
1339 |
CUSTOM_NULLABLE_NAME, |
1340 |
CUSTOM_NULLABLE_CONTENT, |
1341 |
CUSTOM_NONNULL_NAME, |
1342 |
CUSTOM_NONNULL_CONTENT, |
1343 |
"Lib.java", |
1344 |
"public class Lib {\n" + |
1345 |
" @org.foo.NonNull Object getObject() { return new Object(); }\n" + // FQN |
1346 |
"}\n", |
1347 |
"X.java", |
1348 |
"import org.foo.NonNull;\n" + // explicit import |
1349 |
"public class X {\n" + |
1350 |
" @NonNull Object getObject(@NonNull Lib l) {\n" + |
1351 |
" return l.getObject();\n" + |
1352 |
" }\n" + |
1353 |
"}\n" |
1354 |
}, |
1355 |
customOptions, |
1356 |
""); |
1357 |
} |
1358 |
|
1359 |
// use of explicit imports throughout |
1360 |
public void test_annotation_import_002() { |
1361 |
Map customOptions = getCompilerOptions(); |
1362 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.Nullable"); |
1363 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1364 |
runConformTest( |
1365 |
new String[] { |
1366 |
CUSTOM_NULLABLE_NAME, |
1367 |
CUSTOM_NULLABLE_CONTENT, |
1368 |
CUSTOM_NONNULL_NAME, |
1369 |
CUSTOM_NONNULL_CONTENT, |
1370 |
"Lib.java", |
1371 |
"import org.foo.NonNull;\n" + |
1372 |
"public class Lib {\n" + |
1373 |
" @NonNull Object getObject() { return new Object(); }\n" + |
1374 |
"}\n", |
1375 |
"X.java", |
1376 |
"import org.foo.NonNull;\n" + |
1377 |
"public class X {\n" + |
1378 |
" @NonNull Object getObject(@org.foo.Nullable String dummy, @NonNull Lib l) {\n" + |
1379 |
" Object o = l.getObject();" + |
1380 |
" return o;\n" + |
1381 |
" }\n" + |
1382 |
"}\n" |
1383 |
}, |
1384 |
customOptions, |
1385 |
""); |
1386 |
} |
1387 |
// explicit import of existing annotation types |
1388 |
// using a Lib without null specifications |
1389 |
public void test_annotation_import_005() { |
1390 |
Map customOptions = getCompilerOptions(); |
1391 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1392 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull"); |
1393 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull"); |
1394 |
runNegativeTest( |
1395 |
true/*shouldFlushOutputDirectory*/, |
1396 |
new String[] { |
1397 |
"org/foo/MayBeNull.java", |
1398 |
"package org.foo;\n" + |
1399 |
"import java.lang.annotation.*;\n" + |
1400 |
"@Retention(RetentionPolicy.CLASS)\n" + |
1401 |
"public @interface MayBeNull {}\n", |
1402 |
|
1403 |
"org/foo/MustNotBeNull.java", |
1404 |
"package org.foo;\n" + |
1405 |
"import java.lang.annotation.*;\n" + |
1406 |
"@Retention(RetentionPolicy.CLASS)\n" + |
1407 |
"public @interface MustNotBeNull {}\n", |
1408 |
|
1409 |
"Lib.java", |
1410 |
"public class Lib {\n" + |
1411 |
" Object getObject() { return new Object(); }\n" + |
1412 |
"}\n", |
1413 |
"X.java", |
1414 |
"import org.foo.*;\n" + |
1415 |
"public class X {\n" + |
1416 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1417 |
" return l.getObject();\n" + |
1418 |
" }\n" + |
1419 |
"}\n", |
1420 |
|
1421 |
}, |
1422 |
null /*no libs*/, |
1423 |
customOptions, |
1424 |
"----------\n" + |
1425 |
"1. ERROR in X.java (at line 4)\n" + |
1426 |
" return l.getObject();\n" + |
1427 |
" ^^^^^^^^^^^^^\n" + |
1428 |
"Potential type mismatch: required \'@MustNotBeNull Object\' but nullness of the provided value is unknown\n" + |
1429 |
"----------\n", |
1430 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1431 |
} |
1432 |
// a non-null method returns a value obtained from an unannotated method, missing annotation types |
1433 |
public void test_annotation_import_006() { |
1434 |
Map customOptions = getCompilerOptions(); |
1435 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1436 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull"); |
1437 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull"); |
1438 |
runNegativeTest( |
1439 |
true/*shouldFlushOutputDirectory*/, |
1440 |
new String[] { |
1441 |
"Lib.java", |
1442 |
"public class Lib {\n" + |
1443 |
" Object getObject() { return new Object(); }\n" + |
1444 |
"}\n", |
1445 |
"X.java", |
1446 |
"public class X {\n" + |
1447 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1448 |
" return l.getObject();\n" + |
1449 |
" }\n" + |
1450 |
"}\n" |
1451 |
}, |
1452 |
null /* no libs */, |
1453 |
customOptions, |
1454 |
"----------\n" + |
1455 |
"1. ERROR in X.java (at line 2)\n" + |
1456 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1457 |
" ^^^^^^^^^^^^^\n" + |
1458 |
"MustNotBeNull cannot be resolved to a type\n" + |
1459 |
"----------\n" + |
1460 |
"2. ERROR in X.java (at line 2)\n" + |
1461 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1462 |
" ^^^^^^^^^^^^^\n" + |
1463 |
"MustNotBeNull cannot be resolved to a type\n" + |
1464 |
"----------\n", |
1465 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1466 |
} |
1467 |
// using nullness defaulting to nonnull, missing annotation types |
1468 |
public void test_annotation_import_007() { |
1469 |
Map customOptions = getCompilerOptions(); |
1470 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1471 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull"); |
1472 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull"); |
1473 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
1474 |
runNegativeTest( |
1475 |
true/*shouldFlushOutputDirectory*/, |
1476 |
new String[] { |
1477 |
"Lib.java", |
1478 |
"public class Lib {\n" + |
1479 |
" Object getObject() { return new Object(); }\n" + |
1480 |
"}\n", |
1481 |
"X.java", |
1482 |
"public class X {\n" + |
1483 |
" Object getObject(Lib l) {\n" + |
1484 |
" return l.getObject();\n" + |
1485 |
" }\n" + |
1486 |
"}\n" |
1487 |
}, |
1488 |
this.LIBS, |
1489 |
customOptions, |
1490 |
"----------\n" + |
1491 |
"1. ERROR in Lib.java (at line 1)\n" + |
1492 |
" public class Lib {\n" + |
1493 |
" ^\n" + |
1494 |
"Buildpath problem: the type org.foo.MustNotBeNull, which is configured as a null annotation type, cannot be resolved\n" + |
1495 |
"----------\n", |
1496 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1497 |
} |
1498 |
|
1499 |
// a null annotation is illegally used on a class: |
1500 |
public void test_illegal_annotation_001() { |
1501 |
runNegativeTest( |
1502 |
new String[] { |
1503 |
"X.java", |
1504 |
"import org.eclipse.jdt.annotation.*;\n" + |
1505 |
"@NonNull public class X {\n" + |
1506 |
"}\n" |
1507 |
}, |
1508 |
"----------\n" + |
1509 |
"1. ERROR in X.java (at line 2)\n" + |
1510 |
" @NonNull public class X {\n" + |
1511 |
" ^^^^^^^^\n" + |
1512 |
"The annotation @NonNull is disallowed for this location\n" + |
1513 |
"----------\n", |
1514 |
this.LIBS, |
1515 |
false/*shouldFlush*/); |
1516 |
} |
1517 |
// this test has been removed: |
1518 |
// setting default to nullable, default applies to a parameter |
1519 |
// public void test_default_nullness_001() |
1520 |
|
1521 |
// a null annotation is illegally defined by its simple name |
1522 |
public void test_illegal_annotation_002() { |
1523 |
Map customOptions = getCompilerOptions(); |
1524 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "NichtNull"); |
1525 |
runNegativeTestWithLibs( |
1526 |
new String[] { |
1527 |
"X.java", |
1528 |
"public class X {\n" + |
1529 |
"}\n" |
1530 |
}, |
1531 |
customOptions, |
1532 |
"----------\n" + |
1533 |
"1. ERROR in X.java (at line 1)\n" + |
1534 |
" public class X {\n" + |
1535 |
" ^\n" + |
1536 |
"Cannot use the unqualified name \'NichtNull\' as an annotation name for null specification\n" + |
1537 |
"----------\n"); |
1538 |
} |
1539 |
|
1540 |
// a null annotation is illegally used on a void method: |
1541 |
public void test_illegal_annotation_003() { |
1542 |
runNegativeTest( |
1543 |
new String[] { |
1544 |
"X.java", |
1545 |
"import org.eclipse.jdt.annotation.*;\n" + |
1546 |
"public class X {\n" + |
1547 |
" @NonNull void foo() {}\n" + |
1548 |
"}\n" |
1549 |
}, |
1550 |
"----------\n" + |
1551 |
"1. ERROR in X.java (at line 3)\n" + |
1552 |
" @NonNull void foo() {}\n" + |
1553 |
" ^^^^^^^^^^^^^\n" + |
1554 |
"The nullness annotation @NonNull is not applicable for the primitive type void\n" + |
1555 |
"----------\n", |
1556 |
this.LIBS, |
1557 |
false/*shouldFlush*/); |
1558 |
} |
1559 |
|
1560 |
// a null annotation is illegally used on a primitive type parameter |
1561 |
public void test_illegal_annotation_004() { |
1562 |
runNegativeTest( |
1563 |
new String[] { |
1564 |
"X.java", |
1565 |
"import org.eclipse.jdt.annotation.*;\n" + |
1566 |
"public class X {\n" + |
1567 |
" void foo(@Nullable int i) {}\n" + |
1568 |
"}\n" |
1569 |
}, |
1570 |
"----------\n" + |
1571 |
"1. ERROR in X.java (at line 3)\n" + |
1572 |
" void foo(@Nullable int i) {}\n" + |
1573 |
" ^^^^^^^^^^^^^\n" + |
1574 |
"The nullness annotation @Nullable is not applicable for the primitive type int\n" + |
1575 |
"----------\n", |
1576 |
this.LIBS, |
1577 |
false/*shouldFlush*/); |
1578 |
} |
1579 |
|
1580 |
// a null annotation is illegally used on a primitive type local var |
1581 |
public void test_illegal_annotation_005() { |
1582 |
runNegativeTest( |
1583 |
new String[] { |
1584 |
"X.java", |
1585 |
"import org.eclipse.jdt.annotation.*;\n" + |
1586 |
"public class X {\n" + |
1587 |
" int foo() {\n" + |
1588 |
" @Nullable int i = 3;\n" + |
1589 |
" return i;\n" + |
1590 |
" }\n" + |
1591 |
"}\n" |
1592 |
}, |
1593 |
"----------\n" + |
1594 |
"1. ERROR in X.java (at line 4)\n" + |
1595 |
" @Nullable int i = 3;\n" + |
1596 |
" ^^^^^^^^^^^^^\n" + |
1597 |
"The nullness annotation @Nullable is not applicable for the primitive type int\n" + |
1598 |
"----------\n", |
1599 |
this.LIBS, |
1600 |
false/*shouldFlush*/); |
1601 |
} |
1602 |
|
1603 |
// a configured annotation type does not exist |
1604 |
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=186342#c133 |
1605 |
public void test_illegal_annotation_006() { |
1606 |
Map customOptions = getCompilerOptions(); |
1607 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "nullAnn.Nullable"); |
1608 |
runNegativeTestWithLibs( |
1609 |
new String[] { |
1610 |
"p/Test.java", |
1611 |
"package p;\n" + |
1612 |
"import nullAnn.*; // 1 \n" + |
1613 |
"\n" + |
1614 |
"public class Test { \n" + |
1615 |
"\n" + |
1616 |
" void foo(@nullAnn.Nullable Object o) { // 2\n" + |
1617 |
" o.toString(); \n" + |
1618 |
" }\n" + |
1619 |
"}" |
1620 |
}, |
1621 |
customOptions, |
1622 |
"----------\n" + |
1623 |
"1. ERROR in p\\Test.java (at line 2)\n" + |
1624 |
" import nullAnn.*; // 1 \n" + |
1625 |
" ^^^^^^^\n" + |
1626 |
"The import nullAnn cannot be resolved\n" + |
1627 |
"----------\n" + |
1628 |
"2. ERROR in p\\Test.java (at line 6)\n" + |
1629 |
" void foo(@nullAnn.Nullable Object o) { // 2\n" + |
1630 |
" ^^^^^^^\n" + |
1631 |
"nullAnn cannot be resolved to a type\n" + |
1632 |
"----------\n"); |
1633 |
} |
1634 |
|
1635 |
public void test_default_nullness_002() { |
1636 |
Map customOptions = getCompilerOptions(); |
1637 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1638 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
1639 |
runNegativeTestWithLibs( |
1640 |
new String[] { |
1641 |
"X.java", |
1642 |
"import org.eclipse.jdt.annotation.*;\n" + |
1643 |
"public class X {\n" + |
1644 |
" Object getObject(@Nullable Object o) {\n" + |
1645 |
" return new Object();\n" + |
1646 |
" }\n" + |
1647 |
"}\n", |
1648 |
"Y.java", |
1649 |
"import org.eclipse.jdt.annotation.*;\n" + |
1650 |
"public class Y extends X {\n" + |
1651 |
" @Override\n" + |
1652 |
" @Nullable Object getObject(Object o) {\n" + // complain illegal return redef and inherited annot is not repeated |
1653 |
" return o;\n" + |
1654 |
" }\n" + |
1655 |
"}\n", |
1656 |
}, |
1657 |
customOptions, |
1658 |
// main error: |
1659 |
"----------\n" + |
1660 |
"1. ERROR in Y.java (at line 4)\n" + |
1661 |
" @Nullable Object getObject(Object o) {\n" + |
1662 |
" ^^^^^^^^^^^^^^^^\n" + |
1663 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1664 |
"----------\n" + |
1665 |
// additional error: |
1666 |
"2. ERROR in Y.java (at line 4)\n" + |
1667 |
" @Nullable Object getObject(Object o) {\n" + |
1668 |
" ^^^^^^\n" + |
1669 |
"Illegal redefinition of parameter o, inherited method from X declares this parameter as @Nullable\n" + |
1670 |
"----------\n"); |
1671 |
} |
1672 |
// package default is non-null |
1673 |
public void test_default_nullness_003() { |
1674 |
Map customOptions = getCompilerOptions(); |
1675 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1676 |
runNegativeTestWithLibs( |
1677 |
new String[] { |
1678 |
"p1/X.java", |
1679 |
"package p1;\n" + |
1680 |
"import org.eclipse.jdt.annotation.*;\n" + |
1681 |
"@NonNullByDefault\n" + |
1682 |
"public class X {\n" + |
1683 |
" protected Object getObject(@Nullable Object o) {\n" + |
1684 |
" return new Object();\n" + |
1685 |
" }\n" + |
1686 |
"}\n", |
1687 |
"p2/package-info.java", |
1688 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1689 |
"package p2;\n", |
1690 |
"p2/Y.java", |
1691 |
"package p2;\n" + |
1692 |
"import org.eclipse.jdt.annotation.*;\n" + |
1693 |
"public class Y extends p1.X {\n" + |
1694 |
" @Override\n" + |
1695 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1696 |
" bar(o);\n" + |
1697 |
" return o;\n" + |
1698 |
" }\n" + |
1699 |
" void bar(Object o2) { }\n" + // parameter is nonnull per package default |
1700 |
"}\n" |
1701 |
}, |
1702 |
customOptions, |
1703 |
"----------\n" + |
1704 |
"1. ERROR in p2\\Y.java (at line 5)\n" + |
1705 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1706 |
" ^^^^^^^^^^^^^^^^\n" + |
1707 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1708 |
"----------\n" + |
1709 |
"2. ERROR in p2\\Y.java (at line 6)\n" + |
1710 |
" bar(o);\n" + |
1711 |
" ^\n" + |
1712 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1713 |
"----------\n"); |
1714 |
} |
1715 |
// package level default is consumed from package-info.class |
1716 |
public void test_default_nullness_003a() { |
1717 |
Map customOptions = getCompilerOptions(); |
1718 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1719 |
runConformTestWithLibs( |
1720 |
new String[] { |
1721 |
"p1/X.java", |
1722 |
"package p1;\n" + |
1723 |
"import org.eclipse.jdt.annotation.*;\n" + |
1724 |
"@NonNullByDefault\n" + |
1725 |
"public class X {\n" + |
1726 |
" protected Object getObject(@Nullable Object o) {\n" + |
1727 |
" return new Object();\n" + |
1728 |
" }\n" + |
1729 |
" protected void bar(Object o2) { }\n" + // parameter is nonnull per type default |
1730 |
"}\n", |
1731 |
"p2/package-info.java", |
1732 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1733 |
"package p2;\n", |
1734 |
}, |
1735 |
customOptions, |
1736 |
""); |
1737 |
// check if default is visible from package-info.class. |
1738 |
runNegativeTestWithLibs( |
1739 |
false, // don't flush |
1740 |
new String[] { |
1741 |
"p2/Y.java", |
1742 |
"package p2;\n" + |
1743 |
"import org.eclipse.jdt.annotation.*;\n" + |
1744 |
"public class Y extends p1.X {\n" + |
1745 |
" @Override\n" + |
1746 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + // can't override inherited default nonnull |
1747 |
" bar(o);\n" + // parameter is nonnull in super class's .class file |
1748 |
" accept(o);\n" + |
1749 |
" return o;\n" + |
1750 |
" }\n" + |
1751 |
" void accept(Object a) {}\n" + // governed by package level default |
1752 |
"}\n" |
1753 |
}, |
1754 |
customOptions, |
1755 |
"----------\n" + |
1756 |
"1. ERROR in p2\\Y.java (at line 5)\n" + |
1757 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1758 |
" ^^^^^^^^^^^^^^^^\n" + |
1759 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1760 |
"----------\n" + |
1761 |
"2. ERROR in p2\\Y.java (at line 6)\n" + |
1762 |
" bar(o);\n" + |
1763 |
" ^\n" + |
1764 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1765 |
"----------\n" + |
1766 |
"3. ERROR in p2\\Y.java (at line 7)\n" + |
1767 |
" accept(o);\n" + |
1768 |
" ^\n" + |
1769 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1770 |
"----------\n"); |
1771 |
} |
1772 |
// don't apply type-level default to non-reference type |
1773 |
public void test_default_nullness_004() { |
1774 |
Map customOptions = getCompilerOptions(); |
1775 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1776 |
runConformTestWithLibs( |
1777 |
new String[] { |
1778 |
"p1/X.java", |
1779 |
"package p1;\n" + |
1780 |
"import org.eclipse.jdt.annotation.*;\n" + |
1781 |
"@NonNullByDefault\n" + |
1782 |
"public class X {\n" + |
1783 |
" protected Object getObject(boolean o) {\n" + |
1784 |
" return new Object();\n" + |
1785 |
" }\n" + |
1786 |
"}\n", |
1787 |
"p2/Y.java", |
1788 |
"package p2;\n" + |
1789 |
"import org.eclipse.jdt.annotation.*;\n" + |
1790 |
"public class Y extends p1.X {\n" + |
1791 |
" @Override\n" + |
1792 |
" protected @NonNull Object getObject(boolean o) {\n" + |
1793 |
" return o ? this : new Object();\n" + |
1794 |
" }\n" + |
1795 |
"}\n" |
1796 |
}, |
1797 |
customOptions, |
1798 |
""); |
1799 |
} |
1800 |
// package default is non-null |
1801 |
// see also Bug 354536 - compiling package-info.java still depends on the order of compilation units |
1802 |
public void test_default_nullness_005() { |
1803 |
Map customOptions = getCompilerOptions(); |
1804 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1805 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1806 |
runNegativeTestWithLibs( |
1807 |
new String[] { |
1808 |
"p1/X.java", |
1809 |
"package p1;\n" + |
1810 |
"public class X {\n" + |
1811 |
" class Inner {" + |
1812 |
" protected Object getObject(String s) {\n" + |
1813 |
" return null;\n" + |
1814 |
" }\n" + |
1815 |
" }\n" + |
1816 |
"}\n", |
1817 |
"p1/package-info.java", |
1818 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1819 |
"package p1;\n", |
1820 |
CUSTOM_NONNULL_NAME, |
1821 |
CUSTOM_NONNULL_CONTENT |
1822 |
}, |
1823 |
customOptions, |
1824 |
"----------\n" + |
1825 |
"1. ERROR in p1\\X.java (at line 4)\n" + |
1826 |
" return null;\n" + |
1827 |
" ^^^^\n" + |
1828 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1829 |
"----------\n"); |
1830 |
} |
1831 |
// package default is non-null, package-info.java read before the annotation type |
1832 |
// compile order: beginToCompile(X.Inner) triggers reading of package-info.java before the annotation type was read |
1833 |
public void test_default_nullness_006() { |
1834 |
Map customOptions = getCompilerOptions(); |
1835 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1836 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1837 |
runNegativeTestWithLibs( |
1838 |
new String[] { |
1839 |
"p1/package-info.java", |
1840 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1841 |
"package p1;\n", |
1842 |
"p1/X.java", |
1843 |
"package p1;\n" + |
1844 |
"public class X {\n" + |
1845 |
" class Inner {" + |
1846 |
" protected Object getObject(String s) {\n" + |
1847 |
" return null;\n" + |
1848 |
" }\n" + |
1849 |
" }\n" + |
1850 |
"}\n", |
1851 |
CUSTOM_NONNULL_NAME, |
1852 |
CUSTOM_NONNULL_CONTENT |
1853 |
}, |
1854 |
customOptions, |
1855 |
"----------\n" + |
1856 |
"1. ERROR in p1\\X.java (at line 4)\n" + |
1857 |
" return null;\n" + |
1858 |
" ^^^^\n" + |
1859 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1860 |
"----------\n"); |
1861 |
} |
1862 |
// global default nonnull, but return may be null |
1863 |
public void test_default_nullness_007() { |
1864 |
Map customOptions = getCompilerOptions(); |
1865 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1866 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
1867 |
runNegativeTestWithLibs( |
1868 |
new String[] { |
1869 |
"X.java", |
1870 |
"import org.eclipse.jdt.annotation.*;\n" + |
1871 |
"public class X {\n" + |
1872 |
" @Nullable Object dangerous() {\n" + |
1873 |
" return null;\n" + |
1874 |
" }\n" + |
1875 |
" Object broken() {\n" + |
1876 |
" return dangerous();\n" + |
1877 |
" }\n" + |
1878 |
"}\n", |
1879 |
|
1880 |
}, |
1881 |
customOptions, |
1882 |
"----------\n" + |
1883 |
"1. ERROR in X.java (at line 7)\n" + |
1884 |
" return dangerous();\n" + |
1885 |
" ^^^^^^^^^^^\n" + |
1886 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1887 |
"----------\n"); |
1888 |
} |
1889 |
|
1890 |
// cancel type level default to comply with super specification |
1891 |
public void test_default_nullness_008() { |
1892 |
Map customOptions = getCompilerOptions(); |
1893 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1894 |
runConformTestWithLibs( |
1895 |
new String[] { |
1896 |
"p1/X.java", |
1897 |
"package p1;\n" + |
1898 |
"public class X {\n" + |
1899 |
" protected Object getObject(Object o) {\n" + |
1900 |
" return new Object();\n" + |
1901 |
" }\n" + |
1902 |
"}\n", |
1903 |
"p2/Y.java", |
1904 |
"package p2;\n" + |
1905 |
"import org.eclipse.jdt.annotation.*;\n" + |
1906 |
"@NonNullByDefault\n" + |
1907 |
"public class Y extends p1.X {\n" + |
1908 |
" @Override\n" + |
1909 |
" @NonNullByDefault(false)\n" + |
1910 |
" protected Object getObject(Object o) {\n" + |
1911 |
" if (o.toString().length() == 0)\n" + // dereference without a warning |
1912 |
" return null;\n" + // return null without a warning |
1913 |
" return o.toString();\n" + |
1914 |
" }\n" + |
1915 |
"}\n" |
1916 |
}, |
1917 |
customOptions, |
1918 |
""); |
1919 |
} |
1920 |
|
1921 |
// cancel outer type level default to comply with super specification |
1922 |
public void test_default_nullness_009() { |
1923 |
Map customOptions = getCompilerOptions(); |
1924 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1925 |
runNegativeTestWithLibs( |
1926 |
new String[] { |
1927 |
"p1/X.java", |
1928 |
"package p1;\n" + |
1929 |
"public class X {\n" + |
1930 |
" protected Object getObject(Object o) {\n" + |
1931 |
" return new Object();\n" + |
1932 |
" }\n" + |
1933 |
"}\n", |
1934 |
"p2/Y.java", |
1935 |
"package p2;\n" + |
1936 |
"import org.eclipse.jdt.annotation.*;\n" + |
1937 |
"@NonNullByDefault\n" + |
1938 |
"public class Y { \n" + |
1939 |
" @NonNullByDefault(false)\n" + |
1940 |
" static class Z extends p1.X {\n" + |
1941 |
" @Override\n" + |
1942 |
" protected Object getObject(Object o) {\n" + |
1943 |
" if (o.toString().length() == 0) {\n" + |
1944 |
" o = null;\n" + // assign null without a warning |
1945 |
" bar(o); // error: arg is declared @NonNull\n" + |
1946 |
" return null;\n" + |
1947 |
" }\n" + |
1948 |
" return o.toString();\n" + |
1949 |
" }\n" + |
1950 |
" String bar(@NonNull Object o) {\n" + |
1951 |
" return getObject(o).toString();" + |
1952 |
" }\n" + |
1953 |
" }\n" + |
1954 |
"}\n" |
1955 |
}, |
1956 |
customOptions, |
1957 |
"----------\n" + |
1958 |
"1. ERROR in p2\\Y.java (at line 11)\n" + |
1959 |
" bar(o); // error: arg is declared @NonNull\n" + |
1960 |
" ^\n" + |
1961 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1962 |
"----------\n"); |
1963 |
} |
1964 |
// non-null declarations are redundant within a default scope. |
1965 |
public void test_default_nullness_010() { |
1966 |
Map customOptions = getCompilerOptions(); |
1967 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1968 |
runConformTestWithLibs( |
1969 |
new String[] { |
1970 |
"p2/Y.java", |
1971 |
"package p2;\n" + |
1972 |
"import org.eclipse.jdt.annotation.*;\n" + |
1973 |
"@NonNullByDefault\n" + |
1974 |
"public class Y {\n" + |
1975 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
1976 |
" return o;\n" + |
1977 |
" }\n" + |
1978 |
"}\n" |
1979 |
}, |
1980 |
customOptions, |
1981 |
"----------\n" + |
1982 |
"1. WARNING in p2\\Y.java (at line 5)\n" + |
1983 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
1984 |
" ^^^^^^^^^^^^^^^\n" + |
1985 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
1986 |
"----------\n" + |
1987 |
"2. WARNING in p2\\Y.java (at line 5)\n" + |
1988 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
1989 |
" ^^^^^^^^^^^^^^^^^\n" + |
1990 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
1991 |
"----------\n"); |
1992 |
} |
1993 |
// a nonnull variable is dereferenced in a loop |
1994 |
public void test_nonnull_var_in_constrol_structure_1() { |
1995 |
Map customOptions = getCompilerOptions(); |
1996 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1997 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
1998 |
runNegativeTestWithLibs( |
1999 |
new String[] { |
2000 |
"X.java", |
2001 |
"import org.eclipse.jdt.annotation.*;\n" + |
2002 |
"public class X {\n" + |
2003 |
" void print4(@NonNull String s) {\n" + |
2004 |
" for (int i=0; i<4; i++)\n" + |
2005 |
" print(s);\n" + |
2006 |
" }\n" + |
2007 |
" void print5(@Nullable String s) {\n" + |
2008 |
" for (int i=0; i<5; i++)\n" + |
2009 |
" print(s);\n" + |
2010 |
" }\n" + |
2011 |
" void print6(boolean b) {\n" + |
2012 |
" String s = b ? null : \"\";\n" + |
2013 |
" for (int i=0; i<5; i++)\n" + |
2014 |
" print(s);\n" + |
2015 |
" }\n" + |
2016 |
" void print(@NonNull String s) {\n" + |
2017 |
" System.out.print(s);\n" + |
2018 |
" }\n" + |
2019 |
"}\n", |
2020 |
|
2021 |
}, |
2022 |
customOptions, |
2023 |
"----------\n" + |
2024 |
"1. WARNING in X.java (at line 3)\n" + |
2025 |
" void print4(@NonNull String s) {\n" + |
2026 |
" ^^^^^^^^^^^^^^^^^\n" + |
2027 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
2028 |
"----------\n" + |
2029 |
"2. ERROR in X.java (at line 9)\n" + |
2030 |
" print(s);\n" + |
2031 |
" ^\n" + |
2032 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2033 |
"----------\n" + |
2034 |
"3. ERROR in X.java (at line 14)\n" + |
2035 |
" print(s);\n" + |
2036 |
" ^\n" + |
2037 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2038 |
"----------\n" + |
2039 |
"4. WARNING in X.java (at line 16)\n" + |
2040 |
" void print(@NonNull String s) {\n" + |
2041 |
" ^^^^^^^^^^^^^^^^^\n" + |
2042 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
2043 |
"----------\n"); |
2044 |
} |
2045 |
// a nonnull variable is dereferenced in a finally block |
2046 |
public void test_nonnull_var_in_constrol_structure_2() { |
2047 |
Map customOptions = getCompilerOptions(); |
2048 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2049 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2050 |
runNegativeTestWithLibs( |
2051 |
new String[] { |
2052 |
"X.java", |
2053 |
"import org.eclipse.jdt.annotation.*;\n" + |
2054 |
"public class X {\n" + |
2055 |
" void print4(String s) {\n" + |
2056 |
" try { /*empty*/ } finally {\n" + |
2057 |
" print(s);\n" + |
2058 |
" }\n" + |
2059 |
" }\n" + |
2060 |
" void print5(@Nullable String s) {\n" + |
2061 |
" try { /*empty*/ } finally {\n" + |
2062 |
" print(s);\n" + |
2063 |
" }\n" + |
2064 |
" }\n" + |
2065 |
" void print6(boolean b) {\n" + |
2066 |
" String s = b ? null : \"\";\n" + |
2067 |
" try { /*empty*/ } finally {\n" + |
2068 |
" print(s);\n" + |
2069 |
" }\n" + |
2070 |
" }\n" + |
2071 |
" void print(String s) {\n" + |
2072 |
" System.out.print(s);\n" + |
2073 |
" }\n" + |
2074 |
"}\n", |
2075 |
|
2076 |
}, |
2077 |
customOptions, |
2078 |
"----------\n" + |
2079 |
"1. ERROR in X.java (at line 10)\n" + |
2080 |
" print(s);\n" + |
2081 |
" ^\n" + |
2082 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2083 |
"----------\n" + |
2084 |
"2. ERROR in X.java (at line 16)\n" + |
2085 |
" print(s);\n" + |
2086 |
" ^\n" + |
2087 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2088 |
"----------\n"); |
2089 |
} |
2090 |
// a nonnull variable is dereferenced in a finally block inside a loop |
2091 |
public void test_nonnull_var_in_constrol_structure_3() { |
2092 |
Map customOptions = getCompilerOptions(); |
2093 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2094 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2095 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.IGNORE); |
2096 |
runNegativeTestWithLibs( |
2097 |
new String[] { |
2098 |
"X.java", |
2099 |
"import org.eclipse.jdt.annotation.*;\n" + |
2100 |
"public class X {\n" + |
2101 |
" void print4(@NonNull String s) {\n" + |
2102 |
" for (int i=0; i<4; i++)\n" + |
2103 |
" try { /*empty*/ } finally {\n" + |
2104 |
" print(s);\n" + |
2105 |
" }\n" + |
2106 |
" }\n" + |
2107 |
" void print5(@Nullable String s) {\n" + |
2108 |
" for (int i=0; i<5; i++)\n" + |
2109 |
" try { /*empty*/ } finally {\n" + |
2110 |
" print(s);\n" + |
2111 |
" }\n" + |
2112 |
" }\n" + |
2113 |
" void print6(boolean b) {\n" + |
2114 |
" String s = b ? null : \"\";\n" + |
2115 |
" for (int i=0; i<4; i++)\n" + |
2116 |
" try { /*empty*/ } finally {\n" + |
2117 |
" print(s);\n" + |
2118 |
" }\n" + |
2119 |
" }\n" + |
2120 |
" void print(@NonNull String s) {\n" + |
2121 |
" System.out.print(s);\n" + |
2122 |
" }\n" + |
2123 |
"}\n", |
2124 |
|
2125 |
}, |
2126 |
customOptions, |
2127 |
"----------\n" + |
2128 |
"1. ERROR in X.java (at line 12)\n" + |
2129 |
" print(s);\n" + |
2130 |
" ^\n" + |
2131 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2132 |
"----------\n" + |
2133 |
"2. ERROR in X.java (at line 19)\n" + |
2134 |
" print(s);\n" + |
2135 |
" ^\n" + |
2136 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2137 |
"----------\n"); |
2138 |
} |
2139 |
// a nonnull variable is dereferenced method of a nested type |
2140 |
public void test_nesting_1() { |
2141 |
Map customOptions = getCompilerOptions(); |
2142 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2143 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2144 |
runNegativeTestWithLibs( |
2145 |
new String[] { |
2146 |
"X.java", |
2147 |
"import org.eclipse.jdt.annotation.*;\n" + |
2148 |
"@NonNullByDefault\n" + |
2149 |
"public class X {\n" + |
2150 |
" void print4(final String s1) {\n" + |
2151 |
" for (int i=0; i<3; i++)\n" + |
2152 |
" new Runnable() {\n" + |
2153 |
" public void run() {\n" + |
2154 |
" print(s1);\n" + |
2155 |
" }\n" + |
2156 |
" }.run();\n" + |
2157 |
" }\n" + |
2158 |
" void print8(final @Nullable String s2) {\n" + |
2159 |
" for (int i=0; i<3; i++)\n" + |
2160 |
" new Runnable() {\n" + |
2161 |
" public void run() {\n" + |
2162 |
" print(s2);\n" + |
2163 |
" }\n" + |
2164 |
" }.run();\n" + |
2165 |
" }\n" + |
2166 |
" void print16(boolean b) {\n" + |
2167 |
" final String s3 = b ? null : \"\";\n" + |
2168 |
" for (int i=0; i<3; i++)\n" + |
2169 |
" new Runnable() {\n" + |
2170 |
" public void run() {\n" + |
2171 |
" @NonNull String s3R = s3;\n" + |
2172 |
" }\n" + |
2173 |
" }.run();\n" + |
2174 |
" }\n" + |
2175 |
" void print(String s) {\n" + |
2176 |
" System.out.print(s);\n" + |
2177 |
" }\n" + |
2178 |
"}\n", |
2179 |
|
2180 |
}, |
2181 |
customOptions, |
2182 |
"----------\n" + |
2183 |
"1. ERROR in X.java (at line 16)\n" + |
2184 |
" print(s2);\n" + |
2185 |
" ^^\n" + |
2186 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2187 |
"----------\n" + |
2188 |
"2. ERROR in X.java (at line 25)\n" + |
2189 |
" @NonNull String s3R = s3;\n" + |
2190 |
" ^^\n" + |
2191 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2192 |
"----------\n"); |
2193 |
} |
2194 |
// Test a regression incurred to the OT/J based implementation |
2195 |
// by the fix in Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop) |
2196 |
public void test_constructor_with_nested_class() { |
2197 |
runConformTest( |
2198 |
new String[] { |
2199 |
"X.java", |
2200 |
"public class X {\n" + |
2201 |
" final Object o1;\n" + |
2202 |
" final Object o2;\n" + |
2203 |
" public X() {\n" + |
2204 |
" this.o1 = new Object() {\n" + |
2205 |
" public String toString() { return \"O1\"; }\n" + |
2206 |
" };\n" + |
2207 |
" this.o2 = new Object();" + |
2208 |
" }\n" + |
2209 |
"}\n" |
2210 |
}, |
2211 |
""); |
2212 |
} |
2213 |
} |