/* * Copyright (c) 2016, 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. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. */ package valhalla.shady; 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"; 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")); JLA = SharedSecrets.getJavaLangAccess(); } public static String getValueTypeClassName(ValueTypeDesc valueTypeDesc) { return getValueTypeClassName(valueTypeDesc.getName()); } public static String getValueTypeClassName(String vccName) { return vccName + DERIVE_VT_CLASSNAME_POSTFIX; } 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; } public static byte[] createValueType(ValueTypeDesc valueTypeDesc) { String valueTypeClassName = getValueTypeClassName(valueTypeDesc); BasicClassBuilder builder = new BasicClassBuilder(valueTypeClassName, 53, 1) .withFlags(DERIVE_VT_CLASS_ACCESS) .withSuperclass(VALUE_CLASS_DESC); ValueTypeDesc.Field[] fields = valueTypeDesc.getFields(); for (ValueTypeDesc.Field field : fields) { builder.withField(field.name, field.type, F -> F.withFlags(field.modifiers)); } byte[] newBytes = builder.build(); 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); public static Class getValueClass() { return (Class)(Object)__Value.class; //hack around static type-system checks } }