1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.util.ArrayList;
  31 import java.util.Arrays;
  32 import java.util.Collections;
  33 
  34 import sun.invoke.empty.Empty;
  35 import sun.invoke.util.ValueConversions;
  36 import sun.invoke.util.VerifyType;
  37 import sun.invoke.util.Wrapper;
  38 import sun.reflect.CallerSensitive;
  39 import sun.reflect.Reflection;
  40 import static java.lang.invoke.LambdaForm.*;
  41 import static java.lang.invoke.MethodHandleStatics.*;
  42 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  43 
  44 /**
  45  * Trusted implementation code for MethodHandle.
  46  * @author jrose
  47  */
  48 /*non-public*/ abstract class MethodHandleImpl {
  49     // Do not adjust this except for special platforms:
  50     private static final int MAX_ARITY;
  51     static {
  52         final Object[] values = { 255 };
  53         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  54             @Override
  55             public Void run() {
  56                 values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255);
  57                 return null;
  58             }
  59         });
  60         MAX_ARITY = (Integer) values[0];
  61     }
  62 
  63     /// Factory methods to create method handles:
  64 
  65     static void initStatics() {
  66         // Trigger selected static initializations.
  67         MemberName.Factory.INSTANCE.getClass();
  68     }
  69 
  70     static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
  71         if (arrayClass == Object[].class)
  72             return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER);
  73         if (!arrayClass.isArray())
  74             throw newIllegalArgumentException("not an array: "+arrayClass);
  75         MethodHandle[] cache = ArrayAccessor.TYPED_ACCESSORS.get(arrayClass);
  76         int cacheIndex = (isSetter ? ArrayAccessor.SETTER_INDEX : ArrayAccessor.GETTER_INDEX);
  77         MethodHandle mh = cache[cacheIndex];
  78         if (mh != null)  return mh;
  79         mh = ArrayAccessor.getAccessor(arrayClass, isSetter);
  80         MethodType correctType = ArrayAccessor.correctType(arrayClass, isSetter);
  81         if (mh.type() != correctType) {
  82             assert(mh.type().parameterType(0) == Object[].class);
  83             assert((isSetter ? mh.type().parameterType(2) : mh.type().returnType()) == Object.class);
  84             assert(isSetter || correctType.parameterType(0).getComponentType() == correctType.returnType());
  85             // safe to view non-strictly, because element type follows from array type
  86             mh = mh.viewAsType(correctType, false);
  87         }
  88         mh = makeIntrinsic(mh, (isSetter ? Intrinsic.ARRAY_STORE : Intrinsic.ARRAY_LOAD));
  89         // Atomically update accessor cache.
  90         synchronized(cache) {
  91             if (cache[cacheIndex] == null) {
  92                 cache[cacheIndex] = mh;
  93             } else {
  94                 // Throw away newly constructed accessor and use cached version.
  95                 mh = cache[cacheIndex];
  96             }
  97         }
  98         return mh;
  99     }
 100 
 101     static final class ArrayAccessor {
 102         /// Support for array element access
 103         static final int GETTER_INDEX = 0, SETTER_INDEX = 1, INDEX_LIMIT = 2;
 104         static final ClassValue<MethodHandle[]> TYPED_ACCESSORS
 105                 = new ClassValue<MethodHandle[]>() {
 106                     @Override
 107                     protected MethodHandle[] computeValue(Class<?> type) {
 108                         return new MethodHandle[INDEX_LIMIT];
 109                     }
 110                 };
 111         static final MethodHandle OBJECT_ARRAY_GETTER, OBJECT_ARRAY_SETTER;
 112         static {
 113             MethodHandle[] cache = TYPED_ACCESSORS.get(Object[].class);
 114             cache[GETTER_INDEX] = OBJECT_ARRAY_GETTER = makeIntrinsic(getAccessor(Object[].class, false), Intrinsic.ARRAY_LOAD);
 115             cache[SETTER_INDEX] = OBJECT_ARRAY_SETTER = makeIntrinsic(getAccessor(Object[].class, true),  Intrinsic.ARRAY_STORE);
 116 
 117             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_GETTER.internalMemberName()));
 118             assert(InvokerBytecodeGenerator.isStaticallyInvocable(ArrayAccessor.OBJECT_ARRAY_SETTER.internalMemberName()));
 119         }
 120 
 121         static int     getElementI(int[]     a, int i)            { return              a[i]; }
 122         static long    getElementJ(long[]    a, int i)            { return              a[i]; }
 123         static float   getElementF(float[]   a, int i)            { return              a[i]; }
 124         static double  getElementD(double[]  a, int i)            { return              a[i]; }
 125         static boolean getElementZ(boolean[] a, int i)            { return              a[i]; }
 126         static byte    getElementB(byte[]    a, int i)            { return              a[i]; }
 127         static short   getElementS(short[]   a, int i)            { return              a[i]; }
 128         static char    getElementC(char[]    a, int i)            { return              a[i]; }
 129         static Object  getElementL(Object[]  a, int i)            { return              a[i]; }
 130 
 131         static void    setElementI(int[]     a, int i, int     x) {              a[i] = x; }
 132         static void    setElementJ(long[]    a, int i, long    x) {              a[i] = x; }
 133         static void    setElementF(float[]   a, int i, float   x) {              a[i] = x; }
 134         static void    setElementD(double[]  a, int i, double  x) {              a[i] = x; }
 135         static void    setElementZ(boolean[] a, int i, boolean x) {              a[i] = x; }
 136         static void    setElementB(byte[]    a, int i, byte    x) {              a[i] = x; }
 137         static void    setElementS(short[]   a, int i, short   x) {              a[i] = x; }
 138         static void    setElementC(char[]    a, int i, char    x) {              a[i] = x; }
 139         static void    setElementL(Object[]  a, int i, Object  x) {              a[i] = x; }
 140 
 141         static String name(Class<?> arrayClass, boolean isSetter) {
 142             Class<?> elemClass = arrayClass.getComponentType();
 143             if (elemClass == null)  throw newIllegalArgumentException("not an array", arrayClass);
 144             return (!isSetter ? "getElement" : "setElement") + Wrapper.basicTypeChar(elemClass);
 145         }
 146         static MethodType type(Class<?> arrayClass, boolean isSetter) {
 147             Class<?> elemClass = arrayClass.getComponentType();
 148             Class<?> arrayArgClass = arrayClass;
 149             if (!elemClass.isPrimitive()) {
 150                 arrayArgClass = Object[].class;
 151                 elemClass = Object.class;
 152             }
 153             return !isSetter ?
 154                     MethodType.methodType(elemClass,  arrayArgClass, int.class) :
 155                     MethodType.methodType(void.class, arrayArgClass, int.class, elemClass);
 156         }
 157         static MethodType correctType(Class<?> arrayClass, boolean isSetter) {
 158             Class<?> elemClass = arrayClass.getComponentType();
 159             return !isSetter ?
 160                     MethodType.methodType(elemClass,  arrayClass, int.class) :
 161                     MethodType.methodType(void.class, arrayClass, int.class, elemClass);
 162         }
 163         static MethodHandle getAccessor(Class<?> arrayClass, boolean isSetter) {
 164             String     name = name(arrayClass, isSetter);
 165             MethodType type = type(arrayClass, isSetter);
 166             try {
 167                 return IMPL_LOOKUP.findStatic(ArrayAccessor.class, name, type);
 168             } catch (ReflectiveOperationException ex) {
 169                 throw uncaughtException(ex);
 170             }
 171         }
 172     }
 173 
 174     /**
 175      * Create a JVM-level adapter method handle to conform the given method
 176      * handle to the similar newType, using only pairwise argument conversions.
 177      * For each argument, convert incoming argument to the exact type needed.
 178      * The argument conversions allowed are casting, boxing and unboxing,
 179      * integral widening or narrowing, and floating point widening or narrowing.
 180      * @param srcType required call type
 181      * @param target original method handle
 182      * @param strict if true, only asType conversions are allowed; if false, explicitCastArguments conversions allowed
 183      * @param monobox if true, unboxing conversions are assumed to be exactly typed (Integer to int only, not long or double)
 184      * @return an adapter to the original handle with the desired new type,
 185      *          or the original target if the types are already identical
 186      *          or null if the adaptation cannot be made
 187      */
 188     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 189                                             boolean strict, boolean monobox) {
 190         MethodType dstType = target.type();
 191         if (srcType == dstType)
 192             return target;
 193         if (USE_LAMBDA_FORM_EDITOR) {
 194             return makePairwiseConvertByEditor(target, srcType, strict, monobox);
 195         } else {
 196             return makePairwiseConvertIndirect(target, srcType, strict, monobox);
 197         }
 198     }
 199 
 200     private static int countNonNull(Object[] array) {
 201         int count = 0;
 202         for (Object x : array) {
 203             if (x != null)  ++count;
 204         }
 205         return count;
 206     }
 207 
 208     static MethodHandle makePairwiseConvertByEditor(MethodHandle target, MethodType srcType,
 209                                                     boolean strict, boolean monobox) {
 210         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
 211         int convCount = countNonNull(convSpecs);
 212         if (convCount == 0)
 213             return target.viewAsType(srcType, strict);
 214         MethodType basicSrcType = srcType.basicType();
 215         MethodType midType = target.type().basicType();
 216         BoundMethodHandle mh = target.rebind();
 217         // FIXME: Reduce number of bindings when there is more than one Class conversion.
 218         // FIXME: Reduce number of bindings when there are repeated conversions.
 219         for (int i = 0; i < convSpecs.length-1; i++) {
 220             Object convSpec = convSpecs[i];
 221             if (convSpec == null)  continue;
 222             MethodHandle fn;
 223             if (convSpec instanceof Class) {
 224                 fn = Lazy.MH_castReference.bindTo(convSpec);
 225             } else {
 226                 fn = (MethodHandle) convSpec;
 227             }
 228             Class<?> newType = basicSrcType.parameterType(i);
 229             if (--convCount == 0)
 230                 midType = srcType;
 231             else
 232                 midType = midType.changeParameterType(i, newType);
 233             LambdaForm form2 = mh.editor().filterArgumentForm(1+i, BasicType.basicType(newType));
 234             mh = mh.copyWithExtendL(midType, form2, fn);
 235             mh = mh.rebind();
 236         }
 237         Object convSpec = convSpecs[convSpecs.length-1];
 238         if (convSpec != null) {
 239             MethodHandle fn;
 240             if (convSpec instanceof Class) {
 241                 if (convSpec == void.class)
 242                     fn = null;
 243                 else
 244                     fn = Lazy.MH_castReference.bindTo(convSpec);
 245             } else {
 246                 fn = (MethodHandle) convSpec;
 247             }
 248             Class<?> newType = basicSrcType.returnType();
 249             assert(--convCount == 0);
 250             midType = srcType;
 251             if (fn != null) {
 252                 mh = mh.rebind();  // rebind if too complex
 253                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), false);
 254                 mh = mh.copyWithExtendL(midType, form2, fn);
 255             } else {
 256                 LambdaForm form2 = mh.editor().filterReturnForm(BasicType.basicType(newType), true);
 257                 mh = mh.copyWith(midType, form2);
 258             }
 259         }
 260         assert(convCount == 0);
 261         assert(mh.type().equals(srcType));
 262         return mh;
 263     }
 264 
 265     static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
 266                                                     boolean strict, boolean monobox) {
 267         assert(target.type().parameterCount() == srcType.parameterCount());
 268         // Calculate extra arguments (temporaries) required in the names array.
 269         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
 270         final int INARG_COUNT = srcType.parameterCount();
 271         int convCount = countNonNull(convSpecs);
 272         boolean retConv = (convSpecs[INARG_COUNT] != null);
 273         boolean retVoid = srcType.returnType() == void.class;
 274         if (retConv && retVoid) {
 275             convCount -= 1;
 276             retConv = false;
 277         }
 278 
 279         final int IN_MH         = 0;
 280         final int INARG_BASE    = 1;
 281         final int INARG_LIMIT   = INARG_BASE + INARG_COUNT;
 282         final int NAME_LIMIT    = INARG_LIMIT + convCount + 1;
 283         final int RETURN_CONV   = (!retConv ? -1         : NAME_LIMIT - 1);
 284         final int OUT_CALL      = (!retConv ? NAME_LIMIT : RETURN_CONV) - 1;
 285         final int RESULT        = (retVoid ? -1 : NAME_LIMIT - 1);
 286 
 287         // Now build a LambdaForm.
 288         MethodType lambdaType = srcType.basicType().invokerType();
 289         Name[] names = arguments(NAME_LIMIT - INARG_LIMIT, lambdaType);
 290 
 291         // Collect the arguments to the outgoing call, maybe with conversions:
 292         final int OUTARG_BASE = 0;  // target MH is Name.function, name Name.arguments[0]
 293         Object[] outArgs = new Object[OUTARG_BASE + INARG_COUNT];
 294 
 295         int nameCursor = INARG_LIMIT;
 296         for (int i = 0; i < INARG_COUNT; i++) {
 297             Object convSpec = convSpecs[i];
 298             if (convSpec == null) {
 299                 // do nothing: difference is trivial
 300                 outArgs[OUTARG_BASE + i] = names[INARG_BASE + i];
 301                 continue;
 302             }
 303 
 304             Name conv;
 305             if (convSpec instanceof Class) {
 306                 Class<?> convClass = (Class<?>) convSpec;
 307                 conv = new Name(Lazy.MH_castReference, convClass, names[INARG_BASE + i]);
 308             } else {
 309                 MethodHandle fn = (MethodHandle) convSpec;
 310                 conv = new Name(fn, names[INARG_BASE + i]);
 311             }
 312             assert(names[nameCursor] == null);
 313             names[nameCursor++] = conv;
 314             assert(outArgs[OUTARG_BASE + i] == null);
 315             outArgs[OUTARG_BASE + i] = conv;
 316         }
 317 
 318         // Build argument array for the call.
 319         assert(nameCursor == OUT_CALL);
 320         names[OUT_CALL] = new Name(target, outArgs);
 321 
 322         Object convSpec = convSpecs[INARG_COUNT];
 323         if (!retConv) {
 324             assert(OUT_CALL == names.length-1);
 325         } else {
 326             Name conv;
 327             if (convSpec == void.class) {
 328                 conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType())));
 329             } else if (convSpec instanceof Class) {
 330                 Class<?> convClass = (Class<?>) convSpec;
 331                 conv = new Name(Lazy.MH_castReference, convClass, names[OUT_CALL]);
 332             } else {
 333                 MethodHandle fn = (MethodHandle) convSpec;
 334                 if (fn.type().parameterCount() == 0)
 335                     conv = new Name(fn);  // don't pass retval to void conversion
 336                 else
 337                     conv = new Name(fn, names[OUT_CALL]);
 338             }
 339             assert(names[RETURN_CONV] == null);
 340             names[RETURN_CONV] = conv;
 341             assert(RETURN_CONV == names.length-1);
 342         }
 343 
 344         LambdaForm form = new LambdaForm("convert", lambdaType.parameterCount(), names, RESULT);
 345         return SimpleMethodHandle.make(srcType, form);
 346     }
 347 
 348     /**
 349      * Identity function, with reference cast.
 350      * @param t an arbitrary reference type
 351      * @param x an arbitrary reference value
 352      * @return the same value x
 353      */
 354     @ForceInline
 355     @SuppressWarnings("unchecked")
 356     static <T,U> T castReference(Class<? extends T> t, U x) {
 357         // inlined Class.cast because we can't ForceInline it
 358         if (x != null && !t.isInstance(x))
 359             throw newClassCastException(t, x);
 360         return (T) x;
 361     }
 362 
 363     private static ClassCastException newClassCastException(Class<?> t, Object obj) {
 364         return new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + t.getName());
 365     }
 366 
 367     static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
 368                                             boolean strict, boolean monobox) {
 369         final int INARG_COUNT = srcType.parameterCount();
 370         Object[] convSpecs = new Object[INARG_COUNT+1];
 371         for (int i = 0; i <= INARG_COUNT; i++) {
 372             boolean isRet = (i == INARG_COUNT);
 373             Class<?> src = isRet ? dstType.returnType() : srcType.parameterType(i);
 374             Class<?> dst = isRet ? srcType.returnType() : dstType.parameterType(i);
 375             if (!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict)) {
 376                 convSpecs[i] = valueConversion(src, dst, strict, monobox);
 377             }
 378         }
 379         return convSpecs;
 380     }
 381     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
 382                                             boolean strict) {
 383         return makePairwiseConvert(target, srcType, strict, /*monobox=*/ false);
 384     }
 385 
 386     /**
 387      * Find a conversion function from the given source to the given destination.
 388      * This conversion function will be used as a LF NamedFunction.
 389      * Return a Class object if a simple cast is needed.
 390      * Return void.class if void is involved.
 391      */
 392     static Object valueConversion(Class<?> src, Class<?> dst, boolean strict, boolean monobox) {
 393         assert(!VerifyType.isNullConversion(src, dst, /*keepInterfaces=*/ strict));  // caller responsibility
 394         if (dst == void.class)
 395             return dst;
 396         MethodHandle fn;
 397         if (src.isPrimitive()) {
 398             if (src == void.class) {
 399                 return void.class;  // caller must recognize this specially
 400             } else if (dst.isPrimitive()) {
 401                 // Examples: int->byte, byte->int, boolean->int (!strict)
 402                 fn = ValueConversions.convertPrimitive(src, dst);
 403             } else {
 404                 // Examples: int->Integer, boolean->Object, float->Number
 405                 Wrapper wsrc = Wrapper.forPrimitiveType(src);
 406                 fn = ValueConversions.boxExact(wsrc);
 407                 assert(fn.type().parameterType(0) == wsrc.primitiveType());
 408                 assert(fn.type().returnType() == wsrc.wrapperType());
 409                 if (!VerifyType.isNullConversion(wsrc.wrapperType(), dst, strict)) {
 410                     // Corner case, such as int->Long, which will probably fail.
 411                     MethodType mt = MethodType.methodType(dst, src);
 412                     if (strict)
 413                         fn = fn.asType(mt);
 414                     else
 415                         fn = MethodHandleImpl.makePairwiseConvert(fn, mt, /*strict=*/ false);
 416                 }
 417             }
 418         } else if (dst.isPrimitive()) {
 419             Wrapper wdst = Wrapper.forPrimitiveType(dst);
 420             if (monobox || src == wdst.wrapperType()) {
 421                 // Use a strongly-typed unboxer, if possible.
 422                 fn = ValueConversions.unboxExact(wdst, strict);
 423             } else {
 424                 // Examples:  Object->int, Number->int, Comparable->int, Byte->int
 425                 // must include additional conversions
 426                 // src must be examined at runtime, to detect Byte, Character, etc.
 427                 fn = (strict
 428                         ? ValueConversions.unboxWiden(wdst)
 429                         : ValueConversions.unboxCast(wdst));
 430             }
 431         } else {
 432             // Simple reference conversion.
 433             // Note:  Do not check for a class hierarchy relation
 434             // between src and dst.  In all cases a 'null' argument
 435             // will pass the cast conversion.
 436             return dst;
 437         }
 438         assert(fn.type().parameterCount() <= 1) : "pc"+Arrays.asList(src.getSimpleName(), dst.getSimpleName(), fn);
 439         return fn;
 440     }
 441 
 442     static MethodHandle makeVarargsCollector(MethodHandle target, Class<?> arrayType) {
 443         MethodType type = target.type();
 444         int last = type.parameterCount() - 1;
 445         if (type.parameterType(last) != arrayType)
 446             target = target.asType(type.changeParameterType(last, arrayType));
 447         target = target.asFixedArity();  // make sure this attribute is turned off
 448         return new AsVarargsCollector(target, arrayType);
 449     }
 450 
 451     private static final class AsVarargsCollector extends DelegatingMethodHandle {
 452         private final MethodHandle target;
 453         private final Class<?> arrayType;
 454         private @Stable MethodHandle asCollectorCache;
 455 
 456         AsVarargsCollector(MethodHandle target, Class<?> arrayType) {
 457             this(target.type(), target, arrayType);
 458         }
 459         AsVarargsCollector(MethodType type, MethodHandle target, Class<?> arrayType) {
 460             super(type, target);
 461             this.target = target;
 462             this.arrayType = arrayType;
 463             this.asCollectorCache = target.asCollector(arrayType, 0);
 464         }
 465 
 466         @Override
 467         public boolean isVarargsCollector() {
 468             return true;
 469         }
 470 
 471         @Override
 472         protected MethodHandle getTarget() {
 473             return target;
 474         }
 475 
 476         @Override
 477         public MethodHandle asFixedArity() {
 478             return target;
 479         }
 480 
 481         @Override
 482         MethodHandle setVarargs(MemberName member) {
 483             if (member.isVarargs())  return this;
 484             return asFixedArity();
 485         }
 486 
 487         @Override
 488         public MethodHandle asTypeUncached(MethodType newType) {
 489             MethodType type = this.type();
 490             int collectArg = type.parameterCount() - 1;
 491             int newArity = newType.parameterCount();
 492             if (newArity == collectArg+1 &&
 493                 type.parameterType(collectArg).isAssignableFrom(newType.parameterType(collectArg))) {
 494                 // if arity and trailing parameter are compatible, do normal thing
 495                 return asTypeCache = asFixedArity().asType(newType);
 496             }
 497             // check cache
 498             MethodHandle acc = asCollectorCache;
 499             if (acc != null && acc.type().parameterCount() == newArity)
 500                 return asTypeCache = acc.asType(newType);
 501             // build and cache a collector
 502             int arrayLength = newArity - collectArg;
 503             MethodHandle collector;
 504             try {
 505                 collector = asFixedArity().asCollector(arrayType, arrayLength);
 506                 assert(collector.type().parameterCount() == newArity) : "newArity="+newArity+" but collector="+collector;
 507             } catch (IllegalArgumentException ex) {
 508                 throw new WrongMethodTypeException("cannot build collector", ex);
 509             }
 510             asCollectorCache = collector;
 511             return asTypeCache = collector.asType(newType);
 512         }
 513 
 514         @Override
 515         boolean viewAsTypeChecks(MethodType newType, boolean strict) {
 516             super.viewAsTypeChecks(newType, true);
 517             if (strict) return true;
 518             // extra assertion for non-strict checks:
 519             assert (type().lastParameterType().getComponentType()
 520                     .isAssignableFrom(
 521                             newType.lastParameterType().getComponentType()))
 522                     : Arrays.asList(this, newType);
 523             return true;
 524         }
 525     }
 526 
 527     /** Factory method:  Spread selected argument. */
 528     static MethodHandle makeSpreadArguments(MethodHandle target,
 529                                             Class<?> spreadArgType, int spreadArgPos, int spreadArgCount) {
 530         MethodType targetType = target.type();
 531 
 532         for (int i = 0; i < spreadArgCount; i++) {
 533             Class<?> arg = VerifyType.spreadArgElementType(spreadArgType, i);
 534             if (arg == null)  arg = Object.class;
 535             targetType = targetType.changeParameterType(spreadArgPos + i, arg);
 536         }
 537         target = target.asType(targetType);
 538 
 539         MethodType srcType = targetType
 540                 .replaceParameterTypes(spreadArgPos, spreadArgPos + spreadArgCount, spreadArgType);
 541         // Now build a LambdaForm.
 542         MethodType lambdaType = srcType.invokerType();
 543         Name[] names = arguments(spreadArgCount + 2, lambdaType);
 544         int nameCursor = lambdaType.parameterCount();
 545         int[] indexes = new int[targetType.parameterCount()];
 546 
 547         for (int i = 0, argIndex = 1; i < targetType.parameterCount() + 1; i++, argIndex++) {
 548             Class<?> src = lambdaType.parameterType(i);
 549             if (i == spreadArgPos) {
 550                 // Spread the array.
 551                 MethodHandle aload = MethodHandles.arrayElementGetter(spreadArgType);
 552                 Name array = names[argIndex];
 553                 names[nameCursor++] = new Name(Lazy.NF_checkSpreadArgument, array, spreadArgCount);
 554                 for (int j = 0; j < spreadArgCount; i++, j++) {
 555                     indexes[i] = nameCursor;
 556                     names[nameCursor++] = new Name(aload, array, j);
 557                 }
 558             } else if (i < indexes.length) {
 559                 indexes[i] = argIndex;
 560             }
 561         }
 562         assert(nameCursor == names.length-1);  // leave room for the final call
 563 
 564         // Build argument array for the call.
 565         Name[] targetArgs = new Name[targetType.parameterCount()];
 566         for (int i = 0; i < targetType.parameterCount(); i++) {
 567             int idx = indexes[i];
 568             targetArgs[i] = names[idx];
 569         }
 570         names[names.length - 1] = new Name(target, (Object[]) targetArgs);
 571 
 572         LambdaForm form = new LambdaForm("spread", lambdaType.parameterCount(), names);
 573         return SimpleMethodHandle.make(srcType, form);
 574     }
 575 
 576     static void checkSpreadArgument(Object av, int n) {
 577         if (av == null) {
 578             if (n == 0)  return;
 579         } else if (av instanceof Object[]) {
 580             int len = ((Object[])av).length;
 581             if (len == n)  return;
 582         } else {
 583             int len = java.lang.reflect.Array.getLength(av);
 584             if (len == n)  return;
 585         }
 586         // fall through to error:
 587         throw newIllegalArgumentException("array is not of length "+n);
 588     }
 589 
 590     /**
 591      * Pre-initialized NamedFunctions for bootstrapping purposes.
 592      * Factored in an inner class to delay initialization until first usage.
 593      */
 594     static class Lazy {
 595         private static final Class<?> MHI = MethodHandleImpl.class;
 596 
 597         static final NamedFunction NF_checkSpreadArgument;
 598         static final NamedFunction NF_guardWithCatch;
 599         static final NamedFunction NF_throwException;
 600 
 601         static final MethodHandle MH_castReference;
 602         static final MethodHandle MH_selectAlternative;
 603         static final MethodHandle MH_copyAsPrimitiveArray;
 604         static final MethodHandle MH_fillNewTypedArray;
 605         static final MethodHandle MH_fillNewArray;
 606         static final MethodHandle MH_arrayIdentity;
 607 
 608         static {
 609             try {
 610                 NF_checkSpreadArgument = new NamedFunction(MHI.getDeclaredMethod("checkSpreadArgument", Object.class, int.class));
 611                 NF_guardWithCatch      = new NamedFunction(MHI.getDeclaredMethod("guardWithCatch", MethodHandle.class, Class.class,
 612                                                                                  MethodHandle.class, Object[].class));
 613                 NF_throwException      = new NamedFunction(MHI.getDeclaredMethod("throwException", Throwable.class));
 614 
 615                 NF_checkSpreadArgument.resolve();
 616                 NF_guardWithCatch.resolve();
 617                 NF_throwException.resolve();
 618 
 619                 MH_castReference        = IMPL_LOOKUP.findStatic(MHI, "castReference",
 620                                             MethodType.methodType(Object.class, Class.class, Object.class));
 621                 MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray",
 622                                             MethodType.methodType(Object.class, Wrapper.class, Object[].class));
 623                 MH_arrayIdentity        = IMPL_LOOKUP.findStatic(MHI, "identity",
 624                                             MethodType.methodType(Object[].class, Object[].class));
 625                 MH_fillNewArray         = IMPL_LOOKUP.findStatic(MHI, "fillNewArray",
 626                                             MethodType.methodType(Object[].class, Integer.class, Object[].class));
 627                 MH_fillNewTypedArray    = IMPL_LOOKUP.findStatic(MHI, "fillNewTypedArray",
 628                                             MethodType.methodType(Object[].class, Object[].class, Integer.class, Object[].class));
 629 
 630                 MH_selectAlternative    = makeIntrinsic(
 631                         IMPL_LOOKUP.findStatic(MHI, "selectAlternative",
 632                                 MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class)),
 633                         Intrinsic.SELECT_ALTERNATIVE);
 634             } catch (ReflectiveOperationException ex) {
 635                 throw newInternalError(ex);
 636             }
 637         }
 638     }
 639 
 640     /** Factory method:  Collect or filter selected argument(s). */
 641     static MethodHandle makeCollectArguments(MethodHandle target,
 642                 MethodHandle collector, int collectArgPos, boolean retainOriginalArgs) {
 643         MethodType targetType = target.type();          // (a..., c, [b...])=>r
 644         MethodType collectorType = collector.type();    // (b...)=>c
 645         int collectArgCount = collectorType.parameterCount();
 646         Class<?> collectValType = collectorType.returnType();
 647         int collectValCount = (collectValType == void.class ? 0 : 1);
 648         MethodType srcType = targetType                 // (a..., [b...])=>r
 649                 .dropParameterTypes(collectArgPos, collectArgPos+collectValCount);
 650         if (!retainOriginalArgs) {                      // (a..., b...)=>r
 651             srcType = srcType.insertParameterTypes(collectArgPos, collectorType.parameterList());
 652         }
 653         // in  arglist: [0: ...keep1 | cpos: collect...  | cpos+cacount: keep2... ]
 654         // out arglist: [0: ...keep1 | cpos: collectVal? | cpos+cvcount: keep2... ]
 655         // out(retain): [0: ...keep1 | cpos: cV? coll... | cpos+cvc+cac: keep2... ]
 656 
 657         // Now build a LambdaForm.
 658         MethodType lambdaType = srcType.invokerType();
 659         Name[] names = arguments(2, lambdaType);
 660         final int collectNamePos = names.length - 2;
 661         final int targetNamePos  = names.length - 1;
 662 
 663         Name[] collectorArgs = Arrays.copyOfRange(names, 1 + collectArgPos, 1 + collectArgPos + collectArgCount);
 664         names[collectNamePos] = new Name(collector, (Object[]) collectorArgs);
 665 
 666         // Build argument array for the target.
 667         // Incoming LF args to copy are: [ (mh) headArgs collectArgs tailArgs ].
 668         // Output argument array is [ headArgs (collectVal)? (collectArgs)? tailArgs ].
 669         Name[] targetArgs = new Name[targetType.parameterCount()];
 670         int inputArgPos  = 1;  // incoming LF args to copy to target
 671         int targetArgPos = 0;  // fill pointer for targetArgs
 672         int chunk = collectArgPos;  // |headArgs|
 673         System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
 674         inputArgPos  += chunk;
 675         targetArgPos += chunk;
 676         if (collectValType != void.class) {
 677             targetArgs[targetArgPos++] = names[collectNamePos];
 678         }
 679         chunk = collectArgCount;
 680         if (retainOriginalArgs) {
 681             System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
 682             targetArgPos += chunk;   // optionally pass on the collected chunk
 683         }
 684         inputArgPos += chunk;
 685         chunk = targetArgs.length - targetArgPos;  // all the rest
 686         System.arraycopy(names, inputArgPos, targetArgs, targetArgPos, chunk);
 687         assert(inputArgPos + chunk == collectNamePos);  // use of rest of input args also
 688         names[targetNamePos] = new Name(target, (Object[]) targetArgs);
 689 
 690         LambdaForm form = new LambdaForm("collect", lambdaType.parameterCount(), names);
 691         return SimpleMethodHandle.make(srcType, form);
 692     }
 693 
 694     @LambdaForm.Hidden
 695     static
 696     MethodHandle selectAlternative(boolean testResult, MethodHandle target, MethodHandle fallback) {
 697         return testResult ? target : fallback;
 698     }
 699 
 700     static
 701     MethodHandle makeGuardWithTest(MethodHandle test,
 702                                    MethodHandle target,
 703                                    MethodHandle fallback) {
 704         MethodType type = target.type();
 705         assert(test.type().equals(type.changeReturnType(boolean.class)) && fallback.type().equals(type));
 706         MethodType basicType = type.basicType();
 707         LambdaForm form = makeGuardWithTestForm(basicType);
 708         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
 709         BoundMethodHandle mh;
 710 
 711         try {
 712             mh = (BoundMethodHandle)
 713                     data.constructor().invokeBasic(type, form,
 714                         (Object) test, (Object) profile(target), (Object) profile(fallback));
 715         } catch (Throwable ex) {
 716             throw uncaughtException(ex);
 717         }
 718         assert(mh.type() == type);
 719         return mh;
 720     }
 721 
 722 
 723     static
 724     MethodHandle profile(MethodHandle target) {
 725         if (DONT_INLINE_THRESHOLD > 0) {
 726             return BlockInliningWrapper.make(target);
 727         } else {
 728             return target;
 729         }
 730     }
 731 
 732     /**
 733      * Block inlining during JIT-compilation of a target method handle if it hasn't been invoked enough times.
 734      * Corresponding LambdaForm has @DontInline when compiled into bytecode.
 735      */
 736     static class BlockInliningWrapper extends DelegatingMethodHandle {
 737         private final MethodHandle target;
 738         private int count;
 739         private volatile boolean isActivated = true;
 740 
 741         private BlockInliningWrapper(MethodHandle target, LambdaForm lform, int count) {
 742             super(target.type(), lform);
 743             this.target = target;
 744             this.count = count;
 745         }
 746 
 747         @Hidden
 748         @Override
 749         protected MethodHandle getTarget() {
 750             return target;
 751         }
 752 
 753         @Override
 754         public MethodHandle asTypeUncached(MethodType newType) {
 755             MethodHandle newTarget = target.asType(newType);
 756             return asTypeCache = isActivated ? make(newTarget)
 757                                              : newTarget; // no need for a wrapper anymore
 758         }
 759 
 760         boolean unblock() {
 761             if (count <= 0) {
 762                 // Try to limit number of updates. MethodHandle.updateForm() doesn't guarantee LF update visibility.
 763                 if (isActivated) {
 764                     isActivated = false;
 765                     return true;
 766                 } else {
 767                     return false;
 768                 }
 769             } else {
 770                 --count;
 771                 return false;
 772             }
 773         }
 774 
 775         static MethodHandle make(MethodHandle target) {
 776             LambdaForm lform = DelegatingMethodHandle.makeReinvokerForm(target,
 777                     MethodTypeForm.LF_DELEGATE_COUNTING, BlockInliningWrapper.class, "reinvoker.dontInline", false,
 778                     DelegatingMethodHandle.NF_getTarget, NF_maybeUnblock);
 779             MethodHandle wrapper = new BlockInliningWrapper(target, lform, DONT_INLINE_THRESHOLD);
 780             return wrapper;
 781         }
 782 
 783         @Hidden
 784         static void maybeUnblock(Object o1) {
 785              BlockInliningWrapper wrapper = (BlockInliningWrapper) o1;
 786              if (wrapper.unblock()) {
 787                  // Reached invocation threshold. Replace counting/blocking wrapper with a reinvoker.
 788                  wrapper.isActivated = false;
 789                  MethodHandle target = wrapper.getTarget();
 790                  LambdaForm lform = DelegatingMethodHandle.makeReinvokerForm(
 791                          target, MethodTypeForm.LF_DELEGATE, DelegatingMethodHandle.class, NF_getTarget);
 792                  wrapper.updateForm(lform);
 793              }
 794         }
 795 
 796         static final NamedFunction NF_maybeUnblock;
 797         static {
 798             Class<?> THIS_CLASS = BlockInliningWrapper.class;
 799             try {
 800                 NF_maybeUnblock = new NamedFunction(THIS_CLASS.getDeclaredMethod("maybeUnblock", Object.class));
 801             } catch (ReflectiveOperationException ex) {
 802                 throw newInternalError(ex);
 803             }
 804         }
 805     }
 806 
 807     static
 808     LambdaForm makeGuardWithTestForm(MethodType basicType) {
 809         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWT);
 810         if (lform != null)  return lform;
 811         final int THIS_MH      = 0;  // the BMH_LLL
 812         final int ARG_BASE     = 1;  // start of incoming arguments
 813         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 814         int nameCursor = ARG_LIMIT;
 815         final int GET_TEST     = nameCursor++;
 816         final int GET_TARGET   = nameCursor++;
 817         final int GET_FALLBACK = nameCursor++;
 818         final int CALL_TEST    = nameCursor++;
 819         final int SELECT_ALT   = nameCursor++;
 820         final int CALL_TARGET  = nameCursor++;
 821         assert(CALL_TARGET == SELECT_ALT+1);  // must be true to trigger IBG.emitSelectAlternative
 822 
 823         MethodType lambdaType = basicType.invokerType();
 824         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 825 
 826         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLL();
 827         names[THIS_MH] = names[THIS_MH].withConstraint(data);
 828         names[GET_TEST]     = new Name(data.getterFunction(0), names[THIS_MH]);
 829         names[GET_TARGET]   = new Name(data.getterFunction(1), names[THIS_MH]);
 830         names[GET_FALLBACK] = new Name(data.getterFunction(2), names[THIS_MH]);
 831 
 832         Object[] invokeArgs = Arrays.copyOfRange(names, 0, ARG_LIMIT, Object[].class);
 833 
 834         // call test
 835         MethodType testType = basicType.changeReturnType(boolean.class).basicType();
 836         invokeArgs[0] = names[GET_TEST];
 837         names[CALL_TEST] = new Name(testType, invokeArgs);
 838 
 839         // call selectAlternative
 840         names[SELECT_ALT] = new Name(Lazy.MH_selectAlternative, names[CALL_TEST],
 841                                      names[GET_TARGET], names[GET_FALLBACK]);
 842 
 843         // call target or fallback
 844         invokeArgs[0] = names[SELECT_ALT];
 845         names[CALL_TARGET] = new Name(basicType, invokeArgs);
 846 
 847         lform = new LambdaForm("guard", lambdaType.parameterCount(), names);
 848 
 849         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWT, lform);
 850     }
 851 
 852     /**
 853      * The LambdaForm shape for catchException combinator is the following:
 854      * <blockquote><pre>{@code
 855      *  guardWithCatch=Lambda(a0:L,a1:L,a2:L)=>{
 856      *    t3:L=BoundMethodHandle$Species_LLLLL.argL0(a0:L);
 857      *    t4:L=BoundMethodHandle$Species_LLLLL.argL1(a0:L);
 858      *    t5:L=BoundMethodHandle$Species_LLLLL.argL2(a0:L);
 859      *    t6:L=BoundMethodHandle$Species_LLLLL.argL3(a0:L);
 860      *    t7:L=BoundMethodHandle$Species_LLLLL.argL4(a0:L);
 861      *    t8:L=MethodHandle.invokeBasic(t6:L,a1:L,a2:L);
 862      *    t9:L=MethodHandleImpl.guardWithCatch(t3:L,t4:L,t5:L,t8:L);
 863      *   t10:I=MethodHandle.invokeBasic(t7:L,t9:L);t10:I}
 864      * }</pre></blockquote>
 865      *
 866      * argL0 and argL2 are target and catcher method handles. argL1 is exception class.
 867      * argL3 and argL4 are auxiliary method handles: argL3 boxes arguments and wraps them into Object[]
 868      * (ValueConversions.array()) and argL4 unboxes result if necessary (ValueConversions.unbox()).
 869      *
 870      * Having t8 and t10 passed outside and not hardcoded into a lambda form allows to share lambda forms
 871      * among catchException combinators with the same basic type.
 872      */
 873     private static LambdaForm makeGuardWithCatchForm(MethodType basicType) {
 874         MethodType lambdaType = basicType.invokerType();
 875 
 876         LambdaForm lform = basicType.form().cachedLambdaForm(MethodTypeForm.LF_GWC);
 877         if (lform != null) {
 878             return lform;
 879         }
 880         final int THIS_MH      = 0;  // the BMH_LLLLL
 881         final int ARG_BASE     = 1;  // start of incoming arguments
 882         final int ARG_LIMIT    = ARG_BASE + basicType.parameterCount();
 883 
 884         int nameCursor = ARG_LIMIT;
 885         final int GET_TARGET       = nameCursor++;
 886         final int GET_CLASS        = nameCursor++;
 887         final int GET_CATCHER      = nameCursor++;
 888         final int GET_COLLECT_ARGS = nameCursor++;
 889         final int GET_UNBOX_RESULT = nameCursor++;
 890         final int BOXED_ARGS       = nameCursor++;
 891         final int TRY_CATCH        = nameCursor++;
 892         final int UNBOX_RESULT     = nameCursor++;
 893 
 894         Name[] names = arguments(nameCursor - ARG_LIMIT, lambdaType);
 895 
 896         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 897         names[THIS_MH]          = names[THIS_MH].withConstraint(data);
 898         names[GET_TARGET]       = new Name(data.getterFunction(0), names[THIS_MH]);
 899         names[GET_CLASS]        = new Name(data.getterFunction(1), names[THIS_MH]);
 900         names[GET_CATCHER]      = new Name(data.getterFunction(2), names[THIS_MH]);
 901         names[GET_COLLECT_ARGS] = new Name(data.getterFunction(3), names[THIS_MH]);
 902         names[GET_UNBOX_RESULT] = new Name(data.getterFunction(4), names[THIS_MH]);
 903 
 904         // FIXME: rework argument boxing/result unboxing logic for LF interpretation
 905 
 906         // t_{i}:L=MethodHandle.invokeBasic(collectArgs:L,a1:L,...);
 907         MethodType collectArgsType = basicType.changeReturnType(Object.class);
 908         MethodHandle invokeBasic = MethodHandles.basicInvoker(collectArgsType);
 909         Object[] args = new Object[invokeBasic.type().parameterCount()];
 910         args[0] = names[GET_COLLECT_ARGS];
 911         System.arraycopy(names, ARG_BASE, args, 1, ARG_LIMIT-ARG_BASE);
 912         names[BOXED_ARGS] = new Name(makeIntrinsic(invokeBasic, Intrinsic.GUARD_WITH_CATCH), args);
 913 
 914         // t_{i+1}:L=MethodHandleImpl.guardWithCatch(target:L,exType:L,catcher:L,t_{i}:L);
 915         Object[] gwcArgs = new Object[] {names[GET_TARGET], names[GET_CLASS], names[GET_CATCHER], names[BOXED_ARGS]};
 916         names[TRY_CATCH] = new Name(Lazy.NF_guardWithCatch, gwcArgs);
 917 
 918         // t_{i+2}:I=MethodHandle.invokeBasic(unbox:L,t_{i+1}:L);
 919         MethodHandle invokeBasicUnbox = MethodHandles.basicInvoker(MethodType.methodType(basicType.rtype(), Object.class));
 920         Object[] unboxArgs  = new Object[] {names[GET_UNBOX_RESULT], names[TRY_CATCH]};
 921         names[UNBOX_RESULT] = new Name(invokeBasicUnbox, unboxArgs);
 922 
 923         lform = new LambdaForm("guardWithCatch", lambdaType.parameterCount(), names);
 924 
 925         return basicType.form().setCachedLambdaForm(MethodTypeForm.LF_GWC, lform);
 926     }
 927 
 928     static
 929     MethodHandle makeGuardWithCatch(MethodHandle target,
 930                                     Class<? extends Throwable> exType,
 931                                     MethodHandle catcher) {
 932         MethodType type = target.type();
 933         LambdaForm form = makeGuardWithCatchForm(type.basicType());
 934 
 935         // Prepare auxiliary method handles used during LambdaForm interpretation.
 936         // Box arguments and wrap them into Object[]: ValueConversions.array().
 937         MethodType varargsType = type.changeReturnType(Object[].class);
 938         MethodHandle collectArgs = varargsArray(type.parameterCount()).asType(varargsType);
 939         // Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore().
 940         MethodHandle unboxResult;
 941         Class<?> rtype = type.returnType();
 942         if (rtype.isPrimitive()) {
 943             if (rtype == void.class) {
 944                 unboxResult = ValueConversions.ignore();
 945             } else {
 946                 Wrapper w = Wrapper.forPrimitiveType(type.returnType());
 947                 unboxResult = ValueConversions.unboxExact(w);
 948             }
 949         } else {
 950             unboxResult = MethodHandles.identity(Object.class);
 951         }
 952 
 953         BoundMethodHandle.SpeciesData data = BoundMethodHandle.speciesData_LLLLL();
 954         BoundMethodHandle mh;
 955         try {
 956             mh = (BoundMethodHandle)
 957                     data.constructor().invokeBasic(type, form, (Object) target, (Object) exType, (Object) catcher,
 958                                                    (Object) collectArgs, (Object) unboxResult);
 959         } catch (Throwable ex) {
 960             throw uncaughtException(ex);
 961         }
 962         assert(mh.type() == type);
 963         return mh;
 964     }
 965 
 966     /**
 967      * Intrinsified during LambdaForm compilation
 968      * (see {@link InvokerBytecodeGenerator#emitGuardWithCatch emitGuardWithCatch}).
 969      */
 970     @LambdaForm.Hidden
 971     static Object guardWithCatch(MethodHandle target, Class<? extends Throwable> exType, MethodHandle catcher,
 972                                  Object... av) throws Throwable {
 973         // Use asFixedArity() to avoid unnecessary boxing of last argument for VarargsCollector case.
 974         try {
 975             return target.asFixedArity().invokeWithArguments(av);
 976         } catch (Throwable t) {
 977             if (!exType.isInstance(t)) throw t;
 978             return catcher.asFixedArity().invokeWithArguments(prepend(t, av));
 979         }
 980     }
 981 
 982     /** Prepend an element {@code elem} to an {@code array}. */
 983     @LambdaForm.Hidden
 984     private static Object[] prepend(Object elem, Object[] array) {
 985         Object[] newArray = new Object[array.length+1];
 986         newArray[0] = elem;
 987         System.arraycopy(array, 0, newArray, 1, array.length);
 988         return newArray;
 989     }
 990 
 991     static
 992     MethodHandle throwException(MethodType type) {
 993         assert(Throwable.class.isAssignableFrom(type.parameterType(0)));
 994         int arity = type.parameterCount();
 995         if (arity > 1) {
 996             MethodHandle mh = throwException(type.dropParameterTypes(1, arity));
 997             mh = MethodHandles.dropArguments(mh, 1, type.parameterList().subList(1, arity));
 998             return mh;
 999         }
1000         return makePairwiseConvert(Lazy.NF_throwException.resolvedHandle(), type, false, true);
1001     }
1002 
1003     static <T extends Throwable> Empty throwException(T t) throws T { throw t; }
1004 
1005     static MethodHandle[] FAKE_METHOD_HANDLE_INVOKE = new MethodHandle[2];
1006     static MethodHandle fakeMethodHandleInvoke(MemberName method) {
1007         int idx;
1008         assert(method.isMethodHandleInvoke());
1009         switch (method.getName()) {
1010         case "invoke":       idx = 0; break;
1011         case "invokeExact":  idx = 1; break;
1012         default:             throw new InternalError(method.getName());
1013         }
1014         MethodHandle mh = FAKE_METHOD_HANDLE_INVOKE[idx];
1015         if (mh != null)  return mh;
1016         MethodType type = MethodType.methodType(Object.class, UnsupportedOperationException.class,
1017                                                 MethodHandle.class, Object[].class);
1018         mh = throwException(type);
1019         mh = mh.bindTo(new UnsupportedOperationException("cannot reflectively invoke MethodHandle"));
1020         if (!method.getInvocationType().equals(mh.type()))
1021             throw new InternalError(method.toString());
1022         mh = mh.withInternalMemberName(method, false);
1023         mh = mh.asVarargsCollector(Object[].class);
1024         assert(method.isVarargs());
1025         FAKE_METHOD_HANDLE_INVOKE[idx] = mh;
1026         return mh;
1027     }
1028 
1029     /**
1030      * Create an alias for the method handle which, when called,
1031      * appears to be called from the same class loader and protection domain
1032      * as hostClass.
1033      * This is an expensive no-op unless the method which is called
1034      * is sensitive to its caller.  A small number of system methods
1035      * are in this category, including Class.forName and Method.invoke.
1036      */
1037     static
1038     MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1039         return BindCaller.bindCaller(mh, hostClass);
1040     }
1041 
1042     // Put the whole mess into its own nested class.
1043     // That way we can lazily load the code and set up the constants.
1044     private static class BindCaller {
1045         static
1046         MethodHandle bindCaller(MethodHandle mh, Class<?> hostClass) {
1047             // Do not use this function to inject calls into system classes.
1048             if (hostClass == null
1049                 ||    (hostClass.isArray() ||
1050                        hostClass.isPrimitive() ||
1051                        hostClass.getName().startsWith("java.") ||
1052                        hostClass.getName().startsWith("sun."))) {
1053                 throw new InternalError();  // does not happen, and should not anyway
1054             }
1055             // For simplicity, convert mh to a varargs-like method.
1056             MethodHandle vamh = prepareForInvoker(mh);
1057             // Cache the result of makeInjectedInvoker once per argument class.
1058             MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
1059             return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
1060         }
1061 
1062         private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
1063             Class<?> bcc = UNSAFE.defineAnonymousClass(hostClass, T_BYTES, null);
1064             if (hostClass.getClassLoader() != bcc.getClassLoader())
1065                 throw new InternalError(hostClass.getName()+" (CL)");
1066             try {
1067                 if (hostClass.getProtectionDomain() != bcc.getProtectionDomain())
1068                     throw new InternalError(hostClass.getName()+" (PD)");
1069             } catch (SecurityException ex) {
1070                 // Self-check was blocked by security manager.  This is OK.
1071                 // In fact the whole try body could be turned into an assertion.
1072             }
1073             try {
1074                 MethodHandle init = IMPL_LOOKUP.findStatic(bcc, "init", MethodType.methodType(void.class));
1075                 init.invokeExact();  // force initialization of the class
1076             } catch (Throwable ex) {
1077                 throw uncaughtException(ex);
1078             }
1079             MethodHandle bccInvoker;
1080             try {
1081                 MethodType invokerMT = MethodType.methodType(Object.class, MethodHandle.class, Object[].class);
1082                 bccInvoker = IMPL_LOOKUP.findStatic(bcc, "invoke_V", invokerMT);
1083             } catch (ReflectiveOperationException ex) {
1084                 throw uncaughtException(ex);
1085             }
1086             // Test the invoker, to ensure that it really injects into the right place.
1087             try {
1088                 MethodHandle vamh = prepareForInvoker(MH_checkCallerClass);
1089                 Object ok = bccInvoker.invokeExact(vamh, new Object[]{hostClass, bcc});
1090             } catch (Throwable ex) {
1091                 throw new InternalError(ex);
1092             }
1093             return bccInvoker;
1094         }
1095         private static ClassValue<MethodHandle> CV_makeInjectedInvoker = new ClassValue<MethodHandle>() {
1096             @Override protected MethodHandle computeValue(Class<?> hostClass) {
1097                 return makeInjectedInvoker(hostClass);
1098             }
1099         };
1100 
1101         // Adapt mh so that it can be called directly from an injected invoker:
1102         private static MethodHandle prepareForInvoker(MethodHandle mh) {
1103             mh = mh.asFixedArity();
1104             MethodType mt = mh.type();
1105             int arity = mt.parameterCount();
1106             MethodHandle vamh = mh.asType(mt.generic());
1107             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1108             vamh = vamh.asSpreader(Object[].class, arity);
1109             vamh.internalForm().compileToBytecode();  // eliminate LFI stack frames
1110             return vamh;
1111         }
1112 
1113         // Undo the adapter effect of prepareForInvoker:
1114         private static MethodHandle restoreToType(MethodHandle vamh,
1115                                                   MethodHandle original,
1116                                                   Class<?> hostClass) {
1117             MethodType type = original.type();
1118             MethodHandle mh = vamh.asCollector(Object[].class, type.parameterCount());
1119             MemberName member = original.internalMemberName();
1120             mh = mh.asType(type);
1121             mh = new WrappedMember(mh, type, member, original.isInvokeSpecial(), hostClass);
1122             return mh;
1123         }
1124 
1125         private static final MethodHandle MH_checkCallerClass;
1126         static {
1127             final Class<?> THIS_CLASS = BindCaller.class;
1128             assert(checkCallerClass(THIS_CLASS, THIS_CLASS));
1129             try {
1130                 MH_checkCallerClass = IMPL_LOOKUP
1131                     .findStatic(THIS_CLASS, "checkCallerClass",
1132                                 MethodType.methodType(boolean.class, Class.class, Class.class));
1133                 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
1134             } catch (Throwable ex) {
1135                 throw new InternalError(ex);
1136             }
1137         }
1138 
1139         @CallerSensitive
1140         private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
1141             // This method is called via MH_checkCallerClass and so it's
1142             // correct to ask for the immediate caller here.
1143             Class<?> actual = Reflection.getCallerClass();
1144             if (actual != expected && actual != expected2)
1145                 throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
1146                                         +(expected == expected2 ? "" : ", or else "+expected2.getName()));
1147             return true;
1148         }
1149 
1150         private static final byte[] T_BYTES;
1151         static {
1152             final Object[] values = {null};
1153             AccessController.doPrivileged(new PrivilegedAction<Void>() {
1154                     public Void run() {
1155                         try {
1156                             Class<T> tClass = T.class;
1157                             String tName = tClass.getName();
1158                             String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
1159                             java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
1160                             int len = uconn.getContentLength();
1161                             byte[] bytes = new byte[len];
1162                             try (java.io.InputStream str = uconn.getInputStream()) {
1163                                 int nr = str.read(bytes);
1164                                 if (nr != len)  throw new java.io.IOException(tResource);
1165                             }
1166                             values[0] = bytes;
1167                         } catch (java.io.IOException ex) {
1168                             throw new InternalError(ex);
1169                         }
1170                         return null;
1171                     }
1172                 });
1173             T_BYTES = (byte[]) values[0];
1174         }
1175 
1176         // The following class is used as a template for Unsafe.defineAnonymousClass:
1177         private static class T {
1178             static void init() { }  // side effect: initializes this class
1179             static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
1180                 return vamh.invokeExact(args);
1181             }
1182         }
1183     }
1184 
1185 
1186     /** This subclass allows a wrapped method handle to be re-associated with an arbitrary member name. */
1187     private static final class WrappedMember extends DelegatingMethodHandle {
1188         private final MethodHandle target;
1189         private final MemberName member;
1190         private final Class<?> callerClass;
1191         private final boolean isInvokeSpecial;
1192 
1193         private WrappedMember(MethodHandle target, MethodType type,
1194                               MemberName member, boolean isInvokeSpecial,
1195                               Class<?> callerClass) {
1196             super(type, target);
1197             this.target = target;
1198             this.member = member;
1199             this.callerClass = callerClass;
1200             this.isInvokeSpecial = isInvokeSpecial;
1201         }
1202 
1203         @Override
1204         MemberName internalMemberName() {
1205             return member;
1206         }
1207         @Override
1208         Class<?> internalCallerClass() {
1209             return callerClass;
1210         }
1211         @Override
1212         boolean isInvokeSpecial() {
1213             return isInvokeSpecial;
1214         }
1215         @Override
1216         protected MethodHandle getTarget() {
1217             return target;
1218         }
1219         @Override
1220         public MethodHandle asTypeUncached(MethodType newType) {
1221             // This MH is an alias for target, except for the MemberName
1222             // Drop the MemberName if there is any conversion.
1223             return asTypeCache = target.asType(newType);
1224         }
1225     }
1226 
1227     static MethodHandle makeWrappedMember(MethodHandle target, MemberName member, boolean isInvokeSpecial) {
1228         if (member.equals(target.internalMemberName()) && isInvokeSpecial == target.isInvokeSpecial())
1229             return target;
1230         return new WrappedMember(target, target.type(), member, isInvokeSpecial, null);
1231     }
1232 
1233     /** Intrinsic IDs */
1234     /*non-public*/
1235     enum Intrinsic {
1236         SELECT_ALTERNATIVE,
1237         GUARD_WITH_CATCH,
1238         NEW_ARRAY,
1239         ARRAY_LOAD,
1240         ARRAY_STORE,
1241         IDENTITY,
1242         ZERO,
1243         NONE // no intrinsic associated
1244     }
1245 
1246     /** Mark arbitrary method handle as intrinsic.
1247      * InvokerBytecodeGenerator uses this info to produce more efficient bytecode shape. */
1248     private static final class IntrinsicMethodHandle extends DelegatingMethodHandle {
1249         private final MethodHandle target;
1250         private final Intrinsic intrinsicName;
1251 
1252         IntrinsicMethodHandle(MethodHandle target, Intrinsic intrinsicName) {
1253             super(target.type(), target);
1254             this.target = target;
1255             this.intrinsicName = intrinsicName;
1256         }
1257 
1258         @Override
1259         protected MethodHandle getTarget() {
1260             return target;
1261         }
1262 
1263         @Override
1264         Intrinsic intrinsicName() {
1265             return intrinsicName;
1266         }
1267 
1268         @Override
1269         public MethodHandle asTypeUncached(MethodType newType) {
1270             // This MH is an alias for target, except for the intrinsic name
1271             // Drop the name if there is any conversion.
1272             return asTypeCache = target.asType(newType);
1273         }
1274 
1275         @Override
1276         String internalProperties() {
1277             return super.internalProperties() +
1278                     "\n& Intrinsic="+intrinsicName;
1279         }
1280 
1281         @Override
1282         public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
1283             if (intrinsicName == Intrinsic.IDENTITY) {
1284                 MethodType resultType = type().asCollectorType(arrayType, arrayLength);
1285                 MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength);
1286                 return newArray.asType(resultType);
1287             }
1288             return super.asCollector(arrayType, arrayLength);
1289         }
1290     }
1291 
1292     static MethodHandle makeIntrinsic(MethodHandle target, Intrinsic intrinsicName) {
1293         if (intrinsicName == target.intrinsicName())
1294             return target;
1295         return new IntrinsicMethodHandle(target, intrinsicName);
1296     }
1297 
1298     static MethodHandle makeIntrinsic(MethodType type, LambdaForm form, Intrinsic intrinsicName) {
1299         return new IntrinsicMethodHandle(SimpleMethodHandle.make(type, form), intrinsicName);
1300     }
1301 
1302     /// Collection of multiple arguments.
1303 
1304     private static MethodHandle findCollector(String name, int nargs, Class<?> rtype, Class<?>... ptypes) {
1305         MethodType type = MethodType.genericMethodType(nargs)
1306                 .changeReturnType(rtype)
1307                 .insertParameterTypes(0, ptypes);
1308         try {
1309             return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, name, type);
1310         } catch (ReflectiveOperationException ex) {
1311             return null;
1312         }
1313     }
1314 
1315     private static final Object[] NO_ARGS_ARRAY = {};
1316     private static Object[] makeArray(Object... args) { return args; }
1317     private static Object[] array() { return NO_ARGS_ARRAY; }
1318     private static Object[] array(Object a0)
1319                 { return makeArray(a0); }
1320     private static Object[] array(Object a0, Object a1)
1321                 { return makeArray(a0, a1); }
1322     private static Object[] array(Object a0, Object a1, Object a2)
1323                 { return makeArray(a0, a1, a2); }
1324     private static Object[] array(Object a0, Object a1, Object a2, Object a3)
1325                 { return makeArray(a0, a1, a2, a3); }
1326     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1327                                   Object a4)
1328                 { return makeArray(a0, a1, a2, a3, a4); }
1329     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1330                                   Object a4, Object a5)
1331                 { return makeArray(a0, a1, a2, a3, a4, a5); }
1332     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1333                                   Object a4, Object a5, Object a6)
1334                 { return makeArray(a0, a1, a2, a3, a4, a5, a6); }
1335     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1336                                   Object a4, Object a5, Object a6, Object a7)
1337                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7); }
1338     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1339                                   Object a4, Object a5, Object a6, Object a7,
1340                                   Object a8)
1341                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8); }
1342     private static Object[] array(Object a0, Object a1, Object a2, Object a3,
1343                                   Object a4, Object a5, Object a6, Object a7,
1344                                   Object a8, Object a9)
1345                 { return makeArray(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); }
1346     private static MethodHandle[] makeArrays() {
1347         ArrayList<MethodHandle> mhs = new ArrayList<>();
1348         for (;;) {
1349             MethodHandle mh = findCollector("array", mhs.size(), Object[].class);
1350             if (mh == null)  break;
1351             mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
1352             mhs.add(mh);
1353         }
1354         assert(mhs.size() == 11);  // current number of methods
1355         return mhs.toArray(new MethodHandle[MAX_ARITY+1]);
1356     }
1357     private static final MethodHandle[] ARRAYS = makeArrays();
1358 
1359     // filling versions of the above:
1360     // using Integer len instead of int len and no varargs to avoid bootstrapping problems
1361     private static Object[] fillNewArray(Integer len, Object[] /*not ...*/ args) {
1362         Object[] a = new Object[len];
1363         fillWithArguments(a, 0, args);
1364         return a;
1365     }
1366     private static Object[] fillNewTypedArray(Object[] example, Integer len, Object[] /*not ...*/ args) {
1367         Object[] a = Arrays.copyOf(example, len);
1368         assert(a.getClass() != Object[].class);
1369         fillWithArguments(a, 0, args);
1370         return a;
1371     }
1372     private static void fillWithArguments(Object[] a, int pos, Object... args) {
1373         System.arraycopy(args, 0, a, pos, args.length);
1374     }
1375     // using Integer pos instead of int pos to avoid bootstrapping problems
1376     private static Object[] fillArray(Integer pos, Object[] a, Object a0)
1377                 { fillWithArguments(a, pos, a0); return a; }
1378     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1)
1379                 { fillWithArguments(a, pos, a0, a1); return a; }
1380     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2)
1381                 { fillWithArguments(a, pos, a0, a1, a2); return a; }
1382     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3)
1383                 { fillWithArguments(a, pos, a0, a1, a2, a3); return a; }
1384     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1385                                   Object a4)
1386                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4); return a; }
1387     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1388                                   Object a4, Object a5)
1389                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5); return a; }
1390     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1391                                   Object a4, Object a5, Object a6)
1392                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6); return a; }
1393     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1394                                   Object a4, Object a5, Object a6, Object a7)
1395                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7); return a; }
1396     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1397                                   Object a4, Object a5, Object a6, Object a7,
1398                                   Object a8)
1399                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8); return a; }
1400     private static Object[] fillArray(Integer pos, Object[] a, Object a0, Object a1, Object a2, Object a3,
1401                                   Object a4, Object a5, Object a6, Object a7,
1402                                   Object a8, Object a9)
1403                 { fillWithArguments(a, pos, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); return a; }
1404     private static MethodHandle[] makeFillArrays() {
1405         ArrayList<MethodHandle> mhs = new ArrayList<>();
1406         mhs.add(null);  // there is no empty fill; at least a0 is required
1407         for (;;) {
1408             MethodHandle mh = findCollector("fillArray", mhs.size(), Object[].class, Integer.class, Object[].class);
1409             if (mh == null)  break;
1410             mhs.add(mh);
1411         }
1412         assert(mhs.size() == 11);  // current number of methods
1413         return mhs.toArray(new MethodHandle[0]);
1414     }
1415     private static final MethodHandle[] FILL_ARRAYS = makeFillArrays();
1416 
1417     private static Object copyAsPrimitiveArray(Wrapper w, Object... boxes) {
1418         Object a = w.makeArray(boxes.length);
1419         w.copyArrayUnboxing(boxes, 0, a, 0, boxes.length);
1420         return a;
1421     }
1422 
1423     /** Return a method handle that takes the indicated number of Object
1424      *  arguments and returns an Object array of them, as if for varargs.
1425      */
1426     static MethodHandle varargsArray(int nargs) {
1427         MethodHandle mh = ARRAYS[nargs];
1428         if (mh != null)  return mh;
1429         mh = findCollector("array", nargs, Object[].class);
1430         if (mh != null)  mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
1431         if (mh != null)  return ARRAYS[nargs] = mh;
1432         mh = buildVarargsArray(Lazy.MH_fillNewArray, Lazy.MH_arrayIdentity, nargs);
1433         assert(assertCorrectArity(mh, nargs));
1434         mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
1435         return ARRAYS[nargs] = mh;
1436     }
1437 
1438     private static boolean assertCorrectArity(MethodHandle mh, int arity) {
1439         assert(mh.type().parameterCount() == arity) : "arity != "+arity+": "+mh;
1440         return true;
1441     }
1442 
1443     // Array identity function (used as Lazy.MH_arrayIdentity).
1444     static <T> T[] identity(T[] x) {
1445         return x;
1446     }
1447 
1448     private static MethodHandle buildVarargsArray(MethodHandle newArray, MethodHandle finisher, int nargs) {
1449         // Build up the result mh as a sequence of fills like this:
1450         //   finisher(fill(fill(newArrayWA(23,x1..x10),10,x11..x20),20,x21..x23))
1451         // The various fill(_,10*I,___*[J]) are reusable.
1452         int leftLen = Math.min(nargs, LEFT_ARGS);  // absorb some arguments immediately
1453         int rightLen = nargs - leftLen;
1454         MethodHandle leftCollector = newArray.bindTo(nargs);
1455         leftCollector = leftCollector.asCollector(Object[].class, leftLen);
1456         MethodHandle mh = finisher;
1457         if (rightLen > 0) {
1458             MethodHandle rightFiller = fillToRight(LEFT_ARGS + rightLen);
1459             if (mh == Lazy.MH_arrayIdentity)
1460                 mh = rightFiller;
1461             else
1462                 mh = MethodHandles.collectArguments(mh, 0, rightFiller);
1463         }
1464         if (mh == Lazy.MH_arrayIdentity)
1465             mh = leftCollector;
1466         else
1467             mh = MethodHandles.collectArguments(mh, 0, leftCollector);
1468         return mh;
1469     }
1470 
1471     private static final int LEFT_ARGS = (FILL_ARRAYS.length - 1);
1472     private static final MethodHandle[] FILL_ARRAY_TO_RIGHT = new MethodHandle[MAX_ARITY+1];
1473     /** fill_array_to_right(N).invoke(a, argL..arg[N-1])
1474      *  fills a[L]..a[N-1] with corresponding arguments,
1475      *  and then returns a.  The value L is a global constant (LEFT_ARGS).
1476      */
1477     private static MethodHandle fillToRight(int nargs) {
1478         MethodHandle filler = FILL_ARRAY_TO_RIGHT[nargs];
1479         if (filler != null)  return filler;
1480         filler = buildFiller(nargs);
1481         assert(assertCorrectArity(filler, nargs - LEFT_ARGS + 1));
1482         return FILL_ARRAY_TO_RIGHT[nargs] = filler;
1483     }
1484     private static MethodHandle buildFiller(int nargs) {
1485         if (nargs <= LEFT_ARGS)
1486             return Lazy.MH_arrayIdentity;  // no args to fill; return the array unchanged
1487         // we need room for both mh and a in mh.invoke(a, arg*[nargs])
1488         final int CHUNK = LEFT_ARGS;
1489         int rightLen = nargs % CHUNK;
1490         int midLen = nargs - rightLen;
1491         if (rightLen == 0) {
1492             midLen = nargs - (rightLen = CHUNK);
1493             if (FILL_ARRAY_TO_RIGHT[midLen] == null) {
1494                 // build some precursors from left to right
1495                 for (int j = LEFT_ARGS % CHUNK; j < midLen; j += CHUNK)
1496                     if (j > LEFT_ARGS)  fillToRight(j);
1497             }
1498         }
1499         if (midLen < LEFT_ARGS) rightLen = nargs - (midLen = LEFT_ARGS);
1500         assert(rightLen > 0);
1501         MethodHandle midFill = fillToRight(midLen);  // recursive fill
1502         MethodHandle rightFill = FILL_ARRAYS[rightLen].bindTo(midLen);  // [midLen..nargs-1]
1503         assert(midFill.type().parameterCount()   == 1 + midLen - LEFT_ARGS);
1504         assert(rightFill.type().parameterCount() == 1 + rightLen);
1505 
1506         // Combine the two fills:
1507         //   right(mid(a, x10..x19), x20..x23)
1508         // The final product will look like this:
1509         //   right(mid(newArrayLeft(24, x0..x9), x10..x19), x20..x23)
1510         if (midLen == LEFT_ARGS)
1511             return rightFill;
1512         else
1513             return MethodHandles.collectArguments(rightFill, 0, midFill);
1514     }
1515 
1516     // Type-polymorphic version of varargs maker.
1517     private static final ClassValue<MethodHandle[]> TYPED_COLLECTORS
1518         = new ClassValue<MethodHandle[]>() {
1519             @Override
1520             protected MethodHandle[] computeValue(Class<?> type) {
1521                 return new MethodHandle[256];
1522             }
1523     };
1524 
1525     static final int MAX_JVM_ARITY = 255;  // limit imposed by the JVM
1526 
1527     /** Return a method handle that takes the indicated number of
1528      *  typed arguments and returns an array of them.
1529      *  The type argument is the array type.
1530      */
1531     static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
1532         Class<?> elemType = arrayType.getComponentType();
1533         if (elemType == null)  throw new IllegalArgumentException("not an array: "+arrayType);
1534         // FIXME: Need more special casing and caching here.
1535         if (nargs >= MAX_JVM_ARITY/2 - 1) {
1536             int slots = nargs;
1537             final int MAX_ARRAY_SLOTS = MAX_JVM_ARITY - 1;  // 1 for receiver MH
1538             if (slots <= MAX_ARRAY_SLOTS && elemType.isPrimitive())
1539                 slots *= Wrapper.forPrimitiveType(elemType).stackSlots();
1540             if (slots > MAX_ARRAY_SLOTS)
1541                 throw new IllegalArgumentException("too many arguments: "+arrayType.getSimpleName()+", length "+nargs);
1542         }
1543         if (elemType == Object.class)
1544             return varargsArray(nargs);
1545         // other cases:  primitive arrays, subtypes of Object[]
1546         MethodHandle cache[] = TYPED_COLLECTORS.get(elemType);
1547         MethodHandle mh = nargs < cache.length ? cache[nargs] : null;
1548         if (mh != null)  return mh;
1549         if (nargs == 0) {
1550             Object example = java.lang.reflect.Array.newInstance(arrayType.getComponentType(), 0);
1551             mh = MethodHandles.constant(arrayType, example);
1552         } else if (elemType.isPrimitive()) {
1553             MethodHandle builder = Lazy.MH_fillNewArray;
1554             MethodHandle producer = buildArrayProducer(arrayType);
1555             mh = buildVarargsArray(builder, producer, nargs);
1556         } else {
1557             Class<? extends Object[]> objArrayType = arrayType.asSubclass(Object[].class);
1558             Object[] example = Arrays.copyOf(NO_ARGS_ARRAY, 0, objArrayType);
1559             MethodHandle builder = Lazy.MH_fillNewTypedArray.bindTo(example);
1560             MethodHandle producer = Lazy.MH_arrayIdentity; // must be weakly typed
1561             mh = buildVarargsArray(builder, producer, nargs);
1562         }
1563         mh = mh.asType(MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType)));
1564         mh = makeIntrinsic(mh, Intrinsic.NEW_ARRAY);
1565         assert(assertCorrectArity(mh, nargs));
1566         if (nargs < cache.length)
1567             cache[nargs] = mh;
1568         return mh;
1569     }
1570 
1571     private static MethodHandle buildArrayProducer(Class<?> arrayType) {
1572         Class<?> elemType = arrayType.getComponentType();
1573         assert(elemType.isPrimitive());
1574         return Lazy.MH_copyAsPrimitiveArray.bindTo(Wrapper.forPrimitiveType(elemType));
1575     }
1576 }