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 |
// null is passed to a non-null parameter in a qualified allocation expression, across CUs |
414 |
public void test_nonnull_parameter_010() { |
415 |
Map customOptions = getCompilerOptions(); |
416 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
417 |
runNegativeTestWithLibs( |
418 |
new String[] { |
419 |
"ContainingInner2.java", |
420 |
"public class ContainingInner2 {\n" + |
421 |
" public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + |
422 |
" }\n" + |
423 |
" public class Inner {\n" + |
424 |
" public Inner (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + |
425 |
" }\n" + |
426 |
" }\n" + |
427 |
"}\n", |
428 |
"X.java", |
429 |
"public class X {\n" + |
430 |
" void create() {\n" + |
431 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
432 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
433 |
" }\n" + |
434 |
"}\n"}, |
435 |
customOptions, |
436 |
"----------\n" + |
437 |
"1. ERROR in X.java (at line 3)\n" + |
438 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
439 |
" ^^^^\n" + |
440 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
441 |
"----------\n" + |
442 |
"2. ERROR in X.java (at line 4)\n" + |
443 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
444 |
" ^^^^\n" + |
445 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
446 |
"----------\n" /* compiler output */); |
447 |
} |
448 |
// null is passed to a non-null parameter in a qualified allocation expression, target class read from .class |
449 |
public void test_nonnull_parameter_011() { |
450 |
Map customOptions = getCompilerOptions(); |
451 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
452 |
runConformTestWithLibs( |
453 |
new String[] { |
454 |
"ContainingInner2.java", |
455 |
"public class ContainingInner2 {\n" + |
456 |
" public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + |
457 |
" }\n" + |
458 |
" public class Inner {\n" + |
459 |
" public Inner (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + |
460 |
" }\n" + |
461 |
" }\n" + |
462 |
"}\n", |
463 |
}, |
464 |
null /*customOptions*/, |
465 |
""); |
466 |
runNegativeTestWithLibs( |
467 |
false, // flush directory |
468 |
new String[] { |
469 |
"X.java", |
470 |
"public class X {\n" + |
471 |
" void create() {\n" + |
472 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
473 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
474 |
" }\n" + |
475 |
"}\n"}, |
476 |
customOptions, |
477 |
"----------\n" + |
478 |
"1. ERROR in X.java (at line 3)\n" + |
479 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
480 |
" ^^^^\n" + |
481 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
482 |
"----------\n" + |
483 |
"2. ERROR in X.java (at line 4)\n" + |
484 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
485 |
" ^^^^\n" + |
486 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
487 |
"----------\n" /* compiler output */); |
488 |
} |
489 |
// null is passed to a non-null parameter in a qualified allocation expression, generic constructor, target class read from .class |
490 |
public void test_nonnull_parameter_012() { |
491 |
Map customOptions = getCompilerOptions(); |
492 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
493 |
runConformTestWithLibs( |
494 |
new String[] { |
495 |
"ContainingInner2.java", |
496 |
"public class ContainingInner2 {\n" + |
497 |
" public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + |
498 |
" }\n" + |
499 |
" public class Inner {\n" + |
500 |
" public <T> Inner (@org.eclipse.jdt.annotation.NonNull T o) {\n" + |
501 |
" }\n" + |
502 |
" }\n" + |
503 |
"}\n", |
504 |
}, |
505 |
null /*customOptions*/, |
506 |
""); |
507 |
runNegativeTestWithLibs( |
508 |
false, // flush directory |
509 |
new String[] { |
510 |
"X.java", |
511 |
"public class X {\n" + |
512 |
" void create() {\n" + |
513 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
514 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
515 |
" }\n" + |
516 |
"}\n"}, |
517 |
customOptions, |
518 |
"----------\n" + |
519 |
"1. ERROR in X.java (at line 3)\n" + |
520 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
521 |
" ^^^^\n" + |
522 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
523 |
"----------\n" + |
524 |
"2. ERROR in X.java (at line 4)\n" + |
525 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
526 |
" ^^^^\n" + |
527 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
528 |
"----------\n" /* compiler output */); |
529 |
} |
530 |
// assigning potential null to a nonnull local variable |
531 |
public void test_nonnull_local_001() { |
532 |
runNegativeTest( |
533 |
new String[] { |
534 |
"X.java", |
535 |
"import org.eclipse.jdt.annotation.*;\n" + |
536 |
"public class X {\n" + |
537 |
" void foo(boolean b, Object p) {\n" + |
538 |
" @NonNull Object o1 = b ? null : new Object();\n" + |
539 |
" @NonNull String o2 = \"\";\n" + |
540 |
" o2 = null;\n" + |
541 |
" @NonNull Object o3 = p;\n" + |
542 |
" }\n" + |
543 |
"}\n"}, |
544 |
"----------\n" + |
545 |
"1. ERROR in X.java (at line 4)\n" + |
546 |
" @NonNull Object o1 = b ? null : new Object();\n" + |
547 |
" ^^^^^^^^^^^^^^^^^^^^^^^\n" + |
548 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
549 |
"----------\n" + |
550 |
"2. ERROR in X.java (at line 6)\n" + |
551 |
" o2 = null;\n" + |
552 |
" ^^^^\n" + |
553 |
"Type mismatch: required \'@NonNull String\' but the provided value is null\n" + |
554 |
"----------\n" + |
555 |
"3. WARNING in X.java (at line 7)\n" + |
556 |
" @NonNull Object o3 = p;\n" + |
557 |
" ^\n" + |
558 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
559 |
"----------\n", |
560 |
this.LIBS, |
561 |
true /* shouldFlush*/); |
562 |
} |
563 |
|
564 |
// a method tries to tighten the type specification, super declares parameter o as @Nullable |
565 |
// other parameters: s is redefined from not constrained to @Nullable which is OK |
566 |
// third is redefined from not constrained to @NonNull which is bad, too |
567 |
public void test_parameter_specification_inheritance_001() { |
568 |
runConformTestWithLibs( |
569 |
new String[] { |
570 |
"Lib.java", |
571 |
"import org.eclipse.jdt.annotation.*;\n" + |
572 |
"public class Lib {\n" + |
573 |
" void foo(String s, @Nullable Object o, Object third) { }\n" + |
574 |
"}\n" |
575 |
}, |
576 |
null /*customOptions*/, |
577 |
""); |
578 |
runNegativeTestWithLibs( |
579 |
false, // don't 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 |
" void foo(@Nullable String s, @NonNull Object o, @NonNull Object third) { System.out.print(o.toString()); }\n" + |
586 |
"}\n" |
587 |
}, |
588 |
null /*customOptions*/, |
589 |
"----------\n" + |
590 |
"1. ERROR in X.java (at line 4)\n" + |
591 |
" void foo(@Nullable String s, @NonNull Object o, @NonNull Object third) { System.out.print(o.toString()); }\n" + |
592 |
" ^^^^^^^^^^^^^^^\n" + |
593 |
"Illegal redefinition of parameter o, inherited method from Lib declares this parameter as @Nullable\n" + |
594 |
"----------\n" + |
595 |
"2. ERROR in X.java (at line 4)\n" + |
596 |
" void foo(@Nullable String s, @NonNull Object o, @NonNull Object third) { System.out.print(o.toString()); }\n" + |
597 |
" ^^^^^^^^^^^^^^^\n" + |
598 |
"Illegal redefinition of parameter third, inherited method from Lib does not constrain this parameter\n" + |
599 |
"----------\n"); |
600 |
} |
601 |
// a method body fails to redeclare the inherited null annotation, super declares parameter as @Nullable |
602 |
public void test_parameter_specification_inheritance_002() { |
603 |
runConformTest( |
604 |
new String[] { |
605 |
"Lib.java", |
606 |
"import org.eclipse.jdt.annotation.*;\n" + |
607 |
"public class Lib {\n" + |
608 |
" void foo(@Nullable Object o) { }\n" + |
609 |
"}\n" |
610 |
}, |
611 |
"", |
612 |
this.LIBS, |
613 |
false/*shouldFlush*/, |
614 |
null/*vmArgs*/); |
615 |
runNegativeTestWithLibs( |
616 |
false, // don't flush |
617 |
new String[] { |
618 |
"X.java", |
619 |
"public class X extends Lib {\n" + |
620 |
" @Override\n" + |
621 |
" void foo(Object o) {\n" + |
622 |
" System.out.print(o.toString());\n" + |
623 |
" }\n" + |
624 |
"}\n" |
625 |
}, |
626 |
null /*customOptions*/, |
627 |
"----------\n" + |
628 |
"1. ERROR in X.java (at line 3)\n" + |
629 |
" void foo(Object o) {\n" + |
630 |
" ^^^^^^\n" + |
631 |
"Missing nullable annotation: inherited method from Lib declares this parameter as @Nullable\n" + |
632 |
"----------\n"); |
633 |
} |
634 |
// a method relaxes the parameter null specification, super interface declares parameter o as @NonNull |
635 |
// other (first) parameter just repeats the inherited @NonNull |
636 |
public void test_parameter_specification_inheritance_003() { |
637 |
runConformTest( |
638 |
new String[] { |
639 |
"IX.java", |
640 |
"import org.eclipse.jdt.annotation.*;\n" + |
641 |
"public interface IX {\n" + |
642 |
" void foo(@NonNull String s, @NonNull Object o);\n" + |
643 |
"}\n", |
644 |
"X.java", |
645 |
"import org.eclipse.jdt.annotation.*;\n" + |
646 |
"public class X implements IX {\n" + |
647 |
" public void foo(@NonNull String s, @Nullable Object o) { ; }\n" + |
648 |
" void bar() { foo(\"OK\", null); }\n" + |
649 |
"}\n" |
650 |
}, |
651 |
"", |
652 |
this.LIBS, |
653 |
false/*shouldFlush*/, |
654 |
null/*vmArgs*/); |
655 |
} |
656 |
// a method adds a @NonNull annotation, super interface has no null annotation |
657 |
// changing other from unconstrained to @Nullable is OK |
658 |
public void test_parameter_specification_inheritance_004() { |
659 |
runConformTest( |
660 |
new String[] { |
661 |
"IX.java", |
662 |
"public interface IX {\n" + |
663 |
" void foo(Object o, Object other);\n" + |
664 |
"}\n" |
665 |
}); |
666 |
runNegativeTestWithLibs( |
667 |
false, // don't flush |
668 |
new String[] { |
669 |
"X.java", |
670 |
"import org.eclipse.jdt.annotation.*;\n" + |
671 |
"public class X implements IX {\n" + |
672 |
" public void foo(@NonNull Object o, @Nullable Object other) { System.out.print(o.toString()); }\n" + |
673 |
"}\n" |
674 |
}, |
675 |
null /*customOptions*/, |
676 |
"----------\n" + |
677 |
"1. ERROR in X.java (at line 3)\n" + |
678 |
" public void foo(@NonNull Object o, @Nullable Object other) { System.out.print(o.toString()); }\n" + |
679 |
" ^^^^^^^^^^^^^^^\n" + |
680 |
"Illegal redefinition of parameter o, inherited method from IX does not constrain this parameter\n" + |
681 |
"----------\n"); |
682 |
} |
683 |
// a method tries to relax the null contract, super declares @NonNull return |
684 |
public void test_parameter_specification_inheritance_005() { |
685 |
runConformTestWithLibs( |
686 |
new String[] { |
687 |
"Lib.java", |
688 |
"import org.eclipse.jdt.annotation.*;\n" + |
689 |
"public class Lib {\n" + |
690 |
" @NonNull Object getObject() { return new Object(); }\n" + |
691 |
"}\n" |
692 |
}, |
693 |
null /*customOptions*/, |
694 |
""); |
695 |
runNegativeTestWithLibs( |
696 |
false, //dont' flush |
697 |
new String[] { |
698 |
"X.java", |
699 |
"import org.eclipse.jdt.annotation.*;\n" + |
700 |
"public class X extends Lib {\n" + |
701 |
" @Override\n" + |
702 |
" @Nullable Object getObject() { return null; }\n" + |
703 |
"}\n" |
704 |
}, |
705 |
null /*customOptions*/, |
706 |
"----------\n" + |
707 |
"1. ERROR in X.java (at line 4)\n" + |
708 |
" @Nullable Object getObject() { return null; }\n" + |
709 |
" ^^^^^^^^^^^^^^^^\n" + |
710 |
"The return type is incompatible with the @NonNull return from Lib.getObject()\n" + |
711 |
"----------\n"); |
712 |
} |
713 |
|
714 |
// super has no constraint for return, sub method confirms the null contract as @Nullable |
715 |
public void test_parameter_specification_inheritance_006() { |
716 |
runConformTest( |
717 |
new String[] { |
718 |
"Lib.java", |
719 |
"public class Lib {\n" + |
720 |
" Object getObject() { return null; }\n" + |
721 |
"}\n" |
722 |
}); |
723 |
runConformTestWithLibs( |
724 |
false, // don't flush |
725 |
new String[] { |
726 |
"X.java", |
727 |
"import org.eclipse.jdt.annotation.*;\n" + |
728 |
"public class X extends Lib {\n" + |
729 |
" @Override\n" + |
730 |
" @Nullable Object getObject() { return null; }\n" + |
731 |
"}\n" |
732 |
}, |
733 |
null /*customOptions*/, |
734 |
""); |
735 |
} |
736 |
// a method body violates the inherited null specification, super declares @NonNull return, missing redeclaration |
737 |
public void test_parameter_specification_inheritance_007() { |
738 |
runConformTestWithLibs( |
739 |
new String[] { |
740 |
"Lib.java", |
741 |
"import org.eclipse.jdt.annotation.*;\n" + |
742 |
"public class Lib {\n" + |
743 |
" @NonNull Object getObject() { return new Object(); }\n" + |
744 |
"}\n" |
745 |
}, |
746 |
null /*customOptions*/, |
747 |
""); |
748 |
runNegativeTestWithLibs( |
749 |
false, // don't flush |
750 |
new String[] { |
751 |
"X.java", |
752 |
"public class X extends Lib {\n" + |
753 |
" @Override\n" + |
754 |
" Object getObject() { return null; }\n" + |
755 |
"}\n" |
756 |
}, |
757 |
null /*customOptions*/, |
758 |
"----------\n" + |
759 |
"1. ERROR in X.java (at line 3)\n" + |
760 |
" Object getObject() { return null; }\n" + |
761 |
" ^^^^^^\n" + |
762 |
"The return type is incompatible with the @NonNull return from Lib.getObject()\n" + |
763 |
"----------\n"); |
764 |
} |
765 |
//a method body violates the @NonNull return specification (repeated from super) |
766 |
public void test_parameter_specification_inheritance_007a() { |
767 |
runConformTestWithLibs( |
768 |
new String[] { |
769 |
"Lib.java", |
770 |
"import org.eclipse.jdt.annotation.*;\n" + |
771 |
"public class Lib {\n" + |
772 |
" @NonNull Object getObject() { return new Object(); }\n" + |
773 |
"}\n" |
774 |
}, |
775 |
null /*customOptions*/, |
776 |
""); |
777 |
runNegativeTestWithLibs( |
778 |
false, // don't flush |
779 |
new String[] { |
780 |
"X.java", |
781 |
"import org.eclipse.jdt.annotation.*;\n" + |
782 |
"public class X extends Lib {\n" + |
783 |
" @Override\n" + |
784 |
" @NonNull Object getObject() { return null; }\n" + |
785 |
"}\n" |
786 |
}, |
787 |
null /*customOptions*/, |
788 |
"----------\n" + |
789 |
"1. ERROR in X.java (at line 4)\n" + |
790 |
" @NonNull Object getObject() { return null; }\n" + |
791 |
" ^^^^\n" + |
792 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
793 |
"----------\n"); |
794 |
} |
795 |
// a client potentially violates the inherited null specification, super interface declares @NonNull parameter |
796 |
public void test_parameter_specification_inheritance_008() { |
797 |
Map options = getCompilerOptions(); |
798 |
options.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
799 |
runConformTestWithLibs( |
800 |
new String[] { |
801 |
"IX.java", |
802 |
"import org.eclipse.jdt.annotation.*;\n" + |
803 |
"public interface IX {\n" + |
804 |
" void printObject(@NonNull Object o);\n" + |
805 |
"}\n" |
806 |
}, |
807 |
null /*customOptions*/, |
808 |
""); |
809 |
runNegativeTestWithLibs( |
810 |
false, // don't flush |
811 |
new String[] { |
812 |
"X.java", |
813 |
"public class X implements IX {\n" + |
814 |
" public void printObject(Object o) { System.out.print(o.toString()); }\n" + |
815 |
"}\n", |
816 |
"M.java", |
817 |
"public class M{\n" + |
818 |
" void foo(IX x, Object o) {\n" + |
819 |
" x.printObject(o);\n" + |
820 |
" }\n" + |
821 |
"}\n" |
822 |
}, |
823 |
options, |
824 |
"----------\n" + |
825 |
// additional error: |
826 |
"1. ERROR in X.java (at line 2)\n" + |
827 |
" public void printObject(Object o) { System.out.print(o.toString()); }\n" + |
828 |
" ^^^^^^\n" + |
829 |
"Missing non-null annotation: inherited method from IX declares this parameter as @NonNull\n" + |
830 |
"----------\n" + |
831 |
// main error: |
832 |
"----------\n" + |
833 |
"1. ERROR in M.java (at line 3)\n" + |
834 |
" x.printObject(o);\n" + |
835 |
" ^\n" + |
836 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
837 |
"----------\n"); |
838 |
} |
839 |
// a static method has a more relaxed null contract than a like method in the super class, but no overriding. |
840 |
public void test_parameter_specification_inheritance_009() { |
841 |
runConformTestWithLibs( |
842 |
new String[] { |
843 |
"Lib.java", |
844 |
"import org.eclipse.jdt.annotation.*;\n" + |
845 |
"public class Lib {\n" + |
846 |
" @NonNull static Object getObject() { return new Object(); }\n" + |
847 |
"}\n", |
848 |
"X.java", |
849 |
"import org.eclipse.jdt.annotation.*;\n" + |
850 |
"public class X extends Lib {\n" + |
851 |
" @Nullable static Object getObject() { return null; }\n" + |
852 |
"}\n" |
853 |
}, |
854 |
null /*customOptions*/, |
855 |
""); |
856 |
} |
857 |
// class default is nonnull, method and its super both use the default |
858 |
public void test_parameter_specification_inheritance_010() { |
859 |
Map customOptions = getCompilerOptions(); |
860 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
861 |
runConformTestWithLibs( |
862 |
new String[] { |
863 |
"p1/X.java", |
864 |
"package p1;\n" + |
865 |
"import org.eclipse.jdt.annotation.*;\n" + |
866 |
"@NonNullByDefault\n" + |
867 |
"public class X {\n" + |
868 |
" protected String getString(String s) {\n" + |
869 |
" if (Character.isLowerCase(s.charAt(0)))\n" + |
870 |
" return getString(s);\n" + |
871 |
" return s;\n" + |
872 |
" }\n" + |
873 |
"}\n", |
874 |
"p1/Y.java", |
875 |
"package p1;\n" + |
876 |
"import org.eclipse.jdt.annotation.*;\n" + |
877 |
"@NonNullByDefault\n" + |
878 |
"public class Y extends X {\n" + |
879 |
" @Override\n" + |
880 |
" protected String getString(String s) {\n" + |
881 |
" return super.getString(s);\n" + |
882 |
" }\n" + |
883 |
"}\n", |
884 |
}, |
885 |
customOptions, |
886 |
""); |
887 |
} |
888 |
// class default is nonnull, method and its super both use the default, super-call passes null |
889 |
public void test_parameter_specification_inheritance_011() { |
890 |
Map customOptions = getCompilerOptions(); |
891 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
892 |
runNegativeTestWithLibs( |
893 |
new String[] { |
894 |
"p1/X.java", |
895 |
"package p1;\n" + |
896 |
"import org.eclipse.jdt.annotation.*;\n" + |
897 |
"@NonNullByDefault\n" + |
898 |
"public class X {\n" + |
899 |
" protected String getString(String s) {\n" + |
900 |
" if (Character.isLowerCase(s.charAt(0)))\n" + |
901 |
" return getString(s);\n" + |
902 |
" return s;\n" + |
903 |
" }\n" + |
904 |
"}\n", |
905 |
"p1/Y.java", |
906 |
"package p1;\n" + |
907 |
"import org.eclipse.jdt.annotation.*;\n" + |
908 |
"@NonNullByDefault\n" + |
909 |
"public class Y extends X {\n" + |
910 |
" @Override\n" + |
911 |
" protected String getString(String s) {\n" + |
912 |
" return super.getString(null);\n" + |
913 |
" }\n" + |
914 |
"}\n", |
915 |
}, |
916 |
customOptions, |
917 |
"----------\n" + |
918 |
"1. ERROR in p1\\Y.java (at line 7)\n" + |
919 |
" return super.getString(null);\n" + |
920 |
" ^^^^\n" + |
921 |
"Type mismatch: required \'@NonNull String\' but the provided value is null\n" + |
922 |
"----------\n"); |
923 |
} |
924 |
// methods from two super types have different null contracts. |
925 |
// sub-class merges both using the weakest common contract |
926 |
public void test_parameter_specification_inheritance_012() { |
927 |
Map customOptions = getCompilerOptions(); |
928 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
929 |
runConformTestWithLibs( |
930 |
new String[] { |
931 |
"p1/X.java", |
932 |
"package p1;\n" + |
933 |
"import org.eclipse.jdt.annotation.*;\n" + |
934 |
"public class X {\n" + |
935 |
" public @Nullable String getString(String s1, @Nullable String s2, @NonNull String s3) {\n" + |
936 |
" return s1;\n" + |
937 |
" }\n" + |
938 |
"}\n", |
939 |
"p1/IY.java", |
940 |
"package p1;\n" + |
941 |
"import org.eclipse.jdt.annotation.*;\n" + |
942 |
"public interface IY {\n" + |
943 |
" @NonNull String getString(@NonNull String s1, @NonNull String s2, @Nullable String s3);\n" + |
944 |
"}\n", |
945 |
"p1/Y.java", |
946 |
"package p1;\n" + |
947 |
"import org.eclipse.jdt.annotation.*;\n" + |
948 |
"public class Y extends X implements IY {\n" + |
949 |
" @Override\n" + |
950 |
" public @NonNull String getString(@Nullable String s1, @Nullable String s2, @Nullable String s3) {\n" + |
951 |
" return \"\";\n" + |
952 |
" }\n" + |
953 |
"}\n", |
954 |
}, |
955 |
customOptions, |
956 |
""); |
957 |
} |
958 |
// methods from two super types have different null contracts. |
959 |
// sub-class overrides this method in non-conforming ways |
960 |
public void test_parameter_specification_inheritance_013() { |
961 |
Map customOptions = getCompilerOptions(); |
962 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
963 |
runNegativeTestWithLibs( |
964 |
new String[] { |
965 |
"p1/X.java", |
966 |
"package p1;\n" + |
967 |
"import org.eclipse.jdt.annotation.*;\n" + |
968 |
"public class X {\n" + |
969 |
" public @Nullable String getString(String s1, @Nullable String s2, @NonNull String s3) {\n" + |
970 |
" return s1;\n" + |
971 |
" }\n" + |
972 |
"}\n", |
973 |
"p1/IY.java", |
974 |
"package p1;\n" + |
975 |
"import org.eclipse.jdt.annotation.*;\n" + |
976 |
"public interface IY {\n" + |
977 |
" @NonNull String getString(@NonNull String s1, @NonNull String s2, @Nullable String s3);\n" + |
978 |
"}\n", |
979 |
"p1/Y.java", |
980 |
"package p1;\n" + |
981 |
"import org.eclipse.jdt.annotation.*;\n" + |
982 |
"public class Y extends X implements IY {\n" + |
983 |
" @Override\n" + |
984 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
985 |
" return \"\";\n" + |
986 |
" }\n" + |
987 |
"}\n", |
988 |
}, |
989 |
customOptions, |
990 |
"----------\n" + |
991 |
"1. ERROR in p1\\Y.java (at line 5)\n" + |
992 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
993 |
" ^^^^^^^^^^^^^^^^\n" + |
994 |
"The return type is incompatible with the @NonNull return from IY.getString(String, String, String)\n" + |
995 |
"----------\n" + |
996 |
"2. ERROR in p1\\Y.java (at line 5)\n" + |
997 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
998 |
" ^^^^^^\n" + |
999 |
"Missing non-null annotation: inherited method from IY declares this parameter as @NonNull\n" + |
1000 |
"----------\n" + |
1001 |
"3. ERROR in p1\\Y.java (at line 5)\n" + |
1002 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
1003 |
" ^^^^^^^^^^^^^^^\n" + |
1004 |
"Illegal redefinition of parameter s2, inherited method from X declares this parameter as @Nullable\n" + |
1005 |
"----------\n" + |
1006 |
"4. ERROR in p1\\Y.java (at line 5)\n" + |
1007 |
" public @Nullable String getString(String s1, @NonNull String s2, @NonNull String s3) {\n" + |
1008 |
" ^^^^^^^^^^^^^^^\n" + |
1009 |
"Illegal redefinition of parameter s3, inherited method from IY declares this parameter as @Nullable\n" + |
1010 |
"----------\n"); |
1011 |
} |
1012 |
// methods from two super types have different null contracts. |
1013 |
// sub-class does not override, but should to bridge the incompatibility |
1014 |
public void test_parameter_specification_inheritance_014() { |
1015 |
Map customOptions = getCompilerOptions(); |
1016 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1017 |
runNegativeTestWithLibs( |
1018 |
new String[] { |
1019 |
"p1/IY.java", |
1020 |
"package p1;\n" + |
1021 |
"import org.eclipse.jdt.annotation.*;\n" + |
1022 |
"public interface IY {\n" + |
1023 |
" public @NonNull String getString1(String s);\n" + |
1024 |
" public @NonNull String getString2(String s);\n" + |
1025 |
" public String getString3(@Nullable String s);\n" + |
1026 |
" public @NonNull String getString4(@Nullable String s);\n" + |
1027 |
" public @NonNull String getString5(@Nullable String s);\n" + |
1028 |
" public @Nullable String getString6(@NonNull String s);\n" + |
1029 |
"}\n", |
1030 |
"p1/X.java", |
1031 |
"package p1;\n" + |
1032 |
"import org.eclipse.jdt.annotation.*;\n" + |
1033 |
"public class X {\n" + |
1034 |
" public @Nullable String getString1(String s) {\n" + // incomp. return |
1035 |
" return s;\n" + |
1036 |
" }\n" + |
1037 |
" public String getString2(String s) {\n" + // incomp. return |
1038 |
" return s;\n" + |
1039 |
" }\n" + |
1040 |
" public String getString3(String s) {\n" + // incomp. arg |
1041 |
" return \"\";\n" + |
1042 |
" }\n" + |
1043 |
" public @NonNull String getString4(@Nullable String s) {\n" + |
1044 |
" return \"\";\n" + |
1045 |
" }\n" + |
1046 |
" public @NonNull String getString5(@NonNull String s) {\n" + // incomp. arg |
1047 |
" return s;\n" + |
1048 |
" }\n" + |
1049 |
" public @NonNull String getString6(@Nullable String s) {\n" + |
1050 |
" return \"\";\n" + |
1051 |
" }\n" + |
1052 |
"}\n", |
1053 |
"p1/Y.java", |
1054 |
"package p1;\n" + |
1055 |
"public class Y extends X implements IY {\n" + |
1056 |
"}\n", |
1057 |
}, |
1058 |
customOptions, |
1059 |
"----------\n" + |
1060 |
"1. ERROR in p1\\Y.java (at line 2)\n" + |
1061 |
" public class Y extends X implements IY {\n" + |
1062 |
" ^\n" + |
1063 |
"The method getString1(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
1064 |
"----------\n" + |
1065 |
"2. ERROR in p1\\Y.java (at line 2)\n" + |
1066 |
" public class Y extends X implements IY {\n" + |
1067 |
" ^\n" + |
1068 |
"The method getString2(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
1069 |
"----------\n" + |
1070 |
"3. ERROR in p1\\Y.java (at line 2)\n" + |
1071 |
" public class Y extends X implements IY {\n" + |
1072 |
" ^\n" + |
1073 |
"The method getString5(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
1074 |
"----------\n" + |
1075 |
"4. ERROR in p1\\Y.java (at line 2)\n" + |
1076 |
" public class Y extends X implements IY {\n" + |
1077 |
" ^\n" + |
1078 |
"The method getString3(String) from X cannot implement the corresponding method from IY due to incompatible nullness constraints\n" + |
1079 |
"----------\n"); |
1080 |
} |
1081 |
// a nullable return value is dereferenced without a check |
1082 |
public void test_nullable_return_001() { |
1083 |
runNegativeTestWithLibs( |
1084 |
new String[] { |
1085 |
"X.java", |
1086 |
"import org.eclipse.jdt.annotation.*;\n" + |
1087 |
"public class X {\n" + |
1088 |
" @Nullable Object getObject() { return null; }\n" + |
1089 |
" void foo() {\n" + |
1090 |
" Object o = getObject();\n" + |
1091 |
" System.out.print(o.toString());\n" + |
1092 |
" }\n" + |
1093 |
"}\n" |
1094 |
}, |
1095 |
"----------\n" + |
1096 |
"1. ERROR in X.java (at line 6)\n" + |
1097 |
" System.out.print(o.toString());\n" + |
1098 |
" ^\n" + |
1099 |
"Potential null pointer access: The variable o may be null at this location\n" + |
1100 |
"----------\n"); |
1101 |
} |
1102 |
// a nullable return value is dereferenced without a check, method is read from .class file |
1103 |
public void test_nullable_return_002() { |
1104 |
runConformTestWithLibs( |
1105 |
new String[] { |
1106 |
"Lib.java", |
1107 |
"import org.eclipse.jdt.annotation.*;\n" + |
1108 |
"public class Lib {\n" + |
1109 |
" @Nullable Object getObject() { return null; }\n" + |
1110 |
"}\n" |
1111 |
}, |
1112 |
null /*customOptions*/, |
1113 |
""); |
1114 |
runNegativeTestWithLibs( |
1115 |
false, // don't flush |
1116 |
new String[] { |
1117 |
"X.java", |
1118 |
"public class X {\n" + |
1119 |
" void foo(Lib l) {\n" + |
1120 |
" Object o = l.getObject();\n" + |
1121 |
" System.out.print(o.toString());\n" + |
1122 |
" }\n" + |
1123 |
"}\n" |
1124 |
}, |
1125 |
null /*customOptions*/, |
1126 |
"----------\n" + |
1127 |
"1. ERROR in X.java (at line 4)\n" + |
1128 |
" System.out.print(o.toString());\n" + |
1129 |
" ^\n" + |
1130 |
"Potential null pointer access: The variable o may be null at this location\n" + |
1131 |
"----------\n"); |
1132 |
} |
1133 |
// a non-null return value is checked for null, method is read from .class file |
1134 |
public void test_nonnull_return_001() { |
1135 |
runConformTestWithLibs( |
1136 |
new String[] { |
1137 |
"Lib.java", |
1138 |
"import org.eclipse.jdt.annotation.*;\n" + |
1139 |
"public class Lib {\n" + |
1140 |
" @NonNull Object getObject() { return new Object(); }\n" + |
1141 |
"}\n" |
1142 |
}, |
1143 |
null /*customOptions*/, |
1144 |
""); |
1145 |
runNegativeTestWithLibs( |
1146 |
false, // don't flush |
1147 |
new String[] { |
1148 |
"X.java", |
1149 |
"public class X {\n" + |
1150 |
" void foo(Lib l) {\n" + |
1151 |
" Object o = l.getObject();\n" + |
1152 |
" if (o != null)\n" + |
1153 |
" System.out.print(o.toString());\n" + |
1154 |
" }\n" + |
1155 |
"}\n" |
1156 |
}, |
1157 |
null /*customOptions*/, |
1158 |
"----------\n" + |
1159 |
"1. ERROR in X.java (at line 4)\n" + |
1160 |
" if (o != null)\n" + |
1161 |
" ^\n" + |
1162 |
"Redundant null check: The variable o cannot be null at this location\n" + |
1163 |
"----------\n"); |
1164 |
} |
1165 |
// a non-null method returns null |
1166 |
public void test_nonnull_return_003() { |
1167 |
runNegativeTestWithLibs( |
1168 |
new String[] { |
1169 |
"X.java", |
1170 |
"import org.eclipse.jdt.annotation.*;\n" + |
1171 |
"public class X {\n" + |
1172 |
" @NonNull Object getObject(boolean b) {\n" + |
1173 |
" if (b)\n" + |
1174 |
" return null;\n" + // definite specification violation despite enclosing "if" |
1175 |
" return new Object();\n" + |
1176 |
" }\n" + |
1177 |
"}\n" |
1178 |
}, |
1179 |
"----------\n" + |
1180 |
"1. ERROR in X.java (at line 5)\n" + |
1181 |
" return null;\n" + |
1182 |
" ^^^^\n" + |
1183 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1184 |
"----------\n"); |
1185 |
} |
1186 |
// a non-null method potentially returns null |
1187 |
public void test_nonnull_return_004() { |
1188 |
runNegativeTestWithLibs( |
1189 |
new String[] { |
1190 |
"X.java", |
1191 |
"import org.eclipse.jdt.annotation.*;\n" + |
1192 |
"public class X {\n" + |
1193 |
" @NonNull Object getObject(@Nullable Object o) {\n" + |
1194 |
" return o;\n" + // 'o' is only potentially null |
1195 |
" }\n" + |
1196 |
"}\n" |
1197 |
}, |
1198 |
null /*customOptions*/, |
1199 |
"----------\n" + |
1200 |
"1. ERROR in X.java (at line 4)\n" + |
1201 |
" return o;\n" + |
1202 |
" ^\n" + |
1203 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1204 |
"----------\n"); |
1205 |
} |
1206 |
// a non-null method returns its non-null argument |
1207 |
public void test_nonnull_return_005() { |
1208 |
runConformTestWithLibs( |
1209 |
new String[] { |
1210 |
"X.java", |
1211 |
"import org.eclipse.jdt.annotation.*;\n" + |
1212 |
"public class X {\n" + |
1213 |
" @NonNull Object getObject(@NonNull Object o) {\n" + |
1214 |
" return o;\n" + |
1215 |
" }\n" + |
1216 |
"}\n" |
1217 |
}, |
1218 |
null, // options |
1219 |
""); |
1220 |
} |
1221 |
//a non-null method has insufficient nullness info for its return value |
1222 |
public void test_nonnull_return_006() { |
1223 |
runNegativeTestWithLibs( |
1224 |
new String[] { |
1225 |
"X.java", |
1226 |
"import org.eclipse.jdt.annotation.*;\n" + |
1227 |
"public class X {\n" + |
1228 |
" @NonNull Object getObject(Object o) {\n" + |
1229 |
" return o;\n" + |
1230 |
" }\n" + |
1231 |
"}\n" |
1232 |
}, |
1233 |
"----------\n" + |
1234 |
"1. WARNING in X.java (at line 4)\n" + |
1235 |
" return o;\n" + |
1236 |
" ^\n" + |
1237 |
"Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" + |
1238 |
"----------\n"); |
1239 |
} |
1240 |
// a result from a nullable method is directly dereferenced |
1241 |
public void test_nonnull_return_007() { |
1242 |
runNegativeTestWithLibs( |
1243 |
new String[] { |
1244 |
"X.java", |
1245 |
"import org.eclipse.jdt.annotation.*;\n" + |
1246 |
"public class X {\n" + |
1247 |
" @Nullable Object getObject() {\n" + |
1248 |
" return null;\n" + |
1249 |
" }\n" + |
1250 |
" void test() {\n" + |
1251 |
" getObject().toString();\n" + |
1252 |
" }\n" + |
1253 |
"}\n" |
1254 |
}, |
1255 |
"----------\n" + |
1256 |
"1. ERROR in X.java (at line 7)\n" + |
1257 |
" getObject().toString();\n" + |
1258 |
" ^^^^^^^^^^^\n" + |
1259 |
"Potential null pointer access: The method getObject() may return null\n" + |
1260 |
"----------\n"); |
1261 |
} |
1262 |
// a result from a nonnull method is directly checked for null: redundant |
1263 |
public void test_nonnull_return_008() { |
1264 |
Map customOptions = getCompilerOptions(); |
1265 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1266 |
runNegativeTestWithLibs( |
1267 |
new String[] { |
1268 |
"X.java", |
1269 |
"import org.eclipse.jdt.annotation.*;\n" + |
1270 |
"public class X {\n" + |
1271 |
" @NonNull Object getObject() {\n" + |
1272 |
" return new Object();\n" + |
1273 |
" }\n" + |
1274 |
" void test() {\n" + |
1275 |
" if (getObject() == null)\n" + |
1276 |
" throw new RuntimeException();\n" + |
1277 |
" }\n" + |
1278 |
"}\n" |
1279 |
}, |
1280 |
customOptions, |
1281 |
"----------\n" + |
1282 |
"1. ERROR in X.java (at line 7)\n" + |
1283 |
" if (getObject() == null)\n" + |
1284 |
" ^^^^^^^^^^^\n" + |
1285 |
"Redundant null check: The method getObject() cannot return null\n" + |
1286 |
"----------\n"); |
1287 |
} |
1288 |
// a result from a nonnull method is directly checked for null (from local): redundant |
1289 |
public void test_nonnull_return_009() { |
1290 |
Map customOptions = getCompilerOptions(); |
1291 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1292 |
runNegativeTestWithLibs( |
1293 |
new String[] { |
1294 |
"X.java", |
1295 |
"import org.eclipse.jdt.annotation.*;\n" + |
1296 |
"public class X {\n" + |
1297 |
" @NonNull Object getObject() {\n" + |
1298 |
" return new Object();\n" + |
1299 |
" }\n" + |
1300 |
" void test() {\n" + |
1301 |
" Object left = null;\n" + |
1302 |
" if (left != getObject())\n" + |
1303 |
" throw new RuntimeException();\n" + |
1304 |
" }\n" + |
1305 |
"}\n" |
1306 |
}, |
1307 |
customOptions, |
1308 |
"----------\n" + |
1309 |
"1. ERROR in X.java (at line 8)\n" + |
1310 |
" if (left != getObject())\n" + |
1311 |
" ^^^^\n" + |
1312 |
"Redundant null check: The variable left can only be null at this location\n" + |
1313 |
"----------\n" + |
1314 |
"2. ERROR in X.java (at line 8)\n" + |
1315 |
" if (left != getObject())\n" + |
1316 |
" ^^^^^^^^^^^\n" + |
1317 |
"Redundant null check: The method getObject() cannot return null\n" + |
1318 |
"----------\n"); |
1319 |
} |
1320 |
// a result from a nonnull method is directly checked for null (from local): not redundant due to loop |
1321 |
public void test_nonnull_return_009a() { |
1322 |
Map customOptions = getCompilerOptions(); |
1323 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1324 |
runConformTestWithLibs( |
1325 |
new String[] { |
1326 |
"X.java", |
1327 |
"import org.eclipse.jdt.annotation.*;\n" + |
1328 |
"public class X {\n" + |
1329 |
" @NonNull Object getObject() {\n" + |
1330 |
" return new Object();\n" + |
1331 |
" }\n" + |
1332 |
" void test() {\n" + |
1333 |
" Object left = null;\n" + |
1334 |
" for (int i=0; i<3; i++) {\n" + |
1335 |
" if (left != getObject())\n" + |
1336 |
" throw new RuntimeException();\n" + |
1337 |
" left = new Object();\n" + |
1338 |
" }\n" + |
1339 |
" }\n" + |
1340 |
"}\n" |
1341 |
}, |
1342 |
customOptions, |
1343 |
""); |
1344 |
} |
1345 |
// a result from a nonnull method is directly checked for null (from local): redundant despite loop |
1346 |
// disabled because only one of two desirable errors is raised |
1347 |
// need to integrate @NonNull expressions (MessageSend and more) into deferred analysis by FlowContext |
1348 |
public void _test_nonnull_return_009b() { |
1349 |
Map customOptions = getCompilerOptions(); |
1350 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1351 |
runConformTestWithLibs( |
1352 |
new String[] { |
1353 |
"X.java", |
1354 |
"import org.eclipse.jdt.annotation.*;\n" + |
1355 |
"public class X {\n" + |
1356 |
" @NonNull Object getObject() {\n" + |
1357 |
" return new Object();\n" + |
1358 |
" }\n" + |
1359 |
" void test() {\n" + |
1360 |
" Object left = null;\n" + |
1361 |
" for (int i=0; i<3; i++) {\n" + |
1362 |
" if (left != getObject())\n" + |
1363 |
" throw new RuntimeException();\n" + |
1364 |
" // left remains null\n" + |
1365 |
" }\n" + |
1366 |
" }\n" + |
1367 |
"}\n" |
1368 |
}, |
1369 |
customOptions, |
1370 |
"----------\n" + |
1371 |
"1. ERROR in X.java (at line 9)\n" + |
1372 |
" if (left != getObject())\n" + |
1373 |
" ^^^^\n" + |
1374 |
"Redundant null check: The variable left can only be null at this location\n" + |
1375 |
"----------\n" + |
1376 |
"2. ERROR in X.java (at line 9)\n" + |
1377 |
" if (left != getObject())\n" + |
1378 |
" ^^^^^^^^^^^\n" + |
1379 |
"Redundant null check: The method getObject() cannot return null\n" + |
1380 |
"----------\n"); |
1381 |
} |
1382 |
// a result from a nullable method is assigned and checked for null (from local): not redundant |
1383 |
// see also Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop |
1384 |
public void test_nonnull_return_010() { |
1385 |
Map customOptions = getCompilerOptions(); |
1386 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, JavaCore.ERROR); |
1387 |
runNegativeTestWithLibs( |
1388 |
new String[] { |
1389 |
"X.java", |
1390 |
"import org.eclipse.jdt.annotation.*;\n" + |
1391 |
"public class X {\n" + |
1392 |
" @Nullable X getX() {\n" + |
1393 |
" return new X();\n" + |
1394 |
" }\n" + |
1395 |
" void test() {\n" + |
1396 |
" X left = this;\n" + |
1397 |
" do {\n" + |
1398 |
" if (left == null) \n" + |
1399 |
" throw new RuntimeException();\n" + |
1400 |
" } while ((left = left.getX()) != null);\n" + // no warning/error here! |
1401 |
" }\n" + |
1402 |
"}\n" |
1403 |
}, |
1404 |
customOptions, |
1405 |
"----------\n" + |
1406 |
"1. ERROR in X.java (at line 9)\n" + |
1407 |
" if (left == null) \n" + |
1408 |
" ^^^^\n" + |
1409 |
"Null comparison always yields false: The variable left cannot be null at this location\n" + |
1410 |
"----------\n"); |
1411 |
} |
1412 |
// a non-null method returns a checked-for null value, but that branch is dead code |
1413 |
public void test_nonnull_return_011() { |
1414 |
Map customOptions = getCompilerOptions(); |
1415 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1416 |
runNegativeTestWithLibs( |
1417 |
new String[] { |
1418 |
"X.java", |
1419 |
"import org.eclipse.jdt.annotation.*;\n" + |
1420 |
"@NonNullByDefault\n" + |
1421 |
"public class X {\n" + |
1422 |
" Object getObject(Object dubious) {\n" + |
1423 |
" if (dubious == null)\n" + // redundant |
1424 |
" return dubious;\n" + // definitely null, but not reported inside dead code |
1425 |
" return new Object();\n" + |
1426 |
" }\n" + |
1427 |
"}\n" |
1428 |
}, |
1429 |
customOptions, |
1430 |
"----------\n" + |
1431 |
"1. ERROR in X.java (at line 5)\n" + |
1432 |
" if (dubious == null)\n" + |
1433 |
" ^^^^^^^\n" + |
1434 |
"Null comparison always yields false: The variable dubious cannot be null at this location\n" + |
1435 |
"----------\n" + |
1436 |
"2. WARNING in X.java (at line 6)\n" + |
1437 |
" return dubious;\n" + |
1438 |
" ^^^^^^^^^^^^^^^\n" + |
1439 |
"Dead code\n" + |
1440 |
"----------\n"); |
1441 |
} |
1442 |
// a non-null method returns a definite null from a conditional expression |
1443 |
// requires the fix for Bug 354554 - [null] conditional with redundant condition yields weak error message |
1444 |
// TODO(SH): ENABLE! |
1445 |
public void _test_nonnull_return_012() { |
1446 |
Map customOptions = getCompilerOptions(); |
1447 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1448 |
runNegativeTestWithLibs( |
1449 |
new String[] { |
1450 |
"X.java", |
1451 |
"import org.eclipse.jdt.annotation.*;\n" + |
1452 |
"@NonNullByDefault\n" + |
1453 |
"public class X {\n" + |
1454 |
" Object getObject(Object dubious) {\n" + |
1455 |
" return dubious == null ? dubious : null;\n" + |
1456 |
" }\n" + |
1457 |
"}\n" |
1458 |
}, |
1459 |
customOptions, |
1460 |
"----------\n" + |
1461 |
"1. ERROR in X.java (at line 5)\n" + |
1462 |
" return dubious == null ? dubious : null;\n" + |
1463 |
" ^^^^^^^\n" + |
1464 |
"Null comparison always yields false: The variable dubious cannot be null at this location\n" + |
1465 |
"----------\n" + |
1466 |
"2. ERROR in X.java (at line 5)\n" + |
1467 |
" return dubious == null ? dubious : null;\n" + |
1468 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
1469 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
1470 |
"----------\n"); |
1471 |
} |
1472 |
// don't apply any default annotations to return void |
1473 |
public void test_nonnull_return_013() { |
1474 |
Map customOptions = getCompilerOptions(); |
1475 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1476 |
runConformTestWithLibs( |
1477 |
new String[] { |
1478 |
"X.java", |
1479 |
"import org.eclipse.jdt.annotation.*;\n" + |
1480 |
"@NonNullByDefault\n" + |
1481 |
"public class X {\n" + |
1482 |
" void getObject() {}\n" + |
1483 |
"}\n", |
1484 |
"Y.java", |
1485 |
"public class Y extends X {\n" + |
1486 |
" @Override\n" + |
1487 |
" void getObject() {}\n" + // don't complain, void takes no (default) annotation |
1488 |
"}\n" |
1489 |
}, |
1490 |
customOptions, |
1491 |
""); |
1492 |
} |
1493 |
//suppress an error regarding null-spec violation |
1494 |
public void test_suppress_001() { |
1495 |
Map customOptions = getCompilerOptions(); |
1496 |
customOptions.put(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, JavaCore.ENABLED); |
1497 |
runConformTestWithLibs( |
1498 |
new String[] { |
1499 |
"X.java", |
1500 |
"import org.eclipse.jdt.annotation.*;\n" + |
1501 |
"public class X {\n" + |
1502 |
" @SuppressWarnings(\"null\")\n" + |
1503 |
" @NonNull Object getObject(@Nullable Object o) {\n" + |
1504 |
" return o;\n" + // 'o' is only potentially null |
1505 |
" }\n" + |
1506 |
"}\n" |
1507 |
}, |
1508 |
customOptions, |
1509 |
""); |
1510 |
} |
1511 |
// mixed use of fully qualified name / explicit import |
1512 |
public void test_annotation_import_001() { |
1513 |
Map customOptions = getCompilerOptions(); |
1514 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.Nullable"); |
1515 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1516 |
runConformTestWithLibs( |
1517 |
new String[] { |
1518 |
CUSTOM_NULLABLE_NAME, |
1519 |
CUSTOM_NULLABLE_CONTENT, |
1520 |
CUSTOM_NONNULL_NAME, |
1521 |
CUSTOM_NONNULL_CONTENT, |
1522 |
"Lib.java", |
1523 |
"public class Lib {\n" + |
1524 |
" @org.foo.NonNull Object getObject() { return new Object(); }\n" + // FQN |
1525 |
"}\n", |
1526 |
"X.java", |
1527 |
"import org.foo.NonNull;\n" + // explicit import |
1528 |
"public class X {\n" + |
1529 |
" @NonNull Object getObject(@NonNull Lib l) {\n" + |
1530 |
" return l.getObject();\n" + |
1531 |
" }\n" + |
1532 |
"}\n" |
1533 |
}, |
1534 |
customOptions, |
1535 |
""); |
1536 |
} |
1537 |
|
1538 |
// use of explicit imports throughout |
1539 |
public void test_annotation_import_002() { |
1540 |
Map customOptions = getCompilerOptions(); |
1541 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.Nullable"); |
1542 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1543 |
runConformTest( |
1544 |
new String[] { |
1545 |
CUSTOM_NULLABLE_NAME, |
1546 |
CUSTOM_NULLABLE_CONTENT, |
1547 |
CUSTOM_NONNULL_NAME, |
1548 |
CUSTOM_NONNULL_CONTENT, |
1549 |
"Lib.java", |
1550 |
"import org.foo.NonNull;\n" + |
1551 |
"public class Lib {\n" + |
1552 |
" @NonNull Object getObject() { return new Object(); }\n" + |
1553 |
"}\n", |
1554 |
"X.java", |
1555 |
"import org.foo.NonNull;\n" + |
1556 |
"public class X {\n" + |
1557 |
" @NonNull Object getObject(@org.foo.Nullable String dummy, @NonNull Lib l) {\n" + |
1558 |
" Object o = l.getObject();" + |
1559 |
" return o;\n" + |
1560 |
" }\n" + |
1561 |
"}\n" |
1562 |
}, |
1563 |
customOptions, |
1564 |
""); |
1565 |
} |
1566 |
// explicit import of existing annotation types |
1567 |
// using a Lib without null specifications |
1568 |
public void test_annotation_import_005() { |
1569 |
Map customOptions = getCompilerOptions(); |
1570 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1571 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull"); |
1572 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull"); |
1573 |
runNegativeTest( |
1574 |
true/*shouldFlushOutputDirectory*/, |
1575 |
new String[] { |
1576 |
"org/foo/MayBeNull.java", |
1577 |
"package org.foo;\n" + |
1578 |
"import java.lang.annotation.*;\n" + |
1579 |
"@Retention(RetentionPolicy.CLASS)\n" + |
1580 |
"public @interface MayBeNull {}\n", |
1581 |
|
1582 |
"org/foo/MustNotBeNull.java", |
1583 |
"package org.foo;\n" + |
1584 |
"import java.lang.annotation.*;\n" + |
1585 |
"@Retention(RetentionPolicy.CLASS)\n" + |
1586 |
"public @interface MustNotBeNull {}\n", |
1587 |
|
1588 |
"Lib.java", |
1589 |
"public class Lib {\n" + |
1590 |
" Object getObject() { return new Object(); }\n" + |
1591 |
"}\n", |
1592 |
"X.java", |
1593 |
"import org.foo.*;\n" + |
1594 |
"public class X {\n" + |
1595 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1596 |
" return l.getObject();\n" + |
1597 |
" }\n" + |
1598 |
"}\n", |
1599 |
|
1600 |
}, |
1601 |
null /*no libs*/, |
1602 |
customOptions, |
1603 |
"----------\n" + |
1604 |
"1. ERROR in X.java (at line 4)\n" + |
1605 |
" return l.getObject();\n" + |
1606 |
" ^^^^^^^^^^^^^\n" + |
1607 |
"Potential type mismatch: required \'@MustNotBeNull Object\' but nullness of the provided value is unknown\n" + |
1608 |
"----------\n", |
1609 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1610 |
} |
1611 |
// a non-null method returns a value obtained from an unannotated method, missing annotation types |
1612 |
public void test_annotation_import_006() { |
1613 |
Map customOptions = getCompilerOptions(); |
1614 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1615 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull"); |
1616 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull"); |
1617 |
runNegativeTest( |
1618 |
true/*shouldFlushOutputDirectory*/, |
1619 |
new String[] { |
1620 |
"Lib.java", |
1621 |
"public class Lib {\n" + |
1622 |
" Object getObject() { return new Object(); }\n" + |
1623 |
"}\n", |
1624 |
"X.java", |
1625 |
"public class X {\n" + |
1626 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1627 |
" return l.getObject();\n" + |
1628 |
" }\n" + |
1629 |
"}\n" |
1630 |
}, |
1631 |
null /* no libs */, |
1632 |
customOptions, |
1633 |
"----------\n" + |
1634 |
"1. ERROR in X.java (at line 2)\n" + |
1635 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1636 |
" ^^^^^^^^^^^^^\n" + |
1637 |
"MustNotBeNull cannot be resolved to a type\n" + |
1638 |
"----------\n" + |
1639 |
"2. ERROR in X.java (at line 2)\n" + |
1640 |
" @MustNotBeNull Object getObject(@MustNotBeNull Lib l) {\n" + |
1641 |
" ^^^^^^^^^^^^^\n" + |
1642 |
"MustNotBeNull cannot be resolved to a type\n" + |
1643 |
"----------\n", |
1644 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1645 |
} |
1646 |
// using nullness defaulting to nonnull, missing annotation types |
1647 |
public void test_annotation_import_007() { |
1648 |
Map customOptions = getCompilerOptions(); |
1649 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
1650 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull"); |
1651 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull"); |
1652 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
1653 |
runNegativeTest( |
1654 |
true/*shouldFlushOutputDirectory*/, |
1655 |
new String[] { |
1656 |
"Lib.java", |
1657 |
"public class Lib {\n" + |
1658 |
" Object getObject() { return new Object(); }\n" + |
1659 |
"}\n", |
1660 |
"X.java", |
1661 |
"public class X {\n" + |
1662 |
" Object getObject(Lib l) {\n" + |
1663 |
" return l.getObject();\n" + |
1664 |
" }\n" + |
1665 |
"}\n" |
1666 |
}, |
1667 |
this.LIBS, |
1668 |
customOptions, |
1669 |
"----------\n" + |
1670 |
"1. ERROR in Lib.java (at line 1)\n" + |
1671 |
" public class Lib {\n" + |
1672 |
" ^\n" + |
1673 |
"Buildpath problem: the type org.foo.MustNotBeNull, which is configured as a null annotation type, cannot be resolved\n" + |
1674 |
"----------\n", |
1675 |
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError); |
1676 |
} |
1677 |
|
1678 |
// a null annotation is illegally used on a class: |
1679 |
public void test_illegal_annotation_001() { |
1680 |
runNegativeTest( |
1681 |
new String[] { |
1682 |
"X.java", |
1683 |
"import org.eclipse.jdt.annotation.*;\n" + |
1684 |
"@NonNull public class X {\n" + |
1685 |
"}\n" |
1686 |
}, |
1687 |
"----------\n" + |
1688 |
"1. ERROR in X.java (at line 2)\n" + |
1689 |
" @NonNull public class X {\n" + |
1690 |
" ^^^^^^^^\n" + |
1691 |
"The annotation @NonNull is disallowed for this location\n" + |
1692 |
"----------\n", |
1693 |
this.LIBS, |
1694 |
false/*shouldFlush*/); |
1695 |
} |
1696 |
// this test has been removed: |
1697 |
// setting default to nullable, default applies to a parameter |
1698 |
// public void test_default_nullness_001() |
1699 |
|
1700 |
// a null annotation is illegally defined by its simple name |
1701 |
// disabled because specific error is not currently raised |
1702 |
public void _test_illegal_annotation_002() { |
1703 |
Map customOptions = getCompilerOptions(); |
1704 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "NichtNull"); |
1705 |
runNegativeTestWithLibs( |
1706 |
new String[] { |
1707 |
"X.java", |
1708 |
"public class X {\n" + |
1709 |
"}\n" |
1710 |
}, |
1711 |
customOptions, |
1712 |
"----------\n" + |
1713 |
"1. ERROR in X.java (at line 1)\n" + |
1714 |
" public class X {\n" + |
1715 |
" ^\n" + |
1716 |
"Cannot use the unqualified name \'NichtNull\' as an annotation name for null specification\n" + |
1717 |
"----------\n"); |
1718 |
} |
1719 |
|
1720 |
// a null annotation is illegally used on a void method: |
1721 |
public void test_illegal_annotation_003() { |
1722 |
runNegativeTest( |
1723 |
new String[] { |
1724 |
"X.java", |
1725 |
"import org.eclipse.jdt.annotation.*;\n" + |
1726 |
"public class X {\n" + |
1727 |
" @NonNull void foo() {}\n" + |
1728 |
"}\n" |
1729 |
}, |
1730 |
"----------\n" + |
1731 |
"1. ERROR in X.java (at line 3)\n" + |
1732 |
" @NonNull void foo() {}\n" + |
1733 |
" ^^^^^^^^^^^^^\n" + |
1734 |
"The nullness annotation @NonNull is not applicable for the primitive type void\n" + |
1735 |
"----------\n", |
1736 |
this.LIBS, |
1737 |
false/*shouldFlush*/); |
1738 |
} |
1739 |
|
1740 |
// a null annotation is illegally used on a primitive type parameter |
1741 |
public void test_illegal_annotation_004() { |
1742 |
runNegativeTest( |
1743 |
new String[] { |
1744 |
"X.java", |
1745 |
"import org.eclipse.jdt.annotation.*;\n" + |
1746 |
"public class X {\n" + |
1747 |
" void foo(@Nullable int i) {}\n" + |
1748 |
"}\n" |
1749 |
}, |
1750 |
"----------\n" + |
1751 |
"1. ERROR in X.java (at line 3)\n" + |
1752 |
" void foo(@Nullable int i) {}\n" + |
1753 |
" ^^^^^^^^^^^^^\n" + |
1754 |
"The nullness annotation @Nullable is not applicable for the primitive type int\n" + |
1755 |
"----------\n", |
1756 |
this.LIBS, |
1757 |
false/*shouldFlush*/); |
1758 |
} |
1759 |
|
1760 |
// a null annotation is illegally used on a primitive type local var |
1761 |
public void test_illegal_annotation_005() { |
1762 |
runNegativeTest( |
1763 |
new String[] { |
1764 |
"X.java", |
1765 |
"import org.eclipse.jdt.annotation.*;\n" + |
1766 |
"public class X {\n" + |
1767 |
" int foo() {\n" + |
1768 |
" @Nullable int i = 3;\n" + |
1769 |
" return i;\n" + |
1770 |
" }\n" + |
1771 |
"}\n" |
1772 |
}, |
1773 |
"----------\n" + |
1774 |
"1. ERROR in X.java (at line 4)\n" + |
1775 |
" @Nullable int i = 3;\n" + |
1776 |
" ^^^^^^^^^^^^^\n" + |
1777 |
"The nullness annotation @Nullable is not applicable for the primitive type int\n" + |
1778 |
"----------\n", |
1779 |
this.LIBS, |
1780 |
false/*shouldFlush*/); |
1781 |
} |
1782 |
|
1783 |
// a configured annotation type does not exist |
1784 |
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=186342#c133 |
1785 |
public void test_illegal_annotation_006() { |
1786 |
Map customOptions = getCompilerOptions(); |
1787 |
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "nullAnn.Nullable"); |
1788 |
runNegativeTestWithLibs( |
1789 |
new String[] { |
1790 |
"p/Test.java", |
1791 |
"package p;\n" + |
1792 |
"import nullAnn.*; // 1 \n" + |
1793 |
"\n" + |
1794 |
"public class Test { \n" + |
1795 |
"\n" + |
1796 |
" void foo(@nullAnn.Nullable Object o) { // 2\n" + |
1797 |
" o.toString(); \n" + |
1798 |
" }\n" + |
1799 |
"}" |
1800 |
}, |
1801 |
customOptions, |
1802 |
"----------\n" + |
1803 |
"1. ERROR in p\\Test.java (at line 2)\n" + |
1804 |
" import nullAnn.*; // 1 \n" + |
1805 |
" ^^^^^^^\n" + |
1806 |
"The import nullAnn cannot be resolved\n" + |
1807 |
"----------\n" + |
1808 |
"2. ERROR in p\\Test.java (at line 6)\n" + |
1809 |
" void foo(@nullAnn.Nullable Object o) { // 2\n" + |
1810 |
" ^^^^^^^\n" + |
1811 |
"nullAnn cannot be resolved to a type\n" + |
1812 |
"----------\n"); |
1813 |
} |
1814 |
|
1815 |
public void test_default_nullness_002() { |
1816 |
Map customOptions = getCompilerOptions(); |
1817 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1818 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
1819 |
runNegativeTestWithLibs( |
1820 |
new String[] { |
1821 |
"X.java", |
1822 |
"import org.eclipse.jdt.annotation.*;\n" + |
1823 |
"public class X {\n" + |
1824 |
" Object getObject(@Nullable Object o) {\n" + |
1825 |
" return new Object();\n" + |
1826 |
" }\n" + |
1827 |
"}\n", |
1828 |
"Y.java", |
1829 |
"import org.eclipse.jdt.annotation.*;\n" + |
1830 |
"public class Y extends X {\n" + |
1831 |
" @Override\n" + |
1832 |
" @Nullable Object getObject(Object o) {\n" + // complain illegal return redef and inherited annot is not repeated |
1833 |
" return o;\n" + |
1834 |
" }\n" + |
1835 |
"}\n", |
1836 |
}, |
1837 |
customOptions, |
1838 |
// main error: |
1839 |
"----------\n" + |
1840 |
"1. ERROR in Y.java (at line 4)\n" + |
1841 |
" @Nullable Object getObject(Object o) {\n" + |
1842 |
" ^^^^^^^^^^^^^^^^\n" + |
1843 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1844 |
"----------\n" + |
1845 |
// additional error: |
1846 |
"2. ERROR in Y.java (at line 4)\n" + |
1847 |
" @Nullable Object getObject(Object o) {\n" + |
1848 |
" ^^^^^^\n" + |
1849 |
"Illegal redefinition of parameter o, inherited method from X declares this parameter as @Nullable\n" + |
1850 |
"----------\n"); |
1851 |
} |
1852 |
// package default is non-null |
1853 |
public void test_default_nullness_003() { |
1854 |
Map customOptions = getCompilerOptions(); |
1855 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1856 |
runNegativeTestWithLibs( |
1857 |
new String[] { |
1858 |
"p1/X.java", |
1859 |
"package p1;\n" + |
1860 |
"import org.eclipse.jdt.annotation.*;\n" + |
1861 |
"@NonNullByDefault\n" + |
1862 |
"public class X {\n" + |
1863 |
" protected Object getObject(@Nullable Object o) {\n" + |
1864 |
" return new Object();\n" + |
1865 |
" }\n" + |
1866 |
"}\n", |
1867 |
"p2/package-info.java", |
1868 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1869 |
"package p2;\n", |
1870 |
"p2/Y.java", |
1871 |
"package p2;\n" + |
1872 |
"import org.eclipse.jdt.annotation.*;\n" + |
1873 |
"public class Y extends p1.X {\n" + |
1874 |
" @Override\n" + |
1875 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1876 |
" bar(o);\n" + |
1877 |
" return o;\n" + |
1878 |
" }\n" + |
1879 |
" void bar(Object o2) { }\n" + // parameter is nonnull per package default |
1880 |
"}\n" |
1881 |
}, |
1882 |
customOptions, |
1883 |
"----------\n" + |
1884 |
"1. ERROR in p2\\Y.java (at line 5)\n" + |
1885 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1886 |
" ^^^^^^^^^^^^^^^^\n" + |
1887 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1888 |
"----------\n" + |
1889 |
"2. ERROR in p2\\Y.java (at line 6)\n" + |
1890 |
" bar(o);\n" + |
1891 |
" ^\n" + |
1892 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1893 |
"----------\n"); |
1894 |
} |
1895 |
// package level default is consumed from package-info.class |
1896 |
public void test_default_nullness_003a() { |
1897 |
Map customOptions = getCompilerOptions(); |
1898 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1899 |
runConformTestWithLibs( |
1900 |
new String[] { |
1901 |
"p1/X.java", |
1902 |
"package p1;\n" + |
1903 |
"import org.eclipse.jdt.annotation.*;\n" + |
1904 |
"@NonNullByDefault\n" + |
1905 |
"public class X {\n" + |
1906 |
" protected Object getObject(@Nullable Object o) {\n" + |
1907 |
" return new Object();\n" + |
1908 |
" }\n" + |
1909 |
" protected void bar(Object o2) { }\n" + // parameter is nonnull per type default |
1910 |
"}\n", |
1911 |
"p2/package-info.java", |
1912 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1913 |
"package p2;\n", |
1914 |
}, |
1915 |
customOptions, |
1916 |
""); |
1917 |
// check if default is visible from package-info.class. |
1918 |
runNegativeTestWithLibs( |
1919 |
false, // don't flush |
1920 |
new String[] { |
1921 |
"p2/Y.java", |
1922 |
"package p2;\n" + |
1923 |
"import org.eclipse.jdt.annotation.*;\n" + |
1924 |
"public class Y extends p1.X {\n" + |
1925 |
" @Override\n" + |
1926 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + // can't override inherited default nonnull |
1927 |
" bar(o);\n" + // parameter is nonnull in super class's .class file |
1928 |
" accept(o);\n" + |
1929 |
" return o;\n" + |
1930 |
" }\n" + |
1931 |
" void accept(Object a) {}\n" + // governed by package level default |
1932 |
"}\n" |
1933 |
}, |
1934 |
customOptions, |
1935 |
"----------\n" + |
1936 |
"1. ERROR in p2\\Y.java (at line 5)\n" + |
1937 |
" protected @Nullable Object getObject(@Nullable Object o) {\n" + |
1938 |
" ^^^^^^^^^^^^^^^^\n" + |
1939 |
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" + |
1940 |
"----------\n" + |
1941 |
"2. ERROR in p2\\Y.java (at line 6)\n" + |
1942 |
" bar(o);\n" + |
1943 |
" ^\n" + |
1944 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1945 |
"----------\n" + |
1946 |
"3. ERROR in p2\\Y.java (at line 7)\n" + |
1947 |
" accept(o);\n" + |
1948 |
" ^\n" + |
1949 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
1950 |
"----------\n"); |
1951 |
} |
1952 |
// don't apply type-level default to non-reference type |
1953 |
public void test_default_nullness_004() { |
1954 |
Map customOptions = getCompilerOptions(); |
1955 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1956 |
runConformTestWithLibs( |
1957 |
new String[] { |
1958 |
"p1/X.java", |
1959 |
"package p1;\n" + |
1960 |
"import org.eclipse.jdt.annotation.*;\n" + |
1961 |
"@NonNullByDefault\n" + |
1962 |
"public class X {\n" + |
1963 |
" protected Object getObject(boolean o) {\n" + |
1964 |
" return new Object();\n" + |
1965 |
" }\n" + |
1966 |
"}\n", |
1967 |
"p2/Y.java", |
1968 |
"package p2;\n" + |
1969 |
"import org.eclipse.jdt.annotation.*;\n" + |
1970 |
"public class Y extends p1.X {\n" + |
1971 |
" @Override\n" + |
1972 |
" protected @NonNull Object getObject(boolean o) {\n" + |
1973 |
" return o ? this : new Object();\n" + |
1974 |
" }\n" + |
1975 |
"}\n" |
1976 |
}, |
1977 |
customOptions, |
1978 |
""); |
1979 |
} |
1980 |
// package default is non-null |
1981 |
// see also Bug 354536 - compiling package-info.java still depends on the order of compilation units |
1982 |
public void test_default_nullness_005() { |
1983 |
Map customOptions = getCompilerOptions(); |
1984 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
1985 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
1986 |
runNegativeTestWithLibs( |
1987 |
new String[] { |
1988 |
"p1/X.java", |
1989 |
"package p1;\n" + |
1990 |
"public class X {\n" + |
1991 |
" class Inner {" + |
1992 |
" protected Object getObject(String s) {\n" + |
1993 |
" return null;\n" + |
1994 |
" }\n" + |
1995 |
" }\n" + |
1996 |
"}\n", |
1997 |
"p1/package-info.java", |
1998 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
1999 |
"package p1;\n", |
2000 |
CUSTOM_NONNULL_NAME, |
2001 |
CUSTOM_NONNULL_CONTENT |
2002 |
}, |
2003 |
customOptions, |
2004 |
"----------\n" + |
2005 |
"1. ERROR in p1\\X.java (at line 4)\n" + |
2006 |
" return null;\n" + |
2007 |
" ^^^^\n" + |
2008 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
2009 |
"----------\n"); |
2010 |
} |
2011 |
// package default is non-null, package-info.java read before the annotation type |
2012 |
// compile order: beginToCompile(X.Inner) triggers reading of package-info.java before the annotation type was read |
2013 |
public void test_default_nullness_006() { |
2014 |
Map customOptions = getCompilerOptions(); |
2015 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2016 |
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.NonNull"); |
2017 |
runNegativeTestWithLibs( |
2018 |
new String[] { |
2019 |
"p1/package-info.java", |
2020 |
"@org.eclipse.jdt.annotation.NonNullByDefault\n" + |
2021 |
"package p1;\n", |
2022 |
"p1/X.java", |
2023 |
"package p1;\n" + |
2024 |
"public class X {\n" + |
2025 |
" class Inner {" + |
2026 |
" protected Object getObject(String s) {\n" + |
2027 |
" return null;\n" + |
2028 |
" }\n" + |
2029 |
" }\n" + |
2030 |
"}\n", |
2031 |
CUSTOM_NONNULL_NAME, |
2032 |
CUSTOM_NONNULL_CONTENT |
2033 |
}, |
2034 |
customOptions, |
2035 |
"----------\n" + |
2036 |
"1. ERROR in p1\\X.java (at line 4)\n" + |
2037 |
" return null;\n" + |
2038 |
" ^^^^\n" + |
2039 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
2040 |
"----------\n"); |
2041 |
} |
2042 |
// global default nonnull, but return may be null |
2043 |
public void test_default_nullness_007() { |
2044 |
Map customOptions = getCompilerOptions(); |
2045 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2046 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2047 |
runNegativeTestWithLibs( |
2048 |
new String[] { |
2049 |
"X.java", |
2050 |
"import org.eclipse.jdt.annotation.*;\n" + |
2051 |
"public class X {\n" + |
2052 |
" @Nullable Object dangerous() {\n" + |
2053 |
" return null;\n" + |
2054 |
" }\n" + |
2055 |
" Object broken() {\n" + |
2056 |
" return dangerous();\n" + |
2057 |
" }\n" + |
2058 |
"}\n", |
2059 |
|
2060 |
}, |
2061 |
customOptions, |
2062 |
"----------\n" + |
2063 |
"1. ERROR in X.java (at line 7)\n" + |
2064 |
" return dangerous();\n" + |
2065 |
" ^^^^^^^^^^^\n" + |
2066 |
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" + |
2067 |
"----------\n"); |
2068 |
} |
2069 |
|
2070 |
// cancel type level default to comply with super specification |
2071 |
public void test_default_nullness_008() { |
2072 |
Map customOptions = getCompilerOptions(); |
2073 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2074 |
runConformTestWithLibs( |
2075 |
new String[] { |
2076 |
"p1/X.java", |
2077 |
"package p1;\n" + |
2078 |
"public class X {\n" + |
2079 |
" protected Object getObject(Object o) {\n" + |
2080 |
" return new Object();\n" + |
2081 |
" }\n" + |
2082 |
"}\n", |
2083 |
"p2/Y.java", |
2084 |
"package p2;\n" + |
2085 |
"import org.eclipse.jdt.annotation.*;\n" + |
2086 |
"@NonNullByDefault\n" + |
2087 |
"public class Y extends p1.X {\n" + |
2088 |
" @Override\n" + |
2089 |
" @NonNullByDefault(false)\n" + |
2090 |
" protected Object getObject(Object o) {\n" + |
2091 |
" if (o.toString().length() == 0)\n" + // dereference without a warning |
2092 |
" return null;\n" + // return null without a warning |
2093 |
" return o.toString();\n" + |
2094 |
" }\n" + |
2095 |
"}\n" |
2096 |
}, |
2097 |
customOptions, |
2098 |
""); |
2099 |
} |
2100 |
|
2101 |
// cancel outer type level default to comply with super specification |
2102 |
public void test_default_nullness_009() { |
2103 |
Map customOptions = getCompilerOptions(); |
2104 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2105 |
runNegativeTestWithLibs( |
2106 |
new String[] { |
2107 |
"p1/X.java", |
2108 |
"package p1;\n" + |
2109 |
"public class X {\n" + |
2110 |
" protected Object getObject(Object o) {\n" + |
2111 |
" return new Object();\n" + |
2112 |
" }\n" + |
2113 |
"}\n", |
2114 |
"p2/Y.java", |
2115 |
"package p2;\n" + |
2116 |
"import org.eclipse.jdt.annotation.*;\n" + |
2117 |
"@NonNullByDefault\n" + |
2118 |
"public class Y { \n" + |
2119 |
" @NonNullByDefault(false)\n" + |
2120 |
" static class Z extends p1.X {\n" + |
2121 |
" @Override\n" + |
2122 |
" protected Object getObject(Object o) {\n" + |
2123 |
" if (o.toString().length() == 0) {\n" + |
2124 |
" o = null;\n" + // assign null without a warning |
2125 |
" bar(o); // error: arg is declared @NonNull\n" + |
2126 |
" return null;\n" + |
2127 |
" }\n" + |
2128 |
" return o.toString();\n" + |
2129 |
" }\n" + |
2130 |
" String bar(@NonNull Object o) {\n" + |
2131 |
" return getObject(o).toString();" + |
2132 |
" }\n" + |
2133 |
" }\n" + |
2134 |
"}\n" |
2135 |
}, |
2136 |
customOptions, |
2137 |
"----------\n" + |
2138 |
"1. ERROR in p2\\Y.java (at line 11)\n" + |
2139 |
" bar(o); // error: arg is declared @NonNull\n" + |
2140 |
" ^\n" + |
2141 |
"Type mismatch: required \'@NonNull Object\' but the provided value is null\n" + |
2142 |
"----------\n"); |
2143 |
} |
2144 |
// non-null declarations are redundant within a default scope. |
2145 |
public void test_default_nullness_010() { |
2146 |
Map customOptions = getCompilerOptions(); |
2147 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2148 |
runConformTestWithLibs( |
2149 |
new String[] { |
2150 |
"p2/Y.java", |
2151 |
"package p2;\n" + |
2152 |
"import org.eclipse.jdt.annotation.*;\n" + |
2153 |
"@NonNullByDefault\n" + |
2154 |
"public class Y {\n" + |
2155 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
2156 |
" return o;\n" + |
2157 |
" }\n" + |
2158 |
"}\n" |
2159 |
}, |
2160 |
customOptions, |
2161 |
"----------\n" + |
2162 |
"1. WARNING in p2\\Y.java (at line 5)\n" + |
2163 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
2164 |
" ^^^^^^^^^^^^^^^\n" + |
2165 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
2166 |
"----------\n" + |
2167 |
"2. WARNING in p2\\Y.java (at line 5)\n" + |
2168 |
" protected @NonNull Object getObject(@NonNull Object o) {\n" + |
2169 |
" ^^^^^^^^^^^^^^^^^\n" + |
2170 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
2171 |
"----------\n"); |
2172 |
} |
2173 |
// a nonnull variable is dereferenced in a loop |
2174 |
public void test_nonnull_var_in_constrol_structure_1() { |
2175 |
Map customOptions = getCompilerOptions(); |
2176 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2177 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2178 |
runNegativeTestWithLibs( |
2179 |
new String[] { |
2180 |
"X.java", |
2181 |
"import org.eclipse.jdt.annotation.*;\n" + |
2182 |
"public class X {\n" + |
2183 |
" void print4(@NonNull String s) {\n" + |
2184 |
" for (int i=0; i<4; i++)\n" + |
2185 |
" print(s);\n" + |
2186 |
" }\n" + |
2187 |
" void print5(@Nullable String s) {\n" + |
2188 |
" for (int i=0; i<5; i++)\n" + |
2189 |
" print(s);\n" + |
2190 |
" }\n" + |
2191 |
" void print6(boolean b) {\n" + |
2192 |
" String s = b ? null : \"\";\n" + |
2193 |
" for (int i=0; i<5; i++)\n" + |
2194 |
" print(s);\n" + |
2195 |
" }\n" + |
2196 |
" void print(@NonNull String s) {\n" + |
2197 |
" System.out.print(s);\n" + |
2198 |
" }\n" + |
2199 |
"}\n", |
2200 |
|
2201 |
}, |
2202 |
customOptions, |
2203 |
"----------\n" + |
2204 |
"1. WARNING in X.java (at line 3)\n" + |
2205 |
" void print4(@NonNull String s) {\n" + |
2206 |
" ^^^^^^^^^^^^^^^^^\n" + |
2207 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
2208 |
"----------\n" + |
2209 |
"2. ERROR in X.java (at line 9)\n" + |
2210 |
" print(s);\n" + |
2211 |
" ^\n" + |
2212 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2213 |
"----------\n" + |
2214 |
"3. ERROR in X.java (at line 14)\n" + |
2215 |
" print(s);\n" + |
2216 |
" ^\n" + |
2217 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2218 |
"----------\n" + |
2219 |
"4. WARNING in X.java (at line 16)\n" + |
2220 |
" void print(@NonNull String s) {\n" + |
2221 |
" ^^^^^^^^^^^^^^^^^\n" + |
2222 |
"The nullness annotation is redundant with a default that applies to this location\n" + |
2223 |
"----------\n"); |
2224 |
} |
2225 |
// a nonnull variable is dereferenced in a finally block |
2226 |
public void test_nonnull_var_in_constrol_structure_2() { |
2227 |
Map customOptions = getCompilerOptions(); |
2228 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2229 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2230 |
runNegativeTestWithLibs( |
2231 |
new String[] { |
2232 |
"X.java", |
2233 |
"import org.eclipse.jdt.annotation.*;\n" + |
2234 |
"public class X {\n" + |
2235 |
" void print4(String s) {\n" + |
2236 |
" try { /*empty*/ } finally {\n" + |
2237 |
" print(s);\n" + |
2238 |
" }\n" + |
2239 |
" }\n" + |
2240 |
" void print5(@Nullable String s) {\n" + |
2241 |
" try { /*empty*/ } finally {\n" + |
2242 |
" print(s);\n" + |
2243 |
" }\n" + |
2244 |
" }\n" + |
2245 |
" void print6(boolean b) {\n" + |
2246 |
" String s = b ? null : \"\";\n" + |
2247 |
" try { /*empty*/ } finally {\n" + |
2248 |
" print(s);\n" + |
2249 |
" }\n" + |
2250 |
" }\n" + |
2251 |
" void print(String s) {\n" + |
2252 |
" System.out.print(s);\n" + |
2253 |
" }\n" + |
2254 |
"}\n", |
2255 |
|
2256 |
}, |
2257 |
customOptions, |
2258 |
"----------\n" + |
2259 |
"1. ERROR in X.java (at line 10)\n" + |
2260 |
" print(s);\n" + |
2261 |
" ^\n" + |
2262 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2263 |
"----------\n" + |
2264 |
"2. ERROR in X.java (at line 16)\n" + |
2265 |
" print(s);\n" + |
2266 |
" ^\n" + |
2267 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2268 |
"----------\n"); |
2269 |
} |
2270 |
// a nonnull variable is dereferenced in a finally block inside a loop |
2271 |
public void test_nonnull_var_in_constrol_structure_3() { |
2272 |
Map customOptions = getCompilerOptions(); |
2273 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2274 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2275 |
customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.IGNORE); |
2276 |
runNegativeTestWithLibs( |
2277 |
new String[] { |
2278 |
"X.java", |
2279 |
"import org.eclipse.jdt.annotation.*;\n" + |
2280 |
"public class X {\n" + |
2281 |
" void print4(@NonNull String s) {\n" + |
2282 |
" for (int i=0; i<4; i++)\n" + |
2283 |
" try { /*empty*/ } finally {\n" + |
2284 |
" print(s);\n" + |
2285 |
" }\n" + |
2286 |
" }\n" + |
2287 |
" void print5(@Nullable String s) {\n" + |
2288 |
" for (int i=0; i<5; i++)\n" + |
2289 |
" try { /*empty*/ } finally {\n" + |
2290 |
" print(s);\n" + |
2291 |
" }\n" + |
2292 |
" }\n" + |
2293 |
" void print6(boolean b) {\n" + |
2294 |
" String s = b ? null : \"\";\n" + |
2295 |
" for (int i=0; i<4; i++)\n" + |
2296 |
" try { /*empty*/ } finally {\n" + |
2297 |
" print(s);\n" + |
2298 |
" }\n" + |
2299 |
" }\n" + |
2300 |
" void print(@NonNull String s) {\n" + |
2301 |
" System.out.print(s);\n" + |
2302 |
" }\n" + |
2303 |
"}\n", |
2304 |
|
2305 |
}, |
2306 |
customOptions, |
2307 |
"----------\n" + |
2308 |
"1. ERROR in X.java (at line 12)\n" + |
2309 |
" print(s);\n" + |
2310 |
" ^\n" + |
2311 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2312 |
"----------\n" + |
2313 |
"2. ERROR in X.java (at line 19)\n" + |
2314 |
" print(s);\n" + |
2315 |
" ^\n" + |
2316 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2317 |
"----------\n"); |
2318 |
} |
2319 |
// a nonnull variable is dereferenced method of a nested type |
2320 |
public void test_nesting_1() { |
2321 |
Map customOptions = getCompilerOptions(); |
2322 |
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR); |
2323 |
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED); |
2324 |
runNegativeTestWithLibs( |
2325 |
new String[] { |
2326 |
"X.java", |
2327 |
"import org.eclipse.jdt.annotation.*;\n" + |
2328 |
"@NonNullByDefault\n" + |
2329 |
"public class X {\n" + |
2330 |
" void print4(final String s1) {\n" + |
2331 |
" for (int i=0; i<3; i++)\n" + |
2332 |
" new Runnable() {\n" + |
2333 |
" public void run() {\n" + |
2334 |
" print(s1);\n" + |
2335 |
" }\n" + |
2336 |
" }.run();\n" + |
2337 |
" }\n" + |
2338 |
" void print8(final @Nullable String s2) {\n" + |
2339 |
" for (int i=0; i<3; i++)\n" + |
2340 |
" new Runnable() {\n" + |
2341 |
" public void run() {\n" + |
2342 |
" print(s2);\n" + |
2343 |
" }\n" + |
2344 |
" }.run();\n" + |
2345 |
" }\n" + |
2346 |
" void print16(boolean b) {\n" + |
2347 |
" final String s3 = b ? null : \"\";\n" + |
2348 |
" for (int i=0; i<3; i++)\n" + |
2349 |
" new Runnable() {\n" + |
2350 |
" public void run() {\n" + |
2351 |
" @NonNull String s3R = s3;\n" + |
2352 |
" }\n" + |
2353 |
" }.run();\n" + |
2354 |
" }\n" + |
2355 |
" void print(String s) {\n" + |
2356 |
" System.out.print(s);\n" + |
2357 |
" }\n" + |
2358 |
"}\n", |
2359 |
|
2360 |
}, |
2361 |
customOptions, |
2362 |
"----------\n" + |
2363 |
"1. ERROR in X.java (at line 16)\n" + |
2364 |
" print(s2);\n" + |
2365 |
" ^^\n" + |
2366 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2367 |
"----------\n" + |
2368 |
"2. ERROR in X.java (at line 25)\n" + |
2369 |
" @NonNull String s3R = s3;\n" + |
2370 |
" ^^\n" + |
2371 |
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" + |
2372 |
"----------\n"); |
2373 |
} |
2374 |
// Test a regression incurred to the OT/J based implementation |
2375 |
// by the fix in Bug 360328 - [compiler][null] detect null problems in nested code (local class inside a loop) |
2376 |
public void test_constructor_with_nested_class() { |
2377 |
runConformTest( |
2378 |
new String[] { |
2379 |
"X.java", |
2380 |
"public class X {\n" + |
2381 |
" final Object o1;\n" + |
2382 |
" final Object o2;\n" + |
2383 |
" public X() {\n" + |
2384 |
" this.o1 = new Object() {\n" + |
2385 |
" public String toString() { return \"O1\"; }\n" + |
2386 |
" };\n" + |
2387 |
" this.o2 = new Object();" + |
2388 |
" }\n" + |
2389 |
"}\n" |
2390 |
}, |
2391 |
""); |
2392 |
} |
2393 |
// test analysis disablement, binary type contains annotation |
2394 |
public void test_disabled_1() { |
2395 |
Map customOptions = getCompilerOptions(); |
2396 |
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR); |
2397 |
runConformTestWithLibs( |
2398 |
new String[] { |
2399 |
"ContainingInner2.java", |
2400 |
"public class ContainingInner2 {\n" + |
2401 |
" public ContainingInner2 (@org.eclipse.jdt.annotation.NonNull Object o) {\n" + |
2402 |
" }\n" + |
2403 |
" public class Inner {\n" + |
2404 |
" public <T> Inner (@org.eclipse.jdt.annotation.NonNull T o) {\n" + |
2405 |
" }\n" + |
2406 |
" }\n" + |
2407 |
"}\n", |
2408 |
}, |
2409 |
null /*customOptions*/, |
2410 |
""); |
2411 |
customOptions.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.DISABLED); |
2412 |
runConformTestWithLibs( |
2413 |
false, // flush directory |
2414 |
new String[] { |
2415 |
"X.java", |
2416 |
"public class X {\n" + |
2417 |
" void create() {\n" + |
2418 |
" ContainingInner2 container = new ContainingInner2(null);\n" + |
2419 |
" ContainingInner2.Inner inner = container.new Inner(null);\n" + |
2420 |
" }\n" + |
2421 |
"}\n"}, |
2422 |
customOptions, |
2423 |
"" /* compiler output */); |
2424 |
} |
2425 |
} |