< prev index next >

src/java.base/share/classes/java/lang/constant/ConstantUtils.java

Print this page
rev 52749 : Bootstrap method consolidation
* clean up and simplify JDK support code for BSM invocation
* simplify JVM bootstrap handshake: use BootstrapCallInfo only
* remove unused JVM paths and data fields
* move bootstrap argument processing from MethodHandleNatives to ConstantPool
* remove ConstantGroup; merge argument access into BootstrapCallInfo
* adjust BSM argument access: remove copyArguments, add argumentRef API
* add metadata-free BSM modes, including symbolic arguments from CP

@@ -70,10 +70,44 @@
             }
         }
         return name;
     }
 
+    /**
+     * validates a bootstrap method: must have a leading Lookup parameter, or else be a valid expression-mode BSM
+     * @param bootstrapMethod the method to validate
+     * @param constantName the name associated with the proposed {@code CONSTANT_Dynamic} constant
+     * @return the method passed if valid
+     */
+    public static DirectMethodHandleDesc validateBootstrapMethod(DirectMethodHandleDesc bootstrapMethod, String constantName) {
+        if (hasLeadingLookupParameter(bootstrapMethod)) return bootstrapMethod;
+        switch (constantName) {
+        case ConstantDescs.INVOKE_NAME:
+        case ConstantDescs.SYMBOLIC_NAME:
+            return bootstrapMethod;
+        }
+        throw new IllegalArgumentException("Invalid bootstrap method: " + bootstrapMethod + " with reserved invocation name " + constantName);
+    }
+
+    /**
+     * validates an expression-mode bootstrap method: must not have a leading Lookup parameter
+     * @param bootstrapMethod the method to validate
+     * @param constantName the name associated with the proposed {@code CONSTANT_Dynamic} constant
+     * @return the method passed if valid
+     */
+    public static DirectMethodHandleDesc validateExpressionModeBootstrapMethod(DirectMethodHandleDesc bootstrapMethod, String constantName) {
+        if (hasLeadingLookupParameter(bootstrapMethod)) {
+                throw new IllegalArgumentException("Bootstrap method has leading Lookup parameter: " + bootstrapMethod);
+        }
+        return validateBootstrapMethod(bootstrapMethod, constantName);
+    }
+
+    private static boolean hasLeadingLookupParameter(DirectMethodHandleDesc bootstrapMethod) {
+        MethodTypeDesc type = bootstrapMethod.methodType();
+        return (type.parameterCount() > 0 && type.parameterType(0).equals(ConstantDescs.CR_MethodHandles_Lookup));
+    }
+
     static void validateClassOrInterface(ClassDesc clazz) {
         if (!clazz.isClassOrInterface())
             throw new IllegalArgumentException("not a class or interface type: " + clazz);
     }
 

@@ -99,42 +133,10 @@
     static String dropFirstAndLastChar(String s) {
         return s.substring(1, s.length() - 1);
     }
 
     /**
-     * Produce an {@code Optional<DynamicConstantDesc<T>>} describing the invocation
-     * of the specified bootstrap with the specified arguments.  The arguments will
-     * be converted to nominal descriptors using the provided lookup.  Helper
-     * method for implementing {@link Constable#describeConstable()}.
-     *
-     * @param <T> the type of the resulting constant
-     * @param bootstrap nominal descriptor for the bootstrap method
-     * @param type nominal descriptor for the type of the resulting constant
-     * @param args nominal descriptors for the bootstrap arguments
-     * @return the nominal descriptor for the dynamic constant
-     */
-    public static<T> Optional<DynamicConstantDesc<T>> symbolizeHelper(MethodHandleDesc bootstrap,
-                                                                      ClassDesc type,
-                                                                      Constable<?>... args) {
-        requireNonNull(bootstrap);
-        requireNonNull(type);
-        requireNonNull(args);
-        try {
-            ConstantDesc<?>[] quotedArgs = new ConstantDesc<?>[args.length + 1];
-            quotedArgs[0] = bootstrap;
-            for (int i=0; i<args.length; i++)
-                quotedArgs[i+1] = args[i].describeConstable().orElseThrow();
-            return Optional.of(DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE,
-                                                           ConstantDescs.DEFAULT_NAME,
-                                                           type, quotedArgs));
-        }
-        catch (NoSuchElementException e) {
-            return Optional.empty();
-        }
-    }
-
-    /**
      * Parse a method descriptor string, and return a list of field descriptor
      * strings, return type first, then parameter types
      *
      * @param descriptor the descriptor string
      * @return the list of types
< prev index next >