|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2020, Red Hat, Inc. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @test |
| 27 | + * @bug 8256809 |
| 28 | + * @summary Verify that erroneous symbols have their type fixed between rounds |
| 29 | + * @library /tools/lib /tools/javac/lib |
| 30 | + * @modules |
| 31 | + * jdk.compiler/com.sun.tools.javac.api |
| 32 | + * jdk.compiler/com.sun.tools.javac.main |
| 33 | + * @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask toolbox.TestRunner |
| 34 | + * JavacTestingAbstractProcessor |
| 35 | + * @run main ErrClassSymbolTypeFixed |
| 36 | + */ |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.Writer; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.nio.file.Paths; |
| 43 | +import java.util.Set; |
| 44 | + |
| 45 | +import javax.annotation.processing.RoundEnvironment; |
| 46 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 47 | +import javax.lang.model.element.TypeElement; |
| 48 | +import javax.lang.model.util.ElementFilter; |
| 49 | +import javax.tools.Diagnostic; |
| 50 | +import javax.tools.JavaFileObject; |
| 51 | + |
| 52 | +import toolbox.JavacTask; |
| 53 | +import toolbox.JarTask; |
| 54 | +import toolbox.Task; |
| 55 | +import toolbox.TestRunner; |
| 56 | +import toolbox.ToolBox; |
| 57 | + |
| 58 | +public class ErrClassSymbolTypeFixed extends TestRunner { |
| 59 | + |
| 60 | + private static final String A_JAVA = "package t1; public @interface A {}"; |
| 61 | + private static final String B_JAVA = "package t2; public class B {}"; |
| 62 | + private static final String C_JAVA = "package t3; import t2.B; public class C extends B {}"; |
| 63 | + private static final String D_JAVA = "import t1.A; import t3.C; import t2.B; @A public class D {}"; |
| 64 | + |
| 65 | + private ToolBox tb; |
| 66 | + |
| 67 | + public ErrClassSymbolTypeFixed() { |
| 68 | + super(System.err); |
| 69 | + tb = new ToolBox(); |
| 70 | + } |
| 71 | + |
| 72 | + public static void main(String... args) throws Exception { |
| 73 | + new ErrClassSymbolTypeFixed().runTests(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testErrorFixed() throws Exception { |
| 78 | + Path base = Paths.get("."); |
| 79 | + Path src1 = base.resolve("src1"); |
| 80 | + Path src2 = base.resolve("src2"); |
| 81 | + Path classes = base.resolve("classes"); |
| 82 | + |
| 83 | + Files.createDirectories(classes); |
| 84 | + |
| 85 | + /* |
| 86 | + * Create and compile the following: |
| 87 | + * A: annotation type |
| 88 | + * B: basic class |
| 89 | + * C: subclass of B |
| 90 | + */ |
| 91 | + tb.writeJavaFiles(src1, A_JAVA, B_JAVA, C_JAVA); |
| 92 | + |
| 93 | + new JavacTask(tb) |
| 94 | + .outdir(classes) |
| 95 | + .files(tb.findJavaFiles(src1)) |
| 96 | + .run() |
| 97 | + .writeAll() |
| 98 | + .getOutput(Task.OutputKind.DIRECT); |
| 99 | + |
| 100 | + // Create a Jar containing only C to include later in the classpath |
| 101 | + Path jarPath = base.resolve("test.jar"); |
| 102 | + new JarTask(tb, jarPath) |
| 103 | + .baseDir(classes) |
| 104 | + .files("t3") |
| 105 | + .run() |
| 106 | + .writeAll() |
| 107 | + .getOutput(Task.OutputKind.DIRECT); |
| 108 | + |
| 109 | + // Delete B from the classpath |
| 110 | + Files.delete(classes.resolve("t2").resolve("B.class")); |
| 111 | + |
| 112 | + /* |
| 113 | + * Write and compile D, which has the following properties: |
| 114 | + * - is annotated with A, causing our processor to regenerate B |
| 115 | + * - imports C before B |
| 116 | + */ |
| 117 | + tb.writeJavaFiles(src2, D_JAVA); |
| 118 | + |
| 119 | + // If the erroneous ClassSymbol is not reset between rounds, |
| 120 | + // a NullPointerException will occur later during flow analysis. |
| 121 | + new JavacTask(tb) |
| 122 | + .classpath(classes, jarPath) |
| 123 | + .options("-processor", ErrClassSymbolProcessor.class.getName(), |
| 124 | + "--processor-path", System.getProperty("test.class.path")) |
| 125 | + .outdir(classes) |
| 126 | + .files(tb.findJavaFiles(src2)) |
| 127 | + .run() |
| 128 | + .writeAll() |
| 129 | + .getOutput(Task.OutputKind.DIRECT); |
| 130 | + } |
| 131 | + |
| 132 | + @SupportedAnnotationTypes("t1.A") |
| 133 | + public static class ErrClassSymbolProcessor extends JavacTestingAbstractProcessor { |
| 134 | + |
| 135 | + @Override |
| 136 | + public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 137 | + for (TypeElement te : ElementFilter.typesIn(roundEnv.getRootElements())) { |
| 138 | + createFile(te); |
| 139 | + } |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + private void createFile(TypeElement te) { |
| 144 | + // Generate B.java when the processor reads the @A annotation on D |
| 145 | + if ("D".equals(te.getSimpleName().toString())) { |
| 146 | + try { |
| 147 | + JavaFileObject fo = processingEnv.getFiler().createSourceFile("B"); |
| 148 | + try (Writer out = fo.openWriter()) { |
| 149 | + out.write(B_JAVA); |
| 150 | + } |
| 151 | + } catch (IOException e) { |
| 152 | + messager.printMessage(Diagnostic.Kind.ERROR, "problem writing file: " + e); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments