MethodHandle.java 74.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24 25
 */

26
package java.lang.invoke;
27 28


29 30 31 32
import java.util.*;
import sun.invoke.util.*;
import sun.misc.Unsafe;

33
import static java.lang.invoke.MethodHandleStatics.*;
34 35
import java.util.logging.Level;
import java.util.logging.Logger;
36

37
/**
38
 * A method handle is a typed, directly executable reference to an underlying method,
39
 * constructor, field, or similar low-level operation, with optional
40
 * transformations of arguments or return values.
41 42 43
 * These transformations are quite general, and include such patterns as
 * {@linkplain #asType conversion},
 * {@linkplain #bindTo insertion},
44 45
 * {@linkplain java.lang.invoke.MethodHandles#dropArguments deletion},
 * and {@linkplain java.lang.invoke.MethodHandles#filterArguments substitution}.
46
 *
47
 * <h1>Method handle contents</h1>
48 49 50 51
 * Method handles are dynamically and strongly typed according to their parameter and return types.
 * They are not distinguished by the name or the defining class of their underlying methods.
 * A method handle must be invoked using a symbolic type descriptor which matches
 * the method handle's own {@linkplain #type type descriptor}.
52
 * <p>
53
 * Every method handle reports its type descriptor via the {@link #type type} accessor.
54
 * This type descriptor is a {@link java.lang.invoke.MethodType MethodType} object,
55
 * whose structure is a series of classes, one of which is
56
 * the return type of the method (or {@code void.class} if none).
57
 * <p>
58 59 60 61
 * A method handle's type controls the types of invocations it accepts,
 * and the kinds of transformations that apply to it.
 * <p>
 * A method handle contains a pair of special invoker methods
62
 * called {@link #invokeExact invokeExact} and {@link #invoke invoke}.
63 64 65 66
 * Both invoker methods provide direct access to the method handle's
 * underlying method, constructor, field, or other operation,
 * as modified by transformations of arguments and return values.
 * Both invokers accept calls which exactly match the method handle's own type.
67
 * The plain, inexact invoker also accepts a range of other call types.
68
 * <p>
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * Method handles are immutable and have no visible state.
 * Of course, they can be bound to underlying methods or data which exhibit state.
 * With respect to the Java Memory Model, any method handle will behave
 * as if all of its (internal) fields are final variables.  This means that any method
 * handle made visible to the application will always be fully formed.
 * This is true even if the method handle is published through a shared
 * variable in a data race.
 * <p>
 * Method handles cannot be subclassed by the user.
 * Implementations may (or may not) create internal subclasses of {@code MethodHandle}
 * which may be visible via the {@link java.lang.Object#getClass Object.getClass}
 * operation.  The programmer should not draw conclusions about a method handle
 * from its specific class, as the method handle class hierarchy (if any)
 * may change from time to time or across implementations from different vendors.
 *
84
 * <h1>Method handle compilation</h1>
85
 * A Java method call expression naming {@code invokeExact} or {@code invoke}
86 87 88 89
 * can invoke a method handle from Java source code.
 * From the viewpoint of source code, these methods can take any arguments
 * and their result can be cast to any return type.
 * Formally this is accomplished by giving the invoker methods
90
 * {@code Object} return types and variable arity {@code Object} arguments,
91
 * but they have an additional quality called <em>signature polymorphism</em>
92 93 94
 * which connects this freedom of invocation directly to the JVM execution stack.
 * <p>
 * As is usual with virtual methods, source-level calls to {@code invokeExact}
95
 * and {@code invoke} compile to an {@code invokevirtual} instruction.
96 97 98 99
 * More unusually, the compiler must record the actual argument types,
 * and may not perform method invocation conversions on the arguments.
 * Instead, it must push them on the stack according to their own unconverted types.
 * The method handle object itself is pushed on the stack before the arguments.
100
 * The compiler then calls the method handle with a symbolic type descriptor which
101 102
 * describes the argument and return types.
 * <p>
103
 * To issue a complete symbolic type descriptor, the compiler must also determine
104 105 106 107 108 109
 * the return type.  This is based on a cast on the method invocation expression,
 * if there is one, or else {@code Object} if the invocation is an expression
 * or else {@code void} if the invocation is a statement.
 * The cast may be to a primitive type (but not {@code void}).
 * <p>
 * As a corner case, an uncasted {@code null} argument is given
110
 * a symbolic type descriptor of {@code java.lang.Void}.
111 112 113
 * The ambiguity with the type {@code Void} is harmless, since there are no references of type
 * {@code Void} except the null reference.
 *
114
 * <h1>Method handle invocation</h1>
115 116 117
 * The first time a {@code invokevirtual} instruction is executed
 * it is linked, by symbolically resolving the names in the instruction
 * and verifying that the method call is statically legal.
118
 * This is true of calls to {@code invokeExact} and {@code invoke}.
119
 * In this case, the symbolic type descriptor emitted by the compiler is checked for
120 121 122
 * correct syntax and names it contains are resolved.
 * Thus, an {@code invokevirtual} instruction which invokes
 * a method handle will always link, as long
123
 * as the symbolic type descriptor is syntactically well-formed
124 125 126 127
 * and the types exist.
 * <p>
 * When the {@code invokevirtual} is executed after linking,
 * the receiving method handle's type is first checked by the JVM
128
 * to ensure that it matches the symbolic type descriptor.
129 130 131 132 133 134 135
 * If the type match fails, it means that the method which the
 * caller is invoking is not present on the individual
 * method handle being invoked.
 * <p>
 * In the case of {@code invokeExact}, the type descriptor of the invocation
 * (after resolving symbolic type names) must exactly match the method type
 * of the receiving method handle.
136
 * In the case of plain, inexact {@code invoke}, the resolved type descriptor
137
 * must be a valid argument to the receiver's {@link #asType asType} method.
138
 * Thus, plain {@code invoke} is more permissive than {@code invokeExact}.
139 140 141 142
 * <p>
 * After type matching, a call to {@code invokeExact} directly
 * and immediately invoke the method handle's underlying method
 * (or other behavior, as the case may be).
143
 * <p>
144
 * A call to plain {@code invoke} works the same as a call to
145
 * {@code invokeExact}, if the symbolic type descriptor specified by the caller
146
 * exactly matches the method handle's own type.
147
 * If there is a type mismatch, {@code invoke} attempts
148 149 150
 * to adjust the type of the receiving method handle,
 * as if by a call to {@link #asType asType},
 * to obtain an exactly invokable method handle {@code M2}.
151 152 153
 * This allows a more powerful negotiation of method type
 * between caller and callee.
 * <p>
154
 * (<em>Note:</em> The adjusted method handle {@code M2} is not directly observable,
155 156
 * and implementations are therefore not required to materialize it.)
 *
157
 * <h1>Invocation checking</h1>
158 159 160
 * In typical programs, method handle type matching will usually succeed.
 * But if a match fails, the JVM will throw a {@link WrongMethodTypeException},
 * either directly (in the case of {@code invokeExact}) or indirectly as if
161
 * by a failed call to {@code asType} (in the case of {@code invoke}).
162
 * <p>
163 164 165 166
 * Thus, a method type mismatch which might show up as a linkage error
 * in a statically typed program can show up as
 * a dynamic {@code WrongMethodTypeException}
 * in a program which uses method handles.
167
 * <p>
168 169 170 171
 * Because method types contain "live" {@code Class} objects,
 * method type matching takes into account both types names and class loaders.
 * Thus, even if a method handle {@code M} is created in one
 * class loader {@code L1} and used in another {@code L2},
172
 * method handle calls are type-safe, because the caller's symbolic type
173
 * descriptor, as resolved in {@code L2},
174
 * is matched against the original callee method's symbolic type descriptor,
175 176 177 178 179 180 181 182 183 184
 * as resolved in {@code L1}.
 * The resolution in {@code L1} happens when {@code M} is created
 * and its type is assigned, while the resolution in {@code L2} happens
 * when the {@code invokevirtual} instruction is linked.
 * <p>
 * Apart from the checking of type descriptors,
 * a method handle's capability to call its underlying method is unrestricted.
 * If a method handle is formed on a non-public method by a class
 * that has access to that method, the resulting handle can be used
 * in any place by any caller who receives a reference to it.
185
 * <p>
186 187 188 189 190 191 192 193 194 195 196 197
 * Unlike with the Core Reflection API, where access is checked every time
 * a reflective method is invoked,
 * method handle access checking is performed
 * <a href="MethodHandles.Lookup.html#access">when the method handle is created</a>.
 * In the case of {@code ldc} (see below), access checking is performed as part of linking
 * the constant pool entry underlying the constant method handle.
 * <p>
 * Thus, handles to non-public methods, or to methods in non-public classes,
 * should generally be kept secret.
 * They should not be passed to untrusted code unless their use from
 * the untrusted code would be harmless.
 *
198
 * <h1>Method handle creation</h1>
199 200 201
 * Java code can create a method handle that directly accesses
 * any method, constructor, or field that is accessible to that code.
 * This is done via a reflective, capability-based API called
202
 * {@link java.lang.invoke.MethodHandles.Lookup MethodHandles.Lookup}
203
 * For example, a static method handle can be obtained
204
 * from {@link java.lang.invoke.MethodHandles.Lookup#findStatic Lookup.findStatic}.
205
 * There are also conversion methods from Core Reflection API objects,
206
 * such as {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}.
207 208 209 210 211 212
 * <p>
 * Like classes and strings, method handles that correspond to accessible
 * fields, methods, and constructors can also be represented directly
 * in a class file's constant pool as constants to be loaded by {@code ldc} bytecodes.
 * A new type of constant pool entry, {@code CONSTANT_MethodHandle},
 * refers directly to an associated {@code CONSTANT_Methodref},
213 214
 * {@code CONSTANT_InterfaceMethodref}, or {@code CONSTANT_Fieldref}
 * constant pool entry.
215 216
 * (For full details on method handle constants,
 * see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.)
217
 * <p>
218 219 220 221 222
 * Method handles produced by lookups or constant loads from methods or
 * constructors with the variable arity modifier bit ({@code 0x0080})
 * have a corresponding variable arity, as if they were defined with
 * the help of {@link #asVarargsCollector asVarargsCollector}.
 * <p>
223 224 225 226 227
 * A method reference may refer either to a static or non-static method.
 * In the non-static case, the method handle type includes an explicit
 * receiver argument, prepended before any other arguments.
 * In the method handle's type, the initial receiver argument is typed
 * according to the class under which the method was initially requested.
228
 * (E.g., if a non-static method handle is obtained via {@code ldc},
229 230
 * the type of the receiver is the class named in the constant pool entry.)
 * <p>
231 232 233 234 235 236 237 238 239 240 241 242 243
 * Method handle constants are subject to the same link-time access checks
 * their corresponding bytecode instructions, and the {@code ldc} instruction
 * will throw corresponding linkage errors if the bytecode behaviors would
 * throw such errors.
 * <p>
 * As a corollary of this, access to protected members is restricted
 * to receivers only of the accessing class, or one of its subclasses,
 * and the accessing class must in turn be a subclass (or package sibling)
 * of the protected member's defining class.
 * If a method reference refers to a protected non-static method or field
 * of a class outside the current package, the receiver argument will
 * be narrowed to the type of the accessing class.
 * <p>
244 245 246
 * When a method handle to a virtual method is invoked, the method is
 * always looked up in the receiver (that is, the first argument).
 * <p>
247
 * A non-virtual method handle to a specific virtual method implementation
248 249
 * can also be created.  These do not perform virtual lookup based on
 * receiver type.  Such a method handle simulates the effect of
250
 * an {@code invokespecial} instruction to the same method.
251
 *
252
 * <h1>Usage examples</h1>
253
 * Here are some examples of usage:
A
alanb 已提交
254
 * <p><blockquote><pre>{@code
255 256 257
Object x, y; String s; int i;
MethodType mt; MethodHandle mh;
MethodHandles.Lookup lookup = MethodHandles.lookup();
258
// mt is (char,char)String
259 260
mt = MethodType.methodType(String.class, char.class, char.class);
mh = lookup.findVirtual(String.class, "replace", mt);
261
s = (String) mh.invokeExact("daddy",'d','n');
262
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
263
assertEquals(s, "nanny");
264
// weakly typed invocation (using MHs.invoke)
265
s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
266
assertEquals(s, "savvy");
267
// mt is (Object[])List
268 269
mt = MethodType.methodType(java.util.List.class, Object[].class);
mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
270
assert(mh.isVarargsCollector());
271 272
x = mh.invoke("one", "two");
// invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
273
assertEquals(x, java.util.Arrays.asList("one","two"));
274
// mt is (Object,Object,Object)Object
275
mt = MethodType.genericMethodType(3);
276
mh = mh.asType(mt);
277
x = mh.invokeExact((Object)1, (Object)2, (Object)3);
278
// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
279 280
assertEquals(x, java.util.Arrays.asList(1,2,3));
// mt is ()int
281 282
mt = MethodType.methodType(int.class);
mh = lookup.findVirtual(java.util.List.class, "size", mt);
283
i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3));
284
// invokeExact(Ljava/util/List;)I
285
assert(i == 3);
286 287 288
mt = MethodType.methodType(void.class, String.class);
mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
mh.invokeExact(System.out, "Hello, world.");
289
// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
A
alanb 已提交
290
 * }</pre></blockquote>
291
 * Each of the above calls to {@code invokeExact} or plain {@code invoke}
292
 * generates a single invokevirtual instruction with
293 294
 * the symbolic type descriptor indicated in the following comment.
 * In these examples, the helper method {@code assertEquals} is assumed to
295
 * be a method which calls {@link java.util.Objects#equals(Object,Object) Objects.equals }
296
 * on its arguments, and asserts that the result is true.
297
 *
298
 * <h1>Exceptions</h1>
299
 * The methods {@code invokeExact} and {@code invoke} are declared
300 301 302 303 304 305 306
 * to throw {@link java.lang.Throwable Throwable},
 * which is to say that there is no static restriction on what a method handle
 * can throw.  Since the JVM does not distinguish between checked
 * and unchecked exceptions (other than by their class, of course),
 * there is no particular effect on bytecode shape from ascribing
 * checked exceptions to method handle invocations.  But in Java source
 * code, methods which perform method handle calls must either explicitly
307
 * throw {@code Throwable}, or else must catch all
308 309 310
 * throwables locally, rethrowing only those which are legal in the context,
 * and wrapping ones which are illegal.
 *
311
 * <h1><a name="sigpoly"></a>Signature polymorphism</h1>
312
 * The unusual compilation and linkage behavior of
313
 * {@code invokeExact} and plain {@code invoke}
314
 * is referenced by the term <em>signature polymorphism</em>.
315 316
 * As defined in the Java Language Specification,
 * a signature polymorphic method is one which can operate with
317 318 319
 * any of a wide range of call signatures and return types.
 * <p>
 * In source code, a call to a signature polymorphic method will
320
 * compile, regardless of the requested symbolic type descriptor.
321
 * As usual, the Java compiler emits an {@code invokevirtual}
322 323
 * instruction with the given symbolic type descriptor against the named method.
 * The unusual part is that the symbolic type descriptor is derived from
324 325 326
 * the actual argument and return types, not from the method declaration.
 * <p>
 * When the JVM processes bytecode containing signature polymorphic calls,
327
 * it will successfully link any such call, regardless of its symbolic type descriptor.
328 329 330 331
 * (In order to retain type safety, the JVM will guard such calls with suitable
 * dynamic type checks, as described elsewhere.)
 * <p>
 * Bytecode generators, including the compiler back end, are required to emit
332
 * untransformed symbolic type descriptors for these methods.
333 334 335
 * Tools which determine symbolic linkage are required to accept such
 * untransformed descriptors, without reporting linkage errors.
 *
336
 * <h1>Interoperation between method handles and the Core Reflection API</h1>
337
 * Using factory methods in the {@link java.lang.invoke.MethodHandles.Lookup Lookup} API,
338 339 340 341
 * any class member represented by a Core Reflection API object
 * can be converted to a behaviorally equivalent method handle.
 * For example, a reflective {@link java.lang.reflect.Method Method} can
 * be converted to a method handle using
342
 * {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}.
343 344 345 346 347
 * The resulting method handles generally provide more direct and efficient
 * access to the underlying class members.
 * <p>
 * As a special case,
 * when the Core Reflection API is used to view the signature polymorphic
348 349 350 351 352 353 354 355 356 357
 * methods {@code invokeExact} or plain {@code invoke} in this class,
 * they appear as ordinary non-polymorphic methods.
 * Their reflective appearance, as viewed by
 * {@link java.lang.Class#getDeclaredMethod Class.getDeclaredMethod},
 * is unaffected by their special status in this API.
 * For example, {@link java.lang.reflect.Method#getModifiers Method.getModifiers}
 * will report exactly those modifier bits required for any similarly
 * declared method, including in this case {@code native} and {@code varargs} bits.
 * <p>
 * As with any reflected method, these methods (when reflected) may be
358
 * invoked via {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}.
359 360 361 362 363
 * However, such reflective calls do not result in method handle invocations.
 * Such a call, if passed the required argument
 * (a single one, of type {@code Object[]}), will ignore the argument and
 * will throw an {@code UnsupportedOperationException}.
 * <p>
364
 * Since {@code invokevirtual} instructions can natively
365
 * invoke method handles under any symbolic type descriptor, this reflective view conflicts
366 367 368
 * with the normal presentation of these methods via bytecodes.
 * Thus, these two native methods, when reflectively viewed by
 * {@code Class.getDeclaredMethod}, may be regarded as placeholders only.
369 370
 * <p>
 * In order to obtain an invoker method for a particular type descriptor,
371
 * use {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker},
372
 * or {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}.
373
 * The {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual}
374
 * API is also able to return a method handle
375
 * to call {@code invokeExact} or plain {@code invoke},
376 377
 * for any specified type descriptor .
 *
378
 * <h1>Interoperation between method handles and Java generics</h1>
379 380 381 382 383 384 385
 * A method handle can be obtained on a method, constructor, or field
 * which is declared with Java generic types.
 * As with the Core Reflection API, the type of the method handle
 * will constructed from the erasure of the source-level type.
 * When a method handle is invoked, the types of its arguments
 * or the return value cast type may be generic types or type instances.
 * If this occurs, the compiler will replace those
386
 * types by their erasures when it constructs the symbolic type descriptor
387
 * for the {@code invokevirtual} instruction.
388
 * <p>
389 390 391
 * Method handles do not represent
 * their function-like types in terms of Java parameterized (generic) types,
 * because there are three mismatches between function-like types and parameterized
392
 * Java types.
393
 * <ul>
394
 * <li>Method types range over all possible arities,
395
 * from no arguments to up to 255 of arguments (a limit imposed by the JVM).
396 397 398 399 400 401 402
 * Generics are not variadic, and so cannot represent this.</li>
 * <li>Method types can specify arguments of primitive types,
 * which Java generic types cannot range over.</li>
 * <li>Higher order functions over method handles (combinators) are
 * often generic across a wide range of function types, including
 * those of multiple arities.  It is impossible to represent such
 * genericity with a Java type parameter.</li>
403
 * </ul>
404 405 406 407 408
 *
 * @see MethodType
 * @see MethodHandles
 * @author John Rose, JSR 292 EG
 */
409
public abstract class MethodHandle {
410
    static { MethodHandleImpl.initStatics(); }
411

412 413
    /**
     * Internal marker interface which distinguishes (to the Java compiler)
414
     * those methods which are <a href="MethodHandle.html#sigpoly">signature polymorphic</a>.
415
     */
416
    @java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
417
    @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
418
    @interface PolymorphicSignature { }
419

420 421 422
    private final MethodType type;
    /*private*/ final LambdaForm form;
    // form is not private so that invokers can easily fetch it
423 424
    /*private*/ MethodHandle asTypeCache;
    // asTypeCache is not private so that invokers can easily fetch it
425 426

    /**
427
     * Reports the type of this method handle.
428
     * Every invocation of this method handle via {@code invokeExact} must exactly match this type.
429 430
     * @return the method handle type
     */
431
    public MethodType type() {
432 433 434 435
        return type;
    }

    /**
436 437
     * Package-private constructor for the method handle implementation hierarchy.
     * Method handle inheritance will be contained completely within
438
     * the {@code java.lang.invoke} package.
439
     */
440
    // @param type type (permanently assigned) of the new method handle
441 442 443
    /*non-public*/ MethodHandle(MethodType type, LambdaForm form) {
        type.getClass();  // explicit NPE
        form.getClass();  // explicit NPE
444
        this.type = type;
445 446 447
        this.form = form;

        form.prepare();  // TO DO:  Try to delay this step until just before invocation.
448
    }
449 450

    /**
451
     * Invokes the method handle, allowing any caller type descriptor, but requiring an exact type match.
452
     * The symbolic type descriptor at the call site of {@code invokeExact} must
453
     * exactly match this method handle's {@link #type type}.
454
     * No conversions are allowed on arguments or return values.
455 456 457 458
     * <p>
     * When this method is observed via the Core Reflection API,
     * it will appear as a single native method, taking an object array and returning an object.
     * If this native method is invoked directly via
459
     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, via JNI,
460
     * or indirectly via {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect},
461
     * it will throw an {@code UnsupportedOperationException}.
462 463
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
464
     * @throws WrongMethodTypeException if the target's type is not identical with the caller's symbolic type descriptor
465
     * @throws Throwable anything thrown by the underlying method propagates unchanged through the method handle call
466
     */
467
    public final native @PolymorphicSignature Object invokeExact(Object... args) throws Throwable;
468

469
    /**
470
     * Invokes the method handle, allowing any caller type descriptor,
471
     * and optionally performing conversions on arguments and return values.
472
     * <p>
473
     * If the call site's symbolic type descriptor exactly matches this method handle's {@link #type type},
474
     * the call proceeds as if by {@link #invokeExact invokeExact}.
475 476
     * <p>
     * Otherwise, the call proceeds as if this method handle were first
477
     * adjusted by calling {@link #asType asType} to adjust this method handle
478
     * to the required type, and then the call proceeds as if by
479 480 481 482 483 484 485
     * {@link #invokeExact invokeExact} on the adjusted method handle.
     * <p>
     * There is no guarantee that the {@code asType} call is actually made.
     * If the JVM can predict the results of making the call, it may perform
     * adaptations directly on the caller's arguments,
     * and call the target method handle according to its own exact type.
     * <p>
486
     * The resolved type descriptor at the call site of {@code invoke} must
487
     * be a valid argument to the receivers {@code asType} method.
488
     * In particular, the caller must specify the same argument arity
489 490
     * as the callee's type,
     * if the callee is not a {@linkplain #asVarargsCollector variable arity collector}.
491 492 493 494
     * <p>
     * When this method is observed via the Core Reflection API,
     * it will appear as a single native method, taking an object array and returning an object.
     * If this native method is invoked directly via
495
     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, via JNI,
496
     * or indirectly via {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect},
497
     * it will throw an {@code UnsupportedOperationException}.
498 499
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
500
     * @throws WrongMethodTypeException if the target's type cannot be adjusted to the caller's symbolic type descriptor
501
     * @throws ClassCastException if the target's type can be adjusted to the caller, but a reference cast fails
502
     * @throws Throwable anything thrown by the underlying method propagates unchanged through the method handle call
503
     */
504 505
    public final native @PolymorphicSignature Object invoke(Object... args) throws Throwable;

506 507 508 509 510 511 512 513 514 515 516 517 518 519
    /**
     * Private method for trusted invocation of a method handle respecting simplified signatures.
     * Type mismatches will not throw {@code WrongMethodTypeException}, but could crash the JVM.
     * <p>
     * The caller signature is restricted to the following basic types:
     * Object, int, long, float, double, and void return.
     * <p>
     * The caller is responsible for maintaining type correctness by ensuring
     * that the each outgoing argument value is a member of the range of the corresponding
     * callee argument type.
     * (The caller should therefore issue appropriate casts and integer narrowing
     * operations on outgoing argument values.)
     * The caller can assume that the incoming result value is part of the range
     * of the callee's return type.
520 521
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
522 523 524
     */
    /*non-public*/ final native @PolymorphicSignature Object invokeBasic(Object... args) throws Throwable;

525 526 527 528 529 530 531
    /**
     * Private method for trusted invocation of a MemberName of kind {@code REF_invokeVirtual}.
     * The caller signature is restricted to basic types as with {@code invokeBasic}.
     * The trailing (not leading) argument must be a MemberName.
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
     */
532 533 534 535 536 537
    /*non-public*/ static native @PolymorphicSignature Object linkToVirtual(Object... args) throws Throwable;

    /**
     * Private method for trusted invocation of a MemberName of kind {@code REF_invokeStatic}.
     * The caller signature is restricted to basic types as with {@code invokeBasic}.
     * The trailing (not leading) argument must be a MemberName.
538 539
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
540 541 542 543 544 545 546
     */
    /*non-public*/ static native @PolymorphicSignature Object linkToStatic(Object... args) throws Throwable;

    /**
     * Private method for trusted invocation of a MemberName of kind {@code REF_invokeSpecial}.
     * The caller signature is restricted to basic types as with {@code invokeBasic}.
     * The trailing (not leading) argument must be a MemberName.
547 548
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
549 550 551 552 553 554 555
     */
    /*non-public*/ static native @PolymorphicSignature Object linkToSpecial(Object... args) throws Throwable;

    /**
     * Private method for trusted invocation of a MemberName of kind {@code REF_invokeInterface}.
     * The caller signature is restricted to basic types as with {@code invokeBasic}.
     * The trailing (not leading) argument must be a MemberName.
556 557
     * @param args the signature-polymorphic parameter list, statically represented using varargs
     * @return the signature-polymorphic result, statically represented using {@code Object}
558 559 560
     */
    /*non-public*/ static native @PolymorphicSignature Object linkToInterface(Object... args) throws Throwable;

561
    /**
562
     * Performs a variable arity invocation, passing the arguments in the given array
563
     * to the method handle, as if via an inexact {@link #invoke invoke} from a call site
564 565 566
     * which mentions only the type {@code Object}, and whose arity is the length
     * of the argument array.
     * <p>
567 568 569 570 571 572
     * Specifically, execution proceeds as if by the following steps,
     * although the methods are not guaranteed to be called if the JVM
     * can predict their effects.
     * <ul>
     * <li>Determine the length of the argument array as {@code N}.
     *     For a null reference, {@code N=0}. </li>
573
     * <li>Determine the general type {@code TN} of {@code N} arguments as
574 575 576 577 578 579 580 581
     *     as {@code TN=MethodType.genericMethodType(N)}.</li>
     * <li>Force the original target method handle {@code MH0} to the
     *     required type, as {@code MH1 = MH0.asType(TN)}. </li>
     * <li>Spread the array into {@code N} separate arguments {@code A0, ...}. </li>
     * <li>Invoke the type-adjusted method handle on the unpacked arguments:
     *     MH1.invokeExact(A0, ...). </li>
     * <li>Take the return value as an {@code Object} reference. </li>
     * </ul>
582
     * <p>
583
     * Because of the action of the {@code asType} step, the following argument
584 585 586 587
     * conversions are applied as necessary:
     * <ul>
     * <li>reference casting
     * <li>unboxing
588
     * <li>widening primitive conversions
589
     * </ul>
590
     * <p>
591 592 593 594 595
     * The result returned by the call is boxed if it is a primitive,
     * or forced to null if the return type is void.
     * <p>
     * This call is equivalent to the following code:
     * <p><blockquote><pre>
596
     * MethodHandle invoker = MethodHandles.spreadInvoker(this.type(), 0);
597
     * Object result = invoker.invokeExact(this, arguments);
598
     * </pre></blockquote>
599
     * <p>
600
     * Unlike the signature polymorphic methods {@code invokeExact} and {@code invoke},
601 602 603
     * {@code invokeWithArguments} can be accessed normally via the Core Reflection API and JNI.
     * It can therefore be used as a bridge between native or reflective code and method handles.
     *
604 605
     * @param arguments the arguments to pass to the target
     * @return the result returned by the target
606 607
     * @throws ClassCastException if an argument cannot be converted by reference casting
     * @throws WrongMethodTypeException if the target's type cannot be adjusted to take the given number of {@code Object} arguments
608
     * @throws Throwable anything thrown by the target method invocation
609
     * @see MethodHandles#spreadInvoker
610
     */
611
    public Object invokeWithArguments(Object... arguments) throws Throwable {
612
        int argc = arguments == null ? 0 : arguments.length;
613
        @SuppressWarnings("LocalVariableHidesMemberVariable")
614
        MethodType type = type();
615
        if (type.parameterCount() != argc || isVarargsCollector()) {
616
            // simulate invoke
617 618
            return asType(MethodType.genericMethodType(argc)).invokeWithArguments(arguments);
        }
619
        MethodHandle invoker = type.invokers().varargsInvoker();
620
        return invoker.invokeExact(this, arguments);
621
    }
622 623

    /**
624
     * Performs a variable arity invocation, passing the arguments in the given array
625
     * to the method handle, as if via an inexact {@link #invoke invoke} from a call site
626 627 628 629 630 631 632 633 634 635
     * which mentions only the type {@code Object}, and whose arity is the length
     * of the argument array.
     * <p>
     * This method is also equivalent to the following code:
     * <p><blockquote><pre>
     * {@link #invokeWithArguments(Object...) invokeWithArguments}(arguments.toArray())
     * </pre></blockquote>
     *
     * @param arguments the arguments to pass to the target
     * @return the result returned by the target
636
     * @throws NullPointerException if {@code arguments} is a null reference
637 638 639 640
     * @throws ClassCastException if an argument cannot be converted by reference casting
     * @throws WrongMethodTypeException if the target's type cannot be adjusted to take the given number of {@code Object} arguments
     * @throws Throwable anything thrown by the target method invocation
     */
641
    public Object invokeWithArguments(java.util.List<?> arguments) throws Throwable {
642
        return invokeWithArguments(arguments.toArray());
643 644
    }

645
    /**
646 647
     * Produces an adapter method handle which adapts the type of the
     * current method handle to a new type.
648
     * The resulting method handle is guaranteed to report a type
649 650 651 652
     * which is equal to the desired new type.
     * <p>
     * If the original type and new type are equal, returns {@code this}.
     * <p>
653 654 655 656 657 658 659 660 661 662
     * The new method handle, when invoked, will perform the following
     * steps:
     * <ul>
     * <li>Convert the incoming argument list to match the original
     *     method handle's argument list.
     * <li>Invoke the original method handle on the converted argument list.
     * <li>Convert any result returned by the original method handle
     *     to the return type of new method handle.
     * </ul>
     * <p>
663
     * This method provides the crucial behavioral difference between
664 665 666 667
     * {@link #invokeExact invokeExact} and plain, inexact {@link #invoke invoke}.
     * The two methods
     * perform the same steps when the caller's type descriptor exactly m atches
     * the callee's, but when the types differ, plain {@link #invoke invoke}
668 669
     * also calls {@code asType} (or some internal equivalent) in order
     * to match up the caller's and callee's types.
670
     * <p>
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
     * If the current method is a variable arity method handle
     * argument list conversion may involve the conversion and collection
     * of several arguments into an array, as
     * {@linkplain #asVarargsCollector described elsewhere}.
     * In every other case, all conversions are applied <em>pairwise</em>,
     * which means that each argument or return value is converted to
     * exactly one argument or return value (or no return value).
     * The applied conversions are defined by consulting the
     * the corresponding component types of the old and new
     * method handle types.
     * <p>
     * Let <em>T0</em> and <em>T1</em> be corresponding new and old parameter types,
     * or old and new return types.  Specifically, for some valid index {@code i}, let
     * <em>T0</em>{@code =newType.parameterType(i)} and <em>T1</em>{@code =this.type().parameterType(i)}.
     * Or else, going the other way for return values, let
     * <em>T0</em>{@code =this.type().returnType()} and <em>T1</em>{@code =newType.returnType()}.
     * If the types are the same, the new method handle makes no change
     * to the corresponding argument or return value (if any).
     * Otherwise, one of the following conversions is applied
     * if possible:
     * <ul>
     * <li>If <em>T0</em> and <em>T1</em> are references, then a cast to <em>T1</em> is applied.
     *     (The types do not need to be related in any particular way.
     *     This is because a dynamic value of null can convert to any reference type.)
     * <li>If <em>T0</em> and <em>T1</em> are primitives, then a Java method invocation
     *     conversion (JLS 5.3) is applied, if one exists.
     *     (Specifically, <em>T0</em> must convert to <em>T1</em> by a widening primitive conversion.)
     * <li>If <em>T0</em> is a primitive and <em>T1</em> a reference,
     *     a Java casting conversion (JLS 5.5) is applied if one exists.
     *     (Specifically, the value is boxed from <em>T0</em> to its wrapper class,
     *     which is then widened as needed to <em>T1</em>.)
     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing
     *     conversion will be applied at runtime, possibly followed
     *     by a Java method invocation conversion (JLS 5.3)
     *     on the primitive value.  (These are the primitive widening conversions.)
     *     <em>T0</em> must be a wrapper class or a supertype of one.
     *     (In the case where <em>T0</em> is Object, these are the conversions
     *     allowed by {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}.)
     *     The unboxing conversion must have a possibility of success, which means that
     *     if <em>T0</em> is not itself a wrapper class, there must exist at least one
     *     wrapper class <em>TW</em> which is a subtype of <em>T0</em> and whose unboxed
     *     primitive value can be widened to <em>T1</em>.
     * <li>If the return type <em>T1</em> is marked as void, any returned value is discarded
     * <li>If the return type <em>T0</em> is void and <em>T1</em> a reference, a null value is introduced.
     * <li>If the return type <em>T0</em> is void and <em>T1</em> a primitive,
     *     a zero value is introduced.
     * </ul>
    * (<em>Note:</em> Both <em>T0</em> and <em>T1</em> may be regarded as static types,
     * because neither corresponds specifically to the <em>dynamic type</em> of any
     * actual argument or return value.)
     * <p>
     * The method handle conversion cannot be made if any one of the required
     * pairwise conversions cannot be made.
     * <p>
     * At runtime, the conversions applied to reference arguments
     * or return values may require additional runtime checks which can fail.
     * An unboxing operation may fail because the original reference is null,
     * causing a {@link java.lang.NullPointerException NullPointerException}.
     * An unboxing operation or a reference cast may also fail on a reference
     * to an object of the wrong type,
     * causing a {@link java.lang.ClassCastException ClassCastException}.
     * Although an unboxing operation may accept several kinds of wrappers,
     * if none are available, a {@code ClassCastException} will be thrown.
734
     *
735 736 737 738
     * @param newType the expected type of the new method handle
     * @return a method handle which delegates to {@code this} after performing
     *           any necessary argument conversions, and arranges for any
     *           necessary return value conversions
739
     * @throws NullPointerException if {@code newType} is a null reference
740
     * @throws WrongMethodTypeException if the conversion cannot be made
741
     * @see MethodHandles#explicitCastArguments
742
     */
743
    public MethodHandle asType(MethodType newType) {
744 745 746 747 748 749 750 751 752
        // Fast path alternative to a heavyweight {@code asType} call.
        // Return 'this' if the conversion will be a no-op.
        if (newType == type) {
            return this;
        }
        // Return 'this.asTypeCache' if the conversion is already memoized.
        MethodHandle atc = asTypeCache;
        if (atc != null && newType == atc.type) {
            return atc;
753
        }
754 755 756 757 758 759 760 761
        return asTypeUncached(newType);
    }

    /** Override this to change asType behavior. */
    /*non-public*/ MethodHandle asTypeUncached(MethodType newType) {
        if (!type.isConvertibleTo(newType))
            throw new WrongMethodTypeException("cannot convert "+this+" to "+newType);
        return asTypeCache = convertArguments(newType);
762 763 764
    }

    /**
765
     * Makes an <em>array-spreading</em> method handle, which accepts a trailing array argument
766 767
     * and spreads its elements as positional arguments.
     * The new method handle adapts, as its <i>target</i>,
768
     * the current method handle.  The type of the adapter will be
769 770 771 772 773
     * the same as the type of the target, except that the final
     * {@code arrayLength} parameters of the target's type are replaced
     * by a single array parameter of type {@code arrayType}.
     * <p>
     * If the array element type differs from any of the corresponding
774
     * argument types on the original target,
775 776
     * the original target is adapted to take the array elements directly,
     * as if by a call to {@link #asType asType}.
777 778 779 780 781 782 783 784 785 786 787 788 789
     * <p>
     * When called, the adapter replaces a trailing array argument
     * by the array's elements, each as its own argument to the target.
     * (The order of the arguments is preserved.)
     * They are converted pairwise by casting and/or unboxing
     * to the types of the trailing parameters of the target.
     * Finally the target is called.
     * What the target eventually returns is returned unchanged by the adapter.
     * <p>
     * Before calling the target, the adapter verifies that the array
     * contains exactly enough elements to provide a correct argument count
     * to the target method handle.
     * (The array may also be null when zero elements are required.)
790 791
     * <p>
     * Here are some simple examples of array-spreading method handles:
A
alanb 已提交
792
     * <blockquote><pre>{@code
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
MethodHandle equals = publicLookup()
  .findVirtual(String.class, "equals", methodType(boolean.class, Object.class));
assert( (boolean) equals.invokeExact("me", (Object)"me"));
assert(!(boolean) equals.invokeExact("me", (Object)"thee"));
// spread both arguments from a 2-array:
MethodHandle eq2 = equals.asSpreader(Object[].class, 2);
assert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" }));
assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" }));
// spread both arguments from a String array:
MethodHandle eq2s = equals.asSpreader(String[].class, 2);
assert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" }));
assert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" }));
// spread second arguments from a 1-array:
MethodHandle eq1 = equals.asSpreader(Object[].class, 1);
assert( (boolean) eq1.invokeExact("me", new Object[]{ "me" }));
assert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" }));
// spread no arguments from a 0-array or null:
MethodHandle eq0 = equals.asSpreader(Object[].class, 0);
assert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0]));
assert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null));
// asSpreader and asCollector are approximate inverses:
for (int n = 0; n <= 2; n++) {
    for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) {
        MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n);
        assert( (boolean) equals2.invokeWithArguments("me", "me"));
        assert(!(boolean) equals2.invokeWithArguments("me", "thee"));
    }
}
MethodHandle caToString = publicLookup()
  .findStatic(Arrays.class, "toString", methodType(String.class, char[].class));
assertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray()));
MethodHandle caString3 = caToString.asCollector(char[].class, 3);
assertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C'));
MethodHandle caToString2 = caString3.asSpreader(char[].class, 2);
assertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray()));
A
alanb 已提交
828
     * }</pre></blockquote>
829 830 831
     * @param arrayType usually {@code Object[]}, the type of the array argument from which to extract the spread arguments
     * @param arrayLength the number of arguments to spread from an incoming array argument
     * @return a new method handle which spreads its final array argument,
832
     *         before calling the original method handle
833
     * @throws NullPointerException if {@code arrayType} is a null reference
834
     * @throws IllegalArgumentException if {@code arrayType} is not an array type
835
     * @throws IllegalArgumentException if target does not have at least
836 837
     *         {@code arrayLength} parameter types,
     *         or if {@code arrayLength} is negative
838
     * @throws WrongMethodTypeException if the implied {@code asType} call fails
839
     * @see #asCollector
840
     */
841
    public MethodHandle asSpreader(Class<?> arrayType, int arrayLength) {
842
        asSpreaderChecks(arrayType, arrayLength);
843 844
        int spreadArgPos = type.parameterCount() - arrayLength;
        return MethodHandleImpl.makeSpreadArguments(this, arrayType, spreadArgPos, arrayLength);
845 846 847 848
    }

    private void asSpreaderChecks(Class<?> arrayType, int arrayLength) {
        spreadArrayChecks(arrayType, arrayLength);
849
        int nargs = type().parameterCount();
850 851
        if (nargs < arrayLength || arrayLength < 0)
            throw newIllegalArgumentException("bad spread array length");
852 853 854 855 856 857 858 859 860 861
        if (arrayType != Object[].class && arrayLength != 0) {
            boolean sawProblem = false;
            Class<?> arrayElement = arrayType.getComponentType();
            for (int i = nargs - arrayLength; i < nargs; i++) {
                if (!MethodType.canConvert(arrayElement, type().parameterType(i))) {
                    sawProblem = true;
                    break;
                }
            }
            if (sawProblem) {
862
                ArrayList<Class<?>> ptypes = new ArrayList<>(type().parameterList());
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
                for (int i = nargs - arrayLength; i < nargs; i++) {
                    ptypes.set(i, arrayElement);
                }
                // elicit an error:
                this.asType(MethodType.methodType(type().returnType(), ptypes));
            }
        }
    }

    private void spreadArrayChecks(Class<?> arrayType, int arrayLength) {
        Class<?> arrayElement = arrayType.getComponentType();
        if (arrayElement == null)
            throw newIllegalArgumentException("not an array type", arrayType);
        if ((arrayLength & 0x7F) != arrayLength) {
            if ((arrayLength & 0xFF) != arrayLength)
                throw newIllegalArgumentException("array length is not legal", arrayLength);
            assert(arrayLength >= 128);
            if (arrayElement == long.class ||
                arrayElement == double.class)
                throw newIllegalArgumentException("array length is not legal for long[] or double[]", arrayLength);
        }
884 885 886
    }

    /**
887
     * Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing
888 889
     * positional arguments and collects them into an array argument.
     * The new method handle adapts, as its <i>target</i>,
890 891
     * the current method handle.  The type of the adapter will be
     * the same as the type of the target, except that a single trailing
892 893 894
     * parameter (usually of type {@code arrayType}) is replaced by
     * {@code arrayLength} parameters whose type is element type of {@code arrayType}.
     * <p>
895
     * If the array type differs from the final argument type on the original target,
896 897
     * the original target is adapted to take the array type directly,
     * as if by a call to {@link #asType asType}.
898
     * <p>
899 900
     * When called, the adapter replaces its trailing {@code arrayLength}
     * arguments by a single new array of type {@code arrayType}, whose elements
901 902 903 904
     * comprise (in order) the replaced arguments.
     * Finally the target is called.
     * What the target eventually returns is returned unchanged by the adapter.
     * <p>
905
     * (The array may also be a shared constant when {@code arrayLength} is zero.)
906 907 908 909 910 911 912 913
     * <p>
     * (<em>Note:</em> The {@code arrayType} is often identical to the last
     * parameter type of the original target.
     * It is an explicit argument for symmetry with {@code asSpreader}, and also
     * to allow the target to use a simple {@code Object} as its last parameter type.)
     * <p>
     * In order to create a collecting adapter which is not restricted to a particular
     * number of collected arguments, use {@link #asVarargsCollector asVarargsCollector} instead.
914 915
     * <p>
     * Here are some examples of array-collecting method handles:
A
alanb 已提交
916
     * <blockquote><pre>{@code
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
MethodHandle deepToString = publicLookup()
  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
assertEquals("[won]",   (String) deepToString.invokeExact(new Object[]{"won"}));
MethodHandle ts1 = deepToString.asCollector(Object[].class, 1);
assertEquals(methodType(String.class, Object.class), ts1.type());
//assertEquals("[won]", (String) ts1.invokeExact(         new Object[]{"won"})); //FAIL
assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"}));
// arrayType can be a subtype of Object[]
MethodHandle ts2 = deepToString.asCollector(String[].class, 2);
assertEquals(methodType(String.class, String.class, String.class), ts2.type());
assertEquals("[two, too]", (String) ts2.invokeExact("two", "too"));
MethodHandle ts0 = deepToString.asCollector(Object[].class, 0);
assertEquals("[]", (String) ts0.invokeExact());
// collectors can be nested, Lisp-style
MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2);
assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D")));
// arrayType can be any primitive array type
MethodHandle bytesToString = publicLookup()
  .findStatic(Arrays.class, "toString", methodType(String.class, byte[].class))
  .asCollector(byte[].class, 3);
assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3));
MethodHandle longsToString = publicLookup()
  .findStatic(Arrays.class, "toString", methodType(String.class, long[].class))
  .asCollector(long[].class, 1);
assertEquals("[123]", (String) longsToString.invokeExact((long)123));
A
alanb 已提交
942
     * }</pre></blockquote>
943
     * @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments
944
     * @param arrayLength the number of arguments to collect into a new array argument
945 946
     * @return a new method handle which collects some trailing argument
     *         into an array, before calling the original method handle
947
     * @throws NullPointerException if {@code arrayType} is a null reference
948
     * @throws IllegalArgumentException if {@code arrayType} is not an array type
949 950
     *         or {@code arrayType} is not assignable to this method handle's trailing parameter type,
     *         or {@code arrayLength} is not a legal array size
951
     * @throws WrongMethodTypeException if the implied {@code asType} call fails
952 953
     * @see #asSpreader
     * @see #asVarargsCollector
954
     */
955
    public MethodHandle asCollector(Class<?> arrayType, int arrayLength) {
956
        asCollectorChecks(arrayType, arrayLength);
957 958 959 960
        int collectArgPos = type().parameterCount()-1;
        MethodHandle target = this;
        if (arrayType != type().parameterType(collectArgPos))
            target = convertArguments(type().changeParameterType(collectArgPos, arrayType));
961
        MethodHandle collector = ValueConversions.varargsArray(arrayType, arrayLength);
962
        return MethodHandles.collectArguments(target, collectArgPos, collector);
963 964
    }

965 966
    // private API: return true if last param exactly matches arrayType
    private boolean asCollectorChecks(Class<?> arrayType, int arrayLength) {
967
        spreadArrayChecks(arrayType, arrayLength);
968
        int nargs = type().parameterCount();
969 970 971 972 973 974
        if (nargs != 0) {
            Class<?> lastParam = type().parameterType(nargs-1);
            if (lastParam == arrayType)  return true;
            if (lastParam.isAssignableFrom(arrayType))  return false;
        }
        throw newIllegalArgumentException("array type not assignable to trailing argument", this, arrayType);
975 976 977
    }

    /**
978
     * Makes a <em>variable arity</em> adapter which is able to accept
979 980 981 982 983
     * any number of trailing positional arguments and collect them
     * into an array argument.
     * <p>
     * The type and behavior of the adapter will be the same as
     * the type and behavior of the target, except that certain
984
     * {@code invoke} and {@code asType} requests can lead to
985 986 987 988 989 990
     * trailing positional arguments being collected into target's
     * trailing parameter.
     * Also, the last parameter type of the adapter will be
     * {@code arrayType}, even if the target has a different
     * last parameter type.
     * <p>
991 992 993 994
     * This transformation may return {@code this} if the method handle is
     * already of variable arity and its trailing parameter type
     * is identical to {@code arrayType}.
     * <p>
995 996 997 998 999 1000 1001
     * When called with {@link #invokeExact invokeExact}, the adapter invokes
     * the target with no argument changes.
     * (<em>Note:</em> This behavior is different from a
     * {@linkplain #asCollector fixed arity collector},
     * since it accepts a whole array of indeterminate length,
     * rather than a fixed number of arguments.)
     * <p>
1002
     * When called with plain, inexact {@link #invoke invoke}, if the caller
1003 1004
     * type is the same as the adapter, the adapter invokes the target as with
     * {@code invokeExact}.
1005
     * (This is the normal behavior for {@code invoke} when types match.)
1006 1007 1008 1009 1010
     * <p>
     * Otherwise, if the caller and adapter arity are the same, and the
     * trailing parameter type of the caller is a reference type identical to
     * or assignable to the trailing parameter type of the adapter,
     * the arguments and return values are converted pairwise,
1011 1012
     * as if by {@link #asType asType} on a fixed arity
     * method handle.
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
     * <p>
     * Otherwise, the arities differ, or the adapter's trailing parameter
     * type is not assignable from the corresponding caller type.
     * In this case, the adapter replaces all trailing arguments from
     * the original trailing argument position onward, by
     * a new array of type {@code arrayType}, whose elements
     * comprise (in order) the replaced arguments.
     * <p>
     * The caller type must provides as least enough arguments,
     * and of the correct type, to satisfy the target's requirement for
     * positional arguments before the trailing array argument.
     * Thus, the caller must supply, at a minimum, {@code N-1} arguments,
     * where {@code N} is the arity of the target.
     * Also, there must exist conversions from the incoming arguments
     * to the target's arguments.
1028
     * As with other uses of plain {@code invoke}, if these basic
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
     * requirements are not fulfilled, a {@code WrongMethodTypeException}
     * may be thrown.
     * <p>
     * In all cases, what the target eventually returns is returned unchanged by the adapter.
     * <p>
     * In the final case, it is exactly as if the target method handle were
     * temporarily adapted with a {@linkplain #asCollector fixed arity collector}
     * to the arity required by the caller type.
     * (As with {@code asCollector}, if the array length is zero,
     * a shared constant may be used instead of a new array.
     * If the implied call to {@code asCollector} would throw
     * an {@code IllegalArgumentException} or {@code WrongMethodTypeException},
     * the call to the variable arity adapter must throw
     * {@code WrongMethodTypeException}.)
     * <p>
     * The behavior of {@link #asType asType} is also specialized for
     * variable arity adapters, to maintain the invariant that
1046
     * plain, inexact {@code invoke} is always equivalent to an {@code asType}
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
     * call to adjust the target type, followed by {@code invokeExact}.
     * Therefore, a variable arity adapter responds
     * to an {@code asType} request by building a fixed arity collector,
     * if and only if the adapter and requested type differ either
     * in arity or trailing argument type.
     * The resulting fixed arity collector has its type further adjusted
     * (if necessary) to the requested type by pairwise conversion,
     * as if by another application of {@code asType}.
     * <p>
     * When a method handle is obtained by executing an {@code ldc} instruction
     * of a {@code CONSTANT_MethodHandle} constant, and the target method is marked
     * as a variable arity method (with the modifier bit {@code 0x0080}),
     * the method handle will accept multiple arities, as if the method handle
     * constant were created by means of a call to {@code asVarargsCollector}.
     * <p>
     * In order to create a collecting adapter which collects a predetermined
     * number of arguments, and whose type reflects this predetermined number,
     * use {@link #asCollector asCollector} instead.
     * <p>
     * No method handle transformations produce new method handles with
     * variable arity, unless they are documented as doing so.
     * Therefore, besides {@code asVarargsCollector},
     * all methods in {@code MethodHandle} and {@code MethodHandles}
     * will return a method handle with fixed arity,
     * except in the cases where they are specified to return their original
     * operand (e.g., {@code asType} of the method handle's own type).
     * <p>
     * Calling {@code asVarargsCollector} on a method handle which is already
     * of variable arity will produce a method handle with the same type and behavior.
     * It may (or may not) return the original variable arity method handle.
     * <p>
     * Here is an example, of a list-making variable arity method handle:
A
alanb 已提交
1079
     * <blockquote><pre>{@code
1080 1081 1082 1083 1084 1085 1086 1087
MethodHandle deepToString = publicLookup()
  .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
MethodHandle ts1 = deepToString.asVarargsCollector(Object[].class);
assertEquals("[won]",   (String) ts1.invokeExact(    new Object[]{"won"}));
assertEquals("[won]",   (String) ts1.invoke(         new Object[]{"won"}));
assertEquals("[won]",   (String) ts1.invoke(                      "won" ));
assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"}));
// findStatic of Arrays.asList(...) produces a variable arity method handle:
1088
MethodHandle asList = publicLookup()
1089 1090 1091
  .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class));
assertEquals(methodType(List.class, Object[].class), asList.type());
assert(asList.isVarargsCollector());
1092 1093 1094
assertEquals("[]", asList.invoke().toString());
assertEquals("[1]", asList.invoke(1).toString());
assertEquals("[two, too]", asList.invoke("two", "too").toString());
1095
String[] argv = { "three", "thee", "tee" };
1096
assertEquals("[three, thee, tee]", asList.invoke(argv).toString());
1097
assertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString());
1098
List ls = (List) asList.invoke((Object)argv);
1099 1100
assertEquals(1, ls.size());
assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
A
alanb 已提交
1101
     * }</pre></blockquote>
1102
     * <p style="font-size:smaller;">
1103
     * <em>Discussion:</em>
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
     * These rules are designed as a dynamically-typed variation
     * of the Java rules for variable arity methods.
     * In both cases, callers to a variable arity method or method handle
     * can either pass zero or more positional arguments, or else pass
     * pre-collected arrays of any length.  Users should be aware of the
     * special role of the final argument, and of the effect of a
     * type match on that final argument, which determines whether
     * or not a single trailing argument is interpreted as a whole
     * array or a single element of an array to be collected.
     * Note that the dynamic type of the trailing argument has no
1114 1115
     * effect on this decision, only a comparison between the symbolic
     * type descriptor of the call site and the type descriptor of the method handle.)
1116
     *
1117 1118 1119
     * @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments
     * @return a new method handle which can collect any number of trailing arguments
     *         into an array, before calling the original method handle
1120
     * @throws NullPointerException if {@code arrayType} is a null reference
1121 1122 1123
     * @throws IllegalArgumentException if {@code arrayType} is not an array type
     *         or {@code arrayType} is not assignable to this method handle's trailing parameter type
     * @see #asCollector
1124
     * @see #isVarargsCollector
1125
     * @see #asFixedArity
1126 1127 1128
     */
    public MethodHandle asVarargsCollector(Class<?> arrayType) {
        Class<?> arrayElement = arrayType.getComponentType();
1129 1130 1131
        boolean lastMatch = asCollectorChecks(arrayType, 0);
        if (isVarargsCollector() && lastMatch)
            return this;
1132
        return MethodHandleImpl.makeVarargsCollector(this, arrayType);
1133 1134 1135
    }

    /**
1136
     * Determines if this method handle
1137 1138 1139 1140
     * supports {@linkplain #asVarargsCollector variable arity} calls.
     * Such method handles arise from the following sources:
     * <ul>
     * <li>a call to {@linkplain #asVarargsCollector asVarargsCollector}
1141
     * <li>a call to a {@linkplain java.lang.invoke.MethodHandles.Lookup lookup method}
1142 1143 1144 1145
     *     which resolves to a variable arity Java method or constructor
     * <li>an {@code ldc} instruction of a {@code CONSTANT_MethodHandle}
     *     which resolves to a variable arity Java method or constructor
     * </ul>
1146
     * @return true if this method handle accepts more than one arity of plain, inexact {@code invoke} calls
1147
     * @see #asVarargsCollector
1148
     * @see #asFixedArity
1149 1150 1151 1152 1153
     */
    public boolean isVarargsCollector() {
        return false;
    }

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    /**
     * Makes a <em>fixed arity</em> method handle which is otherwise
     * equivalent to the the current method handle.
     * <p>
     * If the current method handle is not of
     * {@linkplain #asVarargsCollector variable arity},
     * the current method handle is returned.
     * This is true even if the current method handle
     * could not be a valid input to {@code asVarargsCollector}.
     * <p>
     * Otherwise, the resulting fixed-arity method handle has the same
     * type and behavior of the current method handle,
     * except that {@link #isVarargsCollector isVarargsCollector}
     * will be false.
     * The fixed-arity method handle may (or may not) be the
     * a previous argument to {@code asVarargsCollector}.
     * <p>
     * Here is an example, of a list-making variable arity method handle:
A
alanb 已提交
1172
     * <blockquote><pre>{@code
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
MethodHandle asListVar = publicLookup()
  .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
  .asVarargsCollector(Object[].class);
MethodHandle asListFix = asListVar.asFixedArity();
assertEquals("[1]", asListVar.invoke(1).toString());
Exception caught = null;
try { asListFix.invoke((Object)1); }
catch (Exception ex) { caught = ex; }
assert(caught instanceof ClassCastException);
assertEquals("[two, too]", asListVar.invoke("two", "too").toString());
try { asListFix.invoke("two", "too"); }
catch (Exception ex) { caught = ex; }
assert(caught instanceof WrongMethodTypeException);
Object[] argv = { "three", "thee", "tee" };
assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString());
assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString());
assertEquals(1, ((List) asListVar.invoke((Object)argv)).size());
assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString());
A
alanb 已提交
1191
     * }</pre></blockquote>
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
     *
     * @return a new method handle which accepts only a fixed number of arguments
     * @see #asVarargsCollector
     * @see #isVarargsCollector
     */
    public MethodHandle asFixedArity() {
        assert(!isVarargsCollector());
        return this;
    }

1202
    /**
1203
     * Binds a value {@code x} to the first argument of a method handle, without invoking it.
1204
     * The new method handle adapts, as its <i>target</i>,
1205
     * the current method handle by binding it to the given argument.
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
     * The type of the bound handle will be
     * the same as the type of the target, except that a single leading
     * reference parameter will be omitted.
     * <p>
     * When called, the bound handle inserts the given value {@code x}
     * as a new leading argument to the target.  The other arguments are
     * also passed unchanged.
     * What the target eventually returns is returned unchanged by the bound handle.
     * <p>
     * The reference {@code x} must be convertible to the first parameter
     * type of the target.
1217 1218 1219
     * <p>
     * (<em>Note:</em>  Because method handles are immutable, the target method handle
     * retains its original type and behavior.)
1220
     * @param x  the value to bind to the first argument of the target
1221 1222
     * @return a new method handle which prepends the given value to the incoming
     *         argument list, before calling the original method handle
1223 1224 1225 1226
     * @throws IllegalArgumentException if the target does not have a
     *         leading parameter type that is a reference type
     * @throws ClassCastException if {@code x} cannot be converted
     *         to the leading parameter type of the target
1227
     * @see MethodHandles#insertArguments
1228
     */
1229
    public MethodHandle bindTo(Object x) {
1230
        Class<?> ptype;
1231 1232 1233 1234
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        MethodType type = type();
        if (type.parameterCount() == 0 ||
            (ptype = type.parameterType(0)).isPrimitive())
1235
            throw newIllegalArgumentException("no leading reference parameter", x);
1236 1237
        x = ptype.cast(x);  // throw CCE if needed
        return bindReceiver(x);
1238
    }
1239

1240
    /**
1241 1242 1243 1244
     * Returns a string representation of the method handle,
     * starting with the string {@code "MethodHandle"} and
     * ending with the string representation of the method handle's type.
     * In other words, this method returns a string equal to the value of:
1245
     * <blockquote><pre>
1246
     * "MethodHandle" + type().toString()
1247 1248
     * </pre></blockquote>
     * <p>
1249
     * (<em>Note:</em>  Future releases of this API may add further information
1250
     * to the string representation.
1251
     * Therefore, the present syntax should not be parsed by applications.)
1252 1253
     *
     * @return a string representation of the method handle
1254
     */
1255 1256
    @Override
    public String toString() {
1257
        if (DEBUG_METHOD_HANDLE_NAMES)  return debugString();
1258 1259 1260
        return standardString();
    }
    String standardString() {
1261 1262
        return "MethodHandle"+type;
    }
1263
    String debugString() {
1264
        return standardString()+"/LF="+internalForm()+internalProperties();
1265 1266 1267 1268 1269 1270 1271
    }

    //// Implementation methods.
    //// Sub-classes can override these default implementations.
    //// All these methods assume arguments are already validated.

    // Other transforms to do:  convert, explicitCast, permute, drop, filter, fold, GWT, catch
1272 1273

    /*non-public*/
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    MethodHandle setVarargs(MemberName member) throws IllegalAccessException {
        if (!member.isVarargs())  return this;
        int argc = type().parameterCount();
        if (argc != 0) {
            Class<?> arrayType = type().parameterType(argc-1);
            if (arrayType.isArray()) {
                return MethodHandleImpl.makeVarargsCollector(this, arrayType);
            }
        }
        throw member.makeAccessException("cannot make variable arity", null);
    }
    /*non-public*/
    MethodHandle viewAsType(MethodType newType) {
        // No actual conversions, just a new view of the same method.
        return MethodHandleImpl.makePairwiseConvert(this, newType, 0);
    }

    // Decoding

    /*non-public*/
    LambdaForm internalForm() {
        return form;
    }

    /*non-public*/
    MemberName internalMemberName() {
        return null;  // DMH returns DMH.member
    }

R
rfield 已提交
1303 1304
    /*non-public*/
    MethodHandle withInternalMemberName(MemberName member) {
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
        if (member != null) {
            return MethodHandleImpl.makeWrappedMember(this, member);
        } else if (internalMemberName() == null) {
            // The required internaMemberName is null, and this MH (like most) doesn't have one.
            return this;
        } else {
            // The following case is rare. Mask the internalMemberName by wrapping the MH in a BMH.
            MethodHandle result = rebind();
            assert (result.internalMemberName() == null);
            return result;
        }
R
rfield 已提交
1316 1317
    }

1318 1319 1320 1321 1322
    /*non-public*/
    boolean isInvokeSpecial() {
        return false;  // DMH.Special returns true
    }

1323 1324
    /*non-public*/
    Object internalValues() {
1325 1326 1327 1328 1329 1330
        return null;
    }

    /*non-public*/
    Object internalProperties() {
        // Override to something like "/FOO=bar"
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
        return "";
    }

    //// Method handle implementation methods.
    //// Sub-classes can override these default implementations.
    //// All these methods assume arguments are already validated.

    /*non-public*/ MethodHandle convertArguments(MethodType newType) {
        // Override this if it can be improved.
        return MethodHandleImpl.makePairwiseConvert(this, newType, 1);
    }

    /*non-public*/
    MethodHandle bindArgument(int pos, char basicType, Object value) {
        // Override this if it can be improved.
        return rebind().bindArgument(pos, basicType, value);
    }

    /*non-public*/
    MethodHandle bindReceiver(Object receiver) {
        // Override this if it can be improved.
        return bindArgument(0, 'L', receiver);
    }

    /*non-public*/
    MethodHandle bindImmediate(int pos, char basicType, Object value) {
        // Bind an immediate value to a position in the arguments.
        // This means, elide the respective argument,
        // and replace all references to it in NamedFunction args with the specified value.

        // CURRENT RESTRICTIONS
        // * only for pos 0 and UNSAFE (position is adjusted in MHImpl to make API usable for others)
        assert pos == 0 && basicType == 'L' && value instanceof Unsafe;
        MethodType type2 = type.dropParameterTypes(pos, pos + 1); // adjustment: ignore receiver!
        LambdaForm form2 = form.bindImmediate(pos + 1, basicType, value); // adjust pos to form-relative pos
        return copyWith(type2, form2);
    }

    /*non-public*/
    MethodHandle copyWith(MethodType mt, LambdaForm lf) {
        throw new InternalError("copyWith: " + this.getClass());
    }

    /*non-public*/
    MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
        // Override this if it can be improved.
        return rebind().dropArguments(srcType, pos, drops);
    }

    /*non-public*/
    MethodHandle permuteArguments(MethodType newType, int[] reorder) {
        // Override this if it can be improved.
        return rebind().permuteArguments(newType, reorder);
    }

    /*non-public*/
    MethodHandle rebind() {
        // Bind 'this' into a new invoker, of the known class BMH.
        MethodType type2 = type();
R
rfield 已提交
1390
        LambdaForm form2 = reinvokerForm(this);
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
        // form2 = lambda (bmh, arg*) { thismh = bmh[0]; invokeBasic(thismh, arg*) }
        return BoundMethodHandle.bindSingle(type2, form2, this);
    }

    /*non-public*/
    MethodHandle reinvokerTarget() {
        throw new InternalError("not a reinvoker MH: "+this.getClass().getName()+": "+this);
    }

    /** Create a LF which simply reinvokes a target of the given basic type.
     *  The target MH must override {@link #reinvokerTarget} to provide the target.
     */
R
rfield 已提交
1403 1404
    static LambdaForm reinvokerForm(MethodHandle target) {
        MethodType mtype = target.type().basicType();
1405 1406
        LambdaForm reinvoker = mtype.form().cachedLambdaForm(MethodTypeForm.LF_REINVOKE);
        if (reinvoker != null)  return reinvoker;
R
rfield 已提交
1407 1408 1409 1410 1411 1412 1413 1414
        if (mtype.parameterSlotCount() >= MethodType.MAX_MH_ARITY)
            return makeReinvokerForm(target.type(), target);  // cannot cache this
        reinvoker = makeReinvokerForm(mtype, null);
        return mtype.form().setCachedLambdaForm(MethodTypeForm.LF_REINVOKE, reinvoker);
    }
    private static LambdaForm makeReinvokerForm(MethodType mtype, MethodHandle customTargetOrNull) {
        boolean customized = (customTargetOrNull != null);
        MethodHandle MH_invokeBasic = customized ? null : MethodHandles.basicInvoker(mtype);
1415 1416 1417 1418
        final int THIS_BMH    = 0;
        final int ARG_BASE    = 1;
        final int ARG_LIMIT   = ARG_BASE + mtype.parameterCount();
        int nameCursor = ARG_LIMIT;
R
rfield 已提交
1419
        final int NEXT_MH     = customized ? -1 : nameCursor++;
1420 1421
        final int REINVOKE    = nameCursor++;
        LambdaForm.Name[] names = LambdaForm.arguments(nameCursor - ARG_LIMIT, mtype.invokerType());
R
rfield 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
        Object[] targetArgs;
        MethodHandle targetMH;
        if (customized) {
            targetArgs = Arrays.copyOfRange(names, ARG_BASE, ARG_LIMIT, Object[].class);
            targetMH = customTargetOrNull;
        } else {
            names[NEXT_MH] = new LambdaForm.Name(NF_reinvokerTarget, names[THIS_BMH]);
            targetArgs = Arrays.copyOfRange(names, THIS_BMH, ARG_LIMIT, Object[].class);
            targetArgs[0] = names[NEXT_MH];  // overwrite this MH with next MH
            targetMH = MethodHandles.basicInvoker(mtype);
        }
        names[REINVOKE] = new LambdaForm.Name(targetMH, targetArgs);
        return new LambdaForm("BMH.reinvoke", ARG_LIMIT, names);
1435 1436 1437 1438 1439 1440 1441 1442
    }

    private static final LambdaForm.NamedFunction NF_reinvokerTarget;
    static {
        try {
            NF_reinvokerTarget = new LambdaForm.NamedFunction(MethodHandle.class
                .getDeclaredMethod("reinvokerTarget"));
        } catch (ReflectiveOperationException ex) {
1443
            throw newInternalError(ex);
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
        }
    }

    /**
     * Replace the old lambda form of this method handle with a new one.
     * The new one must be functionally equivalent to the old one.
     * Threads may continue running the old form indefinitely,
     * but it is likely that the new one will be preferred for new executions.
     * Use with discretion.
     * @param newForm
     */
    /*non-public*/
    void updateForm(LambdaForm newForm) {
        if (form == newForm)  return;
        // ISSUE: Should we have a memory fence here?
        UNSAFE.putObject(this, FORM_OFFSET, newForm);
        this.form.prepare();  // as in MethodHandle.<init>
    }

    private static final long FORM_OFFSET;
    static {
        try {
            FORM_OFFSET = UNSAFE.objectFieldOffset(MethodHandle.class.getDeclaredField("form"));
        } catch (ReflectiveOperationException ex) {
1468
            throw newInternalError(ex);
1469
        }
1470
    }
1471
}