1 /* |
1 /* |
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
27 * @library /tools/lib |
27 * @library /tools/lib |
28 * @modules |
28 * @modules |
29 * jdk.compiler/com.sun.tools.javac.api |
29 * jdk.compiler/com.sun.tools.javac.api |
30 * jdk.compiler/com.sun.tools.javac.code |
30 * jdk.compiler/com.sun.tools.javac.code |
31 * jdk.compiler/com.sun.tools.javac.main |
31 * jdk.compiler/com.sun.tools.javac.main |
32 * jdk.jdeps/com.sun.tools.javap |
32 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase |
33 * @build ToolBox ModuleTestBase |
|
34 * @run main EdgeCases |
33 * @run main EdgeCases |
35 */ |
34 */ |
36 |
35 |
37 import java.io.Writer; |
36 import java.io.Writer; |
38 import java.nio.file.Files; |
37 import java.nio.file.Files; |
49 import javax.tools.JavaFileObject; |
48 import javax.tools.JavaFileObject; |
50 import javax.tools.StandardJavaFileManager; |
49 import javax.tools.StandardJavaFileManager; |
51 import javax.tools.ToolProvider; |
50 import javax.tools.ToolProvider; |
52 |
51 |
53 import com.sun.source.tree.CompilationUnitTree; |
52 import com.sun.source.tree.CompilationUnitTree; |
54 import com.sun.source.util.JavacTask; |
53 //import com.sun.source.util.JavacTask; // conflicts with toolbox.JavacTask |
55 import com.sun.tools.javac.api.JavacTaskImpl; |
54 import com.sun.tools.javac.api.JavacTaskImpl; |
56 import com.sun.tools.javac.code.Symbol.ModuleSymbol; |
55 import com.sun.tools.javac.code.Symbol.ModuleSymbol; |
57 |
56 |
|
57 import toolbox.JarTask; |
|
58 import toolbox.JavacTask; |
|
59 import toolbox.Task; |
|
60 import toolbox.ToolBox; |
|
61 |
58 public class EdgeCases extends ModuleTestBase { |
62 public class EdgeCases extends ModuleTestBase { |
59 |
63 |
60 public static void main(String... args) throws Exception { |
64 public static void main(String... args) throws Exception { |
61 new EdgeCases().runTests(); |
65 new EdgeCases().runTests(); |
62 } |
66 } |
66 Path src = base.resolve("src"); |
70 Path src = base.resolve("src"); |
67 tb.writeJavaFiles(src, "package test; import undef.Any; public class Test {}"); |
71 tb.writeJavaFiles(src, "package test; import undef.Any; public class Test {}"); |
68 Path classes = base.resolve("classes"); |
72 Path classes = base.resolve("classes"); |
69 tb.createDirectories(classes); |
73 tb.createDirectories(classes); |
70 |
74 |
71 List<String> log = tb.new JavacTask() |
75 List<String> log = new JavacTask(tb) |
72 .options("-XaddExports:undef/undef=ALL-UNNAMED", "-XDrawDiagnostics") |
76 .options("-XaddExports:undef/undef=ALL-UNNAMED", "-XDrawDiagnostics") |
73 .outdir(classes) |
77 .outdir(classes) |
74 .files(findJavaFiles(src)) |
78 .files(findJavaFiles(src)) |
75 .run(ToolBox.Expect.FAIL) |
79 .run(Task.Expect.FAIL) |
76 .writeAll() |
80 .writeAll() |
77 .getOutputLines(ToolBox.OutputKind.DIRECT); |
81 .getOutputLines(Task.OutputKind.DIRECT); |
78 |
82 |
79 List<String> expected = Arrays.asList("- compiler.err.cant.find.module: undef", |
83 List<String> expected = Arrays.asList("- compiler.err.cant.find.module: undef", |
80 "Test.java:1:27: compiler.err.doesnt.exist: undef", |
84 "Test.java:1:27: compiler.err.doesnt.exist: undef", |
81 "2 errors"); |
85 "2 errors"); |
82 |
86 |
92 Path m1 = moduleSrc.resolve("m1"); |
96 Path m1 = moduleSrc.resolve("m1"); |
93 |
97 |
94 tb.writeJavaFiles(m1, "module m1 { }"); |
98 tb.writeJavaFiles(m1, "module m1 { }"); |
95 |
99 |
96 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc)); |
100 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(findJavaFiles(moduleSrc)); |
97 JavacTask task = (JavacTask) compiler.getTask(null, fm, null, null, null, files); |
101 com.sun.source.util.JavacTask task = |
|
102 (com.sun.source.util.JavacTask) compiler.getTask(null, fm, null, null, null, files); |
98 |
103 |
99 task.analyze(); |
104 task.analyze(); |
100 |
105 |
101 ModuleSymbol msym = (ModuleSymbol) task.getElements().getModuleElement("m1"); |
106 ModuleSymbol msym = (ModuleSymbol) task.getElements().getModuleElement("m1"); |
102 |
107 |
158 "module m3 { requires m2; }", |
163 "module m3 { requires m2; }", |
159 "package test; public class Test { { api2.Api2.get().call(); api2.Api2.get().toString(); } }"); |
164 "package test; public class Test { { api2.Api2.get().call(); api2.Api2.get().toString(); } }"); |
160 Path classes = base.resolve("classes"); |
165 Path classes = base.resolve("classes"); |
161 tb.createDirectories(classes); |
166 tb.createDirectories(classes); |
162 |
167 |
163 String log = tb.new JavacTask() |
168 String log = new JavacTask(tb) |
164 .options("-XDrawDiagnostics", |
169 .options("-XDrawDiagnostics", |
165 "-modulesourcepath", src.toString()) |
170 "-modulesourcepath", src.toString()) |
166 .outdir(classes) |
171 .outdir(classes) |
167 .files(findJavaFiles(src)) |
172 .files(findJavaFiles(src)) |
168 .run(ToolBox.Expect.FAIL) |
173 .run(Task.Expect.FAIL) |
169 .writeAll() |
174 .writeAll() |
170 .getOutput(ToolBox.OutputKind.DIRECT); |
175 .getOutput(Task.OutputKind.DIRECT); |
171 |
176 |
172 if (!log.contains("Test.java:1:52: compiler.err.not.def.access.class.intf.cant.access: call(), api1.Api1") || |
177 if (!log.contains("Test.java:1:52: compiler.err.not.def.access.class.intf.cant.access: call(), api1.Api1") || |
173 !log.contains("Test.java:1:76: compiler.err.not.def.access.class.intf.cant.access: toString(), java.lang.Object")) |
178 !log.contains("Test.java:1:76: compiler.err.not.def.access.class.intf.cant.access: toString(), java.lang.Object")) |
174 throw new Exception("expected output not found"); |
179 throw new Exception("expected output not found"); |
175 } |
180 } |
181 Path automaticSrc = base.resolve("automaticSrc"); |
186 Path automaticSrc = base.resolve("automaticSrc"); |
182 tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}"); |
187 tb.writeJavaFiles(automaticSrc, "package api1; public class Api1 {}"); |
183 Path automaticClasses = base.resolve("automaticClasses"); |
188 Path automaticClasses = base.resolve("automaticClasses"); |
184 tb.createDirectories(automaticClasses); |
189 tb.createDirectories(automaticClasses); |
185 |
190 |
186 String automaticLog = tb.new JavacTask() |
191 String automaticLog = new JavacTask(tb) |
187 .outdir(automaticClasses) |
192 .outdir(automaticClasses) |
188 .files(findJavaFiles(automaticSrc)) |
193 .files(findJavaFiles(automaticSrc)) |
189 .run() |
194 .run() |
190 .writeAll() |
195 .writeAll() |
191 .getOutput(ToolBox.OutputKind.DIRECT); |
196 .getOutput(Task.OutputKind.DIRECT); |
192 |
197 |
193 if (!automaticLog.isEmpty()) |
198 if (!automaticLog.isEmpty()) |
194 throw new Exception("expected output not found: " + automaticLog); |
199 throw new Exception("expected output not found: " + automaticLog); |
195 |
200 |
196 Path modulePath = base.resolve("module-path"); |
201 Path modulePath = base.resolve("module-path"); |
197 |
202 |
198 Files.createDirectories(modulePath); |
203 Files.createDirectories(modulePath); |
199 |
204 |
200 Path automaticJar = modulePath.resolve("m1-1.0.jar"); |
205 Path automaticJar = modulePath.resolve("m1-1.0.jar"); |
201 |
206 |
202 tb.new JarTask(automaticJar) |
207 new JarTask(tb, automaticJar) |
203 .baseDir(automaticClasses) |
208 .baseDir(automaticClasses) |
204 .files("api1/Api1.class") |
209 .files("api1/Api1.class") |
205 .run(); |
210 .run(); |
206 |
211 |
207 Path src = base.resolve("src"); |
212 Path src = base.resolve("src"); |
214 "module m3 { requires m1; requires m2; }", |
219 "module m3 { requires m1; requires m2; }", |
215 "package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }"); |
220 "package test; public class Test { { api2.Api2.get(); api1.Api1 a1; } }"); |
216 Path classes = base.resolve("classes"); |
221 Path classes = base.resolve("classes"); |
217 tb.createDirectories(classes); |
222 tb.createDirectories(classes); |
218 |
223 |
219 tb.new JavacTask() |
224 new JavacTask(tb) |
220 .options("-modulepath", modulePath.toString(), |
225 .options("-modulepath", modulePath.toString(), |
221 "-modulesourcepath", src.toString()) |
226 "-modulesourcepath", src.toString()) |
222 .outdir(classes) |
227 .outdir(classes) |
223 .files(findJavaFiles(src_m2)) |
228 .files(findJavaFiles(src_m2)) |
224 .run() |
229 .run() |
225 .writeAll(); |
230 .writeAll(); |
226 |
231 |
227 tb.new JavacTask() |
232 new JavacTask(tb) |
228 .options("-modulepath", modulePath.toString(), |
233 .options("-modulepath", modulePath.toString(), |
229 "-modulesourcepath", src.toString()) |
234 "-modulesourcepath", src.toString()) |
230 .outdir(classes) |
235 .outdir(classes) |
231 .files(findJavaFiles(src_m3)) |
236 .files(findJavaFiles(src_m3)) |
232 .run() |
237 .run() |
242 tb.writeJavaFiles(src_m1, |
247 tb.writeJavaFiles(src_m1, |
243 "package test; public class Test {}"); |
248 "package test; public class Test {}"); |
244 Path classes = base.resolve("classes"); |
249 Path classes = base.resolve("classes"); |
245 tb.createDirectories(classes); |
250 tb.createDirectories(classes); |
246 |
251 |
247 tb.new JavacTask() |
252 new JavacTask(tb) |
248 .options("-sourcepath", src_m1.toString(), |
253 .options("-sourcepath", src_m1.toString(), |
249 "-XDrawDiagnostics") |
254 "-XDrawDiagnostics") |
250 .outdir(classes) |
255 .outdir(classes) |
251 .files(findJavaFiles(src_m1.resolve("test"))) |
256 .files(findJavaFiles(src_m1.resolve("test"))) |
252 .run(ToolBox.Expect.FAIL) |
257 .run(Task.Expect.FAIL) |
253 .writeAll(); |
258 .writeAll(); |
254 |
259 |
255 tb.writeJavaFiles(src_m1, |
260 tb.writeJavaFiles(src_m1, |
256 "module m1 {}"); |
261 "module m1 {}"); |
257 |
262 |
258 tb.new JavacTask() |
263 new JavacTask(tb) |
259 .options("-sourcepath", src_m1.toString()) |
264 .options("-sourcepath", src_m1.toString()) |
260 .outdir(classes) |
265 .outdir(classes) |
261 .files(findJavaFiles(src_m1.resolve("test"))) |
266 .files(findJavaFiles(src_m1.resolve("test"))) |
262 .run() |
267 .run() |
263 .writeAll(); |
268 .writeAll(); |