18b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath/*
28b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
38b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
58b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This code is free software; you can redistribute it and/or modify it
68b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * under the terms of the GNU General Public License version 2 only, as
78b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * published by the Free Software Foundation.  Oracle designates this
88b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * particular file as subject to the "Classpath" exception as provided
98b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * by Oracle in the LICENSE file that accompanied this code.
108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This code is distributed in the hope that it will be useful, but WITHOUT
128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * version 2 for more details (a copy is included in the LICENSE file that
158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * accompanied this code).
168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * You should have received a copy of the GNU General Public License version
188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * 2 along with this work; if not, write to the Free Software Foundation,
198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * or visit www.oracle.com if you need additional information or have any
238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * questions.
248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath */
258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathpackage java.lang.invoke;
278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
29faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamathimport dalvik.system.EmulatedStackFrame;
308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathimport static java.lang.invoke.MethodHandleStatics.*;
328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath/**
348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A method handle is a typed, directly executable reference to an underlying method,
358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * constructor, field, or similar low-level operation, with optional
368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * transformations of arguments or return values.
378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * These transformations are quite general, and include such patterns as
388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@linkplain #asType conversion},
398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@linkplain #bindTo insertion},
406774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * {@linkplain java.lang.invoke.MethodHandles#dropArguments deletion},
41f4ee5c6dccff8f7e037f95b26767e552ea93d41bNarayan Kamath * and {@linkplain java.lang.invoke.MethodHandles#filterArguments substitution}.
428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Method handle contents</h1>
448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Method handles are dynamically and strongly typed according to their parameter and return types.
458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * They are not distinguished by the name or the defining class of their underlying methods.
468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A method handle must be invoked using a symbolic type descriptor which matches
478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the method handle's own {@linkplain #type type descriptor}.
488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Every method handle reports its type descriptor via the {@link #type type} accessor.
508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This type descriptor is a {@link java.lang.invoke.MethodType MethodType} object,
518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * whose structure is a series of classes, one of which is
528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the return type of the method (or {@code void.class} if none).
538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A method handle's type controls the types of invocations it accepts,
558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and the kinds of transformations that apply to it.
568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A method handle contains a pair of special invoker methods
588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * called {@link #invokeExact invokeExact} and {@link #invoke invoke}.
598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Both invoker methods provide direct access to the method handle's
608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * underlying method, constructor, field, or other operation,
618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * as modified by transformations of arguments and return values.
628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Both invokers accept calls which exactly match the method handle's own type.
638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The plain, inexact invoker also accepts a range of other call types.
648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Method handles are immutable and have no visible state.
668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Of course, they can be bound to underlying methods or data which exhibit state.
678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * With respect to the Java Memory Model, any method handle will behave
688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * as if all of its (internal) fields are final variables.  This means that any method
698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * handle made visible to the application will always be fully formed.
708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This is true even if the method handle is published through a shared
718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * variable in a data race.
728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Method handles cannot be subclassed by the user.
748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Implementations may (or may not) create internal subclasses of {@code MethodHandle}
758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * which may be visible via the {@link java.lang.Object#getClass Object.getClass}
768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * operation.  The programmer should not draw conclusions about a method handle
778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * from its specific class, as the method handle class hierarchy (if any)
788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * may change from time to time or across implementations from different vendors.
798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Method handle compilation</h1>
818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A Java method call expression naming {@code invokeExact} or {@code invoke}
828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * can invoke a method handle from Java source code.
838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * From the viewpoint of source code, these methods can take any arguments
848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and their result can be cast to any return type.
858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Formally this is accomplished by giving the invoker methods
868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@code Object} return types and variable arity {@code Object} arguments,
878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * but they have an additional quality called <em>signature polymorphism</em>
888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * which connects this freedom of invocation directly to the JVM execution stack.
898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As is usual with virtual methods, source-level calls to {@code invokeExact}
918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and {@code invoke} compile to an {@code invokevirtual} instruction.
928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * More unusually, the compiler must record the actual argument types,
938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and may not perform method invocation conversions on the arguments.
948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Instead, it must push them on the stack according to their own unconverted types.
958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The method handle object itself is pushed on the stack before the arguments.
968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The compiler then calls the method handle with a symbolic type descriptor which
978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * describes the argument and return types.
988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * To issue a complete symbolic type descriptor, the compiler must also determine
1008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the return type.  This is based on a cast on the method invocation expression,
1018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * if there is one, or else {@code Object} if the invocation is an expression
1028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * or else {@code void} if the invocation is a statement.
1038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The cast may be to a primitive type (but not {@code void}).
1048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As a corner case, an uncasted {@code null} argument is given
1068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * a symbolic type descriptor of {@code java.lang.Void}.
1078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The ambiguity with the type {@code Void} is harmless, since there are no references of type
1088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@code Void} except the null reference.
1098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
1108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Method handle invocation</h1>
1118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The first time a {@code invokevirtual} instruction is executed
1128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * it is linked, by symbolically resolving the names in the instruction
1138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and verifying that the method call is statically legal.
1148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This is true of calls to {@code invokeExact} and {@code invoke}.
1158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In this case, the symbolic type descriptor emitted by the compiler is checked for
1168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * correct syntax and names it contains are resolved.
1178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Thus, an {@code invokevirtual} instruction which invokes
1188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * a method handle will always link, as long
1198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * as the symbolic type descriptor is syntactically well-formed
1208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and the types exist.
1218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * When the {@code invokevirtual} is executed after linking,
1238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the receiving method handle's type is first checked by the JVM
1248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * to ensure that it matches the symbolic type descriptor.
1258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * If the type match fails, it means that the method which the
1268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * caller is invoking is not present on the individual
1278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * method handle being invoked.
1288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In the case of {@code invokeExact}, the type descriptor of the invocation
1308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (after resolving symbolic type names) must exactly match the method type
1318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * of the receiving method handle.
1328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In the case of plain, inexact {@code invoke}, the resolved type descriptor
1338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * must be a valid argument to the receiver's {@link #asType asType} method.
1348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Thus, plain {@code invoke} is more permissive than {@code invokeExact}.
1358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * After type matching, a call to {@code invokeExact} directly
1378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and immediately invoke the method handle's underlying method
1388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (or other behavior, as the case may be).
1398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A call to plain {@code invoke} works the same as a call to
1418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@code invokeExact}, if the symbolic type descriptor specified by the caller
1428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * exactly matches the method handle's own type.
1438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * If there is a type mismatch, {@code invoke} attempts
1448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * to adjust the type of the receiving method handle,
1458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * as if by a call to {@link #asType asType},
1468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * to obtain an exactly invokable method handle {@code M2}.
1478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This allows a more powerful negotiation of method type
1488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * between caller and callee.
1498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (<em>Note:</em> The adjusted method handle {@code M2} is not directly observable,
1518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and implementations are therefore not required to materialize it.)
1528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
1538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Invocation checking</h1>
1548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In typical programs, method handle type matching will usually succeed.
1558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * But if a match fails, the JVM will throw a {@link WrongMethodTypeException},
1568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * either directly (in the case of {@code invokeExact}) or indirectly as if
1578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * by a failed call to {@code asType} (in the case of {@code invoke}).
1588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Thus, a method type mismatch which might show up as a linkage error
1608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * in a statically typed program can show up as
1618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * a dynamic {@code WrongMethodTypeException}
1628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * in a program which uses method handles.
1638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Because method types contain "live" {@code Class} objects,
1658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * method type matching takes into account both types names and class loaders.
1668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Thus, even if a method handle {@code M} is created in one
1678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * class loader {@code L1} and used in another {@code L2},
1688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * method handle calls are type-safe, because the caller's symbolic type
1698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * descriptor, as resolved in {@code L2},
1708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * is matched against the original callee method's symbolic type descriptor,
1718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * as resolved in {@code L1}.
1728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The resolution in {@code L1} happens when {@code M} is created
1738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and its type is assigned, while the resolution in {@code L2} happens
1748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * when the {@code invokevirtual} instruction is linked.
1758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Apart from the checking of type descriptors,
1778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * a method handle's capability to call its underlying method is unrestricted.
1788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * If a method handle is formed on a non-public method by a class
1798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * that has access to that method, the resulting handle can be used
1808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * in any place by any caller who receives a reference to it.
1818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Unlike with the Core Reflection API, where access is checked every time
1838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * a reflective method is invoked,
1848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * method handle access checking is performed
1858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <a href="MethodHandles.Lookup.html#access">when the method handle is created</a>.
1868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In the case of {@code ldc} (see below), access checking is performed as part of linking
1878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the constant pool entry underlying the constant method handle.
1888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
1898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Thus, handles to non-public methods, or to methods in non-public classes,
1908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * should generally be kept secret.
1918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * They should not be passed to untrusted code unless their use from
1928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the untrusted code would be harmless.
1938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
1948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Method handle creation</h1>
1958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Java code can create a method handle that directly accesses
1968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * any method, constructor, or field that is accessible to that code.
1978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * This is done via a reflective, capability-based API called
1986774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * {@link java.lang.invoke.MethodHandles.Lookup MethodHandles.Lookup}
1998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * For example, a static method handle can be obtained
2006774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * from {@link java.lang.invoke.MethodHandles.Lookup#findStatic Lookup.findStatic}.
2018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * There are also conversion methods from Core Reflection API objects,
2026774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * such as {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}.
2038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Like classes and strings, method handles that correspond to accessible
2058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * fields, methods, and constructors can also be represented directly
2068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * in a class file's constant pool as constants to be loaded by {@code ldc} bytecodes.
2078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A new type of constant pool entry, {@code CONSTANT_MethodHandle},
2088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * refers directly to an associated {@code CONSTANT_Methodref},
2098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@code CONSTANT_InterfaceMethodref}, or {@code CONSTANT_Fieldref}
2108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * constant pool entry.
2118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (For full details on method handle constants,
2128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.)
2138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Method handles produced by lookups or constant loads from methods or
2158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * constructors with the variable arity modifier bit ({@code 0x0080})
2168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * have a corresponding variable arity, as if they were defined with
2178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the help of {@link #asVarargsCollector asVarargsCollector}.
2188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A method reference may refer either to a static or non-static method.
2208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In the non-static case, the method handle type includes an explicit
2218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * receiver argument, prepended before any other arguments.
2228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In the method handle's type, the initial receiver argument is typed
2238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * according to the class under which the method was initially requested.
2248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (E.g., if a non-static method handle is obtained via {@code ldc},
2258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the type of the receiver is the class named in the constant pool entry.)
2268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Method handle constants are subject to the same link-time access checks
2288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * their corresponding bytecode instructions, and the {@code ldc} instruction
2298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * will throw corresponding linkage errors if the bytecode behaviors would
2308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * throw such errors.
2318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As a corollary of this, access to protected members is restricted
2338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * to receivers only of the accessing class, or one of its subclasses,
2348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and the accessing class must in turn be a subclass (or package sibling)
2358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * of the protected member's defining class.
2368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * If a method reference refers to a protected non-static method or field
2378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * of a class outside the current package, the receiver argument will
2388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * be narrowed to the type of the accessing class.
2398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * When a method handle to a virtual method is invoked, the method is
2418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * always looked up in the receiver (that is, the first argument).
2428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
2438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A non-virtual method handle to a specific virtual method implementation
2448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * can also be created.  These do not perform virtual lookup based on
2458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * receiver type.  Such a method handle simulates the effect of
2468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * an {@code invokespecial} instruction to the same method.
2478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
2488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Usage examples</h1>
2498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Here are some examples of usage:
2508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <blockquote><pre>{@code
2518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathObject x, y; String s; int i;
2528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodType mt; MethodHandle mh;
2538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandles.Lookup lookup = MethodHandles.lookup();
2548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// mt is (char,char)String
2558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmt = MethodType.methodType(String.class, char.class, char.class);
2568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmh = lookup.findVirtual(String.class, "replace", mt);
2578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamaths = (String) mh.invokeExact("daddy",'d','n');
2588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
2598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(s, "nanny");
2608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// weakly typed invocation (using MHs.invoke)
2618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamaths = (String) mh.invokeWithArguments("sappy", 'p', 'v');
2628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(s, "savvy");
2638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// mt is (Object[])List
2648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmt = MethodType.methodType(java.util.List.class, Object[].class);
2658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
2668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(mh.isVarargsCollector());
2678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathx = mh.invoke("one", "two");
2688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
2698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(x, java.util.Arrays.asList("one","two"));
2708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// mt is (Object,Object,Object)Object
2718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmt = MethodType.genericMethodType(3);
2728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmh = mh.asType(mt);
2738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathx = mh.invokeExact((Object)1, (Object)2, (Object)3);
2748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
2758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(x, java.util.Arrays.asList(1,2,3));
2768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// mt is ()int
2778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmt = MethodType.methodType(int.class);
2788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmh = lookup.findVirtual(java.util.List.class, "size", mt);
2798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathi = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3));
2808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// invokeExact(Ljava/util/List;)I
2818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(i == 3);
2828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmt = MethodType.methodType(void.class, String.class);
2838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
2848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathmh.invokeExact(System.out, "Hello, world.");
2858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
2868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * }</pre></blockquote>
2878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Each of the above calls to {@code invokeExact} or plain {@code invoke}
2888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * generates a single invokevirtual instruction with
2898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the symbolic type descriptor indicated in the following comment.
2908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In these examples, the helper method {@code assertEquals} is assumed to
2918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * be a method which calls {@link java.util.Objects#equals(Object,Object) Objects.equals}
2928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * on its arguments, and asserts that the result is true.
2938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
2948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Exceptions</h1>
2958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The methods {@code invokeExact} and {@code invoke} are declared
2968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * to throw {@link java.lang.Throwable Throwable},
2978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * which is to say that there is no static restriction on what a method handle
2988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * can throw.  Since the JVM does not distinguish between checked
2998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and unchecked exceptions (other than by their class, of course),
3008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * there is no particular effect on bytecode shape from ascribing
3018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * checked exceptions to method handle invocations.  But in Java source
3028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * code, methods which perform method handle calls must either explicitly
3038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * throw {@code Throwable}, or else must catch all
3048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * throwables locally, rethrowing only those which are legal in the context,
3058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * and wrapping ones which are illegal.
3068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
3078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1><a name="sigpoly"></a>Signature polymorphism</h1>
3088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The unusual compilation and linkage behavior of
3098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@code invokeExact} and plain {@code invoke}
3108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * is referenced by the term <em>signature polymorphism</em>.
3118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As defined in the Java Language Specification,
3128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * a signature polymorphic method is one which can operate with
3138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * any of a wide range of call signatures and return types.
3148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In source code, a call to a signature polymorphic method will
3168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * compile, regardless of the requested symbolic type descriptor.
3178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As usual, the Java compiler emits an {@code invokevirtual}
3188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * instruction with the given symbolic type descriptor against the named method.
3198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The unusual part is that the symbolic type descriptor is derived from
3208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * the actual argument and return types, not from the method declaration.
3218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * When the JVM processes bytecode containing signature polymorphic calls,
3238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * it will successfully link any such call, regardless of its symbolic type descriptor.
3248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (In order to retain type safety, the JVM will guard such calls with suitable
3258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * dynamic type checks, as described elsewhere.)
3268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Bytecode generators, including the compiler back end, are required to emit
3288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * untransformed symbolic type descriptors for these methods.
3298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Tools which determine symbolic linkage are required to accept such
3308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * untransformed descriptors, without reporting linkage errors.
3318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
3328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Interoperation between method handles and the Core Reflection API</h1>
3336774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * Using factory methods in the {@link java.lang.invoke.MethodHandles.Lookup Lookup} API,
3348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * any class member represented by a Core Reflection API object
3358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * can be converted to a behaviorally equivalent method handle.
3368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * For example, a reflective {@link java.lang.reflect.Method Method} can
3378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * be converted to a method handle using
3386774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}.
3398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The resulting method handles generally provide more direct and efficient
3408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * access to the underlying class members.
3418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As a special case,
3438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * when the Core Reflection API is used to view the signature polymorphic
3448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * methods {@code invokeExact} or plain {@code invoke} in this class,
3458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * they appear as ordinary non-polymorphic methods.
3468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Their reflective appearance, as viewed by
3478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@link java.lang.Class#getDeclaredMethod Class.getDeclaredMethod},
3488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * is unaffected by their special status in this API.
3498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * For example, {@link java.lang.reflect.Method#getModifiers Method.getModifiers}
3508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * will report exactly those modifier bits required for any similarly
3518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * declared method, including in this case {@code native} and {@code varargs} bits.
3528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As with any reflected method, these methods (when reflected) may be
3548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * invoked via {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}.
3558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * However, such reflective calls do not result in method handle invocations.
3568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Such a call, if passed the required argument
3578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * (a single one, of type {@code Object[]}), will ignore the argument and
3588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * will throw an {@code UnsupportedOperationException}.
3598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Since {@code invokevirtual} instructions can natively
3618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * invoke method handles under any symbolic type descriptor, this reflective view conflicts
3628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * with the normal presentation of these methods via bytecodes.
3638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Thus, these two native methods, when reflectively viewed by
3648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * {@code Class.getDeclaredMethod}, may be regarded as placeholders only.
3658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In order to obtain an invoker method for a particular type descriptor,
3676774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * use {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker},
3686774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * or {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}.
3696774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * The {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual}
3708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * API is also able to return a method handle
3718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * to call {@code invokeExact} or plain {@code invoke},
3728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * for any specified type descriptor .
3738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
3748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1>Interoperation between method handles and Java generics</h1>
3758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * A method handle can be obtained on a method, constructor, or field
3768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * which is declared with Java generic types.
3778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * As with the Core Reflection API, the type of the method handle
3788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * will constructed from the erasure of the source-level type.
3798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * When a method handle is invoked, the types of its arguments
3808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * or the return value cast type may be generic types or type instances.
3818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * If this occurs, the compiler will replace those
3828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * types by their erasures when it constructs the symbolic type descriptor
3838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * for the {@code invokevirtual} instruction.
3848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <p>
3858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Method handles do not represent
3868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * their function-like types in terms of Java parameterized (generic) types,
3878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * because there are three mismatches between function-like types and parameterized
3888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Java types.
3898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <ul>
3908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>Method types range over all possible arities,
3918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * from no arguments to up to the  <a href="MethodHandle.html#maxarity">maximum number</a> of allowed arguments.
3928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Generics are not variadic, and so cannot represent this.</li>
3938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>Method types can specify arguments of primitive types,
3948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * which Java generic types cannot range over.</li>
3958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>Higher order functions over method handles (combinators) are
3968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * often generic across a wide range of function types, including
3978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * those of multiple arities.  It is impossible to represent such
3988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * genericity with a Java type parameter.</li>
3998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * </ul>
4008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
4018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <h1><a name="maxarity"></a>Arity limits</h1>
4028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * The JVM imposes on all methods and constructors of any kind an absolute
4038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * limit of 255 stacked arguments.  This limit can appear more restrictive
4048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * in certain cases:
4058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <ul>
4068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>A {@code long} or {@code double} argument counts (for purposes of arity limits) as two argument slots.
4078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>A non-static method consumes an extra argument for the object on which the method is called.
4088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>A constructor consumes an extra argument for the object which is being constructed.
4098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * <li>Since a method handle&rsquo;s {@code invoke} method (or other signature-polymorphic method) is non-virtual,
4108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *     it consumes an extra argument for the method handle itself, in addition to any non-virtual receiver object.
4118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * </ul>
4128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * These limits imply that certain method handles cannot be created, solely because of the JVM limit on stacked arguments.
4138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * For example, if a static JVM method accepts exactly 255 arguments, a method handle cannot be created for it.
4148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * Attempts to create method handles with impossible method types lead to an {@link IllegalArgumentException}.
4158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * In particular, a method handle&rsquo;s type must not have an arity of the exact maximum 255.
4168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath *
4178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * @see MethodType
4186774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath * @see MethodHandles
4198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath * @author John Rose, JSR 292 EG
4208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath */
4218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathpublic abstract class MethodHandle {
4228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // Android-changed:
4238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
4248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // static { MethodHandleImpl.initStatics(); }
4258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
4268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // LambdaForm and customizationCount are currently unused in our implementation
4278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // and will be substituted with appropriate implementation / delegate classes.
4288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
4298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*private*/ final LambdaForm form;
4308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // form is not private so that invokers can easily fetch it
4318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ byte customizationCount;
4328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // customizationCount should be accessible from invokers
4338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
4348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
4358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
4368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Internal marker interface which distinguishes (to the Java compiler)
4378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * those methods which are <a href="MethodHandle.html#sigpoly">signature polymorphic</a>.
438cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath     *
439cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath     * @hide
4408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
4418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    @java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
4428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
443cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath    public @interface PolymorphicSignature { }
4448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
445ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath    /**
446ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     * The type of this method handle, this corresponds to the exact type of the method
447ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     * being invoked.
448ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     */
4498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    private final MethodType type;
450ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath
451ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath    /**
452ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     * The nominal type of this method handle, will be non-null if a method handle declares
453ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     * a different type from its "real" type, which is either the type of the method being invoked
454ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     * or the type of the emulated stackframe expected by an underyling adapter.
455ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     */
456ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath    private MethodType nominalType;
457ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath
45854168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath    /**
45986dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath     * The spread invoker associated with this type with zero trailing arguments.
46086dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath     * This is used to speed up invokeWithArguments.
46186dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath     */
46286dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath    private MethodHandle cachedSpreadInvoker;
46386dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath
46486dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath    /**
46554168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath     * The INVOKE* constants and SGET/SPUT and IGET/IPUT constants specify the behaviour of this
46654168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath     * method handle with respect to the ArtField* or the ArtMethod* that it operates on. These
46754168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath     * behaviours are equivalent to the dex bytecode behaviour on the respective method_id or
46854168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath     * field_id in the equivalent instruction.
469faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     *
470faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * INVOKE_TRANSFORM is a special type of handle which doesn't encode any dex bytecode behaviour,
471faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * instead it transforms the list of input arguments or performs other higher order operations
472faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * before (optionally) delegating to another method handle.
473704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     *
474704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     * INVOKE_CALLSITE_TRANSFORM is a variation on INVOKE_TRANSFORM where the method type of
475704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     * a MethodHandle dynamically varies based on the callsite. This is used by
476704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     * the VarargsCollector implementation which places any number of trailing arguments
477704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     * into an array before invoking an arity method. The "any number of trailing arguments" means
478704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     * it would otherwise generate WrongMethodTypeExceptions as the callsite method type and
479704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson     * VarargsCollector method type appear incompatible.
48054168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath     */
48154168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath
4825bf2e16e7e789d8668c83851a1486e7bb1a286ffNarayan Kamath    /** @hide */ public static final int INVOKE_VIRTUAL = 0;
4835bf2e16e7e789d8668c83851a1486e7bb1a286ffNarayan Kamath    /** @hide */ public static final int INVOKE_SUPER = 1;
4845bf2e16e7e789d8668c83851a1486e7bb1a286ffNarayan Kamath    /** @hide */ public static final int INVOKE_DIRECT = 2;
4855bf2e16e7e789d8668c83851a1486e7bb1a286ffNarayan Kamath    /** @hide */ public static final int INVOKE_STATIC = 3;
4865bf2e16e7e789d8668c83851a1486e7bb1a286ffNarayan Kamath    /** @hide */ public static final int INVOKE_INTERFACE = 4;
487faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath    /** @hide */ public static final int INVOKE_TRANSFORM = 5;
488704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson    /** @hide */ public static final int INVOKE_CALLSITE_TRANSFORM = 6;
48951e2e77cb38173a1598a7f014d5a0232da4818a8Orion Hodson    /** @hide */ public static final int INVOKE_VAR_HANDLE = 7;
49051e2e77cb38173a1598a7f014d5a0232da4818a8Orion Hodson    /** @hide */ public static final int INVOKE_VAR_HANDLE_EXACT = 8;
49151e2e77cb38173a1598a7f014d5a0232da4818a8Orion Hodson    /** @hide */ public static final int IGET = 9;
49251e2e77cb38173a1598a7f014d5a0232da4818a8Orion Hodson    /** @hide */ public static final int IPUT = 10;
49351e2e77cb38173a1598a7f014d5a0232da4818a8Orion Hodson    /** @hide */ public static final int SGET = 11;
49451e2e77cb38173a1598a7f014d5a0232da4818a8Orion Hodson    /** @hide */ public static final int SPUT = 12;
49554168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath
49654168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath    // The kind of this method handle (used by the runtime). This is one of the INVOKE_*
49754168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath    // constants or SGET/SPUT, IGET/IPUT.
498cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath    /** @hide */ protected final int handleKind;
49954168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath
500ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath    // The ArtMethod* or ArtField* associated with this method handle (used by the runtime).
501cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath    /** @hide */ protected final long artFieldOrMethod;
502ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath
503cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath    /** @hide */
504ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath    protected MethodHandle(long artFieldOrMethod, int handleKind, MethodType type) {
505ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath        this.artFieldOrMethod = artFieldOrMethod;
506ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath        this.handleKind = handleKind;
507ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath        this.type = type;
508ed438115cf887c4541cb292244b8234c0aa19b1cNarayan Kamath    }
5098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
5108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
5118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Reports the type of this method handle.
5128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Every invocation of this method handle via {@code invokeExact} must exactly match this type.
5138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return the method handle type
5148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
5158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodType type() {
516ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        if (nominalType != null) {
517ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath            return nominalType;
518ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        }
519ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath
5208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        return type;
5218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
5228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
5238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
5248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Invokes the method handle, allowing any caller type descriptor, but requiring an exact type match.
5258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The symbolic type descriptor at the call site of {@code invokeExact} must
5268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * exactly match this method handle's {@link #type type}.
5278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * No conversions are allowed on arguments or return values.
5288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When this method is observed via the Core Reflection API,
5308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * it will appear as a single native method, taking an object array and returning an object.
5318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If this native method is invoked directly via
5328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, via JNI,
5336774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath     * or indirectly via {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect},
5348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * it will throw an {@code UnsupportedOperationException}.
5358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param args the signature-polymorphic parameter list, statically represented using varargs
5368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return the signature-polymorphic result, statically represented using {@code Object}
5378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the target's type is not identical with the caller's symbolic type descriptor
5388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws Throwable anything thrown by the underlying method propagates unchanged through the method handle call
5398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
5408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public final native @PolymorphicSignature Object invokeExact(Object... args) throws Throwable;
5418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
5428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
5438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Invokes the method handle, allowing any caller type descriptor,
5448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * and optionally performing conversions on arguments and return values.
5458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the call site's symbolic type descriptor exactly matches this method handle's {@link #type type},
5478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the call proceeds as if by {@link #invokeExact invokeExact}.
5488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Otherwise, the call proceeds as if this method handle were first
5508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * adjusted by calling {@link #asType asType} to adjust this method handle
5518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the required type, and then the call proceeds as if by
5528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@link #invokeExact invokeExact} on the adjusted method handle.
5538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * There is no guarantee that the {@code asType} call is actually made.
5558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the JVM can predict the results of making the call, it may perform
5568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * adaptations directly on the caller's arguments,
5578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * and call the target method handle according to its own exact type.
5588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The resolved type descriptor at the call site of {@code invoke} must
5608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * be a valid argument to the receivers {@code asType} method.
5618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In particular, the caller must specify the same argument arity
5628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as the callee's type,
5638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * if the callee is not a {@linkplain #asVarargsCollector variable arity collector}.
5648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When this method is observed via the Core Reflection API,
5668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * it will appear as a single native method, taking an object array and returning an object.
5678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If this native method is invoked directly via
5688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, via JNI,
5696774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath     * or indirectly via {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect},
5708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * it will throw an {@code UnsupportedOperationException}.
5718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param args the signature-polymorphic parameter list, statically represented using varargs
5728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return the signature-polymorphic result, statically represented using {@code Object}
5738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the target's type cannot be adjusted to the caller's symbolic type descriptor
5748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws ClassCastException if the target's type can be adjusted to the caller, but a reference cast fails
5758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws Throwable anything thrown by the underlying method propagates unchanged through the method handle call
5768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
5778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public final native @PolymorphicSignature Object invoke(Object... args) throws Throwable;
5788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
5798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // Android-changed: Removed implementation details.
5808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
5818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ final native @PolymorphicSignature Object invokeBasic(Object... args)
5828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ static native @PolymorphicSignature Object linkToVirtual(Object... args)
5838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ static native @PolymorphicSignature Object linkToStatic(Object... args)
5848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ static native @PolymorphicSignature Object linkToSpecial(Object... args)
5858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ static native @PolymorphicSignature Object linkToInterface(Object... args)
5868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
5878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
5888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Performs a variable arity invocation, passing the arguments in the given list
5898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the method handle, as if via an inexact {@link #invoke invoke} from a call site
5908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * which mentions only the type {@code Object}, and whose arity is the length
5918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * of the argument list.
5928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
5938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Specifically, execution proceeds as if by the following steps,
5948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * although the methods are not guaranteed to be called if the JVM
5958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * can predict their effects.
5968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <ul>
5978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Determine the length of the argument array as {@code N}.
5988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     For a null reference, {@code N=0}. </li>
5998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Determine the general type {@code TN} of {@code N} arguments as
6008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     as {@code TN=MethodType.genericMethodType(N)}.</li>
6018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Force the original target method handle {@code MH0} to the
6028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     required type, as {@code MH1 = MH0.asType(TN)}. </li>
6038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Spread the array into {@code N} separate arguments {@code A0, ...}. </li>
6048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Invoke the type-adjusted method handle on the unpacked arguments:
6058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     MH1.invokeExact(A0, ...). </li>
6068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Take the return value as an {@code Object} reference. </li>
6078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * </ul>
6088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Because of the action of the {@code asType} step, the following argument
6108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * conversions are applied as necessary:
6118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <ul>
6128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>reference casting
6138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>unboxing
6148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>widening primitive conversions
6158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * </ul>
6168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The result returned by the call is boxed if it is a primitive,
6188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * or forced to null if the return type is void.
6198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * This call is equivalent to the following code:
6218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
6228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * MethodHandle invoker = MethodHandles.spreadInvoker(this.type(), 0);
6238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Object result = invoker.invokeExact(this, arguments);
6248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
6258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Unlike the signature polymorphic methods {@code invokeExact} and {@code invoke},
6278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code invokeWithArguments} can be accessed normally via the Core Reflection API and JNI.
6288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * It can therefore be used as a bridge between native or reflective code and method handles.
6298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *
6308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arguments the arguments to pass to the target
6318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return the result returned by the target
6328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws ClassCastException if an argument cannot be converted by reference casting
6338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the target's type cannot be adjusted to take the given number of {@code Object} arguments
6348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws Throwable anything thrown by the target method invocation
6356774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath     * @see MethodHandles#spreadInvoker
6368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
6378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public Object invokeWithArguments(Object... arguments) throws Throwable {
63886dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath        MethodHandle invoker = null;
63986dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath        synchronized (this) {
64086dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath            if (cachedSpreadInvoker == null) {
64186dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath                cachedSpreadInvoker = MethodHandles.spreadInvoker(this.type(), 0);
64286dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath            }
64386dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath
64486dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath            invoker = cachedSpreadInvoker;
64586dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath        }
64686dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath
64786dc78f165d24514cb094db95e8bad99618626e9Narayan Kamath        return invoker.invoke(this, arguments);
6488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
6498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
6508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
6518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Performs a variable arity invocation, passing the arguments in the given array
6528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the method handle, as if via an inexact {@link #invoke invoke} from a call site
6538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * which mentions only the type {@code Object}, and whose arity is the length
6548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * of the argument array.
6558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * This method is also equivalent to the following code:
6578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
6588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *   invokeWithArguments(arguments.toArray()
6598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
6608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *
6618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arguments the arguments to pass to the target
6628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return the result returned by the target
6638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws NullPointerException if {@code arguments} is a null reference
6648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws ClassCastException if an argument cannot be converted by reference casting
6658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the target's type cannot be adjusted to take the given number of {@code Object} arguments
6668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws Throwable anything thrown by the target method invocation
6678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
6688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public Object invokeWithArguments(java.util.List<?> arguments) throws Throwable {
6698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        return invokeWithArguments(arguments.toArray());
6708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
6718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
6728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
6738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Produces an adapter method handle which adapts the type of the
6748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * current method handle to a new type.
6758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The resulting method handle is guaranteed to report a type
6768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * which is equal to the desired new type.
6778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the original type and new type are equal, returns {@code this}.
6798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The new method handle, when invoked, will perform the following
6818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * steps:
6828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <ul>
6838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Convert the incoming argument list to match the original
6848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     method handle's argument list.
6858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Invoke the original method handle on the converted argument list.
6868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>Convert any result returned by the original method handle
6878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     to the return type of new method handle.
6888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * </ul>
6898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * This method provides the crucial behavioral difference between
6918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@link #invokeExact invokeExact} and plain, inexact {@link #invoke invoke}.
6928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The two methods
6938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * perform the same steps when the caller's type descriptor exactly m atches
6948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the callee's, but when the types differ, plain {@link #invoke invoke}
6958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * also calls {@code asType} (or some internal equivalent) in order
6968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to match up the caller's and callee's types.
6978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
6988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the current method is a variable arity method handle
6998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * argument list conversion may involve the conversion and collection
7008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * of several arguments into an array, as
7018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@linkplain #asVarargsCollector described elsewhere}.
7028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In every other case, all conversions are applied <em>pairwise</em>,
7038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * which means that each argument or return value is converted to
7048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * exactly one argument or return value (or no return value).
7058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The applied conversions are defined by consulting the
7068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the corresponding component types of the old and new
7078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * method handle types.
7088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
7098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Let <em>T0</em> and <em>T1</em> be corresponding new and old parameter types,
7108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * or old and new return types.  Specifically, for some valid index {@code i}, let
7118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <em>T0</em>{@code =newType.parameterType(i)} and <em>T1</em>{@code =this.type().parameterType(i)}.
7128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Or else, going the other way for return values, let
7138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <em>T0</em>{@code =this.type().returnType()} and <em>T1</em>{@code =newType.returnType()}.
7148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the types are the same, the new method handle makes no change
7158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the corresponding argument or return value (if any).
7168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Otherwise, one of the following conversions is applied
7178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * if possible:
7188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <ul>
7198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If <em>T0</em> and <em>T1</em> are references, then a cast to <em>T1</em> is applied.
7208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     (The types do not need to be related in any particular way.
7218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     This is because a dynamic value of null can convert to any reference type.)
7228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If <em>T0</em> and <em>T1</em> are primitives, then a Java method invocation
7238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     conversion (JLS 5.3) is applied, if one exists.
7248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     (Specifically, <em>T0</em> must convert to <em>T1</em> by a widening primitive conversion.)
7258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If <em>T0</em> is a primitive and <em>T1</em> a reference,
7268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     a Java casting conversion (JLS 5.5) is applied if one exists.
7278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     (Specifically, the value is boxed from <em>T0</em> to its wrapper class,
7288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     which is then widened as needed to <em>T1</em>.)
7298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing
7308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     conversion will be applied at runtime, possibly followed
7318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     by a Java method invocation conversion (JLS 5.3)
7328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     on the primitive value.  (These are the primitive widening conversions.)
7338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     <em>T0</em> must be a wrapper class or a supertype of one.
7348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     (In the case where <em>T0</em> is Object, these are the conversions
7358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     allowed by {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}.)
7368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     The unboxing conversion must have a possibility of success, which means that
7378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     if <em>T0</em> is not itself a wrapper class, there must exist at least one
7388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     wrapper class <em>TW</em> which is a subtype of <em>T0</em> and whose unboxed
7398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     primitive value can be widened to <em>T1</em>.
7408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If the return type <em>T1</em> is marked as void, any returned value is discarded
7418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If the return type <em>T0</em> is void and <em>T1</em> a reference, a null value is introduced.
7428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>If the return type <em>T0</em> is void and <em>T1</em> a primitive,
7438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     a zero value is introduced.
7448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * </ul>
7458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (<em>Note:</em> Both <em>T0</em> and <em>T1</em> may be regarded as static types,
7468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * because neither corresponds specifically to the <em>dynamic type</em> of any
7478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * actual argument or return value.)
7488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
7498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The method handle conversion cannot be made if any one of the required
7508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * pairwise conversions cannot be made.
7518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
7528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * At runtime, the conversions applied to reference arguments
7538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * or return values may require additional runtime checks which can fail.
7548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * An unboxing operation may fail because the original reference is null,
7558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * causing a {@link java.lang.NullPointerException NullPointerException}.
7568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * An unboxing operation or a reference cast may also fail on a reference
7578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to an object of the wrong type,
7588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * causing a {@link java.lang.ClassCastException ClassCastException}.
7598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Although an unboxing operation may accept several kinds of wrappers,
7608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * if none are available, a {@code ClassCastException} will be thrown.
7618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *
7628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param newType the expected type of the new method handle
7638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a method handle which delegates to {@code this} after performing
7648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *           any necessary argument conversions, and arranges for any
7658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *           necessary return value conversions
7668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws NullPointerException if {@code newType} is a null reference
7678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the conversion cannot be made
7686774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath     * @see MethodHandles#explicitCastArguments
7698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
7708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodHandle asType(MethodType newType) {
7718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        // Fast path alternative to a heavyweight {@code asType} call.
7728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        // Return 'this' if the conversion will be a no-op.
7738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (newType == type) {
7748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            return this;
7758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        }
7768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
777ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        if (!type.isConvertibleTo(newType)) {
778ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath            throw new WrongMethodTypeException("cannot convert " + this + " to " + newType);
7798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        }
7808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
781ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        MethodHandle mh = duplicate();
782ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        mh.nominalType = newType;
783ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        return mh;
7848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
7858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
7868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
7878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Makes an <em>array-spreading</em> method handle, which accepts a trailing array argument
7888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * and spreads its elements as positional arguments.
7898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The new method handle adapts, as its <i>target</i>,
7908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the current method handle.  The type of the adapter will be
7918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the same as the type of the target, except that the final
7928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code arrayLength} parameters of the target's type are replaced
7938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * by a single array parameter of type {@code arrayType}.
7948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
7958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the array element type differs from any of the corresponding
7968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * argument types on the original target,
7978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the original target is adapted to take the array elements directly,
7988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as if by a call to {@link #asType asType}.
7998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
8008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When called, the adapter replaces a trailing array argument
8018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * by the array's elements, each as its own argument to the target.
8028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (The order of the arguments is preserved.)
8038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * They are converted pairwise by casting and/or unboxing
8048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the types of the trailing parameters of the target.
8058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Finally the target is called.
8068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * What the target eventually returns is returned unchanged by the adapter.
8078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
8088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Before calling the target, the adapter verifies that the array
8098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * contains exactly enough elements to provide a correct argument count
8108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the target method handle.
8118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (The array may also be null when zero elements are required.)
8128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
8138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If, when the adapter is called, the supplied array argument does
8148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * not have the correct number of elements, the adapter will throw
8158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * an {@link IllegalArgumentException} instead of invoking the target.
8168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
8178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Here are some simple examples of array-spreading method handles:
8188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
8198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle equals = publicLookup()
8208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findVirtual(String.class, "equals", methodType(boolean.class, Object.class));
8218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert( (boolean) equals.invokeExact("me", (Object)"me"));
8228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(!(boolean) equals.invokeExact("me", (Object)"thee"));
8238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// spread both arguments from a 2-array:
8248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle eq2 = equals.asSpreader(Object[].class, 2);
8258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" }));
8268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" }));
8278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// try to spread from anything but a 2-array:
8288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathfor (int n = 0; n <= 10; n++) {
8298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  Object[] badArityArgs = (n == 2 ? null : new Object[n]);
8308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  try { assert((boolean) eq2.invokeExact(badArityArgs) && false); }
8318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  catch (IllegalArgumentException ex) { } // OK
8328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath}
8338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// spread both arguments from a String array:
8348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle eq2s = equals.asSpreader(String[].class, 2);
8358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" }));
8368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" }));
8378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// spread second arguments from a 1-array:
8388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle eq1 = equals.asSpreader(Object[].class, 1);
8398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert( (boolean) eq1.invokeExact("me", new Object[]{ "me" }));
8408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" }));
8418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// spread no arguments from a 0-array or null:
8428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle eq0 = equals.asSpreader(Object[].class, 0);
8438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0]));
8448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null));
8458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// asSpreader and asCollector are approximate inverses:
8468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathfor (int n = 0; n <= 2; n++) {
8478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) {
8488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n);
8498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        assert( (boolean) equals2.invokeWithArguments("me", "me"));
8508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        assert(!(boolean) equals2.invokeWithArguments("me", "thee"));
8518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
8528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath}
8538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle caToString = publicLookup()
8548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "toString", methodType(String.class, char[].class));
8558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray()));
8568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle caString3 = caToString.asCollector(char[].class, 3);
8578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C'));
8588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle caToString2 = caString3.asSpreader(char[].class, 2);
8598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray()));
8608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
8618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arrayType usually {@code Object[]}, the type of the array argument from which to extract the spread arguments
8628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arrayLength the number of arguments to spread from an incoming array argument
8638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a new method handle which spreads its final array argument,
8648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         before calling the original method handle
8658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws NullPointerException if {@code arrayType} is a null reference
8668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws IllegalArgumentException if {@code arrayType} is not an array type,
8678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or if target does not have at least
8688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         {@code arrayLength} parameter types,
8698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or if {@code arrayLength} is negative,
8708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or if the resulting method handle's type would have
8718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         <a href="MethodHandle.html#maxarity">too many parameters</a>
8728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the implied {@code asType} call fails
8738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asCollector
8748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
8758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodHandle asSpreader(Class<?> arrayType, int arrayLength) {
8768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        MethodType postSpreadType = asSpreaderChecks(arrayType, arrayLength);
8778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
878378d458718d98d264b58734c87568ee5de9a6781Narayan Kamath        final int targetParamCount = postSpreadType.parameterCount();
879378d458718d98d264b58734c87568ee5de9a6781Narayan Kamath        MethodType dropArrayArgs = postSpreadType.dropParameterTypes(
880378d458718d98d264b58734c87568ee5de9a6781Narayan Kamath                (targetParamCount - arrayLength), targetParamCount);
881378d458718d98d264b58734c87568ee5de9a6781Narayan Kamath        MethodType adapterType = dropArrayArgs.appendParameterTypes(arrayType);
882378d458718d98d264b58734c87568ee5de9a6781Narayan Kamath
883378d458718d98d264b58734c87568ee5de9a6781Narayan Kamath        return new Transformers.Spreader(this, adapterType, arrayLength);
8848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
8858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
8868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
8878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * See if {@code asSpreader} can be validly called with the given arguments.
8888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Return the type of the method handle call after spreading but before conversions.
8898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
8908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    private MethodType asSpreaderChecks(Class<?> arrayType, int arrayLength) {
8918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        spreadArrayChecks(arrayType, arrayLength);
8928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        int nargs = type().parameterCount();
8938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (nargs < arrayLength || arrayLength < 0)
8948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            throw newIllegalArgumentException("bad spread array length");
8958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        Class<?> arrayElement = arrayType.getComponentType();
8968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        MethodType mtype = type();
8978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        boolean match = true, fail = false;
8988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        for (int i = nargs - arrayLength; i < nargs; i++) {
8998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            Class<?> ptype = mtype.parameterType(i);
9008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            if (ptype != arrayElement) {
9018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                match = false;
9028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                if (!MethodType.canConvert(arrayElement, ptype)) {
9038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                    fail = true;
9048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                    break;
9058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                }
9068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            }
9078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        }
9088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (match)  return mtype;
9098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        MethodType needType = mtype.asSpreaderType(arrayType, arrayLength);
9108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (!fail)  return needType;
9118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        // elicit an error:
9128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        this.asType(needType);
9138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        throw newInternalError("should not return", null);
9148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
9158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
9168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    private void spreadArrayChecks(Class<?> arrayType, int arrayLength) {
9178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        Class<?> arrayElement = arrayType.getComponentType();
9188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (arrayElement == null)
9198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            throw newIllegalArgumentException("not an array type", arrayType);
9208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if ((arrayLength & 0x7F) != arrayLength) {
9218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            if ((arrayLength & 0xFF) != arrayLength)
9228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                throw newIllegalArgumentException("array length is not legal", arrayLength);
9238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            assert(arrayLength >= 128);
9248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            if (arrayElement == long.class ||
9258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                arrayElement == double.class)
9268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath                throw newIllegalArgumentException("array length is not legal for long[] or double[]", arrayLength);
9278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        }
9288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
9298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
9308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
9318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing
9328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * positional arguments and collects them into an array argument.
9338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The new method handle adapts, as its <i>target</i>,
9348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the current method handle.  The type of the adapter will be
9358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the same as the type of the target, except that a single trailing
9368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * parameter (usually of type {@code arrayType}) is replaced by
9378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code arrayLength} parameters whose type is element type of {@code arrayType}.
9388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
9398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the array type differs from the final argument type on the original target,
9408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the original target is adapted to take the array type directly,
9418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as if by a call to {@link #asType asType}.
9428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
9438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When called, the adapter replaces its trailing {@code arrayLength}
9448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * arguments by a single new array of type {@code arrayType}, whose elements
9458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * comprise (in order) the replaced arguments.
9468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Finally the target is called.
9478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * What the target eventually returns is returned unchanged by the adapter.
9488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
9498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (The array may also be a shared constant when {@code arrayLength} is zero.)
9508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
9518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (<em>Note:</em> The {@code arrayType} is often identical to the last
9528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * parameter type of the original target.
9538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * It is an explicit argument for symmetry with {@code asSpreader}, and also
9548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to allow the target to use a simple {@code Object} as its last parameter type.)
9558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
9568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In order to create a collecting adapter which is not restricted to a particular
9578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * number of collected arguments, use {@link #asVarargsCollector asVarargsCollector} instead.
9588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
9598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Here are some examples of array-collecting method handles:
9608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
9618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle deepToString = publicLookup()
9628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
9638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[won]",   (String) deepToString.invokeExact(new Object[]{"won"}));
9648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle ts1 = deepToString.asCollector(Object[].class, 1);
9658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(methodType(String.class, Object.class), ts1.type());
9668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath//assertEquals("[won]", (String) ts1.invokeExact(         new Object[]{"won"})); //FAIL
9678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"}));
9688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// arrayType can be a subtype of Object[]
9698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle ts2 = deepToString.asCollector(String[].class, 2);
9708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(methodType(String.class, String.class, String.class), ts2.type());
9718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[two, too]", (String) ts2.invokeExact("two", "too"));
9728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle ts0 = deepToString.asCollector(Object[].class, 0);
9738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[]", (String) ts0.invokeExact());
9748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// collectors can be nested, Lisp-style
9758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2);
9768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D")));
9778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// arrayType can be any primitive array type
9788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle bytesToString = publicLookup()
9798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "toString", methodType(String.class, byte[].class))
9808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .asCollector(byte[].class, 3);
9818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3));
9828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle longsToString = publicLookup()
9838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "toString", methodType(String.class, long[].class))
9848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .asCollector(long[].class, 1);
9858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[123]", (String) longsToString.invokeExact((long)123));
9868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
9878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments
9888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arrayLength the number of arguments to collect into a new array argument
9898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a new method handle which collects some trailing argument
9908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         into an array, before calling the original method handle
9918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws NullPointerException if {@code arrayType} is a null reference
9928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws IllegalArgumentException if {@code arrayType} is not an array type
9938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or {@code arrayType} is not assignable to this method handle's trailing parameter type,
9948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or {@code arrayLength} is not a legal array size,
9958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or the resulting method handle's type would have
9968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         <a href="MethodHandle.html#maxarity">too many parameters</a>
9978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws WrongMethodTypeException if the implied {@code asType} call fails
9988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asSpreader
9998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asVarargsCollector
10008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
10018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
10028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        asCollectorChecks(arrayType, arrayLength);
10038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
10040e8de7372de378e00c429dbf9d55526d433a7a21Narayan Kamath        return new Transformers.Collector(this, arrayType, arrayLength);
10058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
10068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
10078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
10088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * See if {@code asCollector} can be validly called with the given arguments.
10098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Return false if the last parameter is not an exact match to arrayType.
10108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
10118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /*non-public*/ boolean asCollectorChecks(Class<?> arrayType, int arrayLength) {
10128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        spreadArrayChecks(arrayType, arrayLength);
10138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        int nargs = type().parameterCount();
10148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (nargs != 0) {
10158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            Class<?> lastParam = type().parameterType(nargs-1);
10168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            if (lastParam == arrayType)  return true;
10178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            if (lastParam.isAssignableFrom(arrayType))  return false;
10188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        }
10198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        throw newIllegalArgumentException("array type not assignable to trailing argument", this, arrayType);
10208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
10218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
10228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
10238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Makes a <em>variable arity</em> adapter which is able to accept
10248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * any number of trailing positional arguments and collect them
10258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * into an array argument.
10268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The type and behavior of the adapter will be the same as
10288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the type and behavior of the target, except that certain
10298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code invoke} and {@code asType} requests can lead to
10308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * trailing positional arguments being collected into target's
10318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * trailing parameter.
10328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Also, the last parameter type of the adapter will be
10338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code arrayType}, even if the target has a different
10348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * last parameter type.
10358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * This transformation may return {@code this} if the method handle is
10378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * already of variable arity and its trailing parameter type
10388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * is identical to {@code arrayType}.
10398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When called with {@link #invokeExact invokeExact}, the adapter invokes
10418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the target with no argument changes.
10428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (<em>Note:</em> This behavior is different from a
1043f4ee5c6dccff8f7e037f95b26767e552ea93d41bNarayan Kamath     * {@linkplain #asCollector fixed arity collector},
10448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * since it accepts a whole array of indeterminate length,
10458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * rather than a fixed number of arguments.)
10468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When called with plain, inexact {@link #invoke invoke}, if the caller
10488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * type is the same as the adapter, the adapter invokes the target as with
10498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code invokeExact}.
10508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (This is the normal behavior for {@code invoke} when types match.)
10518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Otherwise, if the caller and adapter arity are the same, and the
10538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * trailing parameter type of the caller is a reference type identical to
10548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * or assignable to the trailing parameter type of the adapter,
10558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the arguments and return values are converted pairwise,
10568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as if by {@link #asType asType} on a fixed arity
10578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * method handle.
10588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Otherwise, the arities differ, or the adapter's trailing parameter
10608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * type is not assignable from the corresponding caller type.
10618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In this case, the adapter replaces all trailing arguments from
10628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the original trailing argument position onward, by
10638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * a new array of type {@code arrayType}, whose elements
10648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * comprise (in order) the replaced arguments.
10658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The caller type must provides as least enough arguments,
10678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * and of the correct type, to satisfy the target's requirement for
10688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * positional arguments before the trailing array argument.
10698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Thus, the caller must supply, at a minimum, {@code N-1} arguments,
10708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * where {@code N} is the arity of the target.
10718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Also, there must exist conversions from the incoming arguments
10728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the target's arguments.
10738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * As with other uses of plain {@code invoke}, if these basic
10748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * requirements are not fulfilled, a {@code WrongMethodTypeException}
10758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * may be thrown.
10768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In all cases, what the target eventually returns is returned unchanged by the adapter.
10788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In the final case, it is exactly as if the target method handle were
1080f4ee5c6dccff8f7e037f95b26767e552ea93d41bNarayan Kamath     * temporarily adapted with a {@linkplain #asCollector fixed arity collector}
10818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the arity required by the caller type.
10828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (As with {@code asCollector}, if the array length is zero,
10838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * a shared constant may be used instead of a new array.
10848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the implied call to {@code asCollector} would throw
10858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * an {@code IllegalArgumentException} or {@code WrongMethodTypeException},
10868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the call to the variable arity adapter must throw
10878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@code WrongMethodTypeException}.)
10888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
10898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The behavior of {@link #asType asType} is also specialized for
10908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * variable arity adapters, to maintain the invariant that
10918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * plain, inexact {@code invoke} is always equivalent to an {@code asType}
10928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * call to adjust the target type, followed by {@code invokeExact}.
10938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Therefore, a variable arity adapter responds
10948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to an {@code asType} request by building a fixed arity collector,
10958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * if and only if the adapter and requested type differ either
10968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * in arity or trailing argument type.
10978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The resulting fixed arity collector has its type further adjusted
10988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (if necessary) to the requested type by pairwise conversion,
10998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as if by another application of {@code asType}.
11008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
11018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When a method handle is obtained by executing an {@code ldc} instruction
11028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * of a {@code CONSTANT_MethodHandle} constant, and the target method is marked
11038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as a variable arity method (with the modifier bit {@code 0x0080}),
11048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the method handle will accept multiple arities, as if the method handle
11058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * constant were created by means of a call to {@code asVarargsCollector}.
11068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
11078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In order to create a collecting adapter which collects a predetermined
11088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * number of arguments, and whose type reflects this predetermined number,
1109f4ee5c6dccff8f7e037f95b26767e552ea93d41bNarayan Kamath     * use {@link #asCollector asCollector} instead.
11108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
11118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * No method handle transformations produce new method handles with
11128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * variable arity, unless they are documented as doing so.
11138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Therefore, besides {@code asVarargsCollector},
11148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * all methods in {@code MethodHandle} and {@code MethodHandles}
11158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * will return a method handle with fixed arity,
11168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * except in the cases where they are specified to return their original
11178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * operand (e.g., {@code asType} of the method handle's own type).
11188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
11198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Calling {@code asVarargsCollector} on a method handle which is already
11208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * of variable arity will produce a method handle with the same type and behavior.
11218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * It may (or may not) return the original variable arity method handle.
11228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
11238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Here is an example, of a list-making variable arity method handle:
11248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
11258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle deepToString = publicLookup()
11268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
11278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle ts1 = deepToString.asVarargsCollector(Object[].class);
11288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[won]",   (String) ts1.invokeExact(    new Object[]{"won"}));
11298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[won]",   (String) ts1.invoke(         new Object[]{"won"}));
11308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[won]",   (String) ts1.invoke(                      "won" ));
11318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"}));
11328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath// findStatic of Arrays.asList(...) produces a variable arity method handle:
11338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle asList = publicLookup()
11348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class));
11358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(methodType(List.class, Object[].class), asList.type());
11368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(asList.isVarargsCollector());
11378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[]", asList.invoke().toString());
11388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[1]", asList.invoke(1).toString());
11398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[two, too]", asList.invoke("two", "too").toString());
11408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathString[] argv = { "three", "thee", "tee" };
11418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[three, thee, tee]", asList.invoke(argv).toString());
11428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString());
11438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathList ls = (List) asList.invoke((Object)argv);
11448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(1, ls.size());
11458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
11468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
11478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p style="font-size:smaller;">
11488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <em>Discussion:</em>
11498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * These rules are designed as a dynamically-typed variation
11508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * of the Java rules for variable arity methods.
11518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In both cases, callers to a variable arity method or method handle
11528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * can either pass zero or more positional arguments, or else pass
11538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * pre-collected arrays of any length.  Users should be aware of the
11548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * special role of the final argument, and of the effect of a
11558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * type match on that final argument, which determines whether
11568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * or not a single trailing argument is interpreted as a whole
11578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * array or a single element of an array to be collected.
11588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Note that the dynamic type of the trailing argument has no
11598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * effect on this decision, only a comparison between the symbolic
11608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * type descriptor of the call site and the type descriptor of the method handle.)
11618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *
11628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments
11638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a new method handle which can collect any number of trailing arguments
11648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         into an array, before calling the original method handle
11658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws NullPointerException if {@code arrayType} is a null reference
11668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws IllegalArgumentException if {@code arrayType} is not an array type
11678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         or {@code arrayType} is not assignable to this method handle's trailing parameter type
11688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asCollector
11698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #isVarargsCollector
11708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asFixedArity
11718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
11728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodHandle asVarargsCollector(Class<?> arrayType) {
11738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        arrayType.getClass(); // explicit NPE
11748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        boolean lastMatch = asCollectorChecks(arrayType, 0);
11758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        if (isVarargsCollector() && lastMatch)
11768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath            return this;
11778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
1178704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        return new Transformers.VarargsCollector(this);
11798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
11808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
11818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
11828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Determines if this method handle
11838b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * supports {@linkplain #asVarargsCollector variable arity} calls.
11848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Such method handles arise from the following sources:
11858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <ul>
11868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>a call to {@linkplain #asVarargsCollector asVarargsCollector}
11876774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath     * <li>a call to a {@linkplain java.lang.invoke.MethodHandles.Lookup lookup method}
11888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     which resolves to a variable arity Java method or constructor
11898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <li>an {@code ldc} instruction of a {@code CONSTANT_MethodHandle}
11908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *     which resolves to a variable arity Java method or constructor
11918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * </ul>
11928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return true if this method handle accepts more than one arity of plain, inexact {@code invoke} calls
11938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asVarargsCollector
11948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asFixedArity
11958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
11968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public boolean isVarargsCollector() {
11978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        return false;
11988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
11998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
12008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
12018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Makes a <em>fixed arity</em> method handle which is otherwise
12028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * equivalent to the current method handle.
12038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * If the current method handle is not of
12058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * {@linkplain #asVarargsCollector variable arity},
12068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the current method handle is returned.
12078b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * This is true even if the current method handle
12088b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * could not be a valid input to {@code asVarargsCollector}.
12098b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12108b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Otherwise, the resulting fixed-arity method handle has the same
12118b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * type and behavior of the current method handle,
12128b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * except that {@link #isVarargsCollector isVarargsCollector}
12138b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * will be false.
12148b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The fixed-arity method handle may (or may not) be the
12158b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * a previous argument to {@code asVarargsCollector}.
12168b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12178b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Here is an example, of a list-making variable arity method handle:
12188b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
12198b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle asListVar = publicLookup()
12208b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
12218b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath  .asVarargsCollector(Object[].class);
12228b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathMethodHandle asListFix = asListVar.asFixedArity();
12238b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[1]", asListVar.invoke(1).toString());
12248b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathException caught = null;
12258b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathtry { asListFix.invoke((Object)1); }
12268b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathcatch (Exception ex) { caught = ex; }
12278b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(caught instanceof ClassCastException);
12288b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[two, too]", asListVar.invoke("two", "too").toString());
12298b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathtry { asListFix.invoke("two", "too"); }
12308b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathcatch (Exception ex) { caught = ex; }
12318b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamathassert(caught instanceof WrongMethodTypeException);
12328b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathObject[] argv = { "three", "thee", "tee" };
12338b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[three, thee, tee]", asListVar.invoke(argv).toString());
12348b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[three, thee, tee]", asListFix.invoke(argv).toString());
12358b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals(1, ((List) asListVar.invoke((Object)argv)).size());
12368b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan KamathassertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString());
12378b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
12388b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *
12398b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a new method handle which accepts only a fixed number of arguments
12408b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #asVarargsCollector
12418b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @see #isVarargsCollector
12428b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
12438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodHandle asFixedArity() {
1244704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        // Android-changed: implementation specific.
1245704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        MethodHandle mh = this;
1246704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        if (mh.isVarargsCollector()) {
1247704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson            mh = ((Transformers.VarargsCollector) mh).asFixedArity();
1248704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        }
1249704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        assert(!mh.isVarargsCollector());
1250704b13a41cc7efd49acf66064109756a248fe0dcOrion Hodson        return mh;
12518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
12528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
12538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
12548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Binds a value {@code x} to the first argument of a method handle, without invoking it.
12558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The new method handle adapts, as its <i>target</i>,
12568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the current method handle by binding it to the given argument.
12578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The type of the bound handle will be
12588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * the same as the type of the target, except that a single leading
12598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * reference parameter will be omitted.
12608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * When called, the bound handle inserts the given value {@code x}
12628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * as a new leading argument to the target.  The other arguments are
12638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * also passed unchanged.
12648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * What the target eventually returns is returned unchanged by the bound handle.
12658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * The reference {@code x} must be convertible to the first parameter
12678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * type of the target.
12688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (<em>Note:</em>  Because method handles are immutable, the target method handle
12708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * retains its original type and behavior.)
12718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @param x  the value to bind to the first argument of the target
12728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a new method handle which prepends the given value to the incoming
12738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         argument list, before calling the original method handle
12748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws IllegalArgumentException if the target does not have a
12758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         leading parameter type that is a reference type
12768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @throws ClassCastException if {@code x} cannot be converted
12778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *         to the leading parameter type of the target
12786774aff0e542cbe31c02570916ff98c6337071dfNarayan Kamath     * @see MethodHandles#insertArguments
12798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
12808b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public MethodHandle bindTo(Object x) {
12818b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        x = type.leadingReferenceParameter().cast(x);  // throw CCE if needed
12828b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
12830ab46853af9f5c16c877e1677d8d27b8fdecc2cdNarayan Kamath        return new Transformers.BindTo(this, x);
12848b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
12858b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
12868b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    /**
12878b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Returns a string representation of the method handle,
12888b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * starting with the string {@code "MethodHandle"} and
12898b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * ending with the string representation of the method handle's type.
12908b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * In other words, this method returns a string equal to the value of:
12918b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <blockquote><pre>{@code
12928b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * "MethodHandle" + type().toString()
12938b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * }</pre></blockquote>
12948b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * <p>
12958b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * (<em>Note:</em>  Future releases of this API may add further information
12968b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * to the string representation.
12978b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * Therefore, the present syntax should not be parsed by applications.)
12988b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     *
12998b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     * @return a string representation of the method handle
13008b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath     */
13018b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    @Override
13028b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    public String toString() {
13038b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        // Android-changed: Removed debugging support.
13048b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath        return "MethodHandle"+type;
13058b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    }
13068b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath
130754168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath    /** @hide */
130854168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath    public int getHandleKind() {
130954168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath        return handleKind;
131054168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath    }
131154168f0e8137b9d2addc6e7bf723aaf1ee3fd744Narayan Kamath
1312cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath    /** @hide */
1313faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath    protected void transform(EmulatedStackFrame arguments) throws Throwable {
1314faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath        throw new AssertionError("MethodHandle.transform should never be called.");
1315faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath    }
1316faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath
1317faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath    /**
1318ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     * Creates a copy of this method handle, copying all relevant data.
1319cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath     *
1320cce1c9e194b4082f00fd6aac2ef9beec75ff5500Narayan Kamath     * @hide
1321ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath     */
1322ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath    protected MethodHandle duplicate() {
1323ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        try {
1324ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath            return (MethodHandle) this.clone();
1325ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        } catch (CloneNotSupportedException cnse) {
1326ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath            throw new AssertionError("Subclass of Transformer is not cloneable");
1327ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath        }
1328ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath    }
1329ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath
1330ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath
1331ff28f8512f99a9507f12b9eb600a374414735394Narayan Kamath    /**
1332faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * This is the entry point for all transform calls, and dispatches to the protected
1333faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * transform method. This layer of indirection exists purely for convenience, because
1334faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * we can invoke-direct on a fixed ArtMethod for all transform variants.
1335faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     *
1336faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * NOTE: If this extra layer of indirection proves to be a problem, we can get rid
1337faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     * of this layer of indirection at the cost of some additional ugliness.
1338faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath     */
1339faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath    private void transformInternal(EmulatedStackFrame arguments) throws Throwable {
1340faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath        transform(arguments);
1341faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath    }
1342faf8883397aed1411590edd1bf5b6681430a10f5Narayan Kamath
13438b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // Android-changed: Removed implementation details :
13448b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13458b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // String standardString();
13468b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // String debugString();
13478b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13488b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //// Implementation methods.
13498b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //// Sub-classes can override these default implementations.
13508b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //// All these methods assume arguments are already validated.
13518b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13528b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // Other transforms to do:  convert, explicitCast, permute, drop, filter, fold, GWT, catch
13538b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13548b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // BoundMethodHandle bindArgumentL(int pos, Object value);
13558b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ MethodHandle setVarargs(MemberName member);
13568b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ MethodHandle viewAsType(MethodType newType, boolean strict);
13578b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ boolean viewAsTypeChecks(MethodType newType, boolean strict);
13588b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13598b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // Decoding
13608b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13618b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ LambdaForm internalForm();
13628b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ MemberName internalMemberName();
13638b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ Class<?> internalCallerClass();
13648b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ MethodHandleImpl.Intrinsic intrinsicName();
13658b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ MethodHandle withInternalMemberName(MemberName member, boolean isInvokeSpecial);
13668b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ boolean isInvokeSpecial();
13678b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ Object internalValues();
13688b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ Object internalProperties();
13698b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13708b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //// Method handle implementation methods.
13718b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //// Sub-classes can override these default implementations.
13728b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //// All these methods assume arguments are already validated.
13738b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    //
13748b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ abstract MethodHandle copyWith(MethodType mt, LambdaForm lf);
13758b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // abstract BoundMethodHandle rebind();
13768b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ void updateForm(LambdaForm newForm);
13778b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // /*non-public*/ void customize();
13788b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath    // private static final long FORM_OFFSET;
13798b532c93d91102f2f3edf9a6b69e2ce9d2a3a8e6Narayan Kamath}
1380