Skip to main content
summaryrefslogtreecommitdiffstats
blob: deabd1537ab9f80f8b41cfc2fadc349221150193 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*******************************************************************************
 * Copyright (c) 2017, 2018 GK Software AG, and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Stephan Herrmann - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.batch.BasicModule;
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
import org.eclipse.jdt.internal.compiler.batch.FileSystem;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.parser.Parser;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;

import junit.framework.Test;

import static org.eclipse.jdt.core.tests.util.Util.createJar;

public class NullAnnotationTests9 extends AbstractNullAnnotationTest {

	public NullAnnotationTests9(String name) {
		super(name);
	}
	
	static {
//			TESTS_NAMES = new String[] { "testBug456497" };
//			TESTS_NUMBERS = new int[] { 001 };
//			TESTS_RANGE = new int[] { 1, 12 };
	}

	public static Test suite() {
		return buildMinimalComplianceTestSuite(testClass(), F_9);
	}

	public static Class<?> testClass() {
		return NullAnnotationTests9.class;
	}

	@Deprecated // super method is deprecated
	protected void setUpAnnotationLib() throws IOException {
		if (this.LIBS == null) {
			String[] defaultLibs = getDefaultClassPaths();
			int len = defaultLibs.length;
			this.LIBS = new String[len+1];
			System.arraycopy(defaultLibs, 0, this.LIBS, 0, len);
			this.LIBS[len] = createAnnotation_2_2_jar(Util.getOutputDirectory() + File.separator, null);
		}
	}

	public static String createAnnotation_2_2_jar(String dirName, String jcl9Path) throws IOException {
		// role our own annotation library as long as o.e.j.annotation is still at BREE 1.8:
		String jarFileName = dirName + "org.eclipse.jdt.annotation_2.2.0.jar";
		createJar(new String[] {
				"module-info.java",
				"module org.eclipse.jdt.annotation {\n" +
				"	exports org.eclipse.jdt.annotation;\n" +
				"}\n",

				"org/eclipse/jdt/annotation/DefaultLocation.java",
				"package org.eclipse.jdt.annotation;\n" + 
				"\n" + 
				"public enum DefaultLocation {\n" + 
				"	\n" + 
				"	PARAMETER, RETURN_TYPE, FIELD, TYPE_PARAMETER, TYPE_BOUND, TYPE_ARGUMENT, ARRAY_CONTENTS\n" + 
				"}\n",
				
				"org/eclipse/jdt/annotation/NonNullByDefault.java",
				"package org.eclipse.jdt.annotation;\n" + 
				"\n" + 
				"import java.lang.annotation.ElementType;\n" + 
				"import static org.eclipse.jdt.annotation.DefaultLocation.*;\n" + 
				"\n" + 
				"import java.lang.annotation.*;\n" + 
				" \n" + 
				"@Documented\n" + 
				"@Retention(RetentionPolicy.CLASS)\n" + 
				"@Target({ ElementType.MODULE, ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.LOCAL_VARIABLE })\n" + 
				"public @interface NonNullByDefault {\n" + 
				"	DefaultLocation[] value() default { PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT };\n" + 
				"}",
				
				"org/eclipse/jdt/annotation/NonNull.java",
				"package org.eclipse.jdt.annotation;\n" +
				"import static java.lang.annotation.ElementType.TYPE_USE;\n" + 
				"\n" + 
				"import java.lang.annotation.*;\n" + 
				" \n" + 
				"@Documented\n" + 
				"@Retention(RetentionPolicy.CLASS)\n" + 
				"@Target({ TYPE_USE })\n" + 
				"public @interface NonNull {\n" + 
				"	// marker annotation with no members\n" + 
				"}\n",
				
				"org/eclipse/jdt/annotation/Nullable.java",
				"package org.eclipse.jdt.annotation;\n" + 
				"\n" + 
				"import static java.lang.annotation.ElementType.TYPE_USE;\n" + 
				"\n" + 
				"import java.lang.annotation.*;\n" + 
				" \n" + 
				"@Documented\n" + 
				"@Retention(RetentionPolicy.CLASS)\n" + 
				"@Target({ TYPE_USE })\n" + 
				"public @interface Nullable {\n" + 
				"	// marker annotation with no members\n" + 
				"}\n"
			},
			null,
			jarFileName,
			jcl9Path != null ? new String[] { jcl9Path } : null,
			"9");
		return jarFileName;
	}

	// -------- internal infrastructure ------------

	Map<String,IModule> moduleMap = new HashMap<>(); // by name
	Map<String,String> file2module = new HashMap<>();

	protected INameEnvironment getNameEnvironment(final String[] testFiles, String[] classPaths) {
		this.classpaths = classPaths == null ? getDefaultClassPaths() : classPaths;
		INameEnvironment[] classLibs = getClassLibs(classPaths == null);
		for (INameEnvironment nameEnvironment : classLibs) {
			((FileSystem) nameEnvironment).scanForModules(createParser());
		}
		return new InMemoryNameEnvironment9(testFiles, this.moduleMap, classLibs);
	}

	// --- same as AbstractRegressionTest9, just in a different inheritance hierarchy:

	@Override
	protected CompilationUnit[] getCompilationUnits(String[] testFiles) {
		Map<String,char[]> moduleFiles= new HashMap<>(); // filename -> modulename

		// scan for all module-info.java:
		for (int i = 0; i < testFiles.length; i+=2) {
			IModule module = extractModuleDesc(testFiles[i], testFiles[i+1]);
			if (module != null) {
				this.moduleMap.put(String.valueOf(module.name()), module);
				moduleFiles.put(testFiles[0], module.name());
			}
		}
		// record module information in CUs:
		CompilationUnit[] compilationUnits = Util.compilationUnits(testFiles);
		for (int i = 0; i < compilationUnits.length; i++) {
			char[] fileName = compilationUnits[i].getFileName();
			String fileNameString = String.valueOf(compilationUnits[i].getFileName());
			if (CharOperation.endsWith(fileName, TypeConstants.MODULE_INFO_FILE_NAME)) {
				compilationUnits[i].module = moduleFiles.get(fileNameString.replace(File.separator, "/"));
			} else {
				String modName = this.file2module.get(fileNameString.replace(File.separator, "/"));
				if (modName != null) {
					compilationUnits[i].module = modName.toCharArray();
				}
			}
		}
		return compilationUnits;
	}

	IModule extractModuleDesc(String fileName, String fileContent) {
		if (fileName.toLowerCase().endsWith(IModule.MODULE_INFO_JAVA)) {
			Parser parser = createParser();
			
			ICompilationUnit cu = new CompilationUnit(fileContent.toCharArray(), fileName, null);
			CompilationResult compilationResult = new CompilationResult(cu, 0, 1, 10);
			CompilationUnitDeclaration unit = parser.parse(cu, compilationResult);
			if (unit.isModuleInfo() && unit.moduleDeclaration != null) {
				return new BasicModule(unit.moduleDeclaration, null);
			}
		}
		return null;
	}

	Parser createParser() {
		Map<String,String> opts = new HashMap<String, String>();
		opts.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_9);
		return new Parser(
				new ProblemReporter(getErrorHandlingPolicy(), new CompilerOptions(opts), getProblemFactory()),
				false);
	}

	// ------------------------------------------------------

	/** Use in tests to associate the CU in file 'fileName' to the module of the given name. */
	void associateToModule(String moduleName, String... fileNames) {
		for (String fileName : fileNames)
			this.file2module.put(fileName, moduleName);
	}
	
	private Runner getDefaultRunner() {
		Runner runner = new Runner();
		runner.classLibraries = this.LIBS;
		runner.libsOnModulePath = true;
		runner.javacTestOptions =
				JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
		return runner;
	}

	public void test_nnbd_in_module_01() {
		associateToModule("my.mod", "my.mod/p/X.java");
		Runner runner = getDefaultRunner();
		runner.testFiles = new String[] {
				"my.mod/module-info.java",
				  "import org.eclipse.jdt.annotation.*;\n" +
				  "@NonNullByDefault\n" +
				  "module my.mod {\n" +
				  "		requires static org.eclipse.jdt.annotation;\n" +
				  "}\n",
				"my.mod/p/X.java",
				  "package p;\n" +
				  "public class X {\n" +
				  "		String f; // missing nn init\n" +
				  "    	void foo(String s) {\n" +
				  "        this.f = s; // OK\n" +
				  "    	}\n" +
				  "}\n"
			};
		runner.expectedCompilerLog =
			"----------\n" + 
			"1. ERROR in my.mod\\p\\X.java (at line 3)\n" + 
			"	String f; // missing nn init\n" + 
			"	       ^\n" + 
			"The @NonNull field f may not have been initialized\n" + 
			"----------\n";
		runner.runNegativeTest();
	}

	public void test_nnbd_in_module_02() throws IOException {
		
		String jarPath = OUTPUT_DIR+"/mod.one.jar";
		createJar(
			new String[] {
				"module-info.java",
				"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
				"module mod.one {\n" +
				"	requires org.eclipse.jdt.annotation;\n" +
				"	exports p.q;\n" +
				"}\n",
				"p/q/API.java",
				"package p.q;\n" +
				"public class API {\n" +
				"	public String id(String in) { return in; }\n" +
				"}\n"
			},
			null, // extra path & content
			jarPath,
			this.LIBS,
			"9");
		
		associateToModule("my.mod", "my.mod/p/X.java");
		Runner runner = new Runner();
		runner.shouldFlushOutputDirectory = false;
		runner.classLibraries = Arrays.copyOf(this.LIBS, this.LIBS.length+1);
		runner.classLibraries[runner.classLibraries.length-1] = jarPath;
		runner.libsOnModulePath = true;
		runner.testFiles = new String[] {
				"my.mod/module-info.java",
				  "import org.eclipse.jdt.annotation.*;\n" +
				  "@NonNullByDefault\n" +
				  "module my.mod {\n" +
				  "		requires static org.eclipse.jdt.annotation;\n" +
				  "		requires mod.one;\n" +
				  "}\n",
				"my.mod/p/X.java",
				  "package p;\n" +
				  "import p.q.API;\n" +
				  "public class X {\n" +
				  "    	void foo(API api) {\n" +
				  "        api.id(api.id(\"\")); // OK\n" +
				  "        api.id(null); // NOK\n" +
				  "    	}\n" +
				  "}\n"
			};
		runner.expectedCompilerLog =
			"----------\n" + 
			"1. ERROR in my.mod\\p\\X.java (at line 6)\n" + 
			"	api.id(null); // NOK\n" + 
			"	       ^^^^\n" + 
			"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" + 
			"----------\n";
		runner.javacTestOptions =
			JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
		runner.runNegativeTest();
	}

	public void test_redundant_nnbd_vs_module() {
		associateToModule("my.mod", "my.mod/p/X.java", "my.mod/p2/package-info.java");
		Runner runner = getDefaultRunner();
		runner.testFiles = new String[] {
				"my.mod/module-info.java",
				  "import org.eclipse.jdt.annotation.*;\n" +
				  "@NonNullByDefault\n" +
				  "module my.mod {\n" +
				  "		requires static org.eclipse.jdt.annotation;\n" +
				  "}\n",
				"my.mod/p/X.java",
				  "package p;\n" +
				  "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
				  "public class X {\n" +
				  "		String f; // missing nn init\n" +
				  "    	void foo(String s) {\n" +
				  "        this.f = s; // OK\n" +
				  "    	}\n" +
				  "}\n",
				"my.mod/p/Y.java",
				  "package p;\n" +
				  "import static org.eclipse.jdt.annotation.DefaultLocation.*;\n" +
				  "@org.eclipse.jdt.annotation.NonNullByDefault(PARAMETER)\n" + // not: FIELD, due to details not redundant
				  "public class Y {\n" +
				  "		String f; // missing init is NOT a problem\n" +
				  "    	void foo(String s) {\n" +
				  "        this.f = s; // OK\n" +
				  "    	}\n" +
				  "}\n",
				"my.mod/p2/package-info.java",
				  "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
				  "package p2;\n"
			};
		runner.expectedCompilerLog =
			"----------\n" + 
			"1. WARNING in my.mod\\p\\X.java (at line 2)\n" + 
			"	@org.eclipse.jdt.annotation.NonNullByDefault\n" + 
			"	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
			"Nullness default is redundant with a default specified for the enclosing module my.mod\n" + 
			"----------\n" + 
			"2. ERROR in my.mod\\p\\X.java (at line 4)\n" + 
			"	String f; // missing nn init\n" + 
			"	       ^\n" + 
			"The @NonNull field f may not have been initialized\n" + 
			"----------\n" + 
			"----------\n" + 
			"1. WARNING in my.mod\\p2\\package-info.java (at line 1)\n" + 
			"	@org.eclipse.jdt.annotation.NonNullByDefault\n" + 
			"	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
			"Nullness default is redundant with a default specified for the enclosing module my.mod\n" + 
			"----------\n";
		runner.runNegativeTest();
	}
	public void testBug536037a() {
		if (this.complianceLevel < ClassFileConstants.JDK10) return;
		runConformTestWithLibs(
			new String[] {
				"Bar.java",
				"@org.eclipse.jdt.annotation.NonNullByDefault\n" + 
				"public class Bar {\n" + 
				"    static void bar(Iterable<String> list) {\n" + 
				"        for(var s : list);\n" + 
				"    }\n" + 
				"}\n"
			},
			null,
			"");
	}
	public void testBug536037b() {
		// tests combination of declaration null-annotations & 'var':
		if (this.complianceLevel < ClassFileConstants.JDK10) return;
		Map<String, String> options = getCompilerOptions();
		options.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "test.NonNull");
		options.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "test.Nullable");
		runNegativeTestWithLibs(
			new String[] {
				"test/NonNull.java",
				"package test;\n" +
				"import java.lang.annotation.*;\n" +
				"@Target({ElementType.LOCAL_VARIABLE,ElementType.PARAMETER}) public @interface NonNull {}\n",
				"test/Nullable.java",
				"package test;\n" +
				"import java.lang.annotation.*;\n" +
				"@Target({ElementType.LOCAL_VARIABLE,ElementType.PARAMETER}) public @interface Nullable {}\n",
				"Bar.java",
				"import test.*;\n" +
				"public class Bar {\n" + 
				"    static void bar1(@Nullable String s1, Iterable<String> list) {\n" + 
				"		@NonNull var s2 = s1;\n" +
				"		for (@NonNull var s : list);\n" +
				"	 }\n" + 
				"    static void bar2(int[] array) {\n" + 
				"		@NonNull var i1 = 3;\n" +
				"		for (@NonNull var s : array);\n" +
				"	 }\n" + 
				"}\n"
			},
			options,
			"----------\n" + 
			"1. ERROR in Bar.java (at line 4)\n" + 
			"	@NonNull var s2 = s1;\n" + 
			"	                  ^^\n" + 
			"Null type mismatch: required \'@NonNull String\' but the provided value is specified as @Nullable\n" + 
			"----------\n" + 
			"2. WARNING in Bar.java (at line 5)\n" + 
			"	for (@NonNull var s : list);\n" + 
			"	                      ^^^^\n" + 
			"Null type safety: The expression of type \'String\' needs unchecked conversion to conform to \'@NonNull String\'\n" + 
			"----------\n" + 
			"3. ERROR in Bar.java (at line 8)\n" + 
			"	@NonNull var i1 = 3;\n" + 
			"	^^^^^^^^\n" + 
			"The nullness annotation @NonNull is not applicable for the primitive type int\n" + 
			"----------\n" + 
			"4. ERROR in Bar.java (at line 9)\n" + 
			"	for (@NonNull var s : array);\n" + 
			"	     ^^^^^^^^\n" + 
			"The nullness annotation @NonNull is not applicable for the primitive type int\n" + 
			"----------\n");
	}
}

Back to the top