< prev index next >
src/java.base/share/classes/valhalla/shady/MinimalValueTypes_1_0.java
Print this page
@@ -26,18 +26,23 @@
import jdk.experimental.bytecode.BasicClassBuilder;
import jdk.internal.misc.Unsafe;
import sun.security.action.GetPropertyAction;
-import java.io.File;
-import java.io.FileOutputStream;
+import java.io.BufferedOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.security.ProtectionDomain;
import java.util.Properties;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
+import jdk.internal.misc.JavaLangAccess;
+import jdk.internal.misc.SharedSecrets;
public class MinimalValueTypes_1_0 {
public static final int V53_1 = 1 << 16 | 53;
public static final int ACC_VALUE = ACC_NATIVE;
public static final String OBJECT_CLASS_DESC = "java/lang/Object";
@@ -46,16 +51,19 @@
public static final String DERIVE_VALUE_TYPE_DESC = "Ljvm/internal/value/DeriveValueType;";
public static final String DERIVE_VT_CLASSNAME_POSTFIX = "$Value";
public static final int DERIVE_VT_CLASS_ACCESS = ACC_PUBLIC|ACC_SUPER|ACC_FINAL|ACC_VALUE|ACC_SYNTHETIC;
public static final boolean DUMP_CLASS_FILES;
+ private static final JavaLangAccess JLA;
static {
// Use same property as in j.l.invoke.MethodHandleStatics
Properties props = GetPropertyAction.privilegedGetProperties();
DUMP_CLASS_FILES = Boolean.parseBoolean(
props.getProperty("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES"));
+
+ JLA = SharedSecrets.getJavaLangAccess();
}
public static String getValueTypeClassName(ValueTypeDesc valueTypeDesc) {
return getValueTypeClassName(valueTypeDesc.getName());
}
@@ -66,52 +74,55 @@
public static String getValueCapableClassName(String valName) {
return valName.substring(0, valName.length() - DERIVE_VT_CLASSNAME_POSTFIX.length());
}
- public static boolean isValueType(Class<?> x) {
- return (x.getModifiers() & ACC_VALUE) != 0;
+ public static boolean isValueType(Class<?> dvt) {
+ return (dvt.getModifiers() & ACC_VALUE) != 0;
}
- public static Class<?> getValueCapableClass(Class<?> x) throws ClassNotFoundException {
- if (!isValueType(x)) {
- throw new IllegalArgumentException("Expected ValueType");
- }
- return Class.forName(getValueCapableClassName(x.getName()), true, x.getClassLoader());
+ public static boolean isValueCapable(Class<?> vcc) {
+ return vcc.getDeclaredAnnotation(jvm.internal.value.DeriveValueType.class) != null;
}
- public static Class<?> getValueTypeClass(Class<?> x) throws ClassNotFoundException {
- if (isValueType(x)) {
- throw new IllegalArgumentException("Expected Value Capable Class");
- }
- return Class.forName(getValueTypeClassName(x.getName()), true, x.getClassLoader());
+ public static Class<?> getValueCapableClass(Class<?> dvt) {
+ if (!isValueType(dvt)) {
+ throw new IllegalArgumentException(dvt + " is not a derived value type");
}
- public static String getValueTypeClassName(Class<?> x) {
- String vtName = x.getName();
- if (!isValueType(x)) {
- vtName += DERIVE_VT_CLASSNAME_POSTFIX;
+ Class<?> c = Class.forName(dvt.getModule(), getValueCapableClassName(dvt.getName()));
+ if (c == null || !isValueCapable(c)) {
+ throw new InternalError(dvt + " not bound to ValueType");
}
- return vtName;
+ return c;
}
- public static boolean classHasValueType(Class<?> x) {
- if (x.getDeclaredAnnotation(jvm.internal.value.DeriveValueType.class) == null) {
- return false;
+ public static Class<?> getValueTypeClass(Class<?> vcc) {
+ if (!isValueCapable(vcc)) {
+ throw new IllegalArgumentException(vcc + " is not a value capable class");
}
- try {
- Class.forName(getValueTypeClassName(x), true, x.getClassLoader());
- return true;
+ return loadValueTypeClass(vcc, getValueTypeClassName(vcc.getName()));
}
- catch (ClassNotFoundException cnfe) {
- return false;
+
+ public static Class<?> loadValueTypeClass(Class<?> vcc, String className) {
+ if (!isValueCapable(vcc)) {
+ throw new IllegalArgumentException(vcc.getName() + " already a derived value type");
}
+ return JLA.loadValueTypeClass(vcc.getModule(), vcc.getClassLoader(), className);
}
- // fds : name/sig pairs
- // fmods : field modifiers
- public static String createDerivedValueType(String vccInternalClassName, ClassLoader cl, ProtectionDomain pd, String[] fds, int[] fmods) {
+ /**
+ * This method is invoked by the VM.
+ *
+ * @param fds : name/sig pairs
+ * @param fmods : field modifiers
+ */
+ public static String createDerivedValueType(String vccInternalClassName,
+ ClassLoader cl,
+ ProtectionDomain pd,
+ String[] fds,
+ int[] fmods) {
String vtInternalClassName = getValueTypeClassName(vccInternalClassName);
ValueTypeDesc valueTypeDesc = new ValueTypeDesc(vccInternalClassName, fds, fmods);
byte[] valueTypeBytes = createValueType(valueTypeDesc);
Class<?> vtClass = Unsafe.getUnsafe().defineClass(vtInternalClassName, valueTypeBytes, 0, valueTypeBytes.length, cl, pd);
return vtInternalClassName;
@@ -134,48 +145,45 @@
maybeDump(valueTypeClassName, newBytes);
return newBytes;
}
/** debugging flag for saving generated class files */
- private static final File DUMP_CLASS_FILES_DIR;
+ private static final Path DUMP_CLASS_FILES_DIR;
static {
if (DUMP_CLASS_FILES) {
try {
- File dumpDir = new File("DUMP_CLASS_FILES");
- if (!dumpDir.exists()) {
- dumpDir.mkdirs();
- }
+ Path dumpDir = Paths.get("DUMP_CLASS_FILES");
+ Files.createDirectories(dumpDir);
DUMP_CLASS_FILES_DIR = dumpDir;
} catch (Exception e) {
throw new InternalError(e);
}
} else {
DUMP_CLASS_FILES_DIR = null;
}
}
public static void maybeDump(final String className, final byte[] classFile) {
- if (DUMP_CLASS_FILES) {
+ if (DUMP_CLASS_FILES_DIR != null) {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
- try {
String dumpName = className;
//dumpName = dumpName.replace('/', '-');
- File dumpFile = new File(DUMP_CLASS_FILES_DIR, dumpName+".class");
+ Path dumpFile = DUMP_CLASS_FILES_DIR.resolve(dumpName + ".class");
System.out.println("dump: " + dumpFile);
- dumpFile.getParentFile().mkdirs();
- FileOutputStream file = new FileOutputStream(dumpFile);
- file.write(classFile);
- file.close();
- return null;
+ try (OutputStream os = Files.newOutputStream(dumpFile);
+ BufferedOutputStream bos = new BufferedOutputStream(os)) {
+ bos.write(classFile);
} catch (IOException ex) {
throw new InternalError(ex);
}
+ return null;
}
});
+
}
}
private final native Class<?> getDerivedValueType(Class<?> ofClass);
< prev index next >