< prev index next >

src/java.base/share/classes/java/lang/invoke/ConstantBootstraps.java

Print this page




  24  */
  25 package java.lang.invoke;
  26 
  27 import sun.invoke.util.Wrapper;
  28 
  29 import static java.lang.invoke.MethodHandleNatives.mapLookupExceptionToError;
  30 import static java.util.Objects.requireNonNull;
  31 
  32 /**
  33  * Bootstrap methods for dynamically-computed constants.
  34  *
  35  * <p>The bootstrap methods in this class will throw a
  36  * {@code NullPointerException} for any reference argument that is {@code null},
  37  * unless the argument is specified to be unused or specified to accept a
  38  * {@code null} value.
  39  *
  40  * @since 11
  41  */
  42 public final class ConstantBootstraps {
  43     // implements the upcall from the JVM, MethodHandleNatives.linkDynamicConstant:
  44     /*non-public*/
  45     static Object makeConstant(MethodHandle bootstrapMethod,
  46                                // Callee information:
  47                                String name, Class<?> type,
  48                                // Extra arguments for BSM, if any:
  49                                Object info,
  50                                // Caller information:
  51                                Class<?> callerClass) {
  52         // Restrict bootstrap methods to those whose first parameter is Lookup
  53         // The motivation here is, in the future, to possibly support BSMs
  54         // that do not accept the meta-data of lookup/name/type, thereby
  55         // allowing the co-opting of existing methods to be used as BSMs as
  56         // long as the static arguments can be passed as method arguments
  57         MethodType mt = bootstrapMethod.type();
  58         if (mt.parameterCount() < 2 ||
  59             !MethodHandles.Lookup.class.isAssignableFrom(mt.parameterType(0))) {
  60             throw new BootstrapMethodError(
  61                     "Invalid bootstrap method declared for resolving a dynamic constant: " + bootstrapMethod);
  62         }




  63 
  64         // BSMI.invoke handles all type checking and exception translation.
  65         // If type is not a reference type, the JVM is expecting a boxed
  66         // version, and will manage unboxing on the other side.
  67         return BootstrapMethodInvoker.invoke(
  68                 type, bootstrapMethod, name, type, info, callerClass);

  69     }
  70 
  71     /**
  72      * Returns a {@code null} object reference for the reference type specified
  73      * by {@code type}.
  74      *
  75      * @param lookup unused
  76      * @param name unused
  77      * @param type a reference type
  78      * @return a {@code null} value
  79      * @throws IllegalArgumentException if {@code type} is not a reference type
  80      */
  81     public static Object nullConstant(MethodHandles.Lookup lookup, String name, Class<?> type) {
  82         if (requireNonNull(type).isPrimitive()) {
  83             throw new IllegalArgumentException(String.format("not reference: %s", type));
  84         }
  85 
  86         return null;
  87     }
  88 




  24  */
  25 package java.lang.invoke;
  26 
  27 import sun.invoke.util.Wrapper;
  28 
  29 import static java.lang.invoke.MethodHandleNatives.mapLookupExceptionToError;
  30 import static java.util.Objects.requireNonNull;
  31 
  32 /**
  33  * Bootstrap methods for dynamically-computed constants.
  34  *
  35  * <p>The bootstrap methods in this class will throw a
  36  * {@code NullPointerException} for any reference argument that is {@code null},
  37  * unless the argument is specified to be unused or specified to accept a
  38  * {@code null} value.
  39  *
  40  * @since 11
  41  */
  42 public final class ConstantBootstraps {
  43     // implements the upcall from the JVM, MethodHandleNatives.linkDynamicConstant:
  44     /**
  45      * makeConstant
  46      *
  47      * @param bootstrapMethod doc
  48      * @param name doc
  49      * @param type doc
  50      * @param info doc
  51      * @param callerClass doc
  52      * @return doc
  53      */
  54     /*non-public*/ static Object makeConstant(MethodHandle bootstrapMethod,
  55                                               // Callee information:
  56                                               String name, Class<?> type,
  57                                               // Extra arguments for BSM, if any:
  58                                               Object info,
  59                                               // Caller information:
  60                                               Class<?> callerClass) {
  61         // Lookup, name and type argument metadata should be included when
  62         // invoking the BSM if there are one or more parameters and the first
  63         // parameter type is MethodHandles.Lookup
  64         MethodType bsmType = bootstrapMethod.type();
  65         boolean includeMetadata = (bsmType.parameterCount() > 0 &&
  66                                    MethodHandles.Lookup.class.isAssignableFrom(bsmType.parameterType(0)));
  67 
  68         // BSMI.invoke handles all type checking and exception translation.
  69         // If type is not a reference type, the JVM is expecting a boxed
  70         // version, and will manage unboxing on the other side.
  71         return BootstrapMethodInvoker.invoke(
  72                 type, bootstrapMethod, name, type, info, callerClass,
  73                 includeMetadata);
  74     }
  75 
  76     /**
  77      * Returns a {@code null} object reference for the reference type specified
  78      * by {@code type}.
  79      *
  80      * @param lookup unused
  81      * @param name unused
  82      * @param type a reference type
  83      * @return a {@code null} value
  84      * @throws IllegalArgumentException if {@code type} is not a reference type
  85      */
  86     public static Object nullConstant(MethodHandles.Lookup lookup, String name, Class<?> type) {
  87         if (requireNonNull(type).isPrimitive()) {
  88             throw new IllegalArgumentException(String.format("not reference: %s", type));
  89         }
  90 
  91         return null;
  92     }
  93 


< prev index next >