Path: blob/master/test/hotspot/jtreg/runtime/ClassFile/PreviewVersion.java
40941 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 819890826* @summary Check that preview minor version and --enable-preview are handled27* correctly.28* @modules java.base/jdk.internal.misc29* @library /test/lib30* @run main PreviewVersion31*/3233import java.io.File;34import jdk.test.lib.compiler.InMemoryJavaCompiler;35import jdk.test.lib.ByteCodeLoader;36import jdk.test.lib.process.OutputAnalyzer;37import jdk.test.lib.process.ProcessTools;38import jdk.test.lib.helpers.ClassFileInstaller;3940public class PreviewVersion {4142public static void main(String args[]) throws Throwable {43System.out.println("Regression test for bug 8198908");4445byte klassbuf[] = InMemoryJavaCompiler.compile("PVTest",46"public class PVTest { " +47"public static void main(String argv[]) { " +48"System.out.println(\"Hi!\"); } }");4950// Set class's minor version to 65535.51klassbuf[4] = -1;52klassbuf[5] = -1;5354// Run the test. This should fail because --enable-preview is not specified.55ClassFileInstaller.writeClassToDisk("PVTest", klassbuf, System.getProperty("test.classes"));56ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(57"-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest");58OutputAnalyzer oa = new OutputAnalyzer(pb.start());59oa.shouldContain("Preview features are not enabled");60oa.shouldHaveExitValue(1);6162// This should be successful because --enable-preview is specified.63pb = ProcessTools.createJavaProcessBuilder("--enable-preview",64"-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest");65oa = new OutputAnalyzer(pb.start());66oa.shouldContain("Hi!");67oa.shouldHaveExitValue(0);6869// Test -Xlog:class+preview70pb = ProcessTools.createJavaProcessBuilder("--enable-preview", "-Xlog:class+preview",71"-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest");72oa = new OutputAnalyzer(pb.start());73oa.shouldContain("[info][class,preview] Loading class PVTest that depends on preview features");74oa.shouldHaveExitValue(0);7576// Subtract 1 from class's major version. The class should fail to load77// because its major_version does not match the JVM current version.78int prev_major_version = (klassbuf[6] << 8 | klassbuf[7]) - 1;79klassbuf[6] = (byte)((prev_major_version >> 8) & 0xff);80klassbuf[7] = (byte)(prev_major_version & 0xff);81try {82ByteCodeLoader.load("PVTest", klassbuf);83throw new RuntimeException("UnsupportedClassVersionError exception not thrown");84} catch (java.lang.UnsupportedClassVersionError e) {85if (!e.getMessage().contains("compiled with preview features that are unsupported")) {86throw new RuntimeException(87"Wrong UnsupportedClassVersionError exception: " + e.getMessage());88}89}9091// Set class's major version to 45. The class should load because class92// version 45.65535 is valid.93klassbuf[6] = 0;94klassbuf[7] = 45;95try {96ByteCodeLoader.load("PVTest", klassbuf);97} catch (java.lang.UnsupportedClassVersionError e) {98throw new RuntimeException(99"Unexpected UnsupportedClassVersionError exception thrown: " + e.getMessage());100}101102// Check that a class with a recent older major version > JDK-11 and a minor version103// that is neither 0 nor 65535 fails to load.104klassbuf[6] = 0;105klassbuf[7] = 56;106klassbuf[4] = 0;107klassbuf[5] = 2;108try {109ByteCodeLoader.load("PVTest", klassbuf);110throw new RuntimeException("UnsupportedClassVersionError exception not thrown");111} catch (java.lang.UnsupportedClassVersionError e) {112if (!e.getMessage().contains("was compiled with an invalid non-zero minor version")) {113throw new RuntimeException(114"Wrong UnsupportedClassVersionError exception: " + e.getMessage());115}116}117}118}119120121