Skip to content

Commit

Permalink
8291360: Create entry points to expose low-level class file information
Browse files Browse the repository at this point in the history
Reviewed-by: dholmes, rriggs
  • Loading branch information
Harold Seigel committed Aug 4, 2022
1 parent ce61eb6 commit a3040fc
Show file tree
Hide file tree
Showing 9 changed files with 812 additions and 1 deletion.
1 change: 1 addition & 0 deletions make/data/hotspot-symbols/symbols-unix
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ JVM_GetClassDeclaredConstructors
JVM_GetClassDeclaredFields
JVM_GetClassDeclaredMethods
JVM_GetClassFieldsCount
JVM_GetClassFileVersion
JVM_GetClassInterfaces
JVM_GetClassMethodsCount
JVM_GetClassModifiers
Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/share/include/jvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,12 @@ JVM_VirtualThreadUnmountBegin(JNIEnv* env, jobject vthread, jboolean last_unmoun
JNIEXPORT void JNICALL
JVM_VirtualThreadUnmountEnd(JNIEnv* env, jobject vthread, jboolean last_unmount);

/*
* Core reflection support.
*/
JNIEXPORT jint JNICALL
JVM_GetClassFileVersion(JNIEnv *env, jclass current);

/*
* This structure is used by the launcher to get the default thread
* stack size from the VM using JNI_GetDefaultJavaVMInitArgs() with a
Expand Down
19 changes: 19 additions & 0 deletions src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4044,3 +4044,22 @@ JVM_ENTRY(void, JVM_VirtualThreadUnmountEnd(JNIEnv* env, jobject vthread, jboole
fatal("Should only be called with JVMTI enabled");
#endif
JVM_END

/*
* Return the current class's class file version. The low order 16 bits of the
* returned jint contain the class's major version. The high order 16 bits
* contain the class's minor version.
*/
JVM_ENTRY(jint, JVM_GetClassFileVersion(JNIEnv* env, jclass current))
oop mirror = JNIHandles::resolve_non_null(current);
if (java_lang_Class::is_primitive(mirror)) {
// return latest major version and minor version of 0.
return JVM_CLASSFILE_MAJOR_VERSION;
}
assert(!java_lang_Class::as_Klass(mirror)->is_array_klass(), "unexpected array class");

Klass* c = java_lang_Class::as_Klass(mirror);
assert(c->is_instance_klass(), "must be");
InstanceKlass* ik = InstanceKlass::cast(c);
return (ik->minor_version() << 16) | ik->major_version();
JVM_END
30 changes: 30 additions & 0 deletions src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -4668,4 +4668,34 @@ public boolean isSealed() {
}

private native Class<?>[] getPermittedSubclasses0();

/*
* Return the class's major and minor class file version packed into an int.
* The high order 16 bits contain the class's minor version. The low order
* 16 bits contain the class's major version.
*
* If the class is an array type then the class file version of its element
* type is returned. If the class is a primitive type then the latest class
* file major version is returned and zero is returned for the minor version.
*/
private int getClassFileVersion() {
Class<?> c = isArray() ? elementType() : this;
return c.getClassFileVersion0();
}

private native int getClassFileVersion0();

/*
* Return the access flags as they were in the class's bytecode, including
* the original setting of ACC_SUPER.
*
* If the class is an array type then the access flags of the element type is
* returned. If the class is a primitive then ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
*/
private int getClassAccessFlagsRaw() {
Class<?> c = isArray() ? elementType() : this;
return c.getClassAccessFlagsRaw0();
}

private native int getClassAccessFlagsRaw0();
}
4 changes: 3 additions & 1 deletion src/java.base/share/native/libjava/Class.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -82,6 +82,8 @@ static JNINativeMethod methods[] = {
{"getRecordComponents0", "()[" RC, (void *)&JVM_GetRecordComponents},
{"isRecord0", "()Z", (void *)&JVM_IsRecord},
{"getPermittedSubclasses0", "()[" CLS, (void *)&JVM_GetPermittedSubclasses},
{"getClassFileVersion0", "()I", (void *)&JVM_GetClassFileVersion},
{"getClassAccessFlagsRaw0", "()I", (void *)&JVM_GetClassAccessFlags},
};

#undef OBJ
Expand Down
78 changes: 78 additions & 0 deletions test/hotspot/jtreg/runtime/ClassFile/ClassAccessFlagsRawTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

/**
* @test
* @bug 8291360
* @summary Test getting a class's raw access flags using java.lang.Class API
* @modules java.base/java.lang:open
* @compile classAccessFlagsRaw.jcod
* @run main/othervm ClassAccessFlagsRawTest
*/

import java.lang.reflect.*;

public class ClassAccessFlagsRawTest {

static Method m;

public static void testIt(String className, int expectedResult) throws Exception {
Class<?> testClass = Class.forName(className);
int flags = (int)m.invoke(testClass);
if (flags != expectedResult) {
throw new RuntimeException(
"expected 0x" + Integer.toHexString(expectedResult) + ", got 0x" + Integer.toHexString(flags) + " for class " + className);
}
}

public static void main(String argv[]) throws Throwable {
Class<?> cl = java.lang.Class.class;
m = cl.getDeclaredMethod("getClassAccessFlagsRaw", new Class[0]);
m.setAccessible(true);

testIt("SUPERset", 0x21); // ACC_SUPER 0x20 + ACC_PUBLIC 0x1
testIt("SUPERnotset", Modifier.PUBLIC);

// test primitive array. should return ACC_ABSTRACT | ACC_FINAL | ACC_PUBLIC.
int flags = (int)m.invoke((new int[3]).getClass());
if (flags != (Modifier.ABSTRACT | Modifier.FINAL | Modifier.PUBLIC)) {
throw new RuntimeException(
"expected 0x411, got 0x" + Integer.toHexString(flags) + " for primitive array");
}

// test object array. should return flags of component.
flags = (int)m.invoke((new SUPERnotset[2]).getClass());
if (flags != Modifier.PUBLIC) {
throw new RuntimeException(
"expected 0x1, got 0x" + Integer.toHexString(flags) + " for object array");
}

// test multi-dimensional object array. should return flags of component.
flags = (int)m.invoke((new SUPERnotset[4][2]).getClass());
if (flags != Modifier.PUBLIC) {
throw new RuntimeException(
"expected 0x1, got 0x" + Integer.toHexString(flags) + " for object array");
}
}
}
91 changes: 91 additions & 0 deletions test/hotspot/jtreg/runtime/ClassFile/ClassFileVersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

/**
* @test
* @bug 8291360
* @summary Test getting the class file version for java.lang.Class API
* @modules java.base/java.lang:open
* @compile classFileVersions.jcod
* @run main/othervm --enable-preview ClassFileVersionTest
*/

import java.lang.reflect.*;

public class ClassFileVersionTest {

static Method m;

public static void testIt(String className, int expectedResult) throws Exception {
Class<?> testClass = Class.forName(className);
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;
throw new RuntimeException(
"Expected " + exp_minor + ":" + exp_major + " but got " + got_minor + ":" + got_major);
}
}

public static void main(String argv[]) throws Throwable {
Class<?> cl = java.lang.Class.class;
m = cl.getDeclaredMethod("getClassFileVersion", new Class[0]);
m.setAccessible(true);

testIt("Version64", 64);
testIt("Version59", 59);
testIt("Version45_3", 0x3002D); // 3:45
// test minor version of 65535.
testIt("Version64_65535", 0xFFFF0040); // 0xFFFF0040 = 65535:64

// 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;
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;
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;
throw new RuntimeException(
"Expected 0:59, but got " + got_minor + ":" + got_major + " for object array");
}
}
}
Loading

1 comment on commit a3040fc

@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.