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