|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | + * @bug 8210649 |
| 27 | + * @summary Check that diagnostics can be printed even after the compilation |
| 28 | + * stopped. |
| 29 | + * @library /tools/lib |
| 30 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 31 | + * jdk.compiler/com.sun.tools.javac.main |
| 32 | + * @build toolbox.TestRunner toolbox.ToolBox T8210649 |
| 33 | + * @run main T8210649 |
| 34 | + */ |
| 35 | + |
| 36 | +import java.io.OutputStream; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.nio.file.Paths; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Set; |
| 42 | +import java.util.stream.Collectors; |
| 43 | + |
| 44 | +import javax.annotation.processing.AbstractProcessor; |
| 45 | +import javax.annotation.processing.RoundEnvironment; |
| 46 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 47 | +import javax.lang.model.SourceVersion; |
| 48 | +import javax.lang.model.element.TypeElement; |
| 49 | +import javax.tools.DiagnosticCollector; |
| 50 | +import javax.tools.JavaCompiler; |
| 51 | +import javax.tools.JavaFileObject; |
| 52 | +import javax.tools.StandardJavaFileManager; |
| 53 | +import javax.tools.ToolProvider; |
| 54 | + |
| 55 | +import java.io.IOException; |
| 56 | +import java.util.ArrayList; |
| 57 | +import java.util.function.Consumer; |
| 58 | +import javax.tools.DiagnosticListener; |
| 59 | +import toolbox.JavacTask; |
| 60 | +import toolbox.TestRunner; |
| 61 | +import toolbox.TestRunner.Test; |
| 62 | +import toolbox.ToolBox; |
| 63 | + |
| 64 | +public class T8210649 extends TestRunner { |
| 65 | + |
| 66 | + public static void main(String... args) throws Exception { |
| 67 | + new T8210649().runTests(m -> new Object[] { Paths.get(m.getName()) }); |
| 68 | + } |
| 69 | + |
| 70 | + private final ToolBox tb = new ToolBox(); |
| 71 | + |
| 72 | + public T8210649() { |
| 73 | + super(System.err); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testErrorsAfter(Path outerBase) throws Exception { |
| 78 | + Path libSrc = outerBase.resolve("libsrc"); |
| 79 | + tb.writeJavaFiles(libSrc, |
| 80 | + "package lib;\n" + |
| 81 | + "@lib2.Helper(1)\n" + |
| 82 | + "public @interface Lib {\n" + |
| 83 | + "}", |
| 84 | + "package lib2;\n" + |
| 85 | + "public @interface Helper {\n" + |
| 86 | + " public int value() default 0;\n" + |
| 87 | + "}"); |
| 88 | + Path libClasses = outerBase.resolve("libclasses"); |
| 89 | + Files.createDirectories(libClasses); |
| 90 | + new JavacTask(tb) |
| 91 | + .outdir(libClasses.toString()) |
| 92 | + .files(tb.findJavaFiles(libSrc)) |
| 93 | + .run() |
| 94 | + .writeAll(); |
| 95 | + Files.delete(libClasses.resolve("lib2").resolve("Helper.class")); |
| 96 | + Path src = outerBase.resolve("src"); |
| 97 | + tb.writeJavaFiles(src, |
| 98 | + "package t;\n" + |
| 99 | + "import lib.Lib;\n" + |
| 100 | + "public class T {\n" + |
| 101 | + " Undefined<String> undef() {}\n" + |
| 102 | + "}"); |
| 103 | + Path classes = outerBase.resolve("classes"); |
| 104 | + Files.createDirectories(classes); |
| 105 | + Path tTarget = classes.resolve("t").resolve("T.class"); |
| 106 | + Files.createDirectories(tTarget.getParent()); |
| 107 | + try (OutputStream in = Files.newOutputStream(tTarget)) {} |
| 108 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 109 | + try (StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null)) { |
| 110 | + Consumer<DiagnosticListener<JavaFileObject>> runCompiler = dl -> { |
| 111 | + try { |
| 112 | + List<String> options = List.of("-processor", "T8210649$P", |
| 113 | + "-processorpath", System.getProperty("test.classes"), |
| 114 | + "-classpath", libClasses.toString() + ":" + classes.toString(), |
| 115 | + "-d", classes.toString()); |
| 116 | + ToolProvider.getSystemJavaCompiler() |
| 117 | + .getTask(null, null, dl, options, null, sjfm.getJavaFileObjects(tb.findJavaFiles(src))) |
| 118 | + .call(); |
| 119 | + } catch (IOException ex) { |
| 120 | + throw new IllegalStateException(ex); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + List<String> expected = new ArrayList<>(); |
| 125 | + runCompiler.accept(d -> expected.add(d.getMessage(null))); |
| 126 | + |
| 127 | + DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>(); |
| 128 | + runCompiler.accept(dc); |
| 129 | + |
| 130 | + List<String> actual = dc.getDiagnostics() |
| 131 | + .stream() |
| 132 | + .map(d -> d.getMessage(null)) |
| 133 | + .collect(Collectors.toList()); |
| 134 | + if (!expected.equals(actual)) { |
| 135 | + throw new IllegalStateException("Unexpected output: " + actual); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @SupportedAnnotationTypes("*") |
| 141 | + public static class P extends AbstractProcessor { |
| 142 | + @Override |
| 143 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public SourceVersion getSupportedSourceVersion() { |
| 149 | + return SourceVersion.latest(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments