--- old/src/java.base/share/classes/java/lang/constant/ConstantUtils.java 2018-09-28 11:54:07.000000000 -0700 +++ new/src/java.base/share/classes/java/lang/constant/ConstantUtils.java 2018-09-28 11:54:06.000000000 -0700 @@ -72,6 +72,40 @@ 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); @@ -101,38 +135,6 @@ } /** - * Produce an {@code Optional>} 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 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 Optional> 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