|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | + * @summary VerifyError after JDK-8369654, incorrect supertype |
| 27 | + * @library /tools/lib |
| 28 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 29 | + * jdk.compiler/com.sun.tools.javac.main |
| 30 | + * jdk.compiler/com.sun.tools.javac.util |
| 31 | + * jdk.compiler/com.sun.tools.javac.code |
| 32 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 33 | + * @run main VerifierErrorWrongSuperTypeTest |
| 34 | + */ |
| 35 | + |
| 36 | +import java.util.*; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.nio.file.Paths; |
| 42 | + |
| 43 | +import toolbox.TestRunner; |
| 44 | +import toolbox.ToolBox; |
| 45 | +import toolbox.JavaTask; |
| 46 | +import toolbox.JavacTask; |
| 47 | +import toolbox.Task; |
| 48 | +import toolbox.Task.OutputKind; |
| 49 | + |
| 50 | +public class VerifierErrorWrongSuperTypeTest extends TestRunner { |
| 51 | + ToolBox tb; |
| 52 | + |
| 53 | + VerifierErrorWrongSuperTypeTest() { |
| 54 | + super(System.err); |
| 55 | + tb = new ToolBox(); |
| 56 | + } |
| 57 | + |
| 58 | + protected void runTests() throws Exception { |
| 59 | + runTests(m -> new Object[]{Paths.get(m.getName())}); |
| 60 | + } |
| 61 | + |
| 62 | + Path[] findJavaFiles(Path... paths) throws IOException { |
| 63 | + return tb.findJavaFiles(paths); |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String... args) throws Exception { |
| 67 | + VerifierErrorWrongSuperTypeTest t = new VerifierErrorWrongSuperTypeTest(); |
| 68 | + t.runTests(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testCompatibilityAfterMakingSuperclassSealed(Path base) throws Exception { |
| 73 | + Path src = base.resolve("src"); |
| 74 | + Path pkg = src.resolve("p"); |
| 75 | + Path v = pkg.resolve("V"); |
| 76 | + tb.writeJavaFiles(v, |
| 77 | + """ |
| 78 | + package p; |
| 79 | + public abstract class V {} |
| 80 | + """ |
| 81 | + ); |
| 82 | + Path d = pkg.resolve("D"); |
| 83 | + tb.writeJavaFiles(d, |
| 84 | + """ |
| 85 | + package p; |
| 86 | + public abstract class D extends V implements Cloneable {} |
| 87 | + """ |
| 88 | + ); |
| 89 | + Path a = pkg.resolve("A"); |
| 90 | + tb.writeJavaFiles(a, |
| 91 | + """ |
| 92 | + package p; |
| 93 | + public class A extends V implements Cloneable {} |
| 94 | + """ |
| 95 | + ); |
| 96 | + Path t = src.resolve("T"); |
| 97 | + tb.writeJavaFiles(t, |
| 98 | + """ |
| 99 | + import p.A; |
| 100 | + import p.D; |
| 101 | + import p.V; |
| 102 | + class T { |
| 103 | + public static void main(String[] args) { |
| 104 | + new T().foo(false, null); |
| 105 | + } |
| 106 | + void foo(boolean b, D d) { |
| 107 | + V u = b ? d : new A(); |
| 108 | + g(u); |
| 109 | + } |
| 110 | + void g(V u) {} |
| 111 | + } |
| 112 | + """ |
| 113 | + ); |
| 114 | + Path out = base.resolve("out"); |
| 115 | + Files.createDirectories(out); |
| 116 | + new JavacTask(tb) |
| 117 | + .outdir(out) |
| 118 | + .files(findJavaFiles(src)) |
| 119 | + .run(); |
| 120 | + |
| 121 | + try { |
| 122 | + new JavaTask(tb) |
| 123 | + .classpath(out.toString()) |
| 124 | + .classArgs("T") |
| 125 | + .run(); |
| 126 | + } catch (Throwable error) { |
| 127 | + throw new AssertionError("execution failed"); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments