|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Alphabet LLC. 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 8309499 |
| 27 | + * @summary Verify that java.lang unavailable error is not swallowed when |
| 28 | + * annotation processor is used. |
| 29 | + * @library /tools/lib /tools/javac/lib |
| 30 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 31 | + * jdk.compiler/com.sun.tools.javac.main |
| 32 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 33 | + * @build NoJavaLangWithAnnotationProcessorTest JavacTestingAbstractProcessor |
| 34 | + * @run main NoJavaLangWithAnnotationProcessorTest |
| 35 | + */ |
| 36 | + |
| 37 | +import java.nio.file.*; |
| 38 | +import java.util.Set; |
| 39 | + |
| 40 | +import javax.annotation.processing.RoundEnvironment; |
| 41 | +import javax.lang.model.element.TypeElement; |
| 42 | + |
| 43 | +import toolbox.JavacTask; |
| 44 | +import toolbox.Task; |
| 45 | +import toolbox.ToolBox; |
| 46 | + |
| 47 | +public class NoJavaLangWithAnnotationProcessorTest extends JavacTestingAbstractProcessor { |
| 48 | + |
| 49 | + private static final String noJavaLangSrc = |
| 50 | + "public class NoJavaLang {\n" + |
| 51 | + " private String s;\n" + |
| 52 | + "}"; |
| 53 | + |
| 54 | + private static final String compilerErrorMessage = |
| 55 | + "compiler.err.no.java.lang"; |
| 56 | + |
| 57 | + // No-Op annotation processor |
| 58 | + @Override |
| 59 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + new NoJavaLangWithAnnotationProcessorTest().run(); |
| 65 | + } |
| 66 | + |
| 67 | + final ToolBox tb = new ToolBox(); |
| 68 | + |
| 69 | + void run() throws Exception { |
| 70 | + testCompilesNormallyWithNonEmptyBootClassPath(); |
| 71 | + testBootClassPath(); |
| 72 | + testModulePath(); |
| 73 | + } |
| 74 | + |
| 75 | + // Normal case with java.lang available |
| 76 | + void testCompilesNormallyWithNonEmptyBootClassPath() { |
| 77 | + new JavacTask(tb) |
| 78 | + .sources(noJavaLangSrc) |
| 79 | + .options("-processor", "NoJavaLangWithAnnotationProcessorTest") |
| 80 | + .run(); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + // test with bootclasspath, for as long as its around |
| 85 | + void testBootClassPath() { |
| 86 | + String[] bcpOpts = {"-XDrawDiagnostics", "-Xlint:-options", "-source", "8", "-target", "8", |
| 87 | + "-bootclasspath", ".", "-classpath", ".", |
| 88 | + "-processor", "NoJavaLangWithAnnotationProcessorTest", "-processorpath", System.getProperty("test.class.path") }; |
| 89 | + test(bcpOpts, compilerErrorMessage); |
| 90 | + } |
| 91 | + |
| 92 | + // test with module path |
| 93 | + void testModulePath() throws Exception { |
| 94 | + // need to ensure there is an empty java.base to avoid different error message |
| 95 | + Files.createDirectories(Paths.get("modules/java.base")); |
| 96 | + new JavacTask(tb) |
| 97 | + .sources("module java.base { }", |
| 98 | + "package java.lang; public class Object {}") |
| 99 | + .outdir("modules/java.base") |
| 100 | + .run(); |
| 101 | + |
| 102 | + Files.delete(Paths.get("modules", "java.base", "java", "lang", "Object.class")); |
| 103 | + |
| 104 | + String[] mpOpts = {"-XDrawDiagnostics", "--system", "none", "--module-path", "modules", |
| 105 | + "-processor", "NoJavaLangWithAnnotationProcessorTest", "-processorpath", System.getProperty("test.class.path") }; |
| 106 | + test(mpOpts, compilerErrorMessage); |
| 107 | + } |
| 108 | + |
| 109 | + private void test(String[] options, String expect) { |
| 110 | + System.err.println("Testing " + java.util.Arrays.toString(options)); |
| 111 | + |
| 112 | + String out = new JavacTask(tb) |
| 113 | + .options(options) |
| 114 | + .sources(noJavaLangSrc) |
| 115 | + .run(Task.Expect.FAIL, 1) |
| 116 | + .writeAll() |
| 117 | + .getOutput(Task.OutputKind.DIRECT); |
| 118 | + |
| 119 | + if (!out.contains(expect)) { |
| 120 | + throw new AssertionError("javac generated error output is not correct"); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments