jdk/src/share/classes/java/lang/invoke/MethodHandles.java
changeset 20535 cc85c8626435
parent 20534 da86f7904e6d
child 20853 505b28fe2b98
equal deleted inserted replaced
20534:da86f7904e6d 20535:cc85c8626435
    47  * method handles. They fall into several categories:
    47  * method handles. They fall into several categories:
    48  * <ul>
    48  * <ul>
    49  * <li>Lookup methods which help create method handles for methods and fields.
    49  * <li>Lookup methods which help create method handles for methods and fields.
    50  * <li>Combinator methods, which combine or transform pre-existing method handles into new ones.
    50  * <li>Combinator methods, which combine or transform pre-existing method handles into new ones.
    51  * <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns.
    51  * <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns.
    52  * <li>Wrapper methods which can convert between method handles and interface types.
       
    53  * </ul>
    52  * </ul>
    54  * <p>
    53  * <p>
    55  * @author John Rose, JSR 292 EG
    54  * @author John Rose, JSR 292 EG
    56  * @since 1.7
    55  * @since 1.7
    57  */
    56  */
    64     // See IMPL_LOOKUP below.
    63     // See IMPL_LOOKUP below.
    65 
    64 
    66     //// Method handle creation from ordinary methods.
    65     //// Method handle creation from ordinary methods.
    67 
    66 
    68     /**
    67     /**
    69      * Returns a {@link Lookup lookup object} on the caller,
    68      * Returns a {@link Lookup lookup object} with
    70      * which has the capability to access any method handle that the caller has access to,
    69      * full capabilities to emulate all supported bytecode behaviors of the caller.
    71      * including direct method handles to private fields and methods.
    70      * These capabilities include <a href="MethodHandles.Lookup.html#privacc">private access</a> to the caller.
       
    71      * Factory methods on the lookup object can create
       
    72      * <a href="MethodHandleInfo.html#directmh">direct method handles</a>
       
    73      * for any member that the caller has access to via bytecodes,
       
    74      * including protected and private fields and methods.
    72      * This lookup object is a <em>capability</em> which may be delegated to trusted agents.
    75      * This lookup object is a <em>capability</em> which may be delegated to trusted agents.
    73      * Do not store it in place where untrusted code can access it.
    76      * Do not store it in place where untrusted code can access it.
    74      * @return a lookup object for the caller of this method
    77      * <p>
       
    78      * This method is caller sensitive, which means that it may return different
       
    79      * values to different callers.
       
    80      * <p>
       
    81      * For any given caller class {@code C}, the lookup object returned by this call
       
    82      * has equivalent capabilities to any lookup object
       
    83      * supplied by the JVM to the bootstrap method of an
       
    84      * <a href="package-summary.html#indyinsn">invokedynamic instruction</a>
       
    85      * executing in the same caller class {@code C}.
       
    86      * @return a lookup object for the caller of this method, with private access
    75      */
    87      */
    76     @CallerSensitive
    88     @CallerSensitive
    77     public static Lookup lookup() {
    89     public static Lookup lookup() {
    78         return new Lookup(Reflection.getCallerClass());
    90         return new Lookup(Reflection.getCallerClass());
    79     }
    91     }
    83      * It can only be used to create method handles to
    95      * It can only be used to create method handles to
    84      * publicly accessible fields and methods.
    96      * publicly accessible fields and methods.
    85      * <p>
    97      * <p>
    86      * As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
    98      * As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
    87      * of this lookup object will be {@link java.lang.Object}.
    99      * of this lookup object will be {@link java.lang.Object}.
    88      * <p>
   100      *
       
   101      * <p style="font-size:smaller;">
       
   102      * <em>Discussion:</em>
    89      * The lookup class can be changed to any other class {@code C} using an expression of the form
   103      * The lookup class can be changed to any other class {@code C} using an expression of the form
    90      * {@linkplain Lookup#in <code>publicLookup().in(C.class)</code>}.
   104      * {@link Lookup#in publicLookup().in(C.class)}.
    91      * Since all classes have equal access to public names,
   105      * Since all classes have equal access to public names,
    92      * such a change would confer no new access rights.
   106      * such a change would confer no new access rights.
    93      * A public lookup object is always subject to
   107      * A public lookup object is always subject to
    94      * <a href="MethodHandles.Lookup.html#secmgr">security manager checks</a>.
   108      * <a href="MethodHandles.Lookup.html#secmgr">security manager checks</a>.
    95      * Also, it cannot access
   109      * Also, it cannot access
    99     public static Lookup publicLookup() {
   113     public static Lookup publicLookup() {
   100         return Lookup.PUBLIC_LOOKUP;
   114         return Lookup.PUBLIC_LOOKUP;
   101     }
   115     }
   102 
   116 
   103     /**
   117     /**
   104      * Performs an unchecked "crack" of a direct method handle.
   118      * Performs an unchecked "crack" of a
       
   119      * <a href="MethodHandleInfo.html#directmh">direct method handle</a>.
   105      * The result is as if the user had obtained a lookup object capable enough
   120      * The result is as if the user had obtained a lookup object capable enough
   106      * to crack the target method handle, called
   121      * to crack the target method handle, called
   107      * {@link java.lang.invoke.MethodHandles.Lookup#revealDirect Lookup.revealDirect}
   122      * {@link java.lang.invoke.MethodHandles.Lookup#revealDirect Lookup.revealDirect}
   108      * on the target to obtain its symbolic reference, and then called
   123      * on the target to obtain its symbolic reference, and then called
   109      * {@link java.lang.invoke.MethodHandleInfo#reflectAs MethodHandleInfo.reflectAs}
   124      * {@link java.lang.invoke.MethodHandleInfo#reflectAs MethodHandleInfo.reflectAs}
   152      * even private ones.
   167      * even private ones.
   153      *
   168      *
   154      * <h1><a name="lookups"></a>Lookup Factory Methods</h1>
   169      * <h1><a name="lookups"></a>Lookup Factory Methods</h1>
   155      * The factory methods on a {@code Lookup} object correspond to all major
   170      * The factory methods on a {@code Lookup} object correspond to all major
   156      * use cases for methods, constructors, and fields.
   171      * use cases for methods, constructors, and fields.
       
   172      * Each method handle created by a factory method is the functional
       
   173      * equivalent of a particular <em>bytecode behavior</em>.
       
   174      * (Bytecode behaviors are described in section 5.4.3.5 of the Java Virtual Machine Specification.)
   157      * Here is a summary of the correspondence between these factory methods and
   175      * Here is a summary of the correspondence between these factory methods and
   158      * the behavior the resulting method handles:
   176      * the behavior the resulting method handles:
   159      * <table border=1 cellpadding=5 summary="lookup method behaviors">
   177      * <table border=1 cellpadding=5 summary="lookup method behaviors">
   160      * <tr><th>lookup expression</th><th>member</th><th>behavior</th></tr>
   178      * <tr>
       
   179      *     <th><a name="equiv"></a>lookup expression</th>
       
   180      *     <th>member</th>
       
   181      *     <th>bytecode behavior</th>
       
   182      * </tr>
   161      * <tr>
   183      * <tr>
   162      *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td>
   184      *     <td>{@link java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td>
   163      *     <td>{@code FT f;}</td><td>{@code (T) this.f;}</td>
   185      *     <td>{@code FT f;}</td><td>{@code (T) this.f;}</td>
   164      * </tr>
   186      * </tr>
   165      * <tr>
   187      * <tr>
   231      * for reflective objects corresponding to the given members.
   253      * for reflective objects corresponding to the given members.
   232      * <p>
   254      * <p>
   233      * In cases where the given member is of variable arity (i.e., a method or constructor)
   255      * In cases where the given member is of variable arity (i.e., a method or constructor)
   234      * the returned method handle will also be of {@linkplain MethodHandle#asVarargsCollector variable arity}.
   256      * the returned method handle will also be of {@linkplain MethodHandle#asVarargsCollector variable arity}.
   235      * In all other cases, the returned method handle will be of fixed arity.
   257      * In all other cases, the returned method handle will be of fixed arity.
   236      * <p>
   258      * <p style="font-size:smaller;">
       
   259      * <em>Discussion:</em>
   237      * The equivalence between looked-up method handles and underlying
   260      * The equivalence between looked-up method handles and underlying
   238      * class members can break down in a few ways:
   261      * class members and bytecode behaviors
   239      * <ul>
   262      * can break down in a few ways:
       
   263      * <ul style="font-size:smaller;">
   240      * <li>If {@code C} is not symbolically accessible from the lookup class's loader,
   264      * <li>If {@code C} is not symbolically accessible from the lookup class's loader,
   241      * the lookup can still succeed, even when there is no equivalent
   265      * the lookup can still succeed, even when there is no equivalent
   242      * Java expression or bytecoded constant.
   266      * Java expression or bytecoded constant.
   243      * <li>Likewise, if {@code T} or {@code MT}
   267      * <li>Likewise, if {@code T} or {@code MT}
   244      * is not symbolically accessible from the lookup class's loader,
   268      * is not symbolically accessible from the lookup class's loader,
   278      * original lookup class.
   302      * original lookup class.
   279      * <p>
   303      * <p>
   280      * A lookup can fail, because
   304      * A lookup can fail, because
   281      * the containing class is not accessible to the lookup class, or
   305      * the containing class is not accessible to the lookup class, or
   282      * because the desired class member is missing, or because the
   306      * because the desired class member is missing, or because the
   283      * desired class member is not accessible to the lookup class.
   307      * desired class member is not accessible to the lookup class, or
       
   308      * because the lookup object is not trusted enough to access the member.
   284      * In any of these cases, a {@code ReflectiveOperationException} will be
   309      * In any of these cases, a {@code ReflectiveOperationException} will be
   285      * thrown from the attempted lookup.  The exact class will be one of
   310      * thrown from the attempted lookup.  The exact class will be one of
   286      * the following:
   311      * the following:
   287      * <ul>
   312      * <ul>
   288      * <li>NoSuchMethodException &mdash; if a method is requested but does not exist
   313      * <li>NoSuchMethodException &mdash; if a method is requested but does not exist
   289      * <li>NoSuchFieldException &mdash; if a field is requested but does not exist
   314      * <li>NoSuchFieldException &mdash; if a field is requested but does not exist
   290      * <li>IllegalAccessException &mdash; if the member exists but an access check fails
   315      * <li>IllegalAccessException &mdash; if the member exists but an access check fails
   291      * </ul>
   316      * </ul>
   292      * <p>
   317      * <p>
   293      * In general, the conditions under which a method handle may be
   318      * In general, the conditions under which a method handle may be
   294      * looked up for a method {@code M} are exactly equivalent to the conditions
   319      * looked up for a method {@code M} are no more restrictive than the conditions
   295      * under which the lookup class could have compiled and resolved a call to {@code M}.
   320      * under which the lookup class could have compiled, verified, and resolved a call to {@code M}.
   296      * Where the JVM would raise exceptions like {@code NoSuchMethodError},
   321      * Where the JVM would raise exceptions like {@code NoSuchMethodError},
   297      * a method handle lookup will generally raise a corresponding
   322      * a method handle lookup will generally raise a corresponding
   298      * checked exception, such as {@code NoSuchMethodException}.
   323      * checked exception, such as {@code NoSuchMethodException}.
   299      * And the effect of invoking the method handle resulting from the lookup
   324      * And the effect of invoking the method handle resulting from the lookup
   300      * is exactly equivalent to executing the compiled and resolved call to {@code M}.
   325      * is <a href="MethodHandles.Lookup.html#equiv">exactly equivalent</a>
       
   326      * to executing the compiled, verified, and resolved call to {@code M}.
   301      * The same point is true of fields and constructors.
   327      * The same point is true of fields and constructors.
       
   328      * <p style="font-size:smaller;">
       
   329      * <em>Discussion:</em>
       
   330      * Access checks only apply to named and reflected methods,
       
   331      * constructors, and fields.
       
   332      * Other method handle creation methods, such as
       
   333      * {@link MethodHandle#asType MethodHandle.asType},
       
   334      * do not require any access checks, and are used
       
   335      * independently of any {@code Lookup} object.
   302      * <p>
   336      * <p>
   303      * If the desired member is {@code protected}, the usual JVM rules apply,
   337      * If the desired member is {@code protected}, the usual JVM rules apply,
   304      * including the requirement that the lookup class must be either be in the
   338      * including the requirement that the lookup class must be either be in the
   305      * same package as the desired member, or must inherit that member.
   339      * same package as the desired member, or must inherit that member.
   306      * (See the Java Virtual Machine Specification, sections 4.9.2, 5.4.3.5, and 6.4.)
   340      * (See the Java Virtual Machine Specification, sections 4.9.2, 5.4.3.5, and 6.4.)
   309      * to objects of the lookup class or one of its subclasses.
   343      * to objects of the lookup class or one of its subclasses.
   310      * This requirement is enforced by narrowing the type of the leading
   344      * This requirement is enforced by narrowing the type of the leading
   311      * {@code this} parameter from {@code C}
   345      * {@code this} parameter from {@code C}
   312      * (which will necessarily be a superclass of the lookup class)
   346      * (which will necessarily be a superclass of the lookup class)
   313      * to the lookup class itself.
   347      * to the lookup class itself.
       
   348      * <p>
       
   349      * The JVM imposes a similar requirement on {@code invokespecial} instruction,
       
   350      * that the receiver argument must match both the resolved method <em>and</em>
       
   351      * the current class.  Again, this requirement is enforced by narrowing the
       
   352      * type of the leading parameter to the resulting method handle.
       
   353      * (See the Java Virtual Machine Specification, section 4.10.1.9.)
   314      * <p>
   354      * <p>
   315      * The JVM represents constructors and static initializer blocks as internal methods
   355      * The JVM represents constructors and static initializer blocks as internal methods
   316      * with special names ({@code "<init>"} and {@code "<clinit>"}).
   356      * with special names ({@code "<init>"} and {@code "<clinit>"}).
   317      * The internal syntax of invocation instructions allows them to refer to such internal
   357      * The internal syntax of invocation instructions allows them to refer to such internal
   318      * methods as if they were normal methods, but the JVM bytecode verifier rejects them.
   358      * methods as if they were normal methods, but the JVM bytecode verifier rejects them.
   329      * {@code C.E} would be unable to those private members.
   369      * {@code C.E} would be unable to those private members.
   330      * A workaround for this limitation is the {@link Lookup#in Lookup.in} method,
   370      * A workaround for this limitation is the {@link Lookup#in Lookup.in} method,
   331      * which can transform a lookup on {@code C.E} into one on any of those other
   371      * which can transform a lookup on {@code C.E} into one on any of those other
   332      * classes, without special elevation of privilege.
   372      * classes, without special elevation of privilege.
   333      * <p>
   373      * <p>
       
   374      * The accesses permitted to a given lookup object may be limited,
       
   375      * according to its set of {@link #lookupModes lookupModes},
       
   376      * to a subset of members normally accessible to the lookup class.
       
   377      * For example, the {@link MethodHandles#publicLookup publicLookup}
       
   378      * method produces a lookup object which is only allowed to access
       
   379      * public members in public classes.
       
   380      * The caller sensitive method {@link MethodHandles#lookup lookup}
       
   381      * produces a lookup object with full capabilities relative to
       
   382      * its caller class, to emulate all supported bytecode behaviors.
       
   383      * Also, the {@link Lookup#in Lookup.in} method may produce a lookup object
       
   384      * with fewer access modes than the original lookup object.
       
   385      *
       
   386      * <p style="font-size:smaller;">
       
   387      * <a name="privacc"></a>
       
   388      * <em>Discussion of private access:</em>
       
   389      * We say that a lookup has <em>private access</em>
       
   390      * if its {@linkplain #lookupModes lookup modes}
       
   391      * include the possibility of accessing {@code private} members.
       
   392      * As documented in the relevant methods elsewhere,
       
   393      * only lookups with private access possess the following capabilities:
       
   394      * <ul style="font-size:smaller;">
       
   395      * <li>access private fields, methods, and constructors of the lookup class
       
   396      * <li>create method handles which invoke <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a> methods,
       
   397      *     such as {@code Class.forName}
       
   398      * <li>create method handles which {@link Lookup#findSpecial emulate invokespecial} instructions
       
   399      * <li>avoid <a href="MethodHandles.Lookup.html#secmgr">package access checks</a>
       
   400      *     for classes accessible to the lookup class
       
   401      * <li>create {@link Lookup#in delegated lookup objects} which have private access to other classes
       
   402      *     within the same package member
       
   403      * </ul>
       
   404      * <p style="font-size:smaller;">
       
   405      * Each of these permissions is a consequence of the fact that a lookup object
       
   406      * with private access can be securely traced back to an originating class,
       
   407      * whose <a href="MethodHandles.Lookup.html#equiv">bytecode behaviors</a> and Java language access permissions
       
   408      * can be reliably determined and emulated by method handles.
       
   409      *
       
   410      * <h1><a name="secmgr"></a>Security manager interactions</h1>
   334      * Although bytecode instructions can only refer to classes in
   411      * Although bytecode instructions can only refer to classes in
   335      * a related class loader, this API can search for methods in any
   412      * a related class loader, this API can search for methods in any
   336      * class, as long as a reference to its {@code Class} object is
   413      * class, as long as a reference to its {@code Class} object is
   337      * available.  Such cross-loader references are also possible with the
   414      * available.  Such cross-loader references are also possible with the
   338      * Core Reflection API, and are impossible to bytecode instructions
   415      * Core Reflection API, and are impossible to bytecode instructions
   341      * to allow applications to check such cross-loader references.
   418      * to allow applications to check such cross-loader references.
   342      * These checks apply to both the {@code MethodHandles.Lookup} API
   419      * These checks apply to both the {@code MethodHandles.Lookup} API
   343      * and the Core Reflection API
   420      * and the Core Reflection API
   344      * (as found on {@link java.lang.Class Class}).
   421      * (as found on {@link java.lang.Class Class}).
   345      * <p>
   422      * <p>
   346      * Access checks only apply to named and reflected methods,
       
   347      * constructors, and fields.
       
   348      * Other method handle creation methods, such as
       
   349      * {@link MethodHandle#asType MethodHandle.asType},
       
   350      * do not require any access checks, and are done
       
   351      * with static methods of {@link MethodHandles},
       
   352      * independently of any {@code Lookup} object.
       
   353      *
       
   354      * <h1>Security manager interactions</h1>
       
   355      * <a name="secmgr"></a>
       
   356      * If a security manager is present, member lookups are subject to
   423      * If a security manager is present, member lookups are subject to
   357      * additional checks.
   424      * additional checks.
   358      * From one to three calls are made to the security manager.
   425      * From one to three calls are made to the security manager.
   359      * Any of these calls can refuse access by throwing a
   426      * Any of these calls can refuse access by throwing a
   360      * {@link java.lang.SecurityException SecurityException}.
   427      * {@link java.lang.SecurityException SecurityException}.
   363      * {@code refc} as the containing class in which the member
   430      * {@code refc} as the containing class in which the member
   364      * is being sought, and {@code defc} as the class in which the
   431      * is being sought, and {@code defc} as the class in which the
   365      * member is actually defined.
   432      * member is actually defined.
   366      * The value {@code lookc} is defined as <em>not present</em>
   433      * The value {@code lookc} is defined as <em>not present</em>
   367      * if the current lookup object does not have
   434      * if the current lookup object does not have
   368      * {@linkplain java.lang.invoke.MethodHandles.Lookup#PRIVATE private access}.
   435      * <a href="MethodHandles.Lookup.html#privacc">private access</a>.
   369      * The calls are made according to the following rules:
   436      * The calls are made according to the following rules:
   370      * <ul>
   437      * <ul>
   371      * <li>If {@code lookc} is not present, or if its class loader is not
   438      * <li><b>Step 1:</b>
       
   439      *     If {@code lookc} is not present, or if its class loader is not
   372      *     the same as or an ancestor of the class loader of {@code refc},
   440      *     the same as or an ancestor of the class loader of {@code refc},
   373      *     then {@link SecurityManager#checkPackageAccess
   441      *     then {@link SecurityManager#checkPackageAccess
   374      *     smgr.checkPackageAccess(refcPkg)} is called,
   442      *     smgr.checkPackageAccess(refcPkg)} is called,
   375      *     where {@code refcPkg} is the package of {@code refc}.
   443      *     where {@code refcPkg} is the package of {@code refc}.
   376      * <li>If the retrieved member is not public and
   444      * <li><b>Step 2:</b>
       
   445      *     If the retrieved member is not public and
   377      *     {@code lookc} is not present, then
   446      *     {@code lookc} is not present, then
   378      *     {@link SecurityManager#checkPermission smgr.checkPermission}
   447      *     {@link SecurityManager#checkPermission smgr.checkPermission}
   379      *     with {@code RuntimePermission("accessDeclaredMembers")} is called.
   448      *     with {@code RuntimePermission("accessDeclaredMembers")} is called.
   380      * <li>If the retrieved member is not public,
   449      * <li><b>Step 3:</b>
       
   450      *     If the retrieved member is not public,
   381      *     and if {@code lookc} is not present,
   451      *     and if {@code lookc} is not present,
   382      *     and if {@code defc} and {@code refc} are different,
   452      *     and if {@code defc} and {@code refc} are different,
   383      *     then {@link SecurityManager#checkPackageAccess
   453      *     then {@link SecurityManager#checkPackageAccess
   384      *     smgr.checkPackageAccess(defcPkg)} is called,
   454      *     smgr.checkPackageAccess(defcPkg)} is called,
   385      *     where {@code defcPkg} is the package of {@code defc}.
   455      *     where {@code defcPkg} is the package of {@code defc}.
   406      * differently behaving method handles.
   476      * differently behaving method handles.
   407      * <p>
   477      * <p>
   408      * In cases where the lookup object is
   478      * In cases where the lookup object is
   409      * {@link MethodHandles#publicLookup() publicLookup()},
   479      * {@link MethodHandles#publicLookup() publicLookup()},
   410      * or some other lookup object without
   480      * or some other lookup object without
   411      * {@linkplain java.lang.invoke.MethodHandles.Lookup#PRIVATE private access},
   481      * <a href="MethodHandles.Lookup.html#privacc">private access</a>,
   412      * the lookup class is disregarded.
   482      * the lookup class is disregarded.
   413      * In such cases, no caller-sensitive method handle can be created,
   483      * In such cases, no caller-sensitive method handle can be created,
   414      * access is forbidden, and the lookup fails with an
   484      * access is forbidden, and the lookup fails with an
   415      * {@code IllegalAccessException}.
   485      * {@code IllegalAccessException}.
       
   486      * <p style="font-size:smaller;">
       
   487      * <em>Discussion:</em>
       
   488      * For example, the caller-sensitive method
       
   489      * {@link java.lang.Class#forName(String) Class.forName(x)}
       
   490      * can return varying classes or throw varying exceptions,
       
   491      * depending on the class loader of the class that calls it.
       
   492      * A public lookup of {@code Class.forName} will fail, because
       
   493      * there is no reasonable way to determine its bytecode behavior.
       
   494      * <p style="font-size:smaller;">
       
   495      * If an application caches method handles for broad sharing,
       
   496      * it should use {@code publicLookup()} to create them.
       
   497      * If there is a lookup of {@code Class.forName}, it will fail,
       
   498      * and the application must take appropriate action in that case.
       
   499      * It may be that a later lookup, perhaps during the invocation of a
       
   500      * bootstrap method, can incorporate the specific identity
       
   501      * of the caller, making the method accessible.
       
   502      * <p style="font-size:smaller;">
       
   503      * The function {@code MethodHandles.lookup} is caller sensitive
       
   504      * so that there can be a secure foundation for lookups.
       
   505      * Nearly all other methods in the JSR 292 API rely on lookup
       
   506      * objects to check access requests.
   416      */
   507      */
   417     public static final
   508     public static final
   418     class Lookup {
   509     class Lookup {
   419         /** The class on behalf of whom the lookup is being performed. */
   510         /** The class on behalf of whom the lookup is being performed. */
   420         private final Class<?> lookupClass;
   511         private final Class<?> lookupClass;
   637          * Produces a method handle for a static method.
   728          * Produces a method handle for a static method.
   638          * The type of the method handle will be that of the method.
   729          * The type of the method handle will be that of the method.
   639          * (Since static methods do not take receivers, there is no
   730          * (Since static methods do not take receivers, there is no
   640          * additional receiver argument inserted into the method handle type,
   731          * additional receiver argument inserted into the method handle type,
   641          * as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
   732          * as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
   642          * The method and all its argument types must be accessible to the lookup class.
   733          * The method and all its argument types must be accessible to the lookup object.
   643          * <p>
   734          * <p>
   644          * The returned method handle will have
   735          * The returned method handle will have
   645          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   736          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   646          * the method's variable arity modifier bit ({@code 0x0080}) is set.
   737          * the method's variable arity modifier bit ({@code 0x0080}) is set.
   647          * <p>
   738          * <p>
   677 
   768 
   678         /**
   769         /**
   679          * Produces a method handle for a virtual method.
   770          * Produces a method handle for a virtual method.
   680          * The type of the method handle will be that of the method,
   771          * The type of the method handle will be that of the method,
   681          * with the receiver type (usually {@code refc}) prepended.
   772          * with the receiver type (usually {@code refc}) prepended.
   682          * The method and all its argument types must be accessible to the lookup class.
   773          * The method and all its argument types must be accessible to the lookup object.
   683          * <p>
   774          * <p>
   684          * When called, the handle will treat the first argument as a receiver
   775          * When called, the handle will treat the first argument as a receiver
   685          * and dispatch on the receiver's type to determine which method
   776          * and dispatch on the receiver's type to determine which method
   686          * implementation to enter.
   777          * implementation to enter.
   687          * (The dispatching action is identical with that performed by an
   778          * (The dispatching action is identical with that performed by an
   694          * <p>
   785          * <p>
   695          * The returned method handle will have
   786          * The returned method handle will have
   696          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   787          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   697          * the method's variable arity modifier bit ({@code 0x0080}) is set.
   788          * the method's variable arity modifier bit ({@code 0x0080}) is set.
   698          * <p>
   789          * <p>
   699          * Because of the general equivalence between {@code invokevirtual}
   790          * Because of the general <a href="MethodHandles.Lookup.html#equiv">equivalence</a> between {@code invokevirtual}
   700          * instructions and method handles produced by {@code findVirtual},
   791          * instructions and method handles produced by {@code findVirtual},
   701          * if the class is {@code MethodHandle} and the name string is
   792          * if the class is {@code MethodHandle} and the name string is
   702          * {@code invokeExact} or {@code invoke}, the resulting
   793          * {@code invokeExact} or {@code invoke}, the resulting
   703          * method handle is equivalent to one produced by
   794          * method handle is equivalent to one produced by
   704          * {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or
   795          * {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or
   768         /**
   859         /**
   769          * Produces a method handle which creates an object and initializes it, using
   860          * Produces a method handle which creates an object and initializes it, using
   770          * the constructor of the specified type.
   861          * the constructor of the specified type.
   771          * The parameter types of the method handle will be those of the constructor,
   862          * The parameter types of the method handle will be those of the constructor,
   772          * while the return type will be a reference to the constructor's class.
   863          * while the return type will be a reference to the constructor's class.
   773          * The constructor and all its argument types must be accessible to the lookup class.
   864          * The constructor and all its argument types must be accessible to the lookup object.
   774          * <p>
   865          * <p>
   775          * <em>(Note:  The requested type must have a return type of {@code void}.
   866          * The requested type must have a return type of {@code void}.
   776          * This is consistent with the JVM's treatment of constructor type descriptors.)</em>
   867          * (This is consistent with the JVM's treatment of constructor type descriptors.)
   777          * <p>
   868          * <p>
   778          * The returned method handle will have
   869          * The returned method handle will have
   779          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   870          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   780          * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
   871          * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
   781          * <p>
   872          * <p>
   815             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
   906             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
   816             return getDirectConstructor(refc, ctor);
   907             return getDirectConstructor(refc, ctor);
   817         }
   908         }
   818 
   909 
   819         /**
   910         /**
   820          * Produces an early-bound method handle for a virtual method,
   911          * Produces an early-bound method handle for a virtual method.
   821          * as if called from an {@code invokespecial}
   912          * It will bypass checks for overriding methods on the receiver,
   822          * instruction from {@code caller}.
   913          * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
       
   914          * instruction from within the explicitly specified {@code specialCaller}.
   823          * The type of the method handle will be that of the method,
   915          * The type of the method handle will be that of the method,
   824          * with a suitably restricted receiver type (such as {@code caller}) prepended.
   916          * with a suitably restricted receiver type prepended.
       
   917          * (The receiver type will be {@code specialCaller} or a subtype.)
   825          * The method and all its argument types must be accessible
   918          * The method and all its argument types must be accessible
   826          * to the caller.
   919          * to the lookup object.
   827          * <p>
   920          * <p>
   828          * When called, the handle will treat the first argument as a receiver,
   921          * Before method resolution,
   829          * but will not dispatch on the receiver's type.
   922          * if the explicitly specified caller class is not identical with the
   830          * (This direct invocation action is identical with that performed by an
   923          * lookup class, or if this lookup object does not have
   831          * {@code invokespecial} instruction.)
   924          * <a href="MethodHandles.Lookup.html#privacc">private access</a>
   832          * <p>
       
   833          * If the explicitly specified caller class is not identical with the
       
   834          * lookup class, or if this lookup object does not have private access
       
   835          * privileges, the access fails.
   925          * privileges, the access fails.
   836          * <p>
   926          * <p>
   837          * The returned method handle will have
   927          * The returned method handle will have
   838          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   928          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
   839          * the method's variable arity modifier bit ({@code 0x0080}) is set.
   929          * the method's variable arity modifier bit ({@code 0x0080}) is set.
   840          * <p>
   930          * <p style="font-size:smaller;">
   841          * <em>(Note:  JVM internal methods named {@code "<init>"} are not visible to this API,
   931          * <em>(Note:  JVM internal methods named {@code "<init>"} are not visible to this API,
   842          * even though the {@code invokespecial} instruction can refer to them
   932          * even though the {@code invokespecial} instruction can refer to them
   843          * in special circumstances.  Use {@link #findConstructor findConstructor}
   933          * in special circumstances.  Use {@link #findConstructor findConstructor}
   844          * to access instance initialization methods in a safe manner.)</em>
   934          * to access instance initialization methods in a safe manner.)</em>
   845          * <p><b>Example:</b>
   935          * <p><b>Example:</b>
   991 
  1081 
   992         /**
  1082         /**
   993          * Produces an early-bound method handle for a non-static method.
  1083          * Produces an early-bound method handle for a non-static method.
   994          * The receiver must have a supertype {@code defc} in which a method
  1084          * The receiver must have a supertype {@code defc} in which a method
   995          * of the given name and type is accessible to the lookup class.
  1085          * of the given name and type is accessible to the lookup class.
   996          * The method and all its argument types must be accessible to the lookup class.
  1086          * The method and all its argument types must be accessible to the lookup object.
   997          * The type of the method handle will be that of the method,
  1087          * The type of the method handle will be that of the method,
   998          * without any insertion of an additional receiver parameter.
  1088          * without any insertion of an additional receiver parameter.
   999          * The given receiver will be bound into the method handle,
  1089          * The given receiver will be bound into the method handle,
  1000          * so that every call to the method handle will invoke the
  1090          * so that every call to the method handle will invoke the
  1001          * requested method on the given receiver.
  1091          * requested method on the given receiver.
  1006          * <em>and</em> the trailing array argument is not the only argument.
  1096          * <em>and</em> the trailing array argument is not the only argument.
  1007          * (If the trailing array argument is the only argument,
  1097          * (If the trailing array argument is the only argument,
  1008          * the given receiver value will be bound to it.)
  1098          * the given receiver value will be bound to it.)
  1009          * <p>
  1099          * <p>
  1010          * This is equivalent to the following code:
  1100          * This is equivalent to the following code:
  1011          * <blockquote><pre>
  1101          * <blockquote><pre>{@code
  1012 import static java.lang.invoke.MethodHandles.*;
  1102 import static java.lang.invoke.MethodHandles.*;
  1013 import static java.lang.invoke.MethodType.*;
  1103 import static java.lang.invoke.MethodType.*;
  1014 ...
  1104 ...
  1015 MethodHandle mh0 = lookup().{@link #findVirtual findVirtual}(defc, name, type);
  1105 MethodHandle mh0 = lookup().findVirtual(defc, name, type);
  1016 MethodHandle mh1 = mh0.{@link MethodHandle#bindTo bindTo}(receiver);
  1106 MethodHandle mh1 = mh0.bindTo(receiver);
  1017 MethodType mt1 = mh1.type();
  1107 MethodType mt1 = mh1.type();
  1018 if (mh0.isVarargsCollector())
  1108 if (mh0.isVarargsCollector())
  1019   mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
  1109   mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
  1020 return mh1;
  1110 return mh1;
  1021          * </pre></blockquote>
  1111          * }</pre></blockquote>
  1022          * where {@code defc} is either {@code receiver.getClass()} or a super
  1112          * where {@code defc} is either {@code receiver.getClass()} or a super
  1023          * type of that class, in which the requested method is accessible
  1113          * type of that class, in which the requested method is accessible
  1024          * to the lookup class.
  1114          * to the lookup class.
  1025          * (Note that {@code bindTo} does not preserve variable arity.)
  1115          * (Note that {@code bindTo} does not preserve variable arity.)
  1026          * @param receiver the object from which the method is accessed
  1116          * @param receiver the object from which the method is accessed
  1032          *                                or if the method's variable arity modifier bit
  1122          *                                or if the method's variable arity modifier bit
  1033          *                                is set and {@code asVarargsCollector} fails
  1123          *                                is set and {@code asVarargsCollector} fails
  1034          * @exception SecurityException if a security manager is present and it
  1124          * @exception SecurityException if a security manager is present and it
  1035          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
  1125          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
  1036          * @throws NullPointerException if any argument is null
  1126          * @throws NullPointerException if any argument is null
       
  1127          * @see MethodHandle#bindTo
       
  1128          * @see #findVirtual
  1037          */
  1129          */
  1038         public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
  1130         public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
  1039             Class<? extends Object> refc = receiver.getClass(); // may get NPE
  1131             Class<? extends Object> refc = receiver.getClass(); // may get NPE
  1040             MemberName method = resolveOrFail(REF_invokeSpecial, refc, name, type);
  1132             MemberName method = resolveOrFail(REF_invokeSpecial, refc, name, type);
  1041             MethodHandle mh = getDirectMethodNoRestrict(REF_invokeSpecial, refc, method, findBoundCallerClass(method));
  1133             MethodHandle mh = getDirectMethodNoRestrict(REF_invokeSpecial, refc, method, findBoundCallerClass(method));
  1042             return mh.bindReceiver(receiver).setVarargs(method);
  1134             return mh.bindReceiver(receiver).setVarargs(method);
  1043         }
  1135         }
  1044 
  1136 
  1045         /**
  1137         /**
  1046          * Makes a direct method handle to <i>m</i>, if the lookup class has permission.
  1138          * Makes a <a href="MethodHandleInfo.html#directmh">direct method handle</a>
       
  1139          * to <i>m</i>, if the lookup class has permission.
  1047          * If <i>m</i> is non-static, the receiver argument is treated as an initial argument.
  1140          * If <i>m</i> is non-static, the receiver argument is treated as an initial argument.
  1048          * If <i>m</i> is virtual, overriding is respected on every call.
  1141          * If <i>m</i> is virtual, overriding is respected on every call.
  1049          * Unlike the Core Reflection API, exceptions are <em>not</em> wrapped.
  1142          * Unlike the Core Reflection API, exceptions are <em>not</em> wrapped.
  1050          * The type of the method handle will be that of the method,
  1143          * The type of the method handle will be that of the method,
  1051          * with the receiver type prepended (but only if it is non-static).
  1144          * with the receiver type prepended (but only if it is non-static).
  1088         }
  1181         }
  1089 
  1182 
  1090         /**
  1183         /**
  1091          * Produces a method handle for a reflected method.
  1184          * Produces a method handle for a reflected method.
  1092          * It will bypass checks for overriding methods on the receiver,
  1185          * It will bypass checks for overriding methods on the receiver,
  1093          * as if by a {@code invokespecial} instruction from within the {@code specialCaller}.
  1186          * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
       
  1187          * instruction from within the explicitly specified {@code specialCaller}.
  1094          * The type of the method handle will be that of the method,
  1188          * The type of the method handle will be that of the method,
  1095          * with the special caller type prepended (and <em>not</em> the receiver of the method).
  1189          * with a suitably restricted receiver type prepended.
       
  1190          * (The receiver type will be {@code specialCaller} or a subtype.)
  1096          * If the method's {@code accessible} flag is not set,
  1191          * If the method's {@code accessible} flag is not set,
  1097          * access checking is performed immediately on behalf of the lookup class,
  1192          * access checking is performed immediately on behalf of the lookup class,
  1098          * as if {@code invokespecial} instruction were being linked.
  1193          * as if {@code invokespecial} instruction were being linked.
       
  1194          * <p>
       
  1195          * Before method resolution,
       
  1196          * if the explicitly specified caller class is not identical with the
       
  1197          * lookup class, or if this lookup object does not have
       
  1198          * <a href="MethodHandles.Lookup.html#privacc">private access</a>
       
  1199          * privileges, the access fails.
  1099          * <p>
  1200          * <p>
  1100          * The returned method handle will have
  1201          * The returned method handle will have
  1101          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
  1202          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
  1102          * the method's variable arity modifier bit ({@code 0x0080}) is set.
  1203          * the method's variable arity modifier bit ({@code 0x0080}) is set.
  1103          * @param m the reflected method
  1204          * @param m the reflected method
  1139          * @throws IllegalAccessException if access checking fails
  1240          * @throws IllegalAccessException if access checking fails
  1140          *                                or if the method's variable arity modifier bit
  1241          *                                or if the method's variable arity modifier bit
  1141          *                                is set and {@code asVarargsCollector} fails
  1242          *                                is set and {@code asVarargsCollector} fails
  1142          * @throws NullPointerException if the argument is null
  1243          * @throws NullPointerException if the argument is null
  1143          */
  1244          */
  1144         @SuppressWarnings("rawtypes")  // Will be Constructor<?> after JSR 292 MR
  1245         public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessException {
  1145         public MethodHandle unreflectConstructor(Constructor c) throws IllegalAccessException {
       
  1146             MemberName ctor = new MemberName(c);
  1246             MemberName ctor = new MemberName(c);
  1147             assert(ctor.isConstructor());
  1247             assert(ctor.isConstructor());
  1148             Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this;
  1248             Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this;
  1149             return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
  1249             return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
  1150         }
  1250         }
  1200         public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
  1300         public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
  1201             return unreflectField(f, true);
  1301             return unreflectField(f, true);
  1202         }
  1302         }
  1203 
  1303 
  1204         /**
  1304         /**
  1205          * Cracks a direct method handle created by this lookup object or a similar one.
  1305          * Cracks a <a href="MethodHandleInfo.html#directmh">direct method handle</a>
       
  1306          * created by this lookup object or a similar one.
  1206          * Security and access checks are performed to ensure that this lookup object
  1307          * Security and access checks are performed to ensure that this lookup object
  1207          * is capable of reproducing the target method handle.
  1308          * is capable of reproducing the target method handle.
  1208          * This means that the cracking may fail if target is a direct method handle
  1309          * This means that the cracking may fail if target is a direct method handle
  1209          * but was created by an unrelated lookup object.
  1310          * but was created by an unrelated lookup object.
  1210          * This can happen if the method handle is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>
  1311          * This can happen if the method handle is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>
  1213          * @return a symbolic reference which can be used to reconstruct this method handle from this lookup object
  1314          * @return a symbolic reference which can be used to reconstruct this method handle from this lookup object
  1214          * @exception SecurityException if a security manager is present and it
  1315          * @exception SecurityException if a security manager is present and it
  1215          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
  1316          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
  1216          * @throws IllegalArgumentException if the target is not a direct method handle or if access checking fails
  1317          * @throws IllegalArgumentException if the target is not a direct method handle or if access checking fails
  1217          * @exception NullPointerException if the target is {@code null}
  1318          * @exception NullPointerException if the target is {@code null}
       
  1319          * @see MethodHandleInfo
  1218          * @since 1.8
  1320          * @since 1.8
  1219          */
  1321          */
  1220         public MethodHandleInfo revealDirect(MethodHandle target) {
  1322         public MethodHandleInfo revealDirect(MethodHandle target) {
  1221             MemberName member = target.internalMemberName();
  1323             MemberName member = target.internalMemberName();
  1222             if (member == null || (!member.isResolved() && !member.isMethodHandleInvoke()))
  1324             if (member == null || (!member.isResolved() && !member.isMethodHandleInvoke()))
  1280             Class<?> caller = lookupClassOrNull();
  1382             Class<?> caller = lookupClassOrNull();
  1281             if (caller != null && !VerifyAccess.isClassAccessible(refc, caller, allowedModes))
  1383             if (caller != null && !VerifyAccess.isClassAccessible(refc, caller, allowedModes))
  1282                 throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this);
  1384                 throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this);
  1283         }
  1385         }
  1284 
  1386 
  1285         /** Check name for an illegal leading "<" character. */
  1387         /** Check name for an illegal leading "&lt;" character. */
  1286         void checkMethodName(byte refKind, String name) throws NoSuchMethodException {
  1388         void checkMethodName(byte refKind, String name) throws NoSuchMethodException {
  1287             if (name.startsWith("<") && refKind != REF_newInvokeSpecial)
  1389             if (name.startsWith("<") && refKind != REF_newInvokeSpecial)
  1288                 throw new NoSuchMethodException("illegal method name: "+name);
  1390                 throw new NoSuchMethodException("illegal method name: "+name);
  1289         }
  1391         }
  1290 
  1392 
  1728      * If, when the invoker is called, the supplied array argument does
  1830      * If, when the invoker is called, the supplied array argument does
  1729      * not have the correct number of elements, the invoker will throw
  1831      * not have the correct number of elements, the invoker will throw
  1730      * an {@link IllegalArgumentException} instead of invoking the target.
  1832      * an {@link IllegalArgumentException} instead of invoking the target.
  1731      * <p>
  1833      * <p>
  1732      * This method is equivalent to the following code (though it may be more efficient):
  1834      * This method is equivalent to the following code (though it may be more efficient):
  1733      * <p><blockquote><pre>
  1835      * <blockquote><pre>{@code
  1734 MethodHandle invoker = MethodHandles.invoker(type);
  1836 MethodHandle invoker = MethodHandles.invoker(type);
  1735 int spreadArgCount = type.parameterCount() - leadingArgCount;
  1837 int spreadArgCount = type.parameterCount() - leadingArgCount;
  1736 invoker = invoker.asSpreader(Object[].class, spreadArgCount);
  1838 invoker = invoker.asSpreader(Object[].class, spreadArgCount);
  1737 return invoker;
  1839 return invoker;
  1738      * </pre></blockquote>
  1840      * }</pre></blockquote>
  1739      * <p>
       
  1740      * This method throws no reflective or security exceptions.
  1841      * This method throws no reflective or security exceptions.
  1741      * @param type the desired target type
  1842      * @param type the desired target type
  1742      * @param leadingArgCount number of fixed arguments, to be passed unchanged to the target
  1843      * @param leadingArgCount number of fixed arguments, to be passed unchanged to the target
  1743      * @return a method handle suitable for invoking any method handle of the given type
  1844      * @return a method handle suitable for invoking any method handle of the given type
  1744      * @throws NullPointerException if {@code type} is null
  1845      * @throws NullPointerException if {@code type} is null
  1760      * The resulting invoker will have a type which is
  1861      * The resulting invoker will have a type which is
  1761      * exactly equal to the desired type, except that it will accept
  1862      * exactly equal to the desired type, except that it will accept
  1762      * an additional leading argument of type {@code MethodHandle}.
  1863      * an additional leading argument of type {@code MethodHandle}.
  1763      * <p>
  1864      * <p>
  1764      * This method is equivalent to the following code (though it may be more efficient):
  1865      * This method is equivalent to the following code (though it may be more efficient):
  1765      * <p><blockquote><pre>
  1866      * {@code publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)}
  1766 publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)
       
  1767      * </pre></blockquote>
       
  1768      *
  1867      *
  1769      * <p style="font-size:smaller;">
  1868      * <p style="font-size:smaller;">
  1770      * <em>Discussion:</em>
  1869      * <em>Discussion:</em>
  1771      * Invoker method handles can be useful when working with variable method handles
  1870      * Invoker method handles can be useful when working with variable method handles
  1772      * of unknown types.
  1871      * of unknown types.
  1777      * (It would not work to call {@code X.invokeExact}, since the type {@code T}
  1876      * (It would not work to call {@code X.invokeExact}, since the type {@code T}
  1778      * is unknown.)
  1877      * is unknown.)
  1779      * If spreading, collecting, or other argument transformations are required,
  1878      * If spreading, collecting, or other argument transformations are required,
  1780      * they can be applied once to the invoker {@code X} and reused on many {@code M}
  1879      * they can be applied once to the invoker {@code X} and reused on many {@code M}
  1781      * method handle values, as long as they are compatible with the type of {@code X}.
  1880      * method handle values, as long as they are compatible with the type of {@code X}.
  1782      * <p>
  1881      * <p style="font-size:smaller;">
  1783      * <em>(Note:  The invoker method is not available via the Core Reflection API.
  1882      * <em>(Note:  The invoker method is not available via the Core Reflection API.
  1784      * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
  1883      * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
  1785      * on the declared {@code invokeExact} or {@code invoke} method will raise an
  1884      * on the declared {@code invokeExact} or {@code invoke} method will raise an
  1786      * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
  1885      * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
  1787      * <p>
  1886      * <p>
  1808      * necessary and box, unbox, or widen primitive values, as if by {@link MethodHandle#asType asType}.
  1907      * necessary and box, unbox, or widen primitive values, as if by {@link MethodHandle#asType asType}.
  1809      * Similarly, the return value will be converted as necessary.
  1908      * Similarly, the return value will be converted as necessary.
  1810      * If the target is a {@linkplain MethodHandle#asVarargsCollector variable arity method handle},
  1909      * If the target is a {@linkplain MethodHandle#asVarargsCollector variable arity method handle},
  1811      * the required arity conversion will be made, again as if by {@link MethodHandle#asType asType}.
  1910      * the required arity conversion will be made, again as if by {@link MethodHandle#asType asType}.
  1812      * <p>
  1911      * <p>
  1813      * A {@linkplain MethodType#genericMethodType general method type},
  1912      * This method is equivalent to the following code (though it may be more efficient):
       
  1913      * {@code publicLookup().findVirtual(MethodHandle.class, "invoke", type)}
       
  1914      * <p style="font-size:smaller;">
       
  1915      * <em>Discussion:</em>
       
  1916      * A {@linkplain MethodType#genericMethodType general method type} is one which
  1814      * mentions only {@code Object} arguments and return values.
  1917      * mentions only {@code Object} arguments and return values.
  1815      * An invoker for such a type is capable of calling any method handle
  1918      * An invoker for such a type is capable of calling any method handle
  1816      * of the same arity as the general type.
  1919      * of the same arity as the general type.
  1817      * <p>
  1920      * <p style="font-size:smaller;">
  1818      * This method is equivalent to the following code (though it may be more efficient):
  1921      * <em>(Note:  The invoker method is not available via the Core Reflection API.
  1819      * <p><blockquote><pre>
  1922      * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
  1820 publicLookup().findVirtual(MethodHandle.class, "invoke", type)
  1923      * on the declared {@code invokeExact} or {@code invoke} method will raise an
  1821      * </pre></blockquote>
  1924      * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
  1822      * <p>
  1925      * <p>
  1823      * This method throws no reflective or security exceptions.
  1926      * This method throws no reflective or security exceptions.
  1824      * @param type the desired target type
  1927      * @param type the desired target type
  1825      * @return a method handle suitable for invoking any method handle convertible to the given type
  1928      * @return a method handle suitable for invoking any method handle convertible to the given type
  1826      * @throws IllegalArgumentException if the resulting method handle's type would have
  1929      * @throws IllegalArgumentException if the resulting method handle's type would have
  2103      * If {@code pos} is zero, the dummy arguments will precede
  2206      * If {@code pos} is zero, the dummy arguments will precede
  2104      * the target's real arguments; if {@code pos} is <i>N</i>
  2207      * the target's real arguments; if {@code pos} is <i>N</i>
  2105      * they will come after.
  2208      * they will come after.
  2106      * <p>
  2209      * <p>
  2107      * <b>Example:</b>
  2210      * <b>Example:</b>
  2108      * <p><blockquote><pre>
  2211      * <p><blockquote><pre>{@code
  2109 import static java.lang.invoke.MethodHandles.*;
  2212 import static java.lang.invoke.MethodHandles.*;
  2110 import static java.lang.invoke.MethodType.*;
  2213 import static java.lang.invoke.MethodType.*;
  2111 ...
  2214 ...
  2112 MethodHandle cat = lookup().findVirtual(String.class,
  2215 MethodHandle cat = lookup().findVirtual(String.class,
  2113   "concat", methodType(String.class, String.class));
  2216   "concat", methodType(String.class, String.class));
  2114 assertEquals("xy", (String) cat.invokeExact("x", "y"));
  2217 assertEquals("xy", (String) cat.invokeExact("x", "y"));
  2115 MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
  2218 MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
  2116 MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
  2219 MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
  2117 assertEquals(bigType, d0.type());
  2220 assertEquals(bigType, d0.type());
  2118 assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
  2221 assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
  2119      * </pre></blockquote>
  2222      * }</pre></blockquote>
  2120      * <p>
  2223      * <p>
  2121      * This method is also equivalent to the following code:
  2224      * This method is also equivalent to the following code:
  2122      * <p><blockquote><pre>
  2225      * <p><blockquote><pre>
  2123      * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}(target, pos, valueTypes.toArray(new Class[0]))
  2226      * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}{@code (target, pos, valueTypes.toArray(new Class[0]))}
  2124      * </pre></blockquote>
  2227      * </pre></blockquote>
  2125      * @param target the method handle to invoke after the arguments are dropped
  2228      * @param target the method handle to invoke after the arguments are dropped
  2126      * @param valueTypes the type(s) of the argument(s) to drop
  2229      * @param valueTypes the type(s) of the argument(s) to drop
  2127      * @param pos position of first argument to drop (zero for the leftmost)
  2230      * @param pos position of first argument to drop (zero for the leftmost)
  2128      * @return a method handle which drops arguments of the given types,
  2231      * @return a method handle which drops arguments of the given types,
  2161      * If {@code pos} is zero, the dummy arguments will precede
  2264      * If {@code pos} is zero, the dummy arguments will precede
  2162      * the target's real arguments; if {@code pos} is <i>N</i>
  2265      * the target's real arguments; if {@code pos} is <i>N</i>
  2163      * they will come after.
  2266      * they will come after.
  2164      * <p>
  2267      * <p>
  2165      * <b>Example:</b>
  2268      * <b>Example:</b>
  2166      * <p><blockquote><pre>
  2269      * <p><blockquote><pre>{@code
  2167 import static java.lang.invoke.MethodHandles.*;
  2270 import static java.lang.invoke.MethodHandles.*;
  2168 import static java.lang.invoke.MethodType.*;
  2271 import static java.lang.invoke.MethodType.*;
  2169 ...
  2272 ...
  2170 MethodHandle cat = lookup().findVirtual(String.class,
  2273 MethodHandle cat = lookup().findVirtual(String.class,
  2171   "concat", methodType(String.class, String.class));
  2274   "concat", methodType(String.class, String.class));
  2176 assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
  2279 assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
  2177 MethodHandle d2 = dropArguments(cat, 2, String.class);
  2280 MethodHandle d2 = dropArguments(cat, 2, String.class);
  2178 assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
  2281 assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
  2179 MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
  2282 MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
  2180 assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
  2283 assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
  2181      * </pre></blockquote>
  2284      * }</pre></blockquote>
  2182      * <p>
  2285      * <p>
  2183      * This method is also equivalent to the following code:
  2286      * This method is also equivalent to the following code:
  2184      * <p><blockquote><pre>
  2287      * <p><blockquote><pre>
  2185      * {@link #dropArguments(MethodHandle,int,List) dropArguments}(target, pos, Arrays.asList(valueTypes))
  2288      * {@link #dropArguments(MethodHandle,int,List) dropArguments}{@code (target, pos, Arrays.asList(valueTypes))}
  2186      * </pre></blockquote>
  2289      * </pre></blockquote>
  2187      * @param target the method handle to invoke after the arguments are dropped
  2290      * @param target the method handle to invoke after the arguments are dropped
  2188      * @param valueTypes the type(s) of the argument(s) to drop
  2291      * @param valueTypes the type(s) of the argument(s) to drop
  2189      * @param pos position of first argument to drop (zero for the leftmost)
  2292      * @param pos position of first argument to drop (zero for the leftmost)
  2190      * @return a method handle which drops arguments of the given types,
  2293      * @return a method handle which drops arguments of the given types,
  2226      * parameter type of the target.
  2329      * parameter type of the target.
  2227      * <p>
  2330      * <p>
  2228      * It is an error if there are elements of {@code filters}
  2331      * It is an error if there are elements of {@code filters}
  2229      * (null or not)
  2332      * (null or not)
  2230      * which do not correspond to argument positions in the target.
  2333      * which do not correspond to argument positions in the target.
  2231      * <b>Example:</b>
  2334      * <p><b>Example:</b>
  2232      * <p><blockquote><pre>
  2335      * <p><blockquote><pre>{@code
  2233 import static java.lang.invoke.MethodHandles.*;
  2336 import static java.lang.invoke.MethodHandles.*;
  2234 import static java.lang.invoke.MethodType.*;
  2337 import static java.lang.invoke.MethodType.*;
  2235 ...
  2338 ...
  2236 MethodHandle cat = lookup().findVirtual(String.class,
  2339 MethodHandle cat = lookup().findVirtual(String.class,
  2237   "concat", methodType(String.class, String.class));
  2340   "concat", methodType(String.class, String.class));
  2242 assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
  2345 assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
  2243 MethodHandle f1 = filterArguments(cat, 1, upcase);
  2346 MethodHandle f1 = filterArguments(cat, 1, upcase);
  2244 assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
  2347 assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
  2245 MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
  2348 MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
  2246 assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
  2349 assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
  2247      * </pre></blockquote>
  2350      * }</pre></blockquote>
  2248      * <p> Here is pseudocode for the resulting adapter:
  2351      * <p> Here is pseudocode for the resulting adapter:
  2249      * <blockquote><pre>
  2352      * <blockquote><pre>{@code
  2250      * V target(P... p, A[i]... a[i], B... b);
  2353      * V target(P... p, A[i]... a[i], B... b);
  2251      * A[i] filter[i](V[i]);
  2354      * A[i] filter[i](V[i]);
  2252      * T adapter(P... p, V[i]... v[i], B... b) {
  2355      * T adapter(P... p, V[i]... v[i], B... b) {
  2253      *   return target(p..., f[i](v[i])..., b...);
  2356      *   return target(p..., f[i](v[i])..., b...);
  2254      * }
  2357      * }
  2255      * </pre></blockquote>
  2358      * }</pre></blockquote>
  2256      *
  2359      *
  2257      * @param target the method handle to invoke after arguments are filtered
  2360      * @param target the method handle to invoke after arguments are filtered
  2258      * @param pos the position of the first argument to filter
  2361      * @param pos the position of the first argument to filter
  2259      * @param filters method handles to call initially on filtered arguments
  2362      * @param filters method handles to call initially on filtered arguments
  2260      * @return method handle which incorporates the specified argument filtering logic
  2363      * @return method handle which incorporates the specified argument filtering logic
  2320      * is supplied by the return value of the filter.
  2423      * is supplied by the return value of the filter.
  2321      * <p>
  2424      * <p>
  2322      * In all cases, {@code pos} must be greater than or equal to zero, and
  2425      * In all cases, {@code pos} must be greater than or equal to zero, and
  2323      * {@code pos} must also be less than or equal to the target's arity.
  2426      * {@code pos} must also be less than or equal to the target's arity.
  2324      * <p><b>Example:</b>
  2427      * <p><b>Example:</b>
  2325      * <p><blockquote><pre>
  2428      * <p><blockquote><pre>{@code
  2326 import static java.lang.invoke.MethodHandles.*;
  2429 import static java.lang.invoke.MethodHandles.*;
  2327 import static java.lang.invoke.MethodType.*;
  2430 import static java.lang.invoke.MethodType.*;
  2328 ...
  2431 ...
  2329 MethodHandle deepToString = publicLookup()
  2432 MethodHandle deepToString = publicLookup()
  2330   .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
  2433   .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
  2345              (String) ts3_ts2_ts1.invokeExact("top", "up", "down", "strange"));
  2448              (String) ts3_ts2_ts1.invokeExact("top", "up", "down", "strange"));
  2346 
  2449 
  2347 MethodHandle ts3_ts2_ts3 = collectArguments(ts3_ts2, 1, ts3);
  2450 MethodHandle ts3_ts2_ts3 = collectArguments(ts3_ts2, 1, ts3);
  2348 assertEquals("[top, [[up, down, strange], charm], bottom]",
  2451 assertEquals("[top, [[up, down, strange], charm], bottom]",
  2349              (String) ts3_ts2_ts3.invokeExact("top", "up", "down", "strange", "charm", "bottom"));
  2452              (String) ts3_ts2_ts3.invokeExact("top", "up", "down", "strange", "charm", "bottom"));
  2350      * </pre></blockquote>
  2453      * }</pre></blockquote>
  2351      * <p> Here is pseudocode for the resulting adapter:
  2454      * <p> Here is pseudocode for the resulting adapter:
  2352      * <blockquote><pre>
  2455      * <blockquote><pre>{@code
  2353      * T target(A...,V,C...);
  2456      * T target(A...,V,C...);
  2354      * V filter(B...);
  2457      * V filter(B...);
  2355      * T adapter(A... a,B... b,C... c) {
  2458      * T adapter(A... a,B... b,C... c) {
  2356      *   V v = filter(b...);
  2459      *   V v = filter(b...);
  2357      *   return target(a...,v,c...);
  2460      *   return target(a...,v,c...);
  2368      * void filter3(B...);
  2471      * void filter3(B...);
  2369      * void adapter3(A... a,B... b,C... c) {
  2472      * void adapter3(A... a,B... b,C... c) {
  2370      *   filter3(b...);
  2473      *   filter3(b...);
  2371      *   return target3(a...,c...);
  2474      *   return target3(a...,c...);
  2372      * }
  2475      * }
  2373      * </pre></blockquote>
  2476      * }</pre></blockquote>
  2374      * <p>
  2477      * <p>
  2375      * A collection adapter {@code collectArguments(mh, 0, coll)} is equivalent to
  2478      * A collection adapter {@code collectArguments(mh, 0, coll)} is equivalent to
  2376      * one which first "folds" the affected arguments, and then drops them, in separate
  2479      * one which first "folds" the affected arguments, and then drops them, in separate
  2377      * steps as follows:
  2480      * steps as follows:
  2378      * <blockquote><pre>{@code
  2481      * <blockquote><pre>{@code
  2424      * The return type of the filter
  2527      * The return type of the filter
  2425      * replaces the return type of the target
  2528      * replaces the return type of the target
  2426      * in the resulting adapted method handle.
  2529      * in the resulting adapted method handle.
  2427      * The argument type of the filter (if any) must be identical to the
  2530      * The argument type of the filter (if any) must be identical to the
  2428      * return type of the target.
  2531      * return type of the target.
  2429      * <b>Example:</b>
  2532      * <p><b>Example:</b>
  2430      * <p><blockquote><pre>
  2533      * <p><blockquote><pre>{@code
  2431 import static java.lang.invoke.MethodHandles.*;
  2534 import static java.lang.invoke.MethodHandles.*;
  2432 import static java.lang.invoke.MethodType.*;
  2535 import static java.lang.invoke.MethodType.*;
  2433 ...
  2536 ...
  2434 MethodHandle cat = lookup().findVirtual(String.class,
  2537 MethodHandle cat = lookup().findVirtual(String.class,
  2435   "concat", methodType(String.class, String.class));
  2538   "concat", methodType(String.class, String.class));
  2436 MethodHandle length = lookup().findVirtual(String.class,
  2539 MethodHandle length = lookup().findVirtual(String.class,
  2437   "length", methodType(int.class));
  2540   "length", methodType(int.class));
  2438 System.out.println((String) cat.invokeExact("x", "y")); // xy
  2541 System.out.println((String) cat.invokeExact("x", "y")); // xy
  2439 MethodHandle f0 = filterReturnValue(cat, length);
  2542 MethodHandle f0 = filterReturnValue(cat, length);
  2440 System.out.println((int) f0.invokeExact("x", "y")); // 2
  2543 System.out.println((int) f0.invokeExact("x", "y")); // 2
  2441      * </pre></blockquote>
  2544      * }</pre></blockquote>
  2442      * <p> Here is pseudocode for the resulting adapter:
  2545      * <p> Here is pseudocode for the resulting adapter:
  2443      * <blockquote><pre>
  2546      * <blockquote><pre>{@code
  2444      * V target(A...);
  2547      * V target(A...);
  2445      * T filter(V);
  2548      * T filter(V);
  2446      * T adapter(A... a) {
  2549      * T adapter(A... a) {
  2447      *   V v = target(a...);
  2550      *   V v = target(a...);
  2448      *   return filter(v);
  2551      *   return filter(v);
  2459      * void filter3(V);
  2562      * void filter3(V);
  2460      * void adapter3(A... a) {
  2563      * void adapter3(A... a) {
  2461      *   V v = target3(a...);
  2564      *   V v = target3(a...);
  2462      *   filter3(v);
  2565      *   filter3(v);
  2463      * }
  2566      * }
  2464      * </pre></blockquote>
  2567      * }</pre></blockquote>
  2465      * @param target the method handle to invoke before filtering the return value
  2568      * @param target the method handle to invoke before filtering the return value
  2466      * @param filter method handle to call on the return value
  2569      * @param filter method handle to call on the return value
  2467      * @return method handle which incorporates the specified return value filtering logic
  2570      * @return method handle which incorporates the specified return value filtering logic
  2468      * @throws NullPointerException if either argument is null
  2571      * @throws NullPointerException if either argument is null
  2469      * @throws IllegalArgumentException if the argument list of {@code filter}
  2572      * @throws IllegalArgumentException if the argument list of {@code filter}
  2515      * that either the combiner or the target does not wish to receive.
  2618      * that either the combiner or the target does not wish to receive.
  2516      * If some of the incoming arguments are destined only for the combiner,
  2619      * If some of the incoming arguments are destined only for the combiner,
  2517      * consider using {@link MethodHandle#asCollector asCollector} instead, since those
  2620      * consider using {@link MethodHandle#asCollector asCollector} instead, since those
  2518      * arguments will not need to be live on the stack on entry to the
  2621      * arguments will not need to be live on the stack on entry to the
  2519      * target.)
  2622      * target.)
  2520      * <b>Example:</b>
  2623      * <p><b>Example:</b>
  2521      * <p><blockquote><pre>
  2624      * <p><blockquote><pre>{@code
  2522 import static java.lang.invoke.MethodHandles.*;
  2625 import static java.lang.invoke.MethodHandles.*;
  2523 import static java.lang.invoke.MethodType.*;
  2626 import static java.lang.invoke.MethodType.*;
  2524 ...
  2627 ...
  2525 MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
  2628 MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
  2526   "println", methodType(void.class, String.class))
  2629   "println", methodType(void.class, String.class))
  2529   "concat", methodType(String.class, String.class));
  2632   "concat", methodType(String.class, String.class));
  2530 assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
  2633 assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
  2531 MethodHandle catTrace = foldArguments(cat, trace);
  2634 MethodHandle catTrace = foldArguments(cat, trace);
  2532 // also prints "boo":
  2635 // also prints "boo":
  2533 assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
  2636 assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
  2534      * </pre></blockquote>
  2637      * }</pre></blockquote>
  2535      * <p> Here is pseudocode for the resulting adapter:
  2638      * <p> Here is pseudocode for the resulting adapter:
  2536      * <blockquote><pre>
  2639      * <blockquote><pre>{@code
  2537      * // there are N arguments in A...
  2640      * // there are N arguments in A...
  2538      * T target(V, A[N]..., B...);
  2641      * T target(V, A[N]..., B...);
  2539      * V combiner(A...);
  2642      * V combiner(A...);
  2540      * T adapter(A... a, B... b) {
  2643      * T adapter(A... a, B... b) {
  2541      *   V v = combiner(a...);
  2644      *   V v = combiner(a...);
  2546      * void combiner2(A...);
  2649      * void combiner2(A...);
  2547      * T adapter2(A... a, B... b) {
  2650      * T adapter2(A... a, B... b) {
  2548      *   combiner2(a...);
  2651      *   combiner2(a...);
  2549      *   return target2(a..., b...);
  2652      *   return target2(a..., b...);
  2550      * }
  2653      * }
  2551      * </pre></blockquote>
  2654      * }</pre></blockquote>
  2552      * @param target the method handle to invoke after arguments are combined
  2655      * @param target the method handle to invoke after arguments are combined
  2553      * @param combiner method handle to call initially on the incoming arguments
  2656      * @param combiner method handle to call initially on the incoming arguments
  2554      * @return method handle which incorporates the specified argument folding logic
  2657      * @return method handle which incorporates the specified argument folding logic
  2555      * @throws NullPointerException if either argument is null
  2658      * @throws NullPointerException if either argument is null
  2556      * @throws IllegalArgumentException if {@code combiner}'s return type
  2659      * @throws IllegalArgumentException if {@code combiner}'s return type
  2589      * All three method handles must have the same corresponding
  2692      * All three method handles must have the same corresponding
  2590      * argument and return types, except that the return type
  2693      * argument and return types, except that the return type
  2591      * of the test must be boolean, and the test is allowed
  2694      * of the test must be boolean, and the test is allowed
  2592      * to have fewer arguments than the other two method handles.
  2695      * to have fewer arguments than the other two method handles.
  2593      * <p> Here is pseudocode for the resulting adapter:
  2696      * <p> Here is pseudocode for the resulting adapter:
  2594      * <blockquote><pre>
  2697      * <blockquote><pre>{@code
  2595      * boolean test(A...);
  2698      * boolean test(A...);
  2596      * T target(A...,B...);
  2699      * T target(A...,B...);
  2597      * T fallback(A...,B...);
  2700      * T fallback(A...,B...);
  2598      * T adapter(A... a,B... b) {
  2701      * T adapter(A... a,B... b) {
  2599      *   if (test(a...))
  2702      *   if (test(a...))
  2600      *     return target(a..., b...);
  2703      *     return target(a..., b...);
  2601      *   else
  2704      *   else
  2602      *     return fallback(a..., b...);
  2705      *     return fallback(a..., b...);
  2603      * }
  2706      * }
  2604      * </pre></blockquote>
  2707      * }</pre></blockquote>
  2605      * Note that the test arguments ({@code a...} in the pseudocode) cannot
  2708      * Note that the test arguments ({@code a...} in the pseudocode) cannot
  2606      * be modified by execution of the test, and so are passed unchanged
  2709      * be modified by execution of the test, and so are passed unchanged
  2607      * from the caller to the target or fallback as appropriate.
  2710      * from the caller to the target or fallback as appropriate.
  2608      * @param test method handle used for test, must return boolean
  2711      * @param test method handle used for test, must return boolean
  2609      * @param target method handle to call if test passes
  2712      * @param target method handle to call if test passes
  2651      * The target and handler must have the same corresponding
  2754      * The target and handler must have the same corresponding
  2652      * argument and return types, except that handler may omit trailing arguments
  2755      * argument and return types, except that handler may omit trailing arguments
  2653      * (similarly to the predicate in {@link #guardWithTest guardWithTest}).
  2756      * (similarly to the predicate in {@link #guardWithTest guardWithTest}).
  2654      * Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
  2757      * Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
  2655      * <p> Here is pseudocode for the resulting adapter:
  2758      * <p> Here is pseudocode for the resulting adapter:
  2656      * <blockquote><pre>
  2759      * <blockquote><pre>{@code
  2657      * T target(A..., B...);
  2760      * T target(A..., B...);
  2658      * T handler(ExType, A...);
  2761      * T handler(ExType, A...);
  2659      * T adapter(A... a, B... b) {
  2762      * T adapter(A... a, B... b) {
  2660      *   try {
  2763      *   try {
  2661      *     return target(a..., b...);
  2764      *     return target(a..., b...);
  2662      *   } catch (ExType ex) {
  2765      *   } catch (ExType ex) {
  2663      *     return handler(ex, a...);
  2766      *     return handler(ex, a...);
  2664      *   }
  2767      *   }
  2665      * }
  2768      * }
  2666      * </pre></blockquote>
  2769      * }</pre></blockquote>
  2667      * Note that the saved arguments ({@code a...} in the pseudocode) cannot
  2770      * Note that the saved arguments ({@code a...} in the pseudocode) cannot
  2668      * be modified by execution of the target, and so are passed unchanged
  2771      * be modified by execution of the target, and so are passed unchanged
  2669      * from the caller to the handler, if the handler is invoked.
  2772      * from the caller to the handler, if the handler is invoked.
  2670      * <p>
  2773      * <p>
  2671      * The target and handler must return the same type, even if the handler
  2774      * The target and handler must return the same type, even if the handler