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

Print this page




  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     static final boolean ENABLE_POOL_PATCHES;
  58     static final boolean MANGLE_CLASS_INFO;
  59 
  60     static {
  61         Properties props = GetPropertyAction.privilegedGetProperties();
  62         ENABLE_POOL_PATCHES = Boolean.parseBoolean(
  63                 props.getProperty("valhalla.enablePoolPatches"));
  64         MANGLE_CLASS_INFO = Boolean.parseBoolean(
  65                 props.getProperty("valhalla.mangleClassInfo"));
  66     }
  67 
  68     public static MethodHandle loadCode(Lookup lookup, String name, MethodType type, Consumer<? super MethodHandleCodeBuilder> builder) {
  69         return loadCode(lookup, name, type.toMethodDescriptorString(), builder);
  70     }
  71 
  72     public static MethodHandle loadCode(Lookup lookup, String name, String type, Consumer<? super MethodHandleCodeBuilder> builder) {
  73         return loadCode(lookup, name, name, type, MethodHandleCodeBuilder::new,
  74                     clazz -> {
  75                         try {
  76                             return lookup.findStatic(clazz, name, MethodType.fromMethodDescriptorString(type, lookup.lookupClass().getClassLoader()));
  77                         } catch (ReflectiveOperationException ex) {
  78                             throw new IllegalStateException(ex);
  79                         }
  80                     },
  81                     builder);
  82     }
  83 
  84     protected static <Z, C extends CodeBuilder<Class<?>, String, byte[], ?>> Z loadCode(
  85             Lookup lookup, String className, String methodName, String type,


 200             public String nullType() {
 201                 return basicTypeHelper.nullType();
 202             }
 203         }
 204 
 205         static class IsolatedMethodPoolHelper implements PoolHelper<Class<?>, String, byte[]> {
 206             BasicPoolHelper basicPoolHelper = new BasicPoolHelper();
 207             String clazz;
 208 
 209             IsolatedMethodPoolHelper(String clazz) {
 210                 this.clazz = clazz;
 211             }
 212 
 213             String from(Class<?> c) {
 214                 String name;
 215                 boolean isValue = MinimalValueTypes_1_0.isValueType(c);
 216                 if (c == THIS_CLASS) {
 217                     //THIS_CLASS cannot be a DVT (by construction) - never mangle
 218                     name = clazz;
 219                 } else {
 220                     name = (isValue && MANGLE_CLASS_INFO) ?
 221                             ";Q" + c.getName() + ";" : //mangle DVT name
 222                             c.getName();
 223                 }
 224                 return name.replaceAll("\\.", "/");
 225             }
 226 
 227             @Override
 228             public int putClass(Class<?> symbol) {
 229                 return basicPoolHelper.putClass(from(symbol));
 230             }
 231 
 232             @Override
 233             public int putFieldRef(Class<?> owner, CharSequence name, String type) {
 234                 return basicPoolHelper.putFieldRef(from(owner), name, type);
 235             }
 236 
 237             @Override
 238             public int putMethodRef(Class<?> owner, CharSequence name, String type, boolean isInterface) {
 239                 return basicPoolHelper.putMethodRef(from(owner), name, type, isInterface);
 240             }
 241 




  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     static final boolean ENABLE_POOL_PATCHES;

  58 
  59     static {
  60         Properties props = GetPropertyAction.privilegedGetProperties();
  61         ENABLE_POOL_PATCHES = Boolean.parseBoolean(
  62                 props.getProperty("valhalla.enablePoolPatches"));


  63     }
  64 
  65     public static MethodHandle loadCode(Lookup lookup, String name, MethodType type, Consumer<? super MethodHandleCodeBuilder> builder) {
  66         return loadCode(lookup, name, type.toMethodDescriptorString(), builder);
  67     }
  68 
  69     public static MethodHandle loadCode(Lookup lookup, String name, String type, Consumer<? super MethodHandleCodeBuilder> builder) {
  70         return loadCode(lookup, name, name, type, MethodHandleCodeBuilder::new,
  71                     clazz -> {
  72                         try {
  73                             return lookup.findStatic(clazz, name, MethodType.fromMethodDescriptorString(type, lookup.lookupClass().getClassLoader()));
  74                         } catch (ReflectiveOperationException ex) {
  75                             throw new IllegalStateException(ex);
  76                         }
  77                     },
  78                     builder);
  79     }
  80 
  81     protected static <Z, C extends CodeBuilder<Class<?>, String, byte[], ?>> Z loadCode(
  82             Lookup lookup, String className, String methodName, String type,


 197             public String nullType() {
 198                 return basicTypeHelper.nullType();
 199             }
 200         }
 201 
 202         static class IsolatedMethodPoolHelper implements PoolHelper<Class<?>, String, byte[]> {
 203             BasicPoolHelper basicPoolHelper = new BasicPoolHelper();
 204             String clazz;
 205 
 206             IsolatedMethodPoolHelper(String clazz) {
 207                 this.clazz = clazz;
 208             }
 209 
 210             String from(Class<?> c) {
 211                 String name;
 212                 boolean isValue = MinimalValueTypes_1_0.isValueType(c);
 213                 if (c == THIS_CLASS) {
 214                     //THIS_CLASS cannot be a DVT (by construction) - never mangle
 215                     name = clazz;
 216                 } else {
 217                     name = isValue ?
 218                             MinimalValueTypes_1_0.mangleValueClassName(c.getName()) :
 219                             c.getName();
 220                 }
 221                 return name.replaceAll("\\.", "/");
 222             }
 223 
 224             @Override
 225             public int putClass(Class<?> symbol) {
 226                 return basicPoolHelper.putClass(from(symbol));
 227             }
 228 
 229             @Override
 230             public int putFieldRef(Class<?> owner, CharSequence name, String type) {
 231                 return basicPoolHelper.putFieldRef(from(owner), name, type);
 232             }
 233 
 234             @Override
 235             public int putMethodRef(Class<?> owner, CharSequence name, String type, boolean isInterface) {
 236                 return basicPoolHelper.putMethodRef(from(owner), name, type, isInterface);
 237             }
 238