|
29 | 29 | * @modules jdk.compiler/com.sun.tools.javac.api
|
30 | 30 | * jdk.compiler/com.sun.tools.javac.launcher
|
31 | 31 | * jdk.compiler/com.sun.tools.javac.main
|
| 32 | + * jdk.jdeps/com.sun.tools.classfile |
32 | 33 | * @build toolbox.JavaTask toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
|
33 | 34 | * @run main SourceLauncherTest
|
34 | 35 | */
|
35 | 36 |
|
| 37 | +import com.sun.tools.classfile.Attribute; |
| 38 | +import com.sun.tools.classfile.Attributes; |
| 39 | +import com.sun.tools.classfile.ClassFile; |
| 40 | +import com.sun.tools.classfile.ClassWriter; |
| 41 | +import com.sun.tools.classfile.ConstantPool; |
| 42 | +import com.sun.tools.classfile.ConstantPool.CPInfo; |
| 43 | +import com.sun.tools.classfile.ModuleResolution_attribute; |
36 | 44 | import java.io.ByteArrayOutputStream;
|
37 | 45 | import java.io.File;
|
38 | 46 | import java.io.IOException;
|
| 47 | +import java.io.OutputStream; |
39 | 48 | import java.io.PrintStream;
|
40 | 49 | import java.io.PrintWriter;
|
41 | 50 | import java.io.StringWriter;
|
|
45 | 54 | import java.nio.file.Paths;
|
46 | 55 | import java.util.ArrayList;
|
47 | 56 | import java.util.Collections;
|
| 57 | +import java.util.HashMap; |
| 58 | +import java.util.Map; |
48 | 59 | import java.util.List;
|
49 | 60 | import java.util.Properties;
|
50 | 61 | import java.util.regex.Pattern;
|
@@ -655,6 +666,83 @@ public void testTargetException1(Path base) throws IOException {
|
655 | 666 | "at Thrower.main(Thrower.java:4)");
|
656 | 667 | }
|
657 | 668 |
|
| 669 | + @Test |
| 670 | + public void testNoDuplicateIncubatorWarning(Path base) throws Exception { |
| 671 | + Path module = base.resolve("lib"); |
| 672 | + Path moduleSrc = module.resolve("src"); |
| 673 | + Path moduleClasses = module.resolve("classes"); |
| 674 | + Files.createDirectories(moduleClasses); |
| 675 | + tb.cleanDirectory(moduleClasses); |
| 676 | + tb.writeJavaFiles(moduleSrc, "module test {}"); |
| 677 | + new JavacTask(tb) |
| 678 | + .outdir(moduleClasses) |
| 679 | + .files(tb.findJavaFiles(moduleSrc)) |
| 680 | + .run() |
| 681 | + .writeAll(); |
| 682 | + markModuleAsIncubator(moduleClasses.resolve("module-info.class")); |
| 683 | + tb.writeJavaFiles(base, "public class Main { public static void main(String... args) {}}"); |
| 684 | + String log = new JavaTask(tb) |
| 685 | + .vmOptions("--module-path", moduleClasses.toString(), |
| 686 | + "--add-modules", "test") |
| 687 | + .className(base.resolve("Main.java").toString()) |
| 688 | + .run(Task.Expect.SUCCESS) |
| 689 | + .writeAll() |
| 690 | + .getOutput(Task.OutputKind.STDERR); |
| 691 | + |
| 692 | + int numberOfWarnings = log.split("WARNING").length - 1; |
| 693 | + |
| 694 | + if (log.contains("warning:") || numberOfWarnings != 1) { |
| 695 | + error("Unexpected warning in error output: " + log); |
| 696 | + } |
| 697 | + |
| 698 | + List<String> compileLog = new JavacTask(tb) |
| 699 | + .options("--module-path", moduleClasses.toString(), |
| 700 | + "--add-modules", "test", |
| 701 | + "-XDrawDiagnostics", |
| 702 | + "-XDsourceLauncher", |
| 703 | + "-XDshould-stop.at=FLOW") |
| 704 | + .files(base.resolve("Main.java").toString()) |
| 705 | + .run(Task.Expect.SUCCESS) |
| 706 | + .writeAll() |
| 707 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 708 | + |
| 709 | + List<String> expectedOutput = List.of( |
| 710 | + "- compiler.warn.incubating.modules: test", |
| 711 | + "1 warning" |
| 712 | + ); |
| 713 | + |
| 714 | + if (!expectedOutput.equals(compileLog)) { |
| 715 | + error("Unexpected options : " + compileLog); |
| 716 | + } |
| 717 | + } |
| 718 | + //where: |
| 719 | + private static void markModuleAsIncubator(Path moduleInfoFile) throws Exception { |
| 720 | + ClassFile cf = ClassFile.read(moduleInfoFile); |
| 721 | + List<CPInfo> newPool = new ArrayList<>(); |
| 722 | + newPool.add(null); |
| 723 | + cf.constant_pool.entries().forEach(newPool::add); |
| 724 | + int moduleResolutionIndex = newPool.size(); |
| 725 | + newPool.add(new ConstantPool.CONSTANT_Utf8_info(Attribute.ModuleResolution)); |
| 726 | + Map<String, Attribute> newAttributes = new HashMap<>(cf.attributes.map); |
| 727 | + newAttributes.put(Attribute.ModuleResolution, |
| 728 | + new ModuleResolution_attribute(moduleResolutionIndex, |
| 729 | + ModuleResolution_attribute.WARN_INCUBATING)); |
| 730 | + ClassFile newClassFile = new ClassFile(cf.magic, |
| 731 | + cf.minor_version, |
| 732 | + cf.major_version, |
| 733 | + new ConstantPool(newPool.toArray(new CPInfo[0])), |
| 734 | + cf.access_flags, |
| 735 | + cf.this_class, |
| 736 | + cf.super_class, |
| 737 | + cf.interfaces, |
| 738 | + cf.fields, |
| 739 | + cf.methods, |
| 740 | + new Attributes(newAttributes)); |
| 741 | + try (OutputStream out = Files.newOutputStream(moduleInfoFile)) { |
| 742 | + new ClassWriter().write(newClassFile, out); |
| 743 | + } |
| 744 | + } |
| 745 | + |
658 | 746 | Result run(Path file, List<String> runtimeArgs, List<String> appArgs) {
|
659 | 747 | List<String> args = new ArrayList<>();
|
660 | 748 | args.add(file.toString());
|
|
0 commit comments