|
| 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 8231599 |
| 27 | + * @summary Verify javac does not crash on preview classfiles from the future |
| 28 | + Java versions. |
| 29 | + * @library /tools/lib |
| 30 | + * @modules |
| 31 | + * jdk.compiler/com.sun.tools.javac.api |
| 32 | + * jdk.compiler/com.sun.tools.javac.jvm |
| 33 | + * jdk.compiler/com.sun.tools.javac.main |
| 34 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 35 | + * @run main TooNewMajorVersionTest |
| 36 | + */ |
| 37 | + |
| 38 | +import toolbox.JavacTask; |
| 39 | +import toolbox.Task; |
| 40 | +import toolbox.TestRunner; |
| 41 | +import toolbox.ToolBox; |
| 42 | + |
| 43 | +import com.sun.tools.javac.jvm.ClassFile.Version; |
| 44 | + |
| 45 | +import java.io.RandomAccessFile; |
| 46 | +import java.nio.file.Files; |
| 47 | +import java.nio.file.Path; |
| 48 | +import java.nio.file.Paths; |
| 49 | +import java.util.List; |
| 50 | + |
| 51 | +public class TooNewMajorVersionTest extends TestRunner { |
| 52 | + |
| 53 | + protected ToolBox tb; |
| 54 | + |
| 55 | + TooNewMajorVersionTest() { |
| 56 | + super(System.err); |
| 57 | + tb = new ToolBox(); |
| 58 | + } |
| 59 | + |
| 60 | + public static void main(String... args) throws Exception { |
| 61 | + TooNewMajorVersionTest t = new TooNewMajorVersionTest(); |
| 62 | + t.runTests(); |
| 63 | + } |
| 64 | + |
| 65 | + protected void runTests() throws Exception { |
| 66 | + runTests(m -> new Object[] { Paths.get(m.getName()) }); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void brokenMajorVersionWithPreview(Path base) throws Exception { |
| 71 | + Path src = base.resolve("src"); |
| 72 | + tb.writeJavaFiles(src, """ |
| 73 | + class C { |
| 74 | + private Object o = null; |
| 75 | + private boolean b = o instanceof String s; |
| 76 | + } |
| 77 | + """); |
| 78 | + Path classes = base.resolve("classes"); |
| 79 | + |
| 80 | + Files.createDirectories(classes); |
| 81 | + |
| 82 | + for (int upgrade = 1; upgrade < 3; upgrade++) { |
| 83 | + new JavacTask(tb) |
| 84 | + .outdir(classes) |
| 85 | + .options("-XDforcePreview", |
| 86 | + "--enable-preview", |
| 87 | + "--release", String.valueOf(Runtime.version().feature())) |
| 88 | + .files(tb.findJavaFiles(src)) |
| 89 | + .run() |
| 90 | + .writeAll(); |
| 91 | + |
| 92 | + Path classfile = classes.resolve("C.class"); |
| 93 | + int wrongMajor; |
| 94 | + |
| 95 | + try (RandomAccessFile f = new RandomAccessFile(classfile.toFile(), "rw")) { |
| 96 | + f.readInt(); |
| 97 | + short minor = f.readShort(); |
| 98 | + if (minor != (-1)) { |
| 99 | + throw new AssertionError("Unexpected minor version: " + minor); |
| 100 | + } |
| 101 | + long point = f.getFilePointer(); |
| 102 | + short major = f.readShort(); |
| 103 | + f.seek(point); |
| 104 | + f.writeShort(wrongMajor = major + upgrade); |
| 105 | + } |
| 106 | + |
| 107 | + Path test = base.resolve("test"); |
| 108 | + tb.writeJavaFiles(test, "class Test extends C {}"); |
| 109 | + Path testClasses = base.resolve("classes"); |
| 110 | + |
| 111 | + Files.createDirectories(testClasses); |
| 112 | + |
| 113 | + for (String extraOption : new String[] {"-XDignored", "--enable-preview"}) { |
| 114 | + List<String> log = new JavacTask(tb) |
| 115 | + .outdir(testClasses) |
| 116 | + .options("-XDrawDiagnostics", |
| 117 | + "-classpath", classes.toString(), |
| 118 | + "--release", String.valueOf(Runtime.version().feature()), |
| 119 | + extraOption) |
| 120 | + .files(tb.findJavaFiles(test)) |
| 121 | + .run(Task.Expect.FAIL) |
| 122 | + .writeAll() |
| 123 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 124 | + List<String> expected = List.of( |
| 125 | + "Test.java:1:20: compiler.err.cant.access: C, (compiler.misc.bad.class.file.header: C.class, (compiler.misc.wrong.version: " + wrongMajor + ", 65535, " + Version.MAX().major + ", 0))", |
| 126 | + "1 error" |
| 127 | + ); |
| 128 | + if (!log.equals(expected)) |
| 129 | + throw new Exception("expected output not found" + log); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments