< prev index next >

jdk/src/java.base/share/classes/jdk/experimental/value/MethodHandleBuilder.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.experimental.value;
  27 
  28 import jdk.experimental.bytecode.BasicClassBuilder.BasicPoolHelper;
  29 import jdk.experimental.bytecode.BasicClassBuilder.BasicTypeHelper;
  30 import jdk.experimental.bytecode.ClassBuilder;
  31 import jdk.experimental.bytecode.CodeBuilder;
  32 import jdk.experimental.bytecode.Flag;
  33 import jdk.experimental.bytecode.MethodBuilder;
  34 import jdk.experimental.bytecode.PoolHelper;
  35 import jdk.experimental.bytecode.TypeHelper;
  36 import jdk.experimental.bytecode.TypeTag;
  37 import jdk.experimental.bytecode.TypedCodeBuilder;
  38 import jdk.experimental.value.MethodHandleBuilder.IsolatedMethodBuilder.IsolatedMethodPoolHelper;
  39 import jdk.internal.misc.Unsafe;
  40 import sun.security.action.GetPropertyAction;
  41 import valhalla.shady.MinimalValueTypes_1_0;

  42 
  43 import java.lang.invoke.MethodHandle;
  44 import java.lang.invoke.MethodHandles.Lookup;
  45 import java.lang.invoke.MethodType;
  46 import java.util.*;
  47 import java.util.function.Consumer;
  48 import java.util.function.Function;
  49 
  50 /**
  51  * Utility class for building method handles.
  52  */
  53 public class MethodHandleBuilder {
  54 
  55     static final Unsafe UNSAFE = Unsafe.getUnsafe();
  56 
  57     public static final boolean ENABLE_POOL_PATCHES;
  58 
  59     static {
  60         Properties props = GetPropertyAction.privilegedGetProperties();
  61         ENABLE_POOL_PATCHES = Boolean.parseBoolean(


  87         isolatedMethodBuilder
  88                 .withSuperclass(Object.class)
  89                 .withMajorVersion(52)
  90                 .withMinorVersion(0)
  91                 .withFlags(Flag.ACC_PUBLIC)
  92                 .withMethod(methodName, type, M ->
  93                         M.withFlags(Flag.ACC_STATIC, Flag.ACC_PUBLIC)
  94                                 .withCode(builderFunc, builder));
  95 
  96         try {
  97             byte[] barr = isolatedMethodBuilder.build();
  98             MinimalValueTypes_1_0.maybeDump(className, barr.clone());
  99             Class<?> clazz = UNSAFE.defineAnonymousClass(lookup.lookupClass(), barr, isolatedMethodBuilder.patches());
 100             UNSAFE.ensureClassInitialized(clazz);
 101             return resFunc.apply(clazz);
 102         } catch (Throwable e) {
 103              throw new IllegalStateException(e);
 104         }
 105     }
 106 
 107     static class IsolatedMethodBuilder extends ClassBuilder<Class<?>, String, IsolatedMethodBuilder> {
 108 
 109         private static final Class<?> THIS_CLASS = new Object() { }.getClass();
 110 
 111         IsolatedMethodBuilder(String clazz, Lookup lookup) {
 112             super(ENABLE_POOL_PATCHES ?
 113                             new IsolatedMethodPatchingPoolHelper(clazz) :
 114                             new IsolatedMethodPoolHelper(clazz),
 115                   new IsolatedMethodTypeHelper(lookup));
 116             withThisClass(THIS_CLASS);
 117         }
 118 
 119         Class<?> thisClass() {
 120             return THIS_CLASS;
 121         }
 122 
 123         Object[] patches() {
 124             return ((IsolatedMethodPoolHelper)poolHelper).patches();
 125         }
 126 
 127         static class IsolatedMethodTypeHelper implements TypeHelper<Class<?>, String> {
 128 
 129             BasicTypeHelper basicTypeHelper = new BasicTypeHelper();
 130             Lookup lookup;
 131 
 132             IsolatedMethodTypeHelper(Lookup lookup) {
 133                 this.lookup = lookup;
 134             }
 135 
 136             @Override
 137             public String elemtype(String s) {
 138                 return basicTypeHelper.elemtype(s);
 139             }


 165                 } else {
 166                     return MinimalValueTypes_1_0.isValueType(aClass) ?
 167                             "Q" + aClass.getName().replaceAll("\\.", "/") + ";" :
 168                             "L" + aClass.getName().replaceAll("\\.", "/") + ";";
 169                 }
 170             }
 171 
 172             @Override
 173             public Class<?> symbol(String desc) {
 174                 if (desc.endsWith("$Value;")) {
 175                     //value class - avoid Class.forName on the DVT
 176                     try {
 177                         int numDims = 0;
 178                         while (desc.startsWith("[")) {
 179                             desc = desc.substring(1);
 180                             numDims++;
 181                         }
 182                         Class<?> box = Class.forName(basicTypeHelper.symbol(desc)
 183                                 .replaceAll("/", ".")
 184                                 .replaceAll("\\$Value", ""), true, lookup.lookupClass().getClassLoader());
 185                         ValueType<?> vt = ValueType.forClass(box);
 186                         return numDims == 0 ?
 187                                 vt.valueClass() : vt.arrayValueClass(numDims);
 188                     } catch (ReflectiveOperationException ex) {
 189                         throw new AssertionError(ex);
 190                     }
 191                 } else {
 192                     try {
 193                         if (desc.startsWith("[")) {
 194                            return Class.forName(desc.replaceAll("/", "."), true, lookup.lookupClass().getClassLoader());
 195                         } else {
 196                            return Class.forName(basicTypeHelper.symbol(desc).replaceAll("/", "."), true, lookup.lookupClass().getClassLoader());
 197                         }
 198                     } catch (ReflectiveOperationException ex) {
 199                         throw new AssertionError(ex);
 200                     }
 201                 }
 202             }
 203 
 204             @Override
 205             public TypeTag tag(String s) {




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.experimental.value;
  27 
  28 import jdk.experimental.bytecode.BasicClassBuilder.BasicPoolHelper;
  29 import jdk.experimental.bytecode.BasicClassBuilder.BasicTypeHelper;
  30 import jdk.experimental.bytecode.ClassBuilder;
  31 import jdk.experimental.bytecode.CodeBuilder;
  32 import jdk.experimental.bytecode.Flag;
  33 import jdk.experimental.bytecode.MethodBuilder;
  34 import jdk.experimental.bytecode.PoolHelper;
  35 import jdk.experimental.bytecode.TypeHelper;
  36 import jdk.experimental.bytecode.TypeTag;
  37 import jdk.experimental.bytecode.TypedCodeBuilder;
  38 import jdk.experimental.value.MethodHandleBuilder.IsolatedMethodBuilder.IsolatedMethodPoolHelper;
  39 import jdk.internal.misc.Unsafe;
  40 import sun.security.action.GetPropertyAction;
  41 import valhalla.shady.MinimalValueTypes_1_0;
  42 import valhalla.shady.ValueTypeHolder;
  43 
  44 import java.lang.invoke.MethodHandle;
  45 import java.lang.invoke.MethodHandles.Lookup;
  46 import java.lang.invoke.MethodType;
  47 import java.util.*;
  48 import java.util.function.Consumer;
  49 import java.util.function.Function;
  50 
  51 /**
  52  * Utility class for building method handles.
  53  */
  54 public class MethodHandleBuilder {
  55 
  56     static final Unsafe UNSAFE = Unsafe.getUnsafe();
  57 
  58     public static final boolean ENABLE_POOL_PATCHES;
  59 
  60     static {
  61         Properties props = GetPropertyAction.privilegedGetProperties();
  62         ENABLE_POOL_PATCHES = Boolean.parseBoolean(


  88         isolatedMethodBuilder
  89                 .withSuperclass(Object.class)
  90                 .withMajorVersion(52)
  91                 .withMinorVersion(0)
  92                 .withFlags(Flag.ACC_PUBLIC)
  93                 .withMethod(methodName, type, M ->
  94                         M.withFlags(Flag.ACC_STATIC, Flag.ACC_PUBLIC)
  95                                 .withCode(builderFunc, builder));
  96 
  97         try {
  98             byte[] barr = isolatedMethodBuilder.build();
  99             MinimalValueTypes_1_0.maybeDump(className, barr.clone());
 100             Class<?> clazz = UNSAFE.defineAnonymousClass(lookup.lookupClass(), barr, isolatedMethodBuilder.patches());
 101             UNSAFE.ensureClassInitialized(clazz);
 102             return resFunc.apply(clazz);
 103         } catch (Throwable e) {
 104              throw new IllegalStateException(e);
 105         }
 106     }
 107 
 108     public static class IsolatedMethodBuilder extends ClassBuilder<Class<?>, String, IsolatedMethodBuilder> {
 109 
 110         private static final Class<?> THIS_CLASS = new Object() { }.getClass();
 111 
 112         public IsolatedMethodBuilder(String clazz, Lookup lookup) {
 113             super(ENABLE_POOL_PATCHES ?
 114                             new IsolatedMethodPatchingPoolHelper(clazz) :
 115                             new IsolatedMethodPoolHelper(clazz),
 116                   new IsolatedMethodTypeHelper(lookup));
 117             withThisClass(THIS_CLASS);
 118         }
 119 
 120         public Class<?> thisClass() {
 121             return THIS_CLASS;
 122         }
 123 
 124         Object[] patches() {
 125             return ((IsolatedMethodPoolHelper)poolHelper).patches();
 126         }
 127 
 128         static class IsolatedMethodTypeHelper implements TypeHelper<Class<?>, String> {
 129 
 130             BasicTypeHelper basicTypeHelper = new BasicTypeHelper();
 131             Lookup lookup;
 132 
 133             IsolatedMethodTypeHelper(Lookup lookup) {
 134                 this.lookup = lookup;
 135             }
 136 
 137             @Override
 138             public String elemtype(String s) {
 139                 return basicTypeHelper.elemtype(s);
 140             }


 166                 } else {
 167                     return MinimalValueTypes_1_0.isValueType(aClass) ?
 168                             "Q" + aClass.getName().replaceAll("\\.", "/") + ";" :
 169                             "L" + aClass.getName().replaceAll("\\.", "/") + ";";
 170                 }
 171             }
 172 
 173             @Override
 174             public Class<?> symbol(String desc) {
 175                 if (desc.endsWith("$Value;")) {
 176                     //value class - avoid Class.forName on the DVT
 177                     try {
 178                         int numDims = 0;
 179                         while (desc.startsWith("[")) {
 180                             desc = desc.substring(1);
 181                             numDims++;
 182                         }
 183                         Class<?> box = Class.forName(basicTypeHelper.symbol(desc)
 184                                 .replaceAll("/", ".")
 185                                 .replaceAll("\\$Value", ""), true, lookup.lookupClass().getClassLoader());
 186                         ValueTypeHolder<?> vt = MinimalValueTypes_1_0.getValueFor(box);
 187                         return numDims == 0 ?
 188                                 vt.valueClass() : vt.arrayValueClass(numDims);
 189                     } catch (ReflectiveOperationException ex) {
 190                         throw new AssertionError(ex);
 191                     }
 192                 } else {
 193                     try {
 194                         if (desc.startsWith("[")) {
 195                            return Class.forName(desc.replaceAll("/", "."), true, lookup.lookupClass().getClassLoader());
 196                         } else {
 197                            return Class.forName(basicTypeHelper.symbol(desc).replaceAll("/", "."), true, lookup.lookupClass().getClassLoader());
 198                         }
 199                     } catch (ReflectiveOperationException ex) {
 200                         throw new AssertionError(ex);
 201                     }
 202                 }
 203             }
 204 
 205             @Override
 206             public TypeTag tag(String s) {


< prev index next >