alanb@5016
|
1 |
/*
|
jlahoda@5675
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
|
alanb@5016
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
alanb@5016
|
4 |
*
|
alanb@5016
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
alanb@5016
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
alanb@5016
|
7 |
* published by the Free Software Foundation.
|
alanb@5016
|
8 |
*
|
alanb@5016
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
alanb@5016
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
alanb@5016
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
alanb@5016
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
alanb@5016
|
13 |
* accompanied this code).
|
alanb@5016
|
14 |
*
|
alanb@5016
|
15 |
* You should have received a copy of the GNU General Public License version
|
alanb@5016
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
alanb@5016
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
alanb@5016
|
18 |
*
|
alanb@5016
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
alanb@5016
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
alanb@5016
|
21 |
* questions.
|
alanb@5016
|
22 |
*/
|
alanb@5016
|
23 |
|
alanb@5016
|
24 |
/*
|
alanb@5016
|
25 |
* @test
|
jlahoda@5877
|
26 |
* @bug 8154283 8167320 8171098 8172809 8173068 8173117 8176045 8177311
|
alanb@5016
|
27 |
* @summary tests for multi-module mode compilation
|
alanb@5016
|
28 |
* @library /tools/lib
|
alanb@5016
|
29 |
* @modules
|
alanb@5016
|
30 |
* jdk.compiler/com.sun.tools.javac.api
|
alanb@5016
|
31 |
* jdk.compiler/com.sun.tools.javac.code
|
alanb@5016
|
32 |
* jdk.compiler/com.sun.tools.javac.main
|
jlahoda@5694
|
33 |
* jdk.compiler/com.sun.tools.javac.processing
|
jlahoda@5677
|
34 |
* jdk.compiler/com.sun.tools.javac.util
|
jjg@5036
|
35 |
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase
|
alanb@5016
|
36 |
* @run main EdgeCases
|
alanb@5016
|
37 |
*/
|
alanb@5016
|
38 |
|
jlahoda@5833
|
39 |
import java.io.BufferedWriter;
|
alanb@5016
|
40 |
import java.io.Writer;
|
alanb@5016
|
41 |
import java.nio.file.Files;
|
alanb@5016
|
42 |
import java.nio.file.Path;
|
alanb@5016
|
43 |
import java.nio.file.Paths;
|
alanb@5016
|
44 |
import java.util.Arrays;
|
alanb@5016
|
45 |
import java.util.HashSet;
|
alanb@5016
|
46 |
import java.util.List;
|
alanb@5016
|
47 |
import java.util.Objects;
|
alanb@5016
|
48 |
import java.util.Set;
|
alanb@5016
|
49 |
|
jlahoda@5677
|
50 |
import javax.annotation.processing.AbstractProcessor;
|
jlahoda@5677
|
51 |
import javax.annotation.processing.RoundEnvironment;
|
jlahoda@5677
|
52 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
jlahoda@5677
|
53 |
import javax.annotation.processing.SupportedOptions;
|
jlahoda@5677
|
54 |
import javax.lang.model.SourceVersion;
|
alanb@5016
|
55 |
import javax.lang.model.element.Element;
|
jlahoda@5677
|
56 |
import javax.lang.model.element.ModuleElement;
|
jlahoda@5677
|
57 |
import javax.lang.model.element.ModuleElement.RequiresDirective;
|
jlahoda@5694
|
58 |
import javax.lang.model.element.PackageElement;
|
jlahoda@5677
|
59 |
import javax.lang.model.element.TypeElement;
|
jlahoda@5677
|
60 |
import javax.lang.model.util.ElementFilter;
|
jlahoda@5694
|
61 |
import javax.lang.model.util.Elements;
|
alanb@5016
|
62 |
import javax.tools.JavaCompiler;
|
alanb@5016
|
63 |
import javax.tools.JavaFileObject;
|
alanb@5016
|
64 |
import javax.tools.StandardJavaFileManager;
|
alanb@5016
|
65 |
import javax.tools.ToolProvider;
|
alanb@5016
|
66 |
|
alanb@5016
|
67 |
import com.sun.source.tree.CompilationUnitTree;
|
jjg@5036
|
68 |
//import com.sun.source.util.JavacTask; // conflicts with toolbox.JavacTask
|
alanb@5016
|
69 |
import com.sun.tools.javac.api.JavacTaskImpl;
|
alanb@5016
|
70 |
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
|
jlahoda@5446
|
71 |
import com.sun.tools.javac.code.Symtab;
|
alanb@5016
|
72 |
|
jjg@5036
|
73 |
import toolbox.JarTask;
|
jjg@5036
|
74 |
import toolbox.JavacTask;
|
jjg@5036
|
75 |
import toolbox.Task;
|
dbalek@5253
|
76 |
import toolbox.Task.Expect;
|
dbalek@5253
|
77 |
import toolbox.Task.OutputKind;
|
jjg@5036
|
78 |
|
alanb@5016
|
79 |
public class EdgeCases extends ModuleTestBase {
|
alanb@5016
|
80 |
|
alanb@5016
|
81 |
public static void main(String... args) throws Exception {
|
alanb@5016
|
82 |
new EdgeCases().runTests();
|
alanb@5016
|
83 |
}
|
alanb@5016
|
84 |
|
alanb@5016
|
85 |
@Test
|
jjg@5095
|
86 |
public void testAddExportUndefinedModule(Path base) throws Exception {
|
alanb@5016
|
87 |
Path src = base.resolve("src");
|
jjg@5505
|
88 |
tb.writeJavaFiles(src, "package test; import undefPackage.Any; public class Test {}");
|
alanb@5016
|
89 |
Path classes = base.resolve("classes");
|
alanb@5016
|
90 |
tb.createDirectories(classes);
|
alanb@5016
|
91 |
|
jjg@5036
|
92 |
List<String> log = new JavacTask(tb)
|
jjg@5505
|
93 |
.options("--add-exports", "undefModule/undefPackage=ALL-UNNAMED",
|
jjg@5505
|
94 |
"-XDrawDiagnostics")
|
alanb@5016
|
95 |
.outdir(classes)
|
alanb@5016
|
96 |
.files(findJavaFiles(src))
|
jjg@5036
|
97 |
.run(Task.Expect.FAIL)
|
alanb@5016
|
98 |
.writeAll()
|
jjg@5036
|
99 |
.getOutputLines(Task.OutputKind.DIRECT);
|
alanb@5016
|
100 |
|
jjg@5505
|
101 |
List<String> expected = Arrays.asList("- compiler.warn.module.for.option.not.found: --add-exports, undefModule",
|
jjg@5505
|
102 |
"Test.java:1:34: compiler.err.doesnt.exist: undefPackage",
|
jjg@5505
|
103 |
"1 error", "1 warning");
|
alanb@5016
|
104 |
|
alanb@5016
|
105 |
if (!expected.equals(log))
|
alanb@5016
|
106 |
throw new Exception("expected output not found: " + log);
|
alanb@5016
|
107 |
}
|
alanb@5016
|
108 |
|
alanb@5016
|
109 |
@Test
|
jjg@5095
|
110 |
public void testModuleSymbolOutterMostClass(Path base) throws Exception {
|
alanb@5016
|
111 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
alanb@5016
|
112 |
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
|
alanb@5016
|
113 |
Path moduleSrc = base.resolve("module-src");
|
alanb@5580
|
114 |
Path m1 = moduleSrc.resolve("m1x");
|
alanb@5016
|
115 |
|
alanb@5580
|
116 |
tb.writeJavaFiles(m1, "module m1x { }");
|
alanb@5016
|
117 |
|
alanb@5016
|
118 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
|
jjg@5036
|
119 |
com.sun.source.util.JavacTask task =
|
jjg@5036
|
120 |
(com.sun.source.util.JavacTask) compiler.getTask(null, fm, null, null, null, files);
|
alanb@5016
|
121 |
|
alanb@5016
|
122 |
task.analyze();
|
alanb@5016
|
123 |
|
alanb@5580
|
124 |
ModuleSymbol msym = (ModuleSymbol) task.getElements().getModuleElement("m1x");
|
alanb@5016
|
125 |
|
alanb@5016
|
126 |
msym.outermostClass();
|
alanb@5016
|
127 |
}
|
alanb@5016
|
128 |
}
|
alanb@5016
|
129 |
|
alanb@5016
|
130 |
@Test
|
jjg@5095
|
131 |
public void testParseEnterAnalyze(Path base) throws Exception {
|
alanb@5016
|
132 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
alanb@5016
|
133 |
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
|
alanb@5016
|
134 |
Path moduleSrc = base.resolve("module-src");
|
alanb@5580
|
135 |
Path m1 = moduleSrc.resolve("m1x");
|
alanb@5016
|
136 |
|
alanb@5580
|
137 |
tb.writeJavaFiles(m1, "module m1x { }",
|
alanb@5016
|
138 |
"package p;",
|
alanb@5016
|
139 |
"package p; class T { }");
|
alanb@5016
|
140 |
|
alanb@5016
|
141 |
Path classes = base.resolve("classes");
|
alanb@5016
|
142 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc));
|
alanb@5016
|
143 |
List<String> options = Arrays.asList("-d", classes.toString(), "-Xpkginfo:always");
|
alanb@5016
|
144 |
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, fm, null, options, null, files);
|
alanb@5016
|
145 |
|
alanb@5016
|
146 |
Iterable<? extends CompilationUnitTree> parsed = task.parse();
|
alanb@5016
|
147 |
Iterable<? extends Element> entered = task.enter(parsed);
|
alanb@5016
|
148 |
Iterable<? extends Element> analyzed = task.analyze(entered);
|
alanb@5016
|
149 |
Iterable<? extends JavaFileObject> generatedFiles = task.generate(analyzed);
|
alanb@5016
|
150 |
|
alanb@5016
|
151 |
Set<String> generated = new HashSet<>();
|
alanb@5016
|
152 |
|
alanb@5016
|
153 |
for (JavaFileObject jfo : generatedFiles) {
|
alanb@5016
|
154 |
generated.add(jfo.getName());
|
alanb@5016
|
155 |
}
|
alanb@5016
|
156 |
|
alanb@5016
|
157 |
Set<String> expected = new HashSet<>(
|
alanb@5016
|
158 |
Arrays.asList(Paths.get("testParseEnterAnalyze", "classes", "p", "package-info.class").toString(),
|
alanb@5016
|
159 |
Paths.get("testParseEnterAnalyze", "classes", "module-info.class").toString(),
|
alanb@5016
|
160 |
Paths.get("testParseEnterAnalyze", "classes", "p", "T.class").toString())
|
alanb@5016
|
161 |
);
|
alanb@5016
|
162 |
|
alanb@5016
|
163 |
if (!Objects.equals(expected, generated))
|
alanb@5016
|
164 |
throw new AssertionError("Incorrect generated files: " + generated);
|
alanb@5016
|
165 |
}
|
alanb@5016
|
166 |
}
|
alanb@5016
|
167 |
|
alanb@5016
|
168 |
@Test
|
jjg@5095
|
169 |
public void testModuleImplicitModuleBoundaries(Path base) throws Exception {
|
alanb@5016
|
170 |
Path src = base.resolve("src");
|
alanb@5580
|
171 |
Path src_m1 = src.resolve("m1x");
|
alanb@5016
|
172 |
tb.writeJavaFiles(src_m1,
|
alanb@5580
|
173 |
"module m1x { exports api1; }",
|
alanb@5016
|
174 |
"package api1; public class Api1 { public void call() { } }");
|
alanb@5580
|
175 |
Path src_m2 = src.resolve("m2x");
|
alanb@5016
|
176 |
tb.writeJavaFiles(src_m2,
|
alanb@5580
|
177 |
"module m2x { requires m1x; exports api2; }",
|
alanb@5016
|
178 |
"package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
|
alanb@5580
|
179 |
Path src_m3 = src.resolve("m3x");
|
alanb@5016
|
180 |
tb.writeJavaFiles(src_m3,
|
alanb@5580
|
181 |
"module m3x { requires m2x; }",
|
alanb@5016
|
182 |
"package test; public class Test { { api2.Api2.get().call(); api2.Api2.get().toString(); } }");
|
alanb@5016
|
183 |
Path classes = base.resolve("classes");
|
alanb@5016
|
184 |
tb.createDirectories(classes);
|
alanb@5016
|
185 |
|
jjg@5036
|
186 |
String log = new JavacTask(tb)
|
alanb@5016
|
187 |
.options("-XDrawDiagnostics",
|
jjg@5307
|
188 |
"--module-source-path", src.toString())
|
alanb@5016
|
189 |
.outdir(classes)
|
alanb@5016
|
190 |
.files(findJavaFiles(src))
|
jjg@5036
|
191 |
.run(Task.Expect.FAIL)
|
alanb@5016
|
192 |
.writeAll()
|
jjg@5036
|
193 |
.getOutput(Task.OutputKind.DIRECT);
|
alanb@5016
|
194 |
|
jlahoda@5650
|
195 |
if (!log.contains("Test.java:1:52: compiler.err.not.def.access.class.intf.cant.access.reason: call(), api1.Api1, api1, (compiler.misc.not.def.access.does.not.read: m3x, api1, m1x)") ||
|
alanb@5016
|
196 |
!log.contains("Test.java:1:76: compiler.err.not.def.access.class.intf.cant.access: toString(), java.lang.Object"))
|
alanb@5016
|
197 |
throw new Exception("expected output not found");
|
alanb@5016
|
198 |
}
|
alanb@5016
|
199 |
|
alanb@5016
|
200 |
@Test
|
jjg@5095
|
201 |
public void testAssignClassToAutomaticModule(Path base) throws Exception {
|
alanb@5016
|
202 |
//check that if a ClassSymbol belongs to an automatic module, it is properly assigned and not
|
alanb@5016
|
203 |
//duplicated when being accessed through a classfile.
|
alanb@5016
|
204 |
Path automaticSrc = base.resolve("automaticSrc");
|
alanb@5016
|
205 |
tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}");
|
alanb@5016
|
206 |
Path automaticClasses = base.resolve("automaticClasses");
|
alanb@5016
|
207 |
tb.createDirectories(automaticClasses);
|
alanb@5016
|
208 |
|
jjg@5036
|
209 |
String automaticLog = new JavacTask(tb)
|
alanb@5016
|
210 |
.outdir(automaticClasses)
|
alanb@5016
|
211 |
.files(findJavaFiles(automaticSrc))
|
alanb@5016
|
212 |
.run()
|
alanb@5016
|
213 |
.writeAll()
|
jjg@5036
|
214 |
.getOutput(Task.OutputKind.DIRECT);
|
alanb@5016
|
215 |
|
alanb@5016
|
216 |
if (!automaticLog.isEmpty())
|
alanb@5016
|
217 |
throw new Exception("expected output not found: " + automaticLog);
|
alanb@5016
|
218 |
|
alanb@5016
|
219 |
Path modulePath = base.resolve("module-path");
|
alanb@5016
|
220 |
|
alanb@5016
|
221 |
Files.createDirectories(modulePath);
|
alanb@5016
|
222 |
|
alanb@5550
|
223 |
Path automaticJar = modulePath.resolve("a-1.0.jar");
|
alanb@5016
|
224 |
|
jjg@5036
|
225 |
new JarTask(tb, automaticJar)
|
alanb@5016
|
226 |
.baseDir(automaticClasses)
|
alanb@5016
|
227 |
.files("api1/Api1.class")
|
alanb@5016
|
228 |
.run();
|
alanb@5016
|
229 |
|
alanb@5016
|
230 |
Path src = base.resolve("src");
|
alanb@5580
|
231 |
Path src_m2 = src.resolve("m2x");
|
alanb@5016
|
232 |
tb.writeJavaFiles(src_m2,
|
alanb@5580
|
233 |
"module m2x { requires a; exports api2; }",
|
alanb@5016
|
234 |
"package api2; public class Api2 { public static api1.Api1 get() { return null; } }");
|
alanb@5580
|
235 |
Path src_m3 = src.resolve("m3x");
|
alanb@5016
|
236 |
tb.writeJavaFiles(src_m3,
|
alanb@5580
|
237 |
"module m3x { requires a; requires m2x; }",
|
alanb@5016
|
238 |
"package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }");
|
alanb@5016
|
239 |
Path classes = base.resolve("classes");
|
alanb@5016
|
240 |
tb.createDirectories(classes);
|
alanb@5016
|
241 |
|
jjg@5036
|
242 |
new JavacTask(tb)
|
jjg@5307
|
243 |
.options("--module-path", modulePath.toString(),
|
jjg@5307
|
244 |
"--module-source-path", src.toString())
|
alanb@5016
|
245 |
.outdir(classes)
|
alanb@5016
|
246 |
.files(findJavaFiles(src_m2))
|
alanb@5016
|
247 |
.run()
|
alanb@5016
|
248 |
.writeAll();
|
alanb@5016
|
249 |
|
jjg@5036
|
250 |
new JavacTask(tb)
|
jjg@5307
|
251 |
.options("--module-path", modulePath.toString(),
|
jjg@5307
|
252 |
"--module-source-path", src.toString())
|
alanb@5016
|
253 |
.outdir(classes)
|
alanb@5016
|
254 |
.files(findJavaFiles(src_m3))
|
alanb@5016
|
255 |
.run()
|
alanb@5016
|
256 |
.writeAll();
|
alanb@5016
|
257 |
}
|
alanb@5016
|
258 |
|
alanb@5016
|
259 |
@Test
|
jjg@5095
|
260 |
public void testEmptyImplicitModuleInfo(Path base) throws Exception {
|
alanb@5016
|
261 |
Path src = base.resolve("src");
|
alanb@5580
|
262 |
Path src_m1 = src.resolve("m1x");
|
alanb@5016
|
263 |
Files.createDirectories(src_m1);
|
alanb@5016
|
264 |
try (Writer w = Files.newBufferedWriter(src_m1.resolve("module-info.java"))) {}
|
alanb@5016
|
265 |
tb.writeJavaFiles(src_m1,
|
alanb@5016
|
266 |
"package test; public class Test {}");
|
alanb@5016
|
267 |
Path classes = base.resolve("classes");
|
alanb@5016
|
268 |
tb.createDirectories(classes);
|
alanb@5016
|
269 |
|
jlahoda@5832
|
270 |
List<String> log = new JavacTask(tb)
|
jjg@5307
|
271 |
.options("--source-path", src_m1.toString(),
|
alanb@5016
|
272 |
"-XDrawDiagnostics")
|
alanb@5016
|
273 |
.outdir(classes)
|
alanb@5016
|
274 |
.files(findJavaFiles(src_m1.resolve("test")))
|
jjg@5036
|
275 |
.run(Task.Expect.FAIL)
|
jlahoda@5832
|
276 |
.writeAll()
|
jlahoda@5832
|
277 |
.getOutputLines(OutputKind.DIRECT);
|
jlahoda@5832
|
278 |
|
jlahoda@5832
|
279 |
List<String> expected = Arrays.asList(
|
jlahoda@5832
|
280 |
"- compiler.err.cant.access: module-info, (compiler.misc.bad.source.file.header: module-info.java, (compiler.misc.file.does.not.contain.module))",
|
jlahoda@5832
|
281 |
"1 error");
|
jlahoda@5832
|
282 |
|
jlahoda@5832
|
283 |
if (!expected.equals(log)) {
|
jlahoda@5832
|
284 |
throw new AssertionError("Unexpected output: " + log);
|
jlahoda@5832
|
285 |
}
|
alanb@5016
|
286 |
|
alanb@5016
|
287 |
tb.writeJavaFiles(src_m1,
|
alanb@5580
|
288 |
"module m1x {}");
|
alanb@5016
|
289 |
|
jjg@5036
|
290 |
new JavacTask(tb)
|
jjg@5307
|
291 |
.options("--source-path", src_m1.toString())
|
alanb@5016
|
292 |
.outdir(classes)
|
alanb@5016
|
293 |
.files(findJavaFiles(src_m1.resolve("test")))
|
alanb@5016
|
294 |
.run()
|
alanb@5016
|
295 |
.writeAll();
|
alanb@5016
|
296 |
|
alanb@5016
|
297 |
}
|
alanb@5016
|
298 |
|
jlahoda@5081
|
299 |
@Test
|
jjg@5095
|
300 |
public void testClassPackageClash(Path base) throws Exception {
|
jlahoda@5081
|
301 |
Path src = base.resolve("src");
|
alanb@5580
|
302 |
Path src_m1 = src.resolve("m1x");
|
jlahoda@5081
|
303 |
tb.writeJavaFiles(src_m1,
|
alanb@5580
|
304 |
"module m1x { exports test.m1x; }",
|
alanb@5580
|
305 |
"package test.m1x;\n" +
|
jlahoda@5081
|
306 |
"public class Test {}\n");
|
alanb@5580
|
307 |
Path src_m2 = src.resolve("m2x");
|
jlahoda@5081
|
308 |
tb.writeJavaFiles(src_m2,
|
alanb@5580
|
309 |
"module m2x { requires m1x; }",
|
jlahoda@5081
|
310 |
"package test;\n" +
|
alanb@5580
|
311 |
"public class m1x {}\n");
|
jlahoda@5081
|
312 |
Path classes = base.resolve("classes");
|
jlahoda@5081
|
313 |
tb.createDirectories(classes);
|
jlahoda@5081
|
314 |
|
jlahoda@5081
|
315 |
List<String> log = new JavacTask(tb)
|
jjg@5307
|
316 |
.options("--module-source-path", src.toString(),
|
jlahoda@5081
|
317 |
"-XDrawDiagnostics")
|
jlahoda@5081
|
318 |
.outdir(classes)
|
jlahoda@5081
|
319 |
.files(findJavaFiles(src))
|
jlahoda@5081
|
320 |
.run(Task.Expect.FAIL)
|
jlahoda@5081
|
321 |
.writeAll()
|
jlahoda@5081
|
322 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5081
|
323 |
|
jlahoda@5081
|
324 |
List<String> expected = Arrays.asList(
|
alanb@5580
|
325 |
"m1x.java:2:8: compiler.err.clash.with.pkg.of.same.name: kindname.class, test.m1x",
|
jlahoda@5081
|
326 |
"1 error"
|
jlahoda@5081
|
327 |
);
|
jlahoda@5081
|
328 |
|
jlahoda@5081
|
329 |
if (!expected.equals(log)) {
|
jlahoda@5081
|
330 |
throw new IllegalStateException(log.toString());
|
jlahoda@5081
|
331 |
}
|
jlahoda@5081
|
332 |
}
|
jlahoda@5081
|
333 |
|
dbalek@5253
|
334 |
@Test
|
dbalek@5253
|
335 |
public void testImplicitJavaBase(Path base) throws Exception {
|
dbalek@5253
|
336 |
Path src = base.resolve("src");
|
dbalek@5253
|
337 |
Path src_java_base = src.resolve("java.base");
|
dbalek@5253
|
338 |
Files.createDirectories(src_java_base);
|
dbalek@5253
|
339 |
tb.writeJavaFiles(src_java_base, "module java.base { exports java.lang; }");
|
dbalek@5253
|
340 |
tb.writeJavaFiles(src_java_base,
|
dbalek@5253
|
341 |
"package java.lang; public class Object {}");
|
dbalek@5253
|
342 |
Path classes = base.resolve("classes");
|
dbalek@5253
|
343 |
tb.createDirectories(classes);
|
dbalek@5253
|
344 |
|
dbalek@5253
|
345 |
//module-info from source:
|
dbalek@5253
|
346 |
new JavacTask(tb)
|
dbalek@5253
|
347 |
.options("-sourcepath", src_java_base.toString())
|
dbalek@5253
|
348 |
.outdir(classes)
|
dbalek@5253
|
349 |
.files(findJavaFiles(src_java_base.resolve("java").resolve("lang").resolve("Object.java")))
|
dbalek@5253
|
350 |
.run()
|
dbalek@5253
|
351 |
.writeAll();
|
dbalek@5253
|
352 |
|
dbalek@5253
|
353 |
//module-info from class:
|
dbalek@5253
|
354 |
if (!Files.exists(classes.resolve("module-info.class"))) {
|
dbalek@5253
|
355 |
throw new AssertionError("module-info.class not created!");
|
dbalek@5253
|
356 |
}
|
dbalek@5253
|
357 |
|
dbalek@5253
|
358 |
new JavacTask(tb)
|
dbalek@5253
|
359 |
.outdir(classes)
|
dbalek@5253
|
360 |
.files(findJavaFiles(src_java_base.resolve("java").resolve("lang").resolve("Object.java")))
|
dbalek@5253
|
361 |
.run()
|
dbalek@5253
|
362 |
.writeAll();
|
dbalek@5253
|
363 |
|
dbalek@5253
|
364 |
//broken module-info.class:
|
dbalek@5253
|
365 |
Files.newOutputStream(classes.resolve("module-info.class")).close();
|
dbalek@5253
|
366 |
|
dbalek@5253
|
367 |
List<String> log = new JavacTask(tb)
|
dbalek@5253
|
368 |
.options("-XDrawDiagnostics")
|
dbalek@5253
|
369 |
.outdir(classes)
|
dbalek@5253
|
370 |
.files(findJavaFiles(src_java_base.resolve("java").resolve("lang").resolve("Object.java")))
|
dbalek@5253
|
371 |
.run(Expect.FAIL)
|
dbalek@5253
|
372 |
.writeAll()
|
dbalek@5253
|
373 |
.getOutputLines(OutputKind.DIRECT);
|
dbalek@5253
|
374 |
|
dbalek@5253
|
375 |
List<String> expected = Arrays.asList(
|
dbalek@5253
|
376 |
"- compiler.err.cant.access: <error>.module-info, (compiler.misc.bad.class.file.header: module-info.class, (compiler.misc.illegal.start.of.class.file))",
|
dbalek@5253
|
377 |
"1 error");
|
dbalek@5253
|
378 |
|
dbalek@5253
|
379 |
if (!expected.equals(log)) {
|
dbalek@5253
|
380 |
throw new AssertionError("Unexpected output: " + log);
|
dbalek@5253
|
381 |
}
|
dbalek@5253
|
382 |
|
jlahoda@5420
|
383 |
//broken module-info.java:
|
jlahoda@5420
|
384 |
Files.delete(classes.resolve("module-info.class"));
|
jlahoda@5420
|
385 |
|
jlahoda@5420
|
386 |
try (Writer out = Files.newBufferedWriter(src_java_base.resolve("module-info.java"))) {
|
jlahoda@5420
|
387 |
out.write("class Broken {}");
|
jlahoda@5420
|
388 |
}
|
jlahoda@5420
|
389 |
|
jlahoda@5420
|
390 |
log = new JavacTask(tb)
|
jlahoda@5420
|
391 |
.options("-sourcepath", src_java_base.toString(),
|
jlahoda@5420
|
392 |
"-XDrawDiagnostics")
|
jlahoda@5420
|
393 |
.outdir(classes)
|
jlahoda@5420
|
394 |
.files(findJavaFiles(src_java_base.resolve("java").resolve("lang").resolve("Object.java")))
|
jlahoda@5420
|
395 |
.run(Expect.FAIL)
|
jlahoda@5420
|
396 |
.writeAll()
|
jlahoda@5420
|
397 |
.getOutputLines(OutputKind.DIRECT);
|
jlahoda@5420
|
398 |
|
jlahoda@5420
|
399 |
expected = Arrays.asList("X");
|
jlahoda@5420
|
400 |
|
jlahoda@5420
|
401 |
if (expected.equals(log)) {
|
jlahoda@5420
|
402 |
throw new AssertionError("Unexpected output: " + log);
|
jlahoda@5420
|
403 |
}
|
jlahoda@5420
|
404 |
}
|
jlahoda@5420
|
405 |
|
jlahoda@5420
|
406 |
@Test
|
jlahoda@5420
|
407 |
public void testModuleInfoNameMismatchSource(Path base) throws Exception {
|
jlahoda@5420
|
408 |
Path src = base.resolve("src");
|
alanb@5580
|
409 |
Path m1 = src.resolve("m1x");
|
jlahoda@5420
|
410 |
Files.createDirectories(m1);
|
jlahoda@5420
|
411 |
tb.writeJavaFiles(m1, "module other { }",
|
jlahoda@5420
|
412 |
"package test; public class Test {}");
|
jlahoda@5420
|
413 |
Path classes = base.resolve("classes");
|
jlahoda@5420
|
414 |
tb.createDirectories(classes);
|
jlahoda@5420
|
415 |
|
jlahoda@5420
|
416 |
List<String> log = new JavacTask(tb)
|
jlahoda@5420
|
417 |
.options("--module-source-path", src.toString(),
|
jlahoda@5420
|
418 |
"-XDrawDiagnostics")
|
jlahoda@5420
|
419 |
.outdir(classes)
|
jlahoda@5420
|
420 |
.files(findJavaFiles(m1.resolve("test").resolve("Test.java")))
|
jlahoda@5420
|
421 |
.run(Expect.FAIL)
|
jlahoda@5420
|
422 |
.writeAll()
|
jlahoda@5420
|
423 |
.getOutputLines(OutputKind.DIRECT);
|
jlahoda@5420
|
424 |
|
jlahoda@5420
|
425 |
List<String> expected = Arrays.asList(
|
alanb@5580
|
426 |
"module-info.java:1:1: compiler.err.module.name.mismatch: other, m1x",
|
alanb@5580
|
427 |
"- compiler.err.cant.access: m1x.module-info, (compiler.misc.cant.resolve.modules)",
|
jlahoda@5420
|
428 |
"2 errors");
|
jlahoda@5420
|
429 |
|
jlahoda@5420
|
430 |
if (!expected.equals(log)) {
|
jlahoda@5420
|
431 |
throw new AssertionError("Unexpected output: " + log);
|
jlahoda@5420
|
432 |
}
|
jlahoda@5420
|
433 |
}
|
jlahoda@5420
|
434 |
|
jlahoda@5420
|
435 |
@Test
|
jlahoda@5420
|
436 |
public void testModuleInfoNameMismatchClass(Path base) throws Exception {
|
jlahoda@5420
|
437 |
Path src = base.resolve("src");
|
jlahoda@5420
|
438 |
Files.createDirectories(src);
|
jlahoda@5420
|
439 |
tb.writeJavaFiles(src, "module other { }",
|
jlahoda@5420
|
440 |
"package test; public class Test {}");
|
jlahoda@5420
|
441 |
Path classes = base.resolve("classes");
|
alanb@5580
|
442 |
Path m1Classes = classes.resolve("m1x");
|
jlahoda@5420
|
443 |
tb.createDirectories(m1Classes);
|
jlahoda@5420
|
444 |
|
jlahoda@5420
|
445 |
new JavacTask(tb)
|
jlahoda@5420
|
446 |
.outdir(m1Classes)
|
jlahoda@5420
|
447 |
.files(findJavaFiles(src))
|
jlahoda@5420
|
448 |
.run()
|
jlahoda@5420
|
449 |
.writeAll()
|
jlahoda@5420
|
450 |
.getOutputLines(OutputKind.DIRECT);
|
jlahoda@5420
|
451 |
|
jlahoda@5420
|
452 |
Path src2 = base.resolve("src2");
|
jlahoda@5420
|
453 |
Files.createDirectories(src2);
|
alanb@5580
|
454 |
tb.writeJavaFiles(src2, "module use { requires m1x; }");
|
jlahoda@5420
|
455 |
|
jlahoda@5420
|
456 |
Path classes2 = base.resolve("classes2");
|
jlahoda@5420
|
457 |
tb.createDirectories(classes2);
|
jlahoda@5420
|
458 |
|
jlahoda@5420
|
459 |
List<String> log = new JavacTask(tb)
|
jlahoda@5420
|
460 |
.options("--module-path", classes.toString(),
|
jlahoda@5420
|
461 |
"-XDrawDiagnostics")
|
jlahoda@5420
|
462 |
.outdir(classes2)
|
jlahoda@5420
|
463 |
.files(findJavaFiles(src2))
|
jlahoda@5420
|
464 |
.run(Expect.FAIL)
|
jlahoda@5420
|
465 |
.writeAll()
|
jlahoda@5420
|
466 |
.getOutputLines(OutputKind.DIRECT);
|
jlahoda@5420
|
467 |
|
jlahoda@5420
|
468 |
List<String> expected = Arrays.asList(
|
alanb@5580
|
469 |
"- compiler.err.cant.access: m1x.module-info, (compiler.misc.bad.class.file.header: module-info.class, (compiler.misc.module.name.mismatch: other, m1x))",
|
dbalek@5955
|
470 |
"module-info.java:1:1: compiler.err.module.not.found: m1x",
|
dbalek@5955
|
471 |
"2 errors");
|
jlahoda@5420
|
472 |
|
jlahoda@5420
|
473 |
if (!expected.equals(log)) {
|
jlahoda@5420
|
474 |
throw new AssertionError("Unexpected output: " + log);
|
jlahoda@5420
|
475 |
}
|
dbalek@5253
|
476 |
}
|
dbalek@5253
|
477 |
|
jlahoda@5446
|
478 |
@Test
|
jlahoda@5446
|
479 |
public void testGetDirectivesComplete(Path base) throws Exception {
|
jlahoda@5446
|
480 |
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
jlahoda@5446
|
481 |
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, null, null, null);
|
jlahoda@5446
|
482 |
Symtab syms = Symtab.instance(task.getContext());
|
jlahoda@5446
|
483 |
|
jlahoda@5446
|
484 |
syms.java_base.getDirectives();
|
jlahoda@5446
|
485 |
}
|
alanb@5550
|
486 |
|
alanb@5550
|
487 |
@Test
|
alanb@5550
|
488 |
public void testPackageInModuleInfo(Path base) throws Exception {
|
alanb@5550
|
489 |
Path src = base.resolve("src");
|
alanb@5550
|
490 |
Files.createDirectories(src);
|
alanb@5550
|
491 |
tb.writeJavaFiles(src, "package p; module foo { }");
|
alanb@5550
|
492 |
Path classes = base.resolve("classes");
|
alanb@5550
|
493 |
tb.createDirectories(classes);
|
alanb@5550
|
494 |
|
alanb@5550
|
495 |
List<String> log = new JavacTask(tb)
|
alanb@5550
|
496 |
.options("-XDrawDiagnostics", "-XDshould-stop.ifError=FLOW")
|
alanb@5550
|
497 |
.outdir(classes)
|
alanb@5550
|
498 |
.files(findJavaFiles(src))
|
alanb@5550
|
499 |
.run(Expect.FAIL)
|
alanb@5550
|
500 |
.writeAll()
|
alanb@5550
|
501 |
.getOutputLines(OutputKind.DIRECT);
|
alanb@5550
|
502 |
|
alanb@5550
|
503 |
List<String> expected = Arrays.asList(
|
alanb@5550
|
504 |
"module-info.java:1:1: compiler.err.no.pkg.in.module-info.java",
|
alanb@5550
|
505 |
"1 error");
|
alanb@5550
|
506 |
|
alanb@5550
|
507 |
if (!expected.equals(log)) {
|
alanb@5550
|
508 |
throw new AssertionError("Unexpected output: " + log);
|
alanb@5550
|
509 |
}
|
alanb@5550
|
510 |
}
|
jlahoda@5675
|
511 |
|
jlahoda@5675
|
512 |
@Test
|
jlahoda@5678
|
513 |
public void testInvisibleClassVisiblePackageClash(Path base) throws Exception {
|
jlahoda@5678
|
514 |
Path src = base.resolve("src");
|
jlahoda@5678
|
515 |
Path src_m1 = src.resolve("m1x");
|
jlahoda@5678
|
516 |
tb.writeJavaFiles(src_m1,
|
jlahoda@5678
|
517 |
"module m1x { }",
|
jlahoda@5678
|
518 |
"package m1x;\n" +
|
jlahoda@5678
|
519 |
"import m1x.a.*; public class Test { A a; }\n",
|
jlahoda@5678
|
520 |
"package m1x.a;\n" +
|
jlahoda@5678
|
521 |
"public class A { }\n");
|
jlahoda@5678
|
522 |
Path src_m2 = src.resolve("m2x");
|
jlahoda@5678
|
523 |
tb.writeJavaFiles(src_m2,
|
jlahoda@5678
|
524 |
"module m2x { }",
|
jlahoda@5678
|
525 |
"package m1x;\n" +
|
jlahoda@5678
|
526 |
"public class a { public static class A { } }\n");
|
jlahoda@5678
|
527 |
Path classes = base.resolve("classes");
|
jlahoda@5678
|
528 |
tb.createDirectories(classes);
|
jlahoda@5678
|
529 |
|
jlahoda@5678
|
530 |
new JavacTask(tb)
|
jlahoda@5678
|
531 |
.options("--module-source-path", src.toString(),
|
jlahoda@5678
|
532 |
"-XDrawDiagnostics")
|
jlahoda@5678
|
533 |
.outdir(classes)
|
jlahoda@5678
|
534 |
.files(findJavaFiles(src))
|
jlahoda@5678
|
535 |
.run()
|
jlahoda@5678
|
536 |
.writeAll();
|
jlahoda@5678
|
537 |
}
|
jlahoda@5678
|
538 |
|
jlahoda@5678
|
539 |
@Test
|
jlahoda@5677
|
540 |
public void testStripUnknownRequired(Path base) throws Exception {
|
jlahoda@5677
|
541 |
Path src = base.resolve("src");
|
jlahoda@5677
|
542 |
Path src_m1 = src.resolve("m1x");
|
jlahoda@5677
|
543 |
tb.writeJavaFiles(src_m1,
|
jlahoda@5677
|
544 |
"module m1x { }");
|
jlahoda@5677
|
545 |
Path src_m2 = src.resolve("m2x");
|
jlahoda@5677
|
546 |
tb.writeJavaFiles(src_m2,
|
jlahoda@5677
|
547 |
"module m2x { }");
|
jlahoda@5677
|
548 |
Path src_m3 = src.resolve("m3x");
|
jlahoda@5677
|
549 |
tb.writeJavaFiles(src_m3,
|
jlahoda@5677
|
550 |
"module m3x { }");
|
jlahoda@5677
|
551 |
Path src_m4 = src.resolve("m4x");
|
jlahoda@5677
|
552 |
tb.writeJavaFiles(src_m4,
|
jlahoda@5677
|
553 |
"module m4x { }");
|
jlahoda@5677
|
554 |
Path src_test = src.resolve("test");
|
jlahoda@5677
|
555 |
tb.writeJavaFiles(src_test,
|
jlahoda@5677
|
556 |
"module test { requires m1x; requires m2x; requires java.base; requires m3x; requires m4x; }");
|
jlahoda@5677
|
557 |
Path src_compile = src.resolve("compile");
|
jlahoda@5677
|
558 |
tb.writeJavaFiles(src_compile,
|
jlahoda@5677
|
559 |
"module compile { exports p to test; }",
|
jlahoda@5677
|
560 |
"package p; public class Test { }");
|
jlahoda@5677
|
561 |
Path classes = base.resolve("classes");
|
jlahoda@5677
|
562 |
tb.createDirectories(classes);
|
jlahoda@5677
|
563 |
|
jlahoda@5677
|
564 |
List<String> log = new JavacTask(tb)
|
jlahoda@5677
|
565 |
.options("-processor", ListRequires.class.getName(),
|
jlahoda@5677
|
566 |
"--module-source-path", src.toString(),
|
jlahoda@5677
|
567 |
"--limit-modules", "compile",
|
jlahoda@5677
|
568 |
"-XDaccessInternalAPI=true")
|
jlahoda@5677
|
569 |
.outdir(classes)
|
jlahoda@5677
|
570 |
.files(findJavaFiles(src_compile))
|
jlahoda@5677
|
571 |
.run(Expect.FAIL)
|
jlahoda@5677
|
572 |
.writeAll()
|
jlahoda@5677
|
573 |
.getOutputLines(Task.OutputKind.STDOUT);
|
jlahoda@5677
|
574 |
|
jlahoda@5677
|
575 |
List<String> expected = Arrays.asList(
|
jlahoda@5677
|
576 |
"from directives:",
|
jlahoda@5677
|
577 |
"java.base",
|
jlahoda@5677
|
578 |
"from requires:",
|
jlahoda@5677
|
579 |
"java.base"
|
jlahoda@5677
|
580 |
);
|
jlahoda@5677
|
581 |
if (!Objects.equals(log, expected))
|
jlahoda@5677
|
582 |
throw new AssertionError("Unexpected output: " + log);
|
jlahoda@5677
|
583 |
}
|
jlahoda@5677
|
584 |
|
jlahoda@5677
|
585 |
@SupportedAnnotationTypes("*")
|
jlahoda@5677
|
586 |
@SupportedOptions("expectedEnclosedElements")
|
jlahoda@5677
|
587 |
public static final class ListRequires extends AbstractProcessor {
|
jlahoda@5677
|
588 |
|
jlahoda@5677
|
589 |
private int round;
|
jlahoda@5677
|
590 |
|
jlahoda@5677
|
591 |
@Override
|
jlahoda@5677
|
592 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jlahoda@5677
|
593 |
if (round++ == 0) {
|
jlahoda@5677
|
594 |
ModuleElement compileE = processingEnv.getElementUtils().getModuleElement("compile");
|
jlahoda@5677
|
595 |
ModuleElement testE = ElementFilter.exportsIn(compileE.getDirectives()).get(0).getTargetModules().get(0);
|
jlahoda@5677
|
596 |
|
jlahoda@5677
|
597 |
System.out.println("from directives:");
|
jlahoda@5677
|
598 |
for (RequiresDirective rd : ElementFilter.requiresIn(testE.getDirectives())) {
|
jlahoda@5677
|
599 |
System.out.println(rd.getDependency().getSimpleName());
|
jlahoda@5677
|
600 |
}
|
jlahoda@5677
|
601 |
|
jlahoda@5677
|
602 |
System.out.println("from requires:");
|
jlahoda@5677
|
603 |
for (RequiresDirective rd : ((ModuleSymbol) testE).requires) {
|
jlahoda@5677
|
604 |
System.out.println(rd.getDependency().getSimpleName());
|
jlahoda@5677
|
605 |
}
|
jlahoda@5677
|
606 |
}
|
jlahoda@5677
|
607 |
|
jlahoda@5677
|
608 |
return false;
|
jlahoda@5677
|
609 |
}
|
jlahoda@5677
|
610 |
|
jlahoda@5677
|
611 |
@Override
|
jlahoda@5677
|
612 |
public SourceVersion getSupportedSourceVersion() {
|
jlahoda@5677
|
613 |
return SourceVersion.latest();
|
jlahoda@5677
|
614 |
}
|
jlahoda@5677
|
615 |
|
jlahoda@5677
|
616 |
}
|
jlahoda@5677
|
617 |
|
jlahoda@5677
|
618 |
@Test
|
jlahoda@5675
|
619 |
public void testOnDemandCompletionModuleInfoJava(Path base) throws Exception {
|
jlahoda@5675
|
620 |
Path src = base.resolve("src");
|
jlahoda@5675
|
621 |
Path src_m1 = src.resolve("m1x");
|
jlahoda@5675
|
622 |
tb.writeJavaFiles(src_m1,
|
jlahoda@5675
|
623 |
"@Deprecated module m1x { }");
|
jlahoda@5675
|
624 |
Path src_m2 = src.resolve("m2x");
|
jlahoda@5675
|
625 |
tb.writeJavaFiles(src_m2,
|
jlahoda@5675
|
626 |
"module m2x { requires m1x; }");
|
jlahoda@5675
|
627 |
Path src_m3 = src.resolve("m3x");
|
jlahoda@5675
|
628 |
tb.writeJavaFiles(src_m3,
|
jlahoda@5675
|
629 |
"module m3x { requires m2x; requires m1x; }");
|
jlahoda@5675
|
630 |
Path classes = base.resolve("classes");
|
jlahoda@5675
|
631 |
tb.createDirectories(classes);
|
jlahoda@5675
|
632 |
|
jlahoda@5675
|
633 |
List<String> log;
|
jlahoda@5675
|
634 |
List<String> expected;
|
jlahoda@5675
|
635 |
|
jlahoda@5675
|
636 |
log = new JavacTask(tb)
|
jlahoda@5675
|
637 |
.options("--module-source-path", src.toString())
|
jlahoda@5675
|
638 |
.outdir(classes)
|
jlahoda@5675
|
639 |
.files(findJavaFiles(src_m1))
|
jlahoda@5675
|
640 |
.run()
|
jlahoda@5675
|
641 |
.writeAll()
|
jlahoda@5675
|
642 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5675
|
643 |
|
jlahoda@5675
|
644 |
expected = Arrays.asList("");
|
jlahoda@5675
|
645 |
|
jlahoda@5675
|
646 |
if (!expected.equals(log)) {
|
jlahoda@5675
|
647 |
throw new IllegalStateException(log.toString());
|
jlahoda@5675
|
648 |
}
|
jlahoda@5675
|
649 |
|
jlahoda@5675
|
650 |
log = new JavacTask(tb)
|
jlahoda@5675
|
651 |
.options("--module-source-path", src.toString(),
|
jlahoda@5675
|
652 |
"-XDrawDiagnostics",
|
jlahoda@5675
|
653 |
"-Xlint:deprecation")
|
jlahoda@5675
|
654 |
.outdir(classes)
|
jlahoda@5675
|
655 |
.files(findJavaFiles(src_m3))
|
jlahoda@5675
|
656 |
.run()
|
jlahoda@5675
|
657 |
.writeAll()
|
jlahoda@5675
|
658 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5675
|
659 |
|
jlahoda@5675
|
660 |
expected = Arrays.asList(
|
jlahoda@5675
|
661 |
"module-info.java:1:23: compiler.warn.has.been.deprecated.module: m1x",
|
jlahoda@5675
|
662 |
"module-info.java:1:37: compiler.warn.has.been.deprecated.module: m1x",
|
jlahoda@5675
|
663 |
"2 warnings"
|
jlahoda@5675
|
664 |
);
|
jlahoda@5675
|
665 |
|
jlahoda@5675
|
666 |
if (!expected.equals(log)) {
|
jlahoda@5675
|
667 |
throw new IllegalStateException(log.toString());
|
jlahoda@5675
|
668 |
}
|
jlahoda@5675
|
669 |
}
|
jlahoda@5675
|
670 |
|
jlahoda@5694
|
671 |
@Test
|
jlahoda@5694
|
672 |
public void testUnnamedPackage(Path base) throws Exception {
|
jlahoda@5694
|
673 |
List<String> out;
|
jlahoda@5694
|
674 |
List<String> expected;
|
jlahoda@5694
|
675 |
|
jlahoda@5694
|
676 |
//-source 8:
|
jlahoda@5694
|
677 |
Path src8 = base.resolve("src8");
|
jlahoda@5694
|
678 |
Files.createDirectories(src8);
|
jlahoda@5694
|
679 |
tb.writeJavaFiles(src8,
|
jlahoda@5694
|
680 |
"package test; public class Test {}");
|
jlahoda@5694
|
681 |
Path classes = base.resolve("classes");
|
jlahoda@5694
|
682 |
tb.createDirectories(classes);
|
jlahoda@5694
|
683 |
|
jlahoda@5694
|
684 |
out = new JavacTask(tb)
|
jlahoda@5694
|
685 |
.options("--source-path", src8.toString(),
|
jlahoda@5694
|
686 |
"-processor", UnnamedPackageProcessor.class.getName(),
|
jlahoda@5694
|
687 |
"-source", "8")
|
jlahoda@5694
|
688 |
.outdir(classes)
|
jlahoda@5694
|
689 |
.files(findJavaFiles(src8))
|
jlahoda@5694
|
690 |
.run()
|
jlahoda@5694
|
691 |
.writeAll()
|
jlahoda@5694
|
692 |
.getOutputLines(OutputKind.STDOUT);
|
jlahoda@5694
|
693 |
|
jlahoda@5694
|
694 |
expected = Arrays.asList("noModule");
|
jlahoda@5694
|
695 |
|
jlahoda@5694
|
696 |
if (!expected.equals(out)) {
|
jlahoda@5694
|
697 |
throw new AssertionError("Unexpected output: " + out);
|
jlahoda@5694
|
698 |
}
|
jlahoda@5694
|
699 |
|
jlahoda@5694
|
700 |
//-source 9, unnamed:
|
jlahoda@5694
|
701 |
Path srcUnnamed = base.resolve("srcUnnamed");
|
jlahoda@5694
|
702 |
Files.createDirectories(srcUnnamed);
|
jlahoda@5694
|
703 |
tb.writeJavaFiles(srcUnnamed,
|
jlahoda@5694
|
704 |
"public class Test {}");
|
jlahoda@5694
|
705 |
Path classesUnnamed = base.resolve("classesUnnamed");
|
jlahoda@5694
|
706 |
tb.createDirectories(classesUnnamed);
|
jlahoda@5694
|
707 |
|
jlahoda@5694
|
708 |
out = new JavacTask(tb)
|
jlahoda@5694
|
709 |
.options("--source-path", srcUnnamed.toString(),
|
jlahoda@5694
|
710 |
"-processor", UnnamedPackageProcessor.class.getName())
|
jlahoda@5694
|
711 |
.outdir(classesUnnamed)
|
jlahoda@5694
|
712 |
.files(findJavaFiles(srcUnnamed))
|
jlahoda@5694
|
713 |
.run()
|
jlahoda@5694
|
714 |
.writeAll()
|
jlahoda@5694
|
715 |
.getOutputLines(OutputKind.STDOUT);
|
jlahoda@5694
|
716 |
|
jlahoda@5694
|
717 |
expected = Arrays.asList("unnamedModule");
|
jlahoda@5694
|
718 |
|
jlahoda@5694
|
719 |
if (!expected.equals(out)) {
|
jlahoda@5694
|
720 |
throw new AssertionError("Unexpected output: " + out);
|
jlahoda@5694
|
721 |
}
|
jlahoda@5694
|
722 |
|
jlahoda@5694
|
723 |
//-source 9, named:
|
jlahoda@5694
|
724 |
Path srcNamed = base.resolve("srcNamed");
|
jlahoda@5694
|
725 |
Files.createDirectories(srcNamed);
|
jlahoda@5694
|
726 |
tb.writeJavaFiles(srcNamed,
|
jlahoda@5694
|
727 |
"module m {}",
|
jlahoda@5694
|
728 |
"public class Test {}");
|
jlahoda@5694
|
729 |
Path classesNamed = base.resolve("classesNamed");
|
jlahoda@5694
|
730 |
tb.createDirectories(classesNamed);
|
jlahoda@5694
|
731 |
|
jlahoda@5694
|
732 |
out = new JavacTask(tb)
|
jlahoda@5694
|
733 |
.options("--source-path", srcNamed.toString(),
|
jlahoda@5694
|
734 |
"-classpath", "",
|
jlahoda@5694
|
735 |
"-processorpath", System.getProperty("test.class.path"),
|
jlahoda@5694
|
736 |
"-processor", UnnamedPackageProcessor.class.getName())
|
jlahoda@5694
|
737 |
.outdir(classesNamed)
|
jlahoda@5694
|
738 |
.files(findJavaFiles(srcNamed))
|
jlahoda@5694
|
739 |
.run()
|
jlahoda@5694
|
740 |
.writeAll()
|
jlahoda@5694
|
741 |
.getOutputLines(OutputKind.STDOUT);
|
jlahoda@5694
|
742 |
|
jlahoda@5694
|
743 |
expected = Arrays.asList("m");
|
jlahoda@5694
|
744 |
|
jlahoda@5694
|
745 |
if (!expected.equals(out)) {
|
jlahoda@5694
|
746 |
throw new AssertionError("Unexpected output: " + out);
|
jlahoda@5694
|
747 |
}
|
jlahoda@5694
|
748 |
|
jlahoda@5694
|
749 |
//-source 9, conflict:
|
jlahoda@5694
|
750 |
Path srcNamed2 = base.resolve("srcNamed2");
|
jlahoda@5694
|
751 |
Path srcNamed2m1 = srcNamed2.resolve("m1x");
|
jlahoda@5694
|
752 |
Files.createDirectories(srcNamed2m1);
|
jlahoda@5694
|
753 |
tb.writeJavaFiles(srcNamed2m1,
|
jlahoda@5694
|
754 |
"module m1x {}",
|
jlahoda@5694
|
755 |
"public class Test {}");
|
jlahoda@5694
|
756 |
Path srcNamed2m2 = srcNamed2.resolve("m2x");
|
jlahoda@5694
|
757 |
Files.createDirectories(srcNamed2m2);
|
jlahoda@5694
|
758 |
tb.writeJavaFiles(srcNamed2m2,
|
jlahoda@5694
|
759 |
"module m2x {}",
|
jlahoda@5694
|
760 |
"public class Test {}");
|
jlahoda@5694
|
761 |
Path classesNamed2 = base.resolve("classesNamed2");
|
jlahoda@5694
|
762 |
tb.createDirectories(classesNamed2);
|
jlahoda@5694
|
763 |
|
jlahoda@5694
|
764 |
out = new JavacTask(tb)
|
jlahoda@5694
|
765 |
.options("--module-source-path", srcNamed2.toString(),
|
jlahoda@5694
|
766 |
"-classpath", "",
|
jlahoda@5694
|
767 |
"-processorpath", System.getProperty("test.class.path"),
|
jlahoda@5694
|
768 |
"-processor", UnnamedPackageProcessor.class.getName(),
|
jlahoda@5694
|
769 |
"-XDshould-stop.ifError=FLOW")
|
jlahoda@5694
|
770 |
.outdir(classesNamed2)
|
jlahoda@5694
|
771 |
.files(findJavaFiles(srcNamed2))
|
jlahoda@5694
|
772 |
.run(Expect.FAIL)
|
jlahoda@5694
|
773 |
.writeAll()
|
jlahoda@5694
|
774 |
.getOutputLines(OutputKind.STDOUT);
|
jlahoda@5694
|
775 |
|
jlahoda@5694
|
776 |
expected = Arrays.asList("null",
|
jlahoda@5694
|
777 |
"m1x: true",
|
jlahoda@5694
|
778 |
"m2x: true");
|
jlahoda@5694
|
779 |
|
jlahoda@5694
|
780 |
if (!expected.equals(out)) {
|
jlahoda@5694
|
781 |
throw new AssertionError("Unexpected output: " + out);
|
jlahoda@5694
|
782 |
}
|
jlahoda@5694
|
783 |
}
|
jlahoda@5694
|
784 |
|
jlahoda@5694
|
785 |
@SupportedAnnotationTypes("*")
|
jlahoda@5694
|
786 |
public static final class UnnamedPackageProcessor extends AbstractProcessor {
|
jlahoda@5694
|
787 |
|
jlahoda@5694
|
788 |
int round = 0;
|
jlahoda@5694
|
789 |
|
jlahoda@5694
|
790 |
@Override
|
jlahoda@5694
|
791 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jlahoda@5694
|
792 |
if (round++ != 0)
|
jlahoda@5694
|
793 |
return false;
|
jlahoda@5694
|
794 |
|
jlahoda@5694
|
795 |
Elements elements = processingEnv.getElementUtils();
|
jlahoda@5694
|
796 |
PackageElement pe = elements.getPackageElement("");
|
jlahoda@5694
|
797 |
|
jlahoda@5694
|
798 |
if (pe == null) {
|
jlahoda@5694
|
799 |
System.out.println("null");
|
jlahoda@5694
|
800 |
} else {
|
jlahoda@5694
|
801 |
ModuleElement mod = (ModuleElement) pe.getEnclosingElement();
|
jlahoda@5694
|
802 |
if (mod == null) {
|
jlahoda@5694
|
803 |
System.out.println("noModule");
|
jlahoda@5694
|
804 |
} else if (mod.isUnnamed()) {
|
jlahoda@5694
|
805 |
System.out.println("unnamedModule");
|
jlahoda@5694
|
806 |
} else {
|
jlahoda@5694
|
807 |
System.out.println(mod);
|
jlahoda@5694
|
808 |
}
|
jlahoda@5694
|
809 |
}
|
jlahoda@5694
|
810 |
|
jlahoda@5694
|
811 |
ModuleElement m1x = elements.getModuleElement("m1x");
|
jlahoda@5694
|
812 |
ModuleElement m2x = elements.getModuleElement("m2x");
|
jlahoda@5694
|
813 |
|
jlahoda@5694
|
814 |
if (m1x != null && m2x != null) {
|
jlahoda@5694
|
815 |
System.out.println("m1x: " + (elements.getPackageElement(m1x, "") != null));
|
jlahoda@5694
|
816 |
System.out.println("m2x: " + (elements.getPackageElement(m2x, "") != null));
|
jlahoda@5694
|
817 |
}
|
jlahoda@5694
|
818 |
|
jlahoda@5694
|
819 |
return false;
|
jlahoda@5694
|
820 |
}
|
jlahoda@5694
|
821 |
|
jlahoda@5694
|
822 |
}
|
jlahoda@5833
|
823 |
|
jlahoda@5833
|
824 |
@Test
|
jlahoda@5833
|
825 |
public void testEmptyInExportedPackage(Path base) throws Exception {
|
jlahoda@5833
|
826 |
Path src = base.resolve("src");
|
jlahoda@5833
|
827 |
Path m = src.resolve("m");
|
jlahoda@5833
|
828 |
tb.writeJavaFiles(m,
|
jlahoda@5833
|
829 |
"module m { exports api; }");
|
jlahoda@5833
|
830 |
Path apiFile = m.resolve("api").resolve("Api.java");
|
jlahoda@5833
|
831 |
Files.createDirectories(apiFile.getParent());
|
jlahoda@5833
|
832 |
try (BufferedWriter w = Files.newBufferedWriter(apiFile)) {
|
jlahoda@5833
|
833 |
w.write("//no package decl");
|
jlahoda@5833
|
834 |
}
|
jlahoda@5833
|
835 |
Path classes = base.resolve("classes");
|
jlahoda@5833
|
836 |
tb.createDirectories(classes);
|
jlahoda@5833
|
837 |
|
jlahoda@5833
|
838 |
List<String> log;
|
jlahoda@5833
|
839 |
List<String> expected =
|
jlahoda@5833
|
840 |
Arrays.asList("module-info.java:1:20: compiler.err.package.empty.or.not.found: api",
|
jlahoda@5833
|
841 |
"1 error");
|
jlahoda@5833
|
842 |
|
jlahoda@5833
|
843 |
System.err.println("file explicitly specified:");
|
jlahoda@5833
|
844 |
|
jlahoda@5833
|
845 |
log = new JavacTask(tb)
|
jlahoda@5833
|
846 |
.options("-XDrawDiagnostics",
|
jlahoda@5833
|
847 |
"--module-source-path", src.toString())
|
jlahoda@5833
|
848 |
.outdir(classes)
|
jlahoda@5833
|
849 |
.files(findJavaFiles(src))
|
jlahoda@5833
|
850 |
.run(Task.Expect.FAIL)
|
jlahoda@5833
|
851 |
.writeAll()
|
jlahoda@5833
|
852 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5833
|
853 |
|
jlahoda@5833
|
854 |
if (!expected.equals(log))
|
jlahoda@5833
|
855 |
throw new Exception("expected output not found: " + log);
|
jlahoda@5833
|
856 |
|
jlahoda@5833
|
857 |
System.err.println("file not specified:");
|
jlahoda@5833
|
858 |
|
jlahoda@5833
|
859 |
tb.cleanDirectory(classes);
|
jlahoda@5833
|
860 |
|
jlahoda@5833
|
861 |
log = new JavacTask(tb)
|
jlahoda@5833
|
862 |
.options("-XDrawDiagnostics",
|
jlahoda@5833
|
863 |
"--module-source-path", src.toString())
|
jlahoda@5833
|
864 |
.outdir(classes)
|
jlahoda@5833
|
865 |
.files(findJavaFiles(m.resolve("module-info.java")))
|
jlahoda@5833
|
866 |
.run(Task.Expect.FAIL)
|
jlahoda@5833
|
867 |
.writeAll()
|
jlahoda@5833
|
868 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5833
|
869 |
|
jlahoda@5833
|
870 |
if (!expected.equals(log))
|
jlahoda@5833
|
871 |
throw new Exception("expected output not found: " + log);
|
jlahoda@5833
|
872 |
}
|
jlahoda@5833
|
873 |
|
jlahoda@5833
|
874 |
@Test
|
jlahoda@5833
|
875 |
public void testJustPackageInExportedPackage(Path base) throws Exception {
|
jlahoda@5833
|
876 |
Path src = base.resolve("src");
|
jlahoda@5833
|
877 |
Path m = src.resolve("m");
|
jlahoda@5833
|
878 |
tb.writeJavaFiles(m,
|
jlahoda@5833
|
879 |
"module m { exports api; }");
|
jlahoda@5833
|
880 |
Path apiFile = m.resolve("api").resolve("Api.java");
|
jlahoda@5833
|
881 |
Files.createDirectories(apiFile.getParent());
|
jlahoda@5833
|
882 |
try (BufferedWriter w = Files.newBufferedWriter(apiFile)) {
|
jlahoda@5833
|
883 |
w.write("package api;");
|
jlahoda@5833
|
884 |
}
|
jlahoda@5833
|
885 |
Path classes = base.resolve("classes");
|
jlahoda@5833
|
886 |
tb.createDirectories(classes);
|
jlahoda@5833
|
887 |
|
jlahoda@5833
|
888 |
System.err.println("file explicitly specified:");
|
jlahoda@5833
|
889 |
|
jlahoda@5833
|
890 |
new JavacTask(tb)
|
jlahoda@5833
|
891 |
.options("-XDrawDiagnostics",
|
jlahoda@5833
|
892 |
"--module-source-path", src.toString())
|
jlahoda@5833
|
893 |
.outdir(classes)
|
jlahoda@5833
|
894 |
.files(findJavaFiles(src))
|
jlahoda@5833
|
895 |
.run()
|
jlahoda@5833
|
896 |
.writeAll();
|
jlahoda@5833
|
897 |
|
jlahoda@5833
|
898 |
System.err.println("file not specified:");
|
jlahoda@5833
|
899 |
|
jlahoda@5833
|
900 |
tb.cleanDirectory(classes);
|
jlahoda@5833
|
901 |
|
jlahoda@5833
|
902 |
new JavacTask(tb)
|
jlahoda@5833
|
903 |
.options("-XDrawDiagnostics",
|
jlahoda@5833
|
904 |
"--module-source-path", src.toString())
|
jlahoda@5833
|
905 |
.outdir(classes)
|
jlahoda@5833
|
906 |
.files(findJavaFiles(m.resolve("module-info.java")))
|
jlahoda@5833
|
907 |
.run()
|
jlahoda@5833
|
908 |
.writeAll();
|
jlahoda@5833
|
909 |
}
|
jlahoda@5833
|
910 |
|
jlahoda@5833
|
911 |
@Test
|
jlahoda@5833
|
912 |
public void testWrongPackageInExportedPackage(Path base) throws Exception {
|
jlahoda@5833
|
913 |
Path src = base.resolve("src");
|
jlahoda@5833
|
914 |
Path m = src.resolve("m");
|
jlahoda@5833
|
915 |
tb.writeJavaFiles(m,
|
jlahoda@5833
|
916 |
"module m { exports api; }");
|
jlahoda@5833
|
917 |
Path apiFile = m.resolve("api").resolve("Api.java");
|
jlahoda@5833
|
918 |
Files.createDirectories(apiFile.getParent());
|
jlahoda@5833
|
919 |
try (BufferedWriter w = Files.newBufferedWriter(apiFile)) {
|
jlahoda@5833
|
920 |
w.write("package impl; public class Api { }");
|
jlahoda@5833
|
921 |
}
|
jlahoda@5833
|
922 |
Path classes = base.resolve("classes");
|
jlahoda@5833
|
923 |
tb.createDirectories(classes);
|
jlahoda@5833
|
924 |
|
jlahoda@5833
|
925 |
List<String> log;
|
jlahoda@5833
|
926 |
|
jlahoda@5833
|
927 |
List<String> expected =
|
jlahoda@5833
|
928 |
Arrays.asList("module-info.java:1:20: compiler.err.package.empty.or.not.found: api",
|
jlahoda@5833
|
929 |
"1 error");
|
jlahoda@5833
|
930 |
|
jlahoda@5833
|
931 |
System.err.println("file explicitly specified:");
|
jlahoda@5833
|
932 |
|
jlahoda@5833
|
933 |
log = new JavacTask(tb)
|
jlahoda@5833
|
934 |
.options("-XDrawDiagnostics",
|
jlahoda@5833
|
935 |
"--module-source-path", src.toString())
|
jlahoda@5833
|
936 |
.outdir(classes)
|
jlahoda@5833
|
937 |
.files(findJavaFiles(src))
|
jlahoda@5833
|
938 |
.run(Task.Expect.FAIL)
|
jlahoda@5833
|
939 |
.writeAll()
|
jlahoda@5833
|
940 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5833
|
941 |
|
jlahoda@5833
|
942 |
if (!expected.equals(log))
|
jlahoda@5833
|
943 |
throw new Exception("expected output not found: " + log);
|
jlahoda@5833
|
944 |
|
jlahoda@5833
|
945 |
System.err.println("file not specified:");
|
jlahoda@5833
|
946 |
|
jlahoda@5833
|
947 |
tb.cleanDirectory(classes);
|
jlahoda@5833
|
948 |
|
jlahoda@5833
|
949 |
log = new JavacTask(tb)
|
jlahoda@5833
|
950 |
.options("-XDrawDiagnostics",
|
jlahoda@5833
|
951 |
"--module-source-path", src.toString())
|
jlahoda@5833
|
952 |
.outdir(classes)
|
jlahoda@5833
|
953 |
.files(findJavaFiles(m.resolve("module-info.java")))
|
jlahoda@5833
|
954 |
.run(Task.Expect.FAIL)
|
jlahoda@5833
|
955 |
.writeAll()
|
jlahoda@5833
|
956 |
.getOutputLines(Task.OutputKind.DIRECT);
|
jlahoda@5833
|
957 |
|
jlahoda@5833
|
958 |
if (!expected.equals(log))
|
jlahoda@5833
|
959 |
throw new Exception("expected output not found: " + log);
|
jlahoda@5833
|
960 |
}
|
jlahoda@5833
|
961 |
|
jlahoda@5877
|
962 |
@Test
|
jlahoda@5877
|
963 |
public void testDependOnUnnamedAccessibility(Path base) throws Exception {
|
jlahoda@5877
|
964 |
Path unnamedSrc = base.resolve("unnamed-src");
|
jlahoda@5877
|
965 |
tb.writeJavaFiles(unnamedSrc,
|
jlahoda@5877
|
966 |
"package p1; public class First { public static p2.Second get() { return null; } }",
|
jlahoda@5877
|
967 |
"package p2; public class Second { public void test() { } }");
|
jlahoda@5877
|
968 |
Path unnamedClasses = base.resolve("unnamed-classes");
|
jlahoda@5877
|
969 |
tb.createDirectories(unnamedClasses);
|
jlahoda@5877
|
970 |
|
jlahoda@5877
|
971 |
System.err.println("compiling unnamed sources:");
|
jlahoda@5877
|
972 |
|
jlahoda@5877
|
973 |
new JavacTask(tb)
|
jlahoda@5877
|
974 |
.outdir(unnamedClasses)
|
jlahoda@5877
|
975 |
.files(findJavaFiles(unnamedSrc))
|
jlahoda@5877
|
976 |
.run()
|
jlahoda@5877
|
977 |
.writeAll();
|
jlahoda@5877
|
978 |
|
jlahoda@5877
|
979 |
//test sources:
|
jlahoda@5877
|
980 |
Path src = base.resolve("src");
|
jlahoda@5877
|
981 |
Path m = src.resolve("m");
|
jlahoda@5877
|
982 |
tb.writeJavaFiles(m,
|
jlahoda@5877
|
983 |
"module m { }",
|
jlahoda@5877
|
984 |
"package p; public class Test { { p1.First.get().test(); } }");
|
jlahoda@5877
|
985 |
Path classes = base.resolve("classes");
|
jlahoda@5877
|
986 |
tb.createDirectories(classes);
|
jlahoda@5877
|
987 |
|
jlahoda@5877
|
988 |
System.err.println("compiling test module:");
|
jlahoda@5877
|
989 |
|
jlahoda@5877
|
990 |
new JavacTask(tb)
|
jlahoda@5877
|
991 |
.options("-classpath", unnamedClasses.toString(),
|
jlahoda@5877
|
992 |
"--add-reads", "m=ALL-UNNAMED")
|
jlahoda@5877
|
993 |
.outdir(classes)
|
jlahoda@5877
|
994 |
.files(findJavaFiles(src))
|
jlahoda@5877
|
995 |
.run()
|
jlahoda@5877
|
996 |
.writeAll();
|
jlahoda@5877
|
997 |
}
|
jlahoda@5877
|
998 |
|
alanb@5016
|
999 |
}
|