25 * @test |
25 * @test |
26 * @summary tests for multi-module mode compilation |
26 * @summary tests for multi-module mode compilation |
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.main |
31 * jdk.compiler/com.sun.tools.javac.main |
|
32 * jdk.compiler/com.sun.tools.javac.processing |
31 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase |
33 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase |
32 * @run main XModuleTest |
34 * @run main XModuleTest |
33 */ |
35 */ |
34 |
36 |
35 import java.nio.file.Path; |
37 import java.nio.file.Path; |
36 import java.util.Arrays; |
38 import java.util.Arrays; |
37 import java.util.List; |
39 import java.util.List; |
38 |
40 import java.util.Set; |
|
41 |
|
42 import javax.annotation.processing.AbstractProcessor; |
|
43 import javax.annotation.processing.RoundEnvironment; |
|
44 import javax.annotation.processing.SupportedAnnotationTypes; |
|
45 import javax.lang.model.SourceVersion; |
|
46 import javax.lang.model.element.ModuleElement; |
|
47 import javax.lang.model.element.TypeElement; |
|
48 import javax.lang.model.util.Elements; |
|
49 |
|
50 import com.sun.tools.javac.code.Symtab; |
|
51 import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
39 import toolbox.JavacTask; |
52 import toolbox.JavacTask; |
40 import toolbox.ModuleBuilder; |
53 import toolbox.ModuleBuilder; |
41 import toolbox.Task; |
54 import toolbox.Task; |
42 import toolbox.TestRunner; |
55 import toolbox.Task.Expect; |
43 import toolbox.ToolBox; |
|
44 |
56 |
45 public class XModuleTest extends ModuleTestBase { |
57 public class XModuleTest extends ModuleTestBase { |
46 |
58 |
47 public static void main(String... args) throws Exception { |
59 public static void main(String... args) throws Exception { |
48 new XModuleTest().runTests(); |
60 new XModuleTest().runTests(); |
109 //note: avoiding use of java.base, as that gets special handling on some places: |
121 //note: avoiding use of java.base, as that gets special handling on some places: |
110 tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element, p.Other { }"); |
122 tb.writeJavaFiles(src, "package javax.lang.model.element; public interface Extra extends Element, p.Other { }"); |
111 Path classes = base.resolve("classes"); |
123 Path classes = base.resolve("classes"); |
112 tb.createDirectories(classes); |
124 tb.createDirectories(classes); |
113 |
125 |
114 String log = new JavacTask(tb) |
126 List<String> log = new JavacTask(tb) |
115 .options("-Xmodule:java.compiler", "--class-path", cpClasses.toString()) |
127 .options("-Xmodule:java.compiler", |
|
128 "--class-path", cpClasses.toString(), |
|
129 "-XDrawDiagnostics") |
116 .outdir(classes) |
130 .outdir(classes) |
117 .files(src.resolve("javax/lang/model/element/Extra.java")) |
131 .files(src.resolve("javax/lang/model/element/Extra.java")) |
118 .run() |
132 .run(Expect.FAIL) |
119 .writeAll() |
133 .writeAll() |
120 .getOutput(Task.OutputKind.DIRECT); |
134 .getOutputLines(Task.OutputKind.DIRECT); |
121 |
135 |
122 if (!log.isEmpty()) |
136 List<String> expectedOut = Arrays.asList( |
|
137 "Extra.java:1:76: compiler.err.doesnt.exist: p", |
|
138 "1 error" |
|
139 ); |
|
140 |
|
141 if (!expectedOut.equals(log)) |
123 throw new Exception("expected output not found: " + log); |
142 throw new Exception("expected output not found: " + log); |
124 } |
143 } |
125 |
144 |
126 @Test |
145 @Test |
127 public void testNoModuleInfoOnSourcePath(Path base) throws Exception { |
146 public void testNoModuleInfoOnSourcePath(Path base) throws Exception { |
300 "-Xmodule:m1") |
319 "-Xmodule:m1") |
301 .files(findJavaFiles(src)) |
320 .files(findJavaFiles(src)) |
302 .run() |
321 .run() |
303 .writeAll(); |
322 .writeAll(); |
304 } |
323 } |
|
324 |
|
325 @Test |
|
326 public void testUnnamedIsolation(Path base) throws Exception { |
|
327 //note: avoiding use of java.base, as that gets special handling on some places: |
|
328 Path sourcePath = base.resolve("source-path"); |
|
329 tb.writeJavaFiles(sourcePath, "package src; public class Src {}"); |
|
330 |
|
331 Path classPathSrc = base.resolve("class-path-src"); |
|
332 tb.writeJavaFiles(classPathSrc, "package cp; public class CP { }"); |
|
333 Path classPath = base.resolve("classPath"); |
|
334 tb.createDirectories(classPath); |
|
335 |
|
336 String cpLog = new JavacTask(tb) |
|
337 .outdir(classPath) |
|
338 .files(findJavaFiles(classPathSrc)) |
|
339 .run() |
|
340 .writeAll() |
|
341 .getOutput(Task.OutputKind.DIRECT); |
|
342 |
|
343 if (!cpLog.isEmpty()) |
|
344 throw new Exception("expected output not found: " + cpLog); |
|
345 |
|
346 Path modulePathSrc = base.resolve("module-path-src"); |
|
347 tb.writeJavaFiles(modulePathSrc, |
|
348 "module m {}", |
|
349 "package m; public class M {}"); |
|
350 Path modulePath = base.resolve("modulePath"); |
|
351 tb.createDirectories(modulePath.resolve("m")); |
|
352 |
|
353 String modLog = new JavacTask(tb) |
|
354 .outdir(modulePath.resolve("m")) |
|
355 .files(findJavaFiles(modulePathSrc)) |
|
356 .run() |
|
357 .writeAll() |
|
358 .getOutput(Task.OutputKind.DIRECT); |
|
359 |
|
360 if (!modLog.isEmpty()) |
|
361 throw new Exception("expected output not found: " + modLog); |
|
362 |
|
363 Path src = base.resolve("src"); |
|
364 tb.writeJavaFiles(src, "package m; public class Extra { }"); |
|
365 Path classes = base.resolve("classes"); |
|
366 tb.createDirectories(classes); |
|
367 |
|
368 String log = new JavacTask(tb) |
|
369 .options("-Xmodule:m", |
|
370 "--class-path", classPath.toString(), |
|
371 "--source-path", sourcePath.toString(), |
|
372 "--module-path", modulePath.toString(), |
|
373 "-XDaccessInternalAPI=true", |
|
374 "--processor-path", System.getProperty("test.classes"), |
|
375 "-processor", CheckModuleContentProcessing.class.getName()) |
|
376 .outdir(classes) |
|
377 .files(findJavaFiles(sourcePath)) |
|
378 .run() |
|
379 .writeAll() |
|
380 .getOutput(Task.OutputKind.DIRECT); |
|
381 |
|
382 if (!log.isEmpty()) |
|
383 throw new Exception("expected output not found: " + log); |
|
384 } |
|
385 |
|
386 @SupportedAnnotationTypes("*") |
|
387 public static final class CheckModuleContentProcessing extends AbstractProcessor { |
|
388 |
|
389 @Override |
|
390 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
391 Symtab syms = Symtab.instance(((JavacProcessingEnvironment) processingEnv).getContext()); |
|
392 Elements elements = processingEnv.getElementUtils(); |
|
393 ModuleElement unnamedModule = syms.unnamedModule; |
|
394 ModuleElement mModule = elements.getModuleElement("m"); |
|
395 |
|
396 assertNonNull("mModule found", mModule); |
|
397 assertNonNull("src.Src from m", elements.getTypeElement(mModule, "src.Src")); |
|
398 assertNull("cp.CP not from m", elements.getTypeElement(mModule, "cp.CP")); |
|
399 assertNull("src.Src not from unnamed", elements.getTypeElement(unnamedModule, "src.Src")); |
|
400 assertNonNull("cp.CP from unnamed", elements.getTypeElement(unnamedModule, "cp.CP")); |
|
401 |
|
402 return false; |
|
403 } |
|
404 |
|
405 @Override |
|
406 public SourceVersion getSupportedSourceVersion() { |
|
407 return SourceVersion.latest(); |
|
408 } |
|
409 |
|
410 } |
|
411 |
|
412 private static void assertNonNull(String msg, Object val) { |
|
413 if (val == null) { |
|
414 throw new AssertionError(msg); |
|
415 } |
|
416 } |
|
417 |
|
418 private static void assertNull(String msg, Object val) { |
|
419 if (val != null) { |
|
420 throw new AssertionError(msg); |
|
421 } |
|
422 } |
305 } |
423 } |