Skip to content

Commit

Permalink
8292060: Make ClassFileVersionTest.java version adapting
Browse files Browse the repository at this point in the history
Reviewed-by: hseigel
  • Loading branch information
jddarcy committed Aug 9, 2022
1 parent 4040927 commit ae1a9a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 106 deletions.
45 changes: 30 additions & 15 deletions test/hotspot/jtreg/runtime/ClassFile/ClassFileVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,34 @@
* @summary Test getting the class file version for java.lang.Class API
* @modules java.base/java.lang:open
* @compile classFileVersions.jcod
* @compile --enable-preview -source ${jdk.version} ClassFileVersionTest.java
* @run main/othervm --enable-preview ClassFileVersionTest
*/

import java.lang.reflect.*;

public class ClassFileVersionTest {

public static final int LOWER_16 = 0x0000_FFFF;
/*
* Include a use of a preview API so that the minor class file
* version of the class file for this class gets set during
* compilation. If a particular class becomes non-preview, any
* currently preview class can be substituted in.
*/
private static final Class<?> PREVIEW_API = java.lang.foreign.MemoryAddress.class;
static Method m;

public static void testIt(String className, int expectedResult) throws Exception {
Class<?> testClass = Class.forName(className);
testIt(Class.forName(className), expectedResult);
}

public static void testIt(Class<?> testClass, int expectedResult) throws Exception {
int ver = (int)m.invoke(testClass);
if (ver != expectedResult) {
int exp_minor = (expectedResult >> 16) & 0x0000FFFF;
int exp_major = expectedResult & 0x0000FFFF;
int got_minor = (ver >> 16) & 0x0000FFFF;
int got_major = ver & 0x0000FFFF;
int exp_minor = (expectedResult >> 16) & LOWER_16;
int exp_major = expectedResult & LOWER_16;
int got_minor = (ver >> 16) & LOWER_16;
int got_major = ver & LOWER_16;
throw new RuntimeException(
"Expected " + exp_minor + ":" + exp_major + " but got " + got_minor + ":" + got_major);
}
Expand All @@ -55,35 +66,39 @@ public static void main(String argv[]) throws Throwable {
m = cl.getDeclaredMethod("getClassFileVersion", new Class[0]);
m.setAccessible(true);

// Class file version is stored like "64.0".
int latestMajor = (int)Double.parseDouble(System.getProperty("java.class.version"));

testIt(Object.class, latestMajor);
// ClassFileVersionTest use preview features so its minor version should be 0xFFFF
testIt(ClassFileVersionTest.class, (~LOWER_16) | latestMajor);
testIt("Version64", 64);
testIt("Version59", 59);
testIt("Version45_3", 0x3002D); // 3:45
// test minor version of 65535.
testIt("Version64_65535", 0xFFFF0040); // 0xFFFF0040 = 65535:64
testIt("Version45_3", 0x0003_002D); // 3:45

// test primitive array. should return latest version.
int ver = (int)m.invoke((new int[3]).getClass());
if (ver != 64) {
int got_minor = (ver >> 16) & 0x0000FFFF;
int got_major = ver & 0x0000FFFF;
int got_minor = (ver >> 16) & LOWER_16;
int got_major = ver & LOWER_16;
throw new RuntimeException(
"Expected 0:64, but got " + got_minor + ":" + got_major + " for primitive array");
}

// test object array. should return class file version of component.
ver = (int)m.invoke((new Version59[2]).getClass());
if (ver != 59) {
int got_minor = (ver >> 16) & 0x0000FFFF;
int got_major = ver & 0x0000FFFF;
int got_minor = (ver >> 16) & LOWER_16;
int got_major = ver & LOWER_16;
throw new RuntimeException(
"Expected 0:59, but got " + got_minor + ":" + got_major + " for object array");
}

// test multi-dimensional object array. should return class file version of component.
ver = (int)m.invoke((new Version59[3][2]).getClass());
if (ver != 59) {
int got_minor = (ver >> 16) & 0x0000FFFF;
int got_major = ver & 0x0000FFFF;
int got_minor = (ver >> 16) & LOWER_16;
int got_major = ver & LOWER_16;
throw new RuntimeException(
"Expected 0:59, but got " + got_minor + ":" + got_major + " for object array");
}
Expand Down
91 changes: 0 additions & 91 deletions test/hotspot/jtreg/runtime/ClassFile/classFileVersions.jcod
Original file line number Diff line number Diff line change
Expand Up @@ -201,97 +201,6 @@ class Version59 {
} // Attributes
} // end class Version59


// Class with major version 64 and minor version 65535
class Version64_65535 {
0xCAFEBABE;
65535; // minor version
64; // version
[14] { // Constant Pool
; // first element is empty
Method #2 #3; // #1 at 0x0A
class #4; // #2 at 0x0F
NameAndType #5 #6; // #3 at 0x12
Utf8 "java/lang/Object"; // #4 at 0x17
Utf8 "<init>"; // #5 at 0x2A
Utf8 "()V"; // #6 at 0x33
class #8; // #7 at 0x39
Utf8 "Version64_65535"; // #8 at 0x3C
Utf8 "Code"; // #9 at 0x48
Utf8 "LineNumberTable"; // #10 at 0x4F
Utf8 "hi"; // #11 at 0x61
Utf8 "SourceFile"; // #12 at 0x66
Utf8 "Version64_65535.java"; // #13 at 0x73
} // Constant Pool

0x0021; // access [ ACC_PUBLIC ACC_SUPER ]
#7;// this_cpx
#2;// super_cpx

[0] { // Interfaces
} // Interfaces

[0] { // Fields
} // Fields

[2] { // Methods
{ // method at 0x90
0x0001; // access
#5; // name_index : <init>
#6; // descriptor_index : ()V
[1] { // Attributes
Attr(#9, 29) { // Code at 0x98
1; // max_stack
1; // max_locals
Bytes[5]{
0x2AB70001B1;
}
[0] { // Traps
} // end Traps
[1] { // Attributes
Attr(#10, 6) { // LineNumberTable at 0xAF
[1] { // line_number_table
0 1; // at 0xBB
}
} // end LineNumberTable
} // Attributes
} // end Code
} // Attributes
}
;
{ // method at 0xBB
0x0009; // access
#11; // name_index : hi
#6; // descriptor_index : ()V
[1] { // Attributes
Attr(#9, 25) { // Code at 0xC3
0; // max_stack
0; // max_locals
Bytes[1]{
0xB1;
}
[0] { // Traps
} // end Traps
[1] { // Attributes
Attr(#10, 6) { // LineNumberTable at 0xD6
[1] { // line_number_table
0 2; // at 0xE2
}
} // end LineNumberTable
} // Attributes
} // end Code
} // Attributes
}
} // Methods

[1] { // Attributes
Attr(#12, 2) { // SourceFile at 0xE4
#13;
} // end SourceFile
} // Attributes
} // end class Version64_65535


// Class with major version 45 and minor version 3
class Version45_3 {
0xCAFEBABE;
Expand Down

1 comment on commit ae1a9a0

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.