< prev index next >
src/java.base/share/classes/valhalla/shady/MinimalValueTypes_1_0.java
Print this page
*** 26,43 ****
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.IOException;
import java.security.ProtectionDomain;
import java.util.Properties;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
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";
--- 26,48 ----
import jdk.experimental.bytecode.BasicClassBuilder;
import jdk.internal.misc.Unsafe;
import sun.security.action.GetPropertyAction;
! 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,61 ****
--- 51,69 ----
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,117 ****
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 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 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 String getValueTypeClassName(Class<?> x) {
! String vtName = x.getName();
! if (!isValueType(x)) {
! vtName += DERIVE_VT_CLASSNAME_POSTFIX;
}
! return vtName;
}
! public static boolean classHasValueType(Class<?> x) {
! if (x.getDeclaredAnnotation(jvm.internal.value.DeriveValueType.class) == null) {
! return false;
}
! try {
! Class.forName(getValueTypeClassName(x), true, x.getClassLoader());
! return true;
}
! catch (ClassNotFoundException cnfe) {
! return false;
}
}
! // fds : name/sig pairs
! // 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;
--- 74,128 ----
public static String getValueCapableClassName(String valName) {
return valName.substring(0, valName.length() - DERIVE_VT_CLASSNAME_POSTFIX.length());
}
! public static boolean isValueType(Class<?> dvt) {
! return (dvt.getModifiers() & ACC_VALUE) != 0;
}
! public static boolean isValueCapable(Class<?> vcc) {
! return vcc.getDeclaredAnnotation(jvm.internal.value.DeriveValueType.class) != null;
}
! public static Class<?> getValueCapableClass(Class<?> dvt) {
! if (!isValueType(dvt)) {
! throw new IllegalArgumentException(dvt + " is not a derived value type");
}
! Class<?> c = Class.forName(dvt.getModule(), getValueCapableClassName(dvt.getName()));
! if (c == null || !isValueCapable(c)) {
! throw new InternalError(dvt + " not bound to ValueType");
}
! return c;
}
! public static Class<?> getValueTypeClass(Class<?> vcc) {
! if (!isValueCapable(vcc)) {
! throw new IllegalArgumentException(vcc + " is not a value capable class");
}
! return loadValueTypeClass(vcc, getValueTypeClassName(vcc.getName()));
}
!
! 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);
}
! /**
! * 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,181 ****
maybeDump(valueTypeClassName, newBytes);
return newBytes;
}
/** debugging flag for saving generated class files */
! private static final File DUMP_CLASS_FILES_DIR;
static {
if (DUMP_CLASS_FILES) {
try {
! File dumpDir = new File("DUMP_CLASS_FILES");
! if (!dumpDir.exists()) {
! dumpDir.mkdirs();
! }
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) {
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");
System.out.println("dump: " + dumpFile);
! dumpFile.getParentFile().mkdirs();
! FileOutputStream file = new FileOutputStream(dumpFile);
! file.write(classFile);
! file.close();
! return null;
} catch (IOException ex) {
throw new InternalError(ex);
}
}
});
}
}
private final native Class<?> getDerivedValueType(Class<?> ofClass);
--- 145,189 ----
maybeDump(valueTypeClassName, newBytes);
return newBytes;
}
/** debugging flag for saving generated class files */
! private static final Path DUMP_CLASS_FILES_DIR;
static {
if (DUMP_CLASS_FILES) {
try {
! 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_DIR != null) {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
String dumpName = className;
//dumpName = dumpName.replace('/', '-');
! Path dumpFile = DUMP_CLASS_FILES_DIR.resolve(dumpName + ".class");
System.out.println("dump: " + dumpFile);
! 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 >