Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/runtime/ClassFile/PreviewVersion.java
40941 views
1
/*
2
* Copyright (c) 2018, 2021, 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 8198908
27
* @summary Check that preview minor version and --enable-preview are handled
28
* correctly.
29
* @modules java.base/jdk.internal.misc
30
* @library /test/lib
31
* @run main PreviewVersion
32
*/
33
34
import java.io.File;
35
import jdk.test.lib.compiler.InMemoryJavaCompiler;
36
import jdk.test.lib.ByteCodeLoader;
37
import jdk.test.lib.process.OutputAnalyzer;
38
import jdk.test.lib.process.ProcessTools;
39
import jdk.test.lib.helpers.ClassFileInstaller;
40
41
public class PreviewVersion {
42
43
public static void main(String args[]) throws Throwable {
44
System.out.println("Regression test for bug 8198908");
45
46
byte klassbuf[] = InMemoryJavaCompiler.compile("PVTest",
47
"public class PVTest { " +
48
"public static void main(String argv[]) { " +
49
"System.out.println(\"Hi!\"); } }");
50
51
// Set class's minor version to 65535.
52
klassbuf[4] = -1;
53
klassbuf[5] = -1;
54
55
// Run the test. This should fail because --enable-preview is not specified.
56
ClassFileInstaller.writeClassToDisk("PVTest", klassbuf, System.getProperty("test.classes"));
57
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
58
"-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest");
59
OutputAnalyzer oa = new OutputAnalyzer(pb.start());
60
oa.shouldContain("Preview features are not enabled");
61
oa.shouldHaveExitValue(1);
62
63
// This should be successful because --enable-preview is specified.
64
pb = ProcessTools.createJavaProcessBuilder("--enable-preview",
65
"-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest");
66
oa = new OutputAnalyzer(pb.start());
67
oa.shouldContain("Hi!");
68
oa.shouldHaveExitValue(0);
69
70
// Test -Xlog:class+preview
71
pb = ProcessTools.createJavaProcessBuilder("--enable-preview", "-Xlog:class+preview",
72
"-cp", "." + File.pathSeparator + System.getProperty("test.classes"), "PVTest");
73
oa = new OutputAnalyzer(pb.start());
74
oa.shouldContain("[info][class,preview] Loading class PVTest that depends on preview features");
75
oa.shouldHaveExitValue(0);
76
77
// Subtract 1 from class's major version. The class should fail to load
78
// because its major_version does not match the JVM current version.
79
int prev_major_version = (klassbuf[6] << 8 | klassbuf[7]) - 1;
80
klassbuf[6] = (byte)((prev_major_version >> 8) & 0xff);
81
klassbuf[7] = (byte)(prev_major_version & 0xff);
82
try {
83
ByteCodeLoader.load("PVTest", klassbuf);
84
throw new RuntimeException("UnsupportedClassVersionError exception not thrown");
85
} catch (java.lang.UnsupportedClassVersionError e) {
86
if (!e.getMessage().contains("compiled with preview features that are unsupported")) {
87
throw new RuntimeException(
88
"Wrong UnsupportedClassVersionError exception: " + e.getMessage());
89
}
90
}
91
92
// Set class's major version to 45. The class should load because class
93
// version 45.65535 is valid.
94
klassbuf[6] = 0;
95
klassbuf[7] = 45;
96
try {
97
ByteCodeLoader.load("PVTest", klassbuf);
98
} catch (java.lang.UnsupportedClassVersionError e) {
99
throw new RuntimeException(
100
"Unexpected UnsupportedClassVersionError exception thrown: " + e.getMessage());
101
}
102
103
// Check that a class with a recent older major version > JDK-11 and a minor version
104
// that is neither 0 nor 65535 fails to load.
105
klassbuf[6] = 0;
106
klassbuf[7] = 56;
107
klassbuf[4] = 0;
108
klassbuf[5] = 2;
109
try {
110
ByteCodeLoader.load("PVTest", klassbuf);
111
throw new RuntimeException("UnsupportedClassVersionError exception not thrown");
112
} catch (java.lang.UnsupportedClassVersionError e) {
113
if (!e.getMessage().contains("was compiled with an invalid non-zero minor version")) {
114
throw new RuntimeException(
115
"Wrong UnsupportedClassVersionError exception: " + e.getMessage());
116
}
117
}
118
}
119
}
120
121