author | jlahoda |
Thu, 12 Apr 2018 09:23:34 +0200 | |
changeset 49579 | fce4252d5227 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
36526 | 1 |
/* |
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
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 |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Ensure named modules cannot refer to classpath types. |
|
27 |
* @library /tools/lib |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
28 |
* @modules jdk.compiler/com.sun.tools.javac.api |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
29 |
* jdk.compiler/com.sun.tools.javac.main |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
30 |
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase |
36526 | 31 |
* @run main ModulesAndClassPathTest |
32 |
*/ |
|
33 |
||
34 |
import java.io.File; |
|
35 |
import java.nio.file.Files; |
|
36 |
import java.nio.file.Path; |
|
37 |
import java.util.Arrays; |
|
38 |
import java.util.List; |
|
39 |
import java.util.Set; |
|
40 |
||
41 |
import javax.annotation.processing.AbstractProcessor; |
|
42 |
import javax.annotation.processing.RoundEnvironment; |
|
43 |
import javax.annotation.processing.SupportedAnnotationTypes; |
|
44 |
import javax.lang.model.element.TypeElement; |
|
45 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
46 |
import toolbox.JarTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
47 |
import toolbox.JavacTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
48 |
import toolbox.Task; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
49 |
|
36526 | 50 |
public class ModulesAndClassPathTest extends ModuleTestBase { |
51 |
||
52 |
public static void main(String... args) throws Exception { |
|
53 |
new ModulesAndClassPathTest().runTests(); |
|
54 |
} |
|
55 |
||
56 |
@Test |
|
37758 | 57 |
public void testModulesAndClassPath(Path base) throws Exception { |
36526 | 58 |
Path jar = prepareTestJar(base); |
59 |
||
60 |
Path moduleSrc = base.resolve("module-src"); |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
61 |
Path m1 = moduleSrc.resolve("m"); |
36526 | 62 |
|
63 |
Path classes = base.resolve("classes"); |
|
64 |
||
65 |
Files.createDirectories(classes); |
|
66 |
||
67 |
tb.writeJavaFiles(m1, |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
68 |
"module m { }", |
36526 | 69 |
"package impl; public class Impl { api.Api api; }"); |
70 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
71 |
List<String> modLog = new JavacTask(tb) |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37758
diff
changeset
|
72 |
.options("--class-path", jar.toString(), |
36526 | 73 |
"-XDrawDiagnostics") |
74 |
.outdir(classes) |
|
75 |
.files(findJavaFiles(moduleSrc)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
76 |
.run(Task.Expect.FAIL) |
36526 | 77 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
78 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 79 |
|
49579
fce4252d5227
8189765: Improve error reporting for compiling against package not visible due to modules
jlahoda
parents:
47216
diff
changeset
|
80 |
List<String> expected = Arrays.asList("Impl.java:1:35: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.unnamed: api, m)", |
36526 | 81 |
"1 error"); |
82 |
||
83 |
if (!expected.equals(modLog)) { |
|
84 |
throw new Exception("unexpected output: " + modLog); |
|
85 |
} |
|
86 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
87 |
new JavacTask(tb) |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37758
diff
changeset
|
88 |
.options("--class-path", jar.toString(), |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
89 |
"--add-reads", "m=ALL-UNNAMED") |
36526 | 90 |
.outdir(classes) |
91 |
.files(findJavaFiles(moduleSrc)) |
|
92 |
.run() |
|
93 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
94 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 95 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
96 |
new JavacTask(tb) |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37758
diff
changeset
|
97 |
.options("--class-path", jar.toString() + File.pathSeparator + System.getProperty("test.classes"), |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
98 |
"--add-reads", "m=ALL-UNNAMED", |
36526 | 99 |
"-processor", ProcessorImpl.class.getName()) |
100 |
.outdir(classes) |
|
101 |
.files(findJavaFiles(moduleSrc)) |
|
102 |
.run() |
|
103 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
104 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 105 |
} |
106 |
||
107 |
@Test |
|
37758 | 108 |
public void testImplicitSourcePathModuleInfo(Path base) throws Exception { |
36526 | 109 |
Path jar = prepareTestJar(base); |
110 |
||
111 |
Path moduleSrc = base.resolve("module-src"); |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
112 |
Path m1 = moduleSrc.resolve("m"); |
36526 | 113 |
|
114 |
Path classes = base.resolve("classes"); |
|
115 |
||
116 |
Files.createDirectories(classes); |
|
117 |
||
118 |
tb.writeJavaFiles(m1, |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
119 |
"module m { }", |
36526 | 120 |
"package impl; public class Impl { api.Api api; }"); |
121 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
122 |
List<String> modLog = new JavacTask(tb) |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37758
diff
changeset
|
123 |
.options("--class-path", jar.toString(), |
36526 | 124 |
"-sourcepath", m1.toString(), |
125 |
"-XDrawDiagnostics") |
|
126 |
.outdir(classes) |
|
127 |
.files(m1.resolve("impl").resolve("Impl.java")) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
128 |
.run(Task.Expect.FAIL) |
36526 | 129 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
130 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 131 |
|
49579
fce4252d5227
8189765: Improve error reporting for compiling against package not visible due to modules
jlahoda
parents:
47216
diff
changeset
|
132 |
List<String> expected = Arrays.asList("Impl.java:1:35: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.unnamed: api, m)", |
36526 | 133 |
"1 error"); |
134 |
||
135 |
if (!expected.equals(modLog)) { |
|
136 |
throw new Exception("unexpected output: " + modLog); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
@Test |
|
37758 | 141 |
public void testModuleInfoFromOutput(Path base) throws Exception { |
36526 | 142 |
Path jar = prepareTestJar(base); |
143 |
||
144 |
Path moduleSrc = base.resolve("module-src"); |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
145 |
Path m1 = moduleSrc.resolve("m"); |
36526 | 146 |
|
147 |
Path classes = base.resolve("classes"); |
|
148 |
||
149 |
Files.createDirectories(classes); |
|
150 |
||
151 |
tb.writeJavaFiles(m1, |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
152 |
"module m { }", |
36526 | 153 |
"package impl; public class Impl { api.Api api; }"); |
154 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
155 |
new JavacTask(tb) |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37758
diff
changeset
|
156 |
.options("--class-path", jar.toString(), |
36526 | 157 |
"-XDrawDiagnostics") |
158 |
.outdir(classes) |
|
159 |
.files(m1.resolve("module-info.java")) |
|
160 |
.run() |
|
161 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
162 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 163 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
164 |
List<String> modLog = new JavacTask(tb) |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
37758
diff
changeset
|
165 |
.options("--class-path", jar.toString(), |
36526 | 166 |
"-XDrawDiagnostics") |
167 |
.outdir(classes) |
|
168 |
.files(m1.resolve("impl").resolve("Impl.java")) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
169 |
.run(Task.Expect.FAIL) |
36526 | 170 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
171 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 172 |
|
49579
fce4252d5227
8189765: Improve error reporting for compiling against package not visible due to modules
jlahoda
parents:
47216
diff
changeset
|
173 |
List<String> expected = Arrays.asList("Impl.java:1:35: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.unnamed: api, m)", |
36526 | 174 |
"1 error"); |
175 |
||
176 |
if (!expected.equals(modLog)) { |
|
177 |
throw new Exception("unexpected output: " + modLog); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
private Path prepareTestJar(Path base) throws Exception { |
|
182 |
Path legacySrc = base.resolve("legacy-src"); |
|
183 |
tb.writeJavaFiles(legacySrc, |
|
184 |
"package api; public abstract class Api {}"); |
|
185 |
Path legacyClasses = base.resolve("legacy-classes"); |
|
186 |
Files.createDirectories(legacyClasses); |
|
187 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
188 |
String log = new JavacTask(tb) |
36526 | 189 |
.options() |
190 |
.outdir(legacyClasses) |
|
191 |
.files(findJavaFiles(legacySrc)) |
|
192 |
.run() |
|
193 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
194 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 195 |
|
196 |
if (!log.isEmpty()) { |
|
197 |
throw new Exception("unexpected output: " + log); |
|
198 |
} |
|
199 |
||
200 |
Path lib = base.resolve("lib"); |
|
201 |
||
202 |
Files.createDirectories(lib); |
|
203 |
||
204 |
Path jar = lib.resolve("test-api-1.0.jar"); |
|
205 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
206 |
new JarTask(tb, jar) |
36526 | 207 |
.baseDir(legacyClasses) |
208 |
.files("api/Api.class") |
|
209 |
.run(); |
|
210 |
||
211 |
return jar; |
|
212 |
} |
|
213 |
||
214 |
@SupportedAnnotationTypes("*") |
|
215 |
public static class ProcessorImpl extends AbstractProcessor { |
|
216 |
@Override |
|
217 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
218 |
return false; |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
@Test |
|
37758 | 223 |
public void testClassOutputVisibleForIncrementalCompilation(Path base) throws Exception { |
36526 | 224 |
Path moduleSrc = base.resolve("module-src"); |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
225 |
Path m1 = moduleSrc.resolve("m"); |
36526 | 226 |
|
227 |
Path classes = base.resolve("classes"); |
|
228 |
||
229 |
Files.createDirectories(classes); |
|
230 |
||
231 |
tb.writeJavaFiles(m1, |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
40308
diff
changeset
|
232 |
"module m { exports impl; }", |
36526 | 233 |
"package impl; public class Impl { }", |
234 |
"package src; public class Src { }", |
|
235 |
"package test; public class TestCP extends impl.Impl { }", |
|
236 |
"package test; public class TestSP extends src.Src { }"); |
|
237 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
238 |
new JavacTask(tb) |
36526 | 239 |
.outdir(classes) |
240 |
.files(m1.resolve("impl").resolve("Impl.java")) |
|
241 |
.run() |
|
242 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
243 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 244 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
245 |
new JavacTask(tb) |
36526 | 246 |
.outdir(classes) |
247 |
.files(m1.resolve("module-info.java")) |
|
248 |
.run() |
|
249 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
250 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 251 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
252 |
new JavacTask(tb) |
36526 | 253 |
.outdir(classes) |
254 |
.files(m1.resolve("test").resolve("TestCP.java")) |
|
255 |
.run() |
|
256 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
257 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 258 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
259 |
new JavacTask(tb) |
36526 | 260 |
.options("-sourcepath", m1.toString()) |
261 |
.outdir(classes) |
|
262 |
.files(m1.resolve("test").resolve("TestSP.java")) |
|
263 |
.run() |
|
264 |
.writeAll() |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
265 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 266 |
} |
267 |
} |