--- old/src/java.base/share/classes/valhalla/shady/MinimalValueTypes_1_0.java 2017-06-21 13:11:28.000000000 -0700 +++ new/src/java.base/share/classes/valhalla/shady/MinimalValueTypes_1_0.java 2017-06-21 13:11:27.000000000 -0700 @@ -28,32 +28,40 @@ 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"; - public static final String VALUE_CLASS_DESC = "java/lang/__Value"; + 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"; + public static final String VALUE_CLASS_DESC = "java/lang/__Value"; 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")); + props.getProperty("java.lang.invoke.MethodHandle.DUMP_CLASS_FILES")); + + JLA = SharedSecrets.getJavaLangAccess(); } public static String getValueTypeClassName(ValueTypeDesc valueTypeDesc) { @@ -68,48 +76,51 @@ 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"); + public static Class getValueCapableClass(Class dvt) { + if (!isValueType(dvt)) { + throw new IllegalArgumentException(dvt + " is not a derived value type"); } - 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; + 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; - } - try { - Class.forName(getValueTypeClassName(x), true, x.getClassLoader()); - return true; + public static Class getValueTypeClass(Class vcc) { + if (!isValueCapable(vcc)) { + throw new IllegalArgumentException(vcc + " is not a value capable class"); } - catch (ClassNotFoundException cnfe) { - return false; + 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); } - // 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); @@ -122,8 +133,8 @@ String valueTypeClassName = getValueTypeClassName(valueTypeDesc); BasicClassBuilder builder = new BasicClassBuilder(valueTypeClassName, 53, 1) - .withFlags(DERIVE_VT_CLASS_ACCESS) - .withSuperclass(VALUE_CLASS_DESC); + .withFlags(DERIVE_VT_CLASS_ACCESS) + .withSuperclass(VALUE_CLASS_DESC); ValueTypeDesc.Field[] fields = valueTypeDesc.getFields(); for (ValueTypeDesc.Field field : fields) { @@ -136,15 +147,13 @@ } /** 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); @@ -155,25 +164,24 @@ } 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"); - 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); - } + 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; + } + }); + } }