23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @summary Ensure named modules cannot refer to classpath types. |
26 * @summary Ensure named modules cannot refer to classpath types. |
27 * @library /tools/lib |
27 * @library /tools/lib |
28 * @modules |
28 * @modules jdk.compiler/com.sun.tools.javac.api |
29 * jdk.compiler/com.sun.tools.javac.api |
29 * jdk.compiler/com.sun.tools.javac.main |
30 * jdk.compiler/com.sun.tools.javac.main |
30 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase |
31 * jdk.jdeps/com.sun.tools.javap |
|
32 * @build ToolBox ModuleTestBase |
|
33 * @run main ModulesAndClassPathTest |
31 * @run main ModulesAndClassPathTest |
34 */ |
32 */ |
35 |
33 |
36 import java.io.File; |
34 import java.io.File; |
37 import java.nio.file.Files; |
35 import java.nio.file.Files; |
43 import javax.annotation.processing.AbstractProcessor; |
41 import javax.annotation.processing.AbstractProcessor; |
44 import javax.annotation.processing.RoundEnvironment; |
42 import javax.annotation.processing.RoundEnvironment; |
45 import javax.annotation.processing.SupportedAnnotationTypes; |
43 import javax.annotation.processing.SupportedAnnotationTypes; |
46 import javax.lang.model.element.TypeElement; |
44 import javax.lang.model.element.TypeElement; |
47 |
45 |
|
46 import toolbox.JarTask; |
|
47 import toolbox.JavacTask; |
|
48 import toolbox.Task; |
|
49 import toolbox.ToolBox; |
|
50 |
48 public class ModulesAndClassPathTest extends ModuleTestBase { |
51 public class ModulesAndClassPathTest extends ModuleTestBase { |
49 |
52 |
50 public static void main(String... args) throws Exception { |
53 public static void main(String... args) throws Exception { |
51 new ModulesAndClassPathTest().runTests(); |
54 new ModulesAndClassPathTest().runTests(); |
52 } |
55 } |
64 |
67 |
65 tb.writeJavaFiles(m1, |
68 tb.writeJavaFiles(m1, |
66 "module m1 { }", |
69 "module m1 { }", |
67 "package impl; public class Impl { api.Api api; }"); |
70 "package impl; public class Impl { api.Api api; }"); |
68 |
71 |
69 List<String> modLog = tb.new JavacTask() |
72 List<String> modLog = new JavacTask(tb) |
70 .options("-classpath", jar.toString(), |
73 .options("-classpath", jar.toString(), |
71 "-XDrawDiagnostics") |
74 "-XDrawDiagnostics") |
72 .outdir(classes) |
75 .outdir(classes) |
73 .files(findJavaFiles(moduleSrc)) |
76 .files(findJavaFiles(moduleSrc)) |
74 .run(ToolBox.Expect.FAIL) |
77 .run(Task.Expect.FAIL) |
75 .writeAll() |
78 .writeAll() |
76 .getOutputLines(ToolBox.OutputKind.DIRECT); |
79 .getOutputLines(Task.OutputKind.DIRECT); |
77 |
80 |
78 List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api", |
81 List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api", |
79 "1 error"); |
82 "1 error"); |
80 |
83 |
81 if (!expected.equals(modLog)) { |
84 if (!expected.equals(modLog)) { |
82 throw new Exception("unexpected output: " + modLog); |
85 throw new Exception("unexpected output: " + modLog); |
83 } |
86 } |
84 |
87 |
85 tb.new JavacTask() |
88 new JavacTask(tb) |
86 .options("-classpath", jar.toString(), |
89 .options("-classpath", jar.toString(), |
87 "-XaddReads:m1=ALL-UNNAMED") |
90 "-XaddReads:m1=ALL-UNNAMED") |
88 .outdir(classes) |
91 .outdir(classes) |
89 .files(findJavaFiles(moduleSrc)) |
92 .files(findJavaFiles(moduleSrc)) |
90 .run() |
93 .run() |
91 .writeAll() |
94 .writeAll() |
92 .getOutputLines(ToolBox.OutputKind.DIRECT); |
95 .getOutputLines(Task.OutputKind.DIRECT); |
93 |
96 |
94 tb.new JavacTask() |
97 new JavacTask(tb) |
95 .options("-classpath", jar.toString() + File.pathSeparator + System.getProperty("test.classes"), |
98 .options("-classpath", jar.toString() + File.pathSeparator + System.getProperty("test.classes"), |
96 "-XaddReads:m1=ALL-UNNAMED", |
99 "-XaddReads:m1=ALL-UNNAMED", |
97 "-processor", ProcessorImpl.class.getName()) |
100 "-processor", ProcessorImpl.class.getName()) |
98 .outdir(classes) |
101 .outdir(classes) |
99 .files(findJavaFiles(moduleSrc)) |
102 .files(findJavaFiles(moduleSrc)) |
100 .run() |
103 .run() |
101 .writeAll() |
104 .writeAll() |
102 .getOutputLines(ToolBox.OutputKind.DIRECT); |
105 .getOutputLines(Task.OutputKind.DIRECT); |
103 } |
106 } |
104 |
107 |
105 @Test |
108 @Test |
106 void testImplicitSourcePathModuleInfo(Path base) throws Exception { |
109 void testImplicitSourcePathModuleInfo(Path base) throws Exception { |
107 Path jar = prepareTestJar(base); |
110 Path jar = prepareTestJar(base); |
115 |
118 |
116 tb.writeJavaFiles(m1, |
119 tb.writeJavaFiles(m1, |
117 "module m1 { }", |
120 "module m1 { }", |
118 "package impl; public class Impl { api.Api api; }"); |
121 "package impl; public class Impl { api.Api api; }"); |
119 |
122 |
120 List<String> modLog = tb.new JavacTask() |
123 List<String> modLog = new JavacTask(tb) |
121 .options("-classpath", jar.toString(), |
124 .options("-classpath", jar.toString(), |
122 "-sourcepath", m1.toString(), |
125 "-sourcepath", m1.toString(), |
123 "-XDrawDiagnostics") |
126 "-XDrawDiagnostics") |
124 .outdir(classes) |
127 .outdir(classes) |
125 .files(m1.resolve("impl").resolve("Impl.java")) |
128 .files(m1.resolve("impl").resolve("Impl.java")) |
126 .run(ToolBox.Expect.FAIL) |
129 .run(Task.Expect.FAIL) |
127 .writeAll() |
130 .writeAll() |
128 .getOutputLines(ToolBox.OutputKind.DIRECT); |
131 .getOutputLines(Task.OutputKind.DIRECT); |
129 |
132 |
130 List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api", |
133 List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api", |
131 "1 error"); |
134 "1 error"); |
132 |
135 |
133 if (!expected.equals(modLog)) { |
136 if (!expected.equals(modLog)) { |
148 |
151 |
149 tb.writeJavaFiles(m1, |
152 tb.writeJavaFiles(m1, |
150 "module m1 { }", |
153 "module m1 { }", |
151 "package impl; public class Impl { api.Api api; }"); |
154 "package impl; public class Impl { api.Api api; }"); |
152 |
155 |
153 tb.new JavacTask() |
156 new JavacTask(tb) |
154 .options("-classpath", jar.toString(), |
157 .options("-classpath", jar.toString(), |
155 "-XDrawDiagnostics") |
158 "-XDrawDiagnostics") |
156 .outdir(classes) |
159 .outdir(classes) |
157 .files(m1.resolve("module-info.java")) |
160 .files(m1.resolve("module-info.java")) |
158 .run() |
161 .run() |
159 .writeAll() |
162 .writeAll() |
160 .getOutputLines(ToolBox.OutputKind.DIRECT); |
163 .getOutputLines(Task.OutputKind.DIRECT); |
161 |
164 |
162 List<String> modLog = tb.new JavacTask() |
165 List<String> modLog = new JavacTask(tb) |
163 .options("-classpath", jar.toString(), |
166 .options("-classpath", jar.toString(), |
164 "-XDrawDiagnostics") |
167 "-XDrawDiagnostics") |
165 .outdir(classes) |
168 .outdir(classes) |
166 .files(m1.resolve("impl").resolve("Impl.java")) |
169 .files(m1.resolve("impl").resolve("Impl.java")) |
167 .run(ToolBox.Expect.FAIL) |
170 .run(Task.Expect.FAIL) |
168 .writeAll() |
171 .writeAll() |
169 .getOutputLines(ToolBox.OutputKind.DIRECT); |
172 .getOutputLines(Task.OutputKind.DIRECT); |
170 |
173 |
171 List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api", |
174 List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api", |
172 "1 error"); |
175 "1 error"); |
173 |
176 |
174 if (!expected.equals(modLog)) { |
177 if (!expected.equals(modLog)) { |
181 tb.writeJavaFiles(legacySrc, |
184 tb.writeJavaFiles(legacySrc, |
182 "package api; public abstract class Api {}"); |
185 "package api; public abstract class Api {}"); |
183 Path legacyClasses = base.resolve("legacy-classes"); |
186 Path legacyClasses = base.resolve("legacy-classes"); |
184 Files.createDirectories(legacyClasses); |
187 Files.createDirectories(legacyClasses); |
185 |
188 |
186 String log = tb.new JavacTask() |
189 String log = new JavacTask(tb) |
187 .options() |
190 .options() |
188 .outdir(legacyClasses) |
191 .outdir(legacyClasses) |
189 .files(findJavaFiles(legacySrc)) |
192 .files(findJavaFiles(legacySrc)) |
190 .run() |
193 .run() |
191 .writeAll() |
194 .writeAll() |
192 .getOutput(ToolBox.OutputKind.DIRECT); |
195 .getOutput(Task.OutputKind.DIRECT); |
193 |
196 |
194 if (!log.isEmpty()) { |
197 if (!log.isEmpty()) { |
195 throw new Exception("unexpected output: " + log); |
198 throw new Exception("unexpected output: " + log); |
196 } |
199 } |
197 |
200 |
231 "package impl; public class Impl { }", |
234 "package impl; public class Impl { }", |
232 "package src; public class Src { }", |
235 "package src; public class Src { }", |
233 "package test; public class TestCP extends impl.Impl { }", |
236 "package test; public class TestCP extends impl.Impl { }", |
234 "package test; public class TestSP extends src.Src { }"); |
237 "package test; public class TestSP extends src.Src { }"); |
235 |
238 |
236 tb.new JavacTask() |
239 new JavacTask(tb) |
237 .outdir(classes) |
240 .outdir(classes) |
238 .files(m1.resolve("impl").resolve("Impl.java")) |
241 .files(m1.resolve("impl").resolve("Impl.java")) |
239 .run() |
242 .run() |
240 .writeAll() |
243 .writeAll() |
241 .getOutputLines(ToolBox.OutputKind.DIRECT); |
244 .getOutputLines(Task.OutputKind.DIRECT); |
242 |
245 |
243 tb.new JavacTask() |
246 new JavacTask(tb) |
244 .outdir(classes) |
247 .outdir(classes) |
245 .files(m1.resolve("module-info.java")) |
248 .files(m1.resolve("module-info.java")) |
246 .run() |
249 .run() |
247 .writeAll() |
250 .writeAll() |
248 .getOutputLines(ToolBox.OutputKind.DIRECT); |
251 .getOutputLines(Task.OutputKind.DIRECT); |
249 |
252 |
250 tb.new JavacTask() |
253 new JavacTask(tb) |
251 .outdir(classes) |
254 .outdir(classes) |
252 .files(m1.resolve("test").resolve("TestCP.java")) |
255 .files(m1.resolve("test").resolve("TestCP.java")) |
253 .run() |
256 .run() |
254 .writeAll() |
257 .writeAll() |
255 .getOutputLines(ToolBox.OutputKind.DIRECT); |
258 .getOutputLines(Task.OutputKind.DIRECT); |
256 |
259 |
257 tb.new JavacTask() |
260 new JavacTask(tb) |
258 .options("-sourcepath", m1.toString()) |
261 .options("-sourcepath", m1.toString()) |
259 .outdir(classes) |
262 .outdir(classes) |
260 .files(m1.resolve("test").resolve("TestSP.java")) |
263 .files(m1.resolve("test").resolve("TestSP.java")) |
261 .run() |
264 .run() |
262 .writeAll() |
265 .writeAll() |
263 .getOutputLines(ToolBox.OutputKind.DIRECT); |
266 .getOutputLines(Task.OutputKind.DIRECT); |
264 } |
267 } |
265 } |
268 } |