MethodHandles.java 114.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2011, 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
import java.lang.reflect.*;
29 30 31
import sun.invoke.util.ValueConversions;
import sun.invoke.util.VerifyAccess;
import sun.invoke.util.Wrapper;
32
import java.util.List;
33 34 35
import java.util.ArrayList;
import java.util.Arrays;
import sun.reflect.Reflection;
36
import static java.lang.invoke.MethodHandleStatics.*;
37
import static java.lang.invoke.MethodHandleNatives.Constants.*;
38 39

/**
40 41
 * This class consists exclusively of static methods that operate on or return
 * method handles. They fall into several categories:
42
 * <ul>
43 44 45
 * <li>Lookup methods which help create method handles for methods and fields.
 * <li>Combinator methods, which combine or transform pre-existing method handles into new ones.
 * <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns.
46
 * <li>Wrapper methods which can convert between method handles and interface types.
47
 * </ul>
48 49 50 51 52 53 54
 * <p>
 * @author John Rose, JSR 292 EG
 */
public class MethodHandles {

    private MethodHandles() { }  // do not instantiate

55
    private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory();
56 57 58 59 60
    static { MethodHandleImpl.initStatics(); }
    // See IMPL_LOOKUP below.

    //// Method handle creation from ordinary methods.

61
    /**
62
     * Returns a {@link Lookup lookup object} on the caller,
63 64 65 66
     * which has the capability to access any method handle that the caller has access to,
     * including direct method handles to private fields and methods.
     * This lookup object is a <em>capability</em> which may be delegated to trusted agents.
     * Do not store it in place where untrusted code can access it.
67
     */
68 69 70 71
    public static Lookup lookup() {
        return new Lookup();
    }

72
    /**
73
     * Returns a {@link Lookup lookup object} which is trusted minimally.
74 75
     * It can only be used to create method handles to
     * publicly accessible fields and methods.
76 77 78 79 80 81 82 83
     * <p>
     * As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
     * of this lookup object will be {@link java.lang.Object}.
     * <p>
     * The lookup class can be changed to any other class {@code C} using an expression of the form
     * {@linkplain Lookup#in <code>publicLookup().in(C.class)</code>}.
     * Since all classes have equal access to public names,
     * such a change would confer no new access rights.
84 85 86 87 88
     */
    public static Lookup publicLookup() {
        return Lookup.PUBLIC_LOOKUP;
    }

89
    /**
90 91 92
     * A <em>lookup object</em> is a factory for creating method handles,
     * when the creation requires access checking.
     * Method handles do not perform
93
     * access checks when they are called, but rather when they are created.
94 95
     * Therefore, method handle access
     * restrictions must be enforced when a method handle is created.
96
     * The caller class against which those restrictions are enforced
97
     * is known as the {@linkplain #lookupClass lookup class}.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
     * <p>
     * A lookup class which needs to create method handles will call
     * {@link MethodHandles#lookup MethodHandles.lookup} to create a factory for itself.
     * When the {@code Lookup} factory object is created, the identity of the lookup class is
     * determined, and securely stored in the {@code Lookup} object.
     * The lookup class (or its delegates) may then use factory methods
     * on the {@code Lookup} object to create method handles for access-checked members.
     * This includes all methods, constructors, and fields which are allowed to the lookup class,
     * even private ones.
     * <p>
     * The factory methods on a {@code Lookup} object correspond to all major
     * use cases for methods, constructors, and fields.
     * Here is a summary of the correspondence between these factory methods and
     * the behavior the resulting method handles:
     * <code>
     * <table border=1 cellpadding=5 summary="lookup method behaviors">
     * <tr><th>lookup expression</th><th>member</th><th>behavior</th></tr>
     * <tr>
116
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td>
117 118 119
     *     <td>FT f;</td><td>(T) this.f;</td>
     * </tr>
     * <tr>
120
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</td>
121 122 123
     *     <td>static<br>FT f;</td><td>(T) C.f;</td>
     * </tr>
     * <tr>
124
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</td>
125 126 127
     *     <td>FT f;</td><td>this.f = x;</td>
     * </tr>
     * <tr>
128
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</td>
129 130 131
     *     <td>static<br>FT f;</td><td>C.f = arg;</td>
     * </tr>
     * <tr>
132
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</td>
133 134 135
     *     <td>T m(A*);</td><td>(T) this.m(arg*);</td>
     * </tr>
     * <tr>
136
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</td>
137 138 139
     *     <td>static<br>T m(A*);</td><td>(T) C.m(arg*);</td>
     * </tr>
     * <tr>
140
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</td>
141 142 143
     *     <td>T m(A*);</td><td>(T) super.m(arg*);</td>
     * </tr>
     * <tr>
144
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</td>
145 146 147
     *     <td>C(A*);</td><td>(T) new C(arg*);</td>
     * </tr>
     * <tr>
148
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</td>
149 150 151
     *     <td>(static)?<br>FT f;</td><td>(FT) aField.get(thisOrNull);</td>
     * </tr>
     * <tr>
152
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</td>
153 154 155
     *     <td>(static)?<br>FT f;</td><td>aField.set(thisOrNull, arg);</td>
     * </tr>
     * <tr>
156
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
157 158 159
     *     <td>(static)?<br>T m(A*);</td><td>(T) aMethod.invoke(thisOrNull, arg*);</td>
     * </tr>
     * <tr>
160
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</td>
161 162 163
     *     <td>C(A*);</td><td>(C) aConstructor.newInstance(arg*);</td>
     * </tr>
     * <tr>
164
     *     <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td>
165 166 167 168 169 170 171 172 173 174 175
     *     <td>(static)?<br>T m(A*);</td><td>(T) aMethod.invoke(thisOrNull, arg*);</td>
     * </tr>
     * </table>
     * </code>
     * Here, the type {@code C} is the class or interface being searched for a member,
     * documented as a parameter named {@code refc} in the lookup methods.
     * The method or constructor type {@code MT} is composed from the return type {@code T}
     * and the sequence of argument types {@code A*}.
     * Both {@code MT} and the field type {@code FT} are documented as a parameter named {@code type}.
     * The formal parameter {@code this} stands for the self-reference of type {@code C};
     * if it is present, it is always the leading argument to the method handle invocation.
176 177
     * (In the case of some {@code protected} members, {@code this} may be
     * restricted in type to the lookup class; see below.)
178 179 180 181 182 183 184
     * The name {@code arg} stands for all the other method handle arguments.
     * In the code examples for the Core Reflection API, the name {@code thisOrNull}
     * stands for a null reference if the accessed method or field is static,
     * and {@code this} otherwise.
     * The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand
     * for reflective objects corresponding to the given members.
     * <p>
185 186 187 188
     * In cases where the given member is of variable arity (i.e., a method or constructor)
     * the returned method handle will also be of {@linkplain MethodHandle#asVarargsCollector variable arity}.
     * In all other cases, the returned method handle will be of fixed arity.
     * <p>
189 190 191 192 193 194 195 196 197 198
     * The equivalence between looked-up method handles and underlying
     * class members can break down in a few ways:
     * <ul>
     * <li>If {@code C} is not symbolically accessible from the lookup class's loader,
     * the lookup can still succeed, even when there is no equivalent
     * Java expression or bytecoded constant.
     * <li>Likewise, if {@code T} or {@code MT}
     * is not symbolically accessible from the lookup class's loader,
     * the lookup can still succeed.
     * For example, lookups for {@code MethodHandle.invokeExact} and
199
     * {@code MethodHandle.invoke} will always succeed, regardless of requested type.
200 201 202 203
     * <li>If there is a security manager installed, it can forbid the lookup
     * on various grounds (<a href="#secmgr">see below</a>).
     * By contrast, the {@code ldc} instruction is not subject to
     * security manager checks.
204 205 206 207 208 209
     * </ul>
     *
     * <h3><a name="access"></a>Access checking</h3>
     * Access checks are applied in the factory methods of {@code Lookup},
     * when a method handle is created.
     * This is a key difference from the Core Reflection API, since
210
     * {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
211 212 213 214 215 216
     * performs access checking against every caller, on every call.
     * <p>
     * All access checks start from a {@code Lookup} object, which
     * compares its recorded lookup class against all requests to
     * create method handles.
     * A single {@code Lookup} object can be used to create any number
217 218 219
     * of access-checked method handles, all checked against a single
     * lookup class.
     * <p>
220 221 222 223 224 225 226
     * A {@code Lookup} object can be shared with other trusted code,
     * such as a metaobject protocol.
     * A shared {@code Lookup} object delegates the capability
     * to create method handles on private members of the lookup class.
     * Even if privileged code uses the {@code Lookup} object,
     * the access checking is confined to the privileges of the
     * original lookup class.
227
     * <p>
228
     * A lookup can fail, because
229 230 231
     * the containing class is not accessible to the lookup class, or
     * because the desired class member is missing, or because the
     * desired class member is not accessible to the lookup class.
232 233 234 235 236 237 238 239
     * In any of these cases, a {@code ReflectiveOperationException} will be
     * thrown from the attempted lookup.  The exact class will be one of
     * the following:
     * <ul>
     * <li>NoSuchMethodException &mdash; if a method is requested but does not exist
     * <li>NoSuchFieldException &mdash; if a field is requested but does not exist
     * <li>IllegalAccessException &mdash; if the member exists but an access check fails
     * </ul>
240
     * <p>
241
     * In general, the conditions under which a method handle may be
242 243 244 245
     * looked up for a method {@code M} are exactly equivalent to the conditions
     * under which the lookup class could have compiled and resolved a call to {@code M}.
     * And the effect of invoking the method handle resulting from the lookup
     * is exactly equivalent to executing the compiled and resolved call to {@code M}.
246
     * The same point is true of fields and constructors.
247
     * <p>
248 249 250 251 252 253 254 255 256 257 258 259
     * If the desired member is {@code protected}, the usual JVM rules apply,
     * including the requirement that the lookup class must be either be in the
     * same package as the desired member, or must inherit that member.
     * (See the Java Virtual Machine Specification, sections 4.9.2, 5.4.3.5, and 6.4.)
     * In addition, if the desired member is a non-static field or method
     * in a different package, the resulting method handle may only be applied
     * to objects of the lookup class or one of its subclasses.
     * This requirement is enforced by narrowing the type of the leading
     * {@code this} parameter from {@code C}
     * (which will necessarily be a superclass of the lookup class)
     * to the lookup class itself.
     * <p>
260
     * In some cases, access between nested classes is obtained by the Java compiler by creating
261 262
     * an wrapper method to access a private method of another class
     * in the same top-level declaration.
263
     * For example, a nested class {@code C.D}
264
     * can access private members within other related classes such as
265 266 267 268 269 270 271
     * {@code C}, {@code C.D.E}, or {@code C.B},
     * but the Java compiler may need to generate wrapper methods in
     * those related classes.  In such cases, a {@code Lookup} object on
     * {@code C.E} would be unable to those private members.
     * A workaround for this limitation is the {@link Lookup#in Lookup.in} method,
     * which can transform a lookup on {@code C.E} into one on any of those other
     * classes, without special elevation of privilege.
272
     * <p>
273 274 275 276 277 278 279 280 281 282 283 284
     * Although bytecode instructions can only refer to classes in
     * a related class loader, this API can search for methods in any
     * class, as long as a reference to its {@code Class} object is
     * available.  Such cross-loader references are also possible with the
     * Core Reflection API, and are impossible to bytecode instructions
     * such as {@code invokestatic} or {@code getfield}.
     * There is a {@linkplain java.lang.SecurityManager security manager API}
     * to allow applications to check such cross-loader references.
     * These checks apply to both the {@code MethodHandles.Lookup} API
     * and the Core Reflection API
     * (as found on {@link java.lang.Class Class}).
     * <p>
285 286 287
     * Access checks only apply to named and reflected methods,
     * constructors, and fields.
     * Other method handle creation methods, such as
288
     * {@link MethodHandle#asType MethodHandle.asType},
289 290 291
     * do not require any access checks, and are done
     * with static methods of {@link MethodHandles},
     * independently of any {@code Lookup} object.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
     *
     * <h3>Security manager interactions</h3>
     * <a name="secmgr"></a>
     * If a security manager is present, member lookups are subject to
     * additional checks.
     * From one to four calls are made to the security manager.
     * Any of these calls can refuse access by throwing a
     * {@link java.lang.SecurityException SecurityException}.
     * Define {@code smgr} as the security manager,
     * {@code refc} as the containing class in which the member
     * is being sought, and {@code defc} as the class in which the
     * member is actually defined.
     * The calls are made according to the following rules:
     * <ul>
     * <li>In all cases, {@link SecurityManager#checkMemberAccess
     *     smgr.checkMemberAccess(refc, Member.PUBLIC)} is called.
     * <li>If the class loader of the lookup class is not
     *     the same as or an ancestor of the class loader of {@code refc},
     *     then {@link SecurityManager#checkPackageAccess
     *     smgr.checkPackageAccess(refcPkg)} is called,
     *     where {@code refcPkg} is the package of {@code refc}.
     * <li>If the retrieved member is not public,
     *     {@link SecurityManager#checkMemberAccess
     *     smgr.checkMemberAccess(defc, Member.DECLARED)} is called.
     *     (Note that {@code defc} might be the same as {@code refc}.)
317 318 319 320 321 322
     *     The default implementation of this security manager method
     *     inspects the stack to determine the original caller of
     *     the reflective request (such as {@code findStatic}),
     *     and performs additional permission checks if the
     *     class loader of {@code defc} differs from the class
     *     loader of the class from which the reflective request came.
323 324 325 326 327 328 329 330
     * <li>If the retrieved member is not public,
     *     and if {@code defc} and {@code refc} are in different class loaders,
     *     and if the class loader of the lookup class is not
     *     the same as or an ancestor of the class loader of {@code defc},
     *     then {@link SecurityManager#checkPackageAccess
     *     smgr.checkPackageAccess(defcPkg)} is called,
     *     where {@code defcPkg} is the package of {@code defc}.
     * </ul>
331 332 333
     */
    public static final
    class Lookup {
334
        /** The class on behalf of whom the lookup is being performed. */
335 336
        private final Class<?> lookupClass;

337
        /** The allowed sorts of members which may be looked up (PUBLIC, etc.). */
338 339
        private final int allowedModes;

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        /** A single-bit mask representing {@code public} access,
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
         *  The value, {@code 0x01}, happens to be the same as the value of the
         *  {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}.
         */
        public static final int PUBLIC = Modifier.PUBLIC;

        /** A single-bit mask representing {@code private} access,
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
         *  The value, {@code 0x02}, happens to be the same as the value of the
         *  {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}.
         */
        public static final int PRIVATE = Modifier.PRIVATE;

        /** A single-bit mask representing {@code protected} access,
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
         *  The value, {@code 0x04}, happens to be the same as the value of the
         *  {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}.
         */
        public static final int PROTECTED = Modifier.PROTECTED;

        /** A single-bit mask representing {@code package} access (default access),
         *  which may contribute to the result of {@link #lookupModes lookupModes}.
         *  The value is {@code 0x08}, which does not correspond meaningfully to
         *  any particular {@linkplain java.lang.reflect.Modifier modifier bit}.
         */
        public static final int PACKAGE = Modifier.STATIC;

        private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE);
        private static final int TRUSTED   = -1;
370 371 372 373 374 375

        private static int fixmods(int mods) {
            mods &= (ALL_MODES - PACKAGE);
            return (mods != 0) ? mods : PACKAGE;
        }

376
        /** Tells which class is performing the lookup.  It is this class against
377 378
         *  which checks are performed for visibility and access permissions.
         *  <p>
379 380
         *  The class implies a maximum level of access permission,
         *  but the permissions may be additionally limited by the bitmask
381
         *  {@link #lookupModes lookupModes}, which controls whether non-public members
382
         *  can be accessed.
383 384 385 386 387
         */
        public Class<?> lookupClass() {
            return lookupClass;
        }

388 389 390 391 392
        // This is just for calling out to MethodHandleImpl.
        private Class<?> lookupClassOrNull() {
            return (allowedModes == TRUSTED) ? null : lookupClass;
        }

393
        /** Tells which access-protection classes of members this lookup object can produce.
394 395 396 397 398
         *  The result is a bit-mask of the bits
         *  {@linkplain #PUBLIC PUBLIC (0x01)},
         *  {@linkplain #PRIVATE PRIVATE (0x02)},
         *  {@linkplain #PROTECTED PROTECTED (0x04)},
         *  and {@linkplain #PACKAGE PACKAGE (0x08)}.
399 400
         *  <p>
         *  A freshly-created lookup object
401
         *  on the {@linkplain java.lang.invoke.MethodHandles#lookup() caller's class}
402 403
         *  has all possible bits set, since the caller class can access all its own members.
         *  A lookup object on a new lookup class
404
         *  {@linkplain java.lang.invoke.MethodHandles.Lookup#in created from a previous lookup object}
405 406 407 408
         *  may have some mode bits set to zero.
         *  The purpose of this is to restrict access via the new lookup object,
         *  so that it can access only names which can be reached by the original
         *  lookup object, and also by the new lookup class.
409
         */
410
        public int lookupModes() {
411 412 413
            return allowedModes & ALL_MODES;
        }

414 415 416 417
        /** Embody the current class (the lookupClass) as a lookup class
         * for method handle creation.
         * Must be called by from a method in this package,
         * which in turn is called by a method not in this package.
418
         * <p>
419 420 421 422
         * Also, don't make it private, lest javac interpose
         * an access$N method.
         */
        Lookup() {
423
            this(getCallerClassAtEntryPoint(false), ALL_MODES);
424 425
            // make sure we haven't accidentally picked up a privileged class:
            checkUnprivilegedlookupClass(lookupClass);
426 427
        }

428
        Lookup(Class<?> lookupClass) {
429 430 431 432
            this(lookupClass, ALL_MODES);
        }

        private Lookup(Class<?> lookupClass, int allowedModes) {
433
            this.lookupClass = lookupClass;
434
            this.allowedModes = allowedModes;
435 436 437
        }

        /**
438
         * Creates a lookup on the specified new lookup class.
439
         * The resulting object will report the specified
440
         * class as its own {@link #lookupClass lookupClass}.
441 442 443
         * <p>
         * However, the resulting {@code Lookup} object is guaranteed
         * to have no more access capabilities than the original.
444
         * In particular, access capabilities can be lost as follows:<ul>
445 446
         * <li>If the new lookup class differs from the old one,
         * protected members will not be accessible by virtue of inheritance.
447
         * (Protected members may continue to be accessible because of package sharing.)
448 449 450 451
         * <li>If the new lookup class is in a different package
         * than the old one, protected and default (package) members will not be accessible.
         * <li>If the new lookup class is not within the same package member
         * as the old one, private members will not be accessible.
452 453 454
         * <li>If the new lookup class is not accessible to the old lookup class,
         * then no members, not even public members, will be accessible.
         * (In all other cases, public members will continue to be accessible.)
455
         * </ul>
456 457 458 459
         *
         * @param requestedLookupClass the desired lookup class for the new lookup object
         * @return a lookup object which reports the desired lookup class
         * @throws NullPointerException if the argument is null
460
         */
461 462 463 464 465 466 467 468 469 470
        public Lookup in(Class<?> requestedLookupClass) {
            requestedLookupClass.getClass();  // null check
            if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
                return new Lookup(requestedLookupClass, ALL_MODES);
            if (requestedLookupClass == this.lookupClass)
                return this;  // keep same capabilities
            int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
            if ((newModes & PACKAGE) != 0
                && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
                newModes &= ~(PACKAGE|PRIVATE);
471
            }
472
            // Allow nestmate lookups to be created without special privilege:
473 474 475 476
            if ((newModes & PRIVATE) != 0
                && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
                newModes &= ~PRIVATE;
            }
477 478
            if ((newModes & PUBLIC) != 0
                && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
479 480 481 482
                // The requested class it not accessible from the lookup class.
                // No permissions.
                newModes = 0;
            }
483 484
            checkUnprivilegedlookupClass(requestedLookupClass);
            return new Lookup(requestedLookupClass, newModes);
485 486
        }

487
        // Make sure outer class is initialized first.
488
        static { IMPL_NAMES.getClass(); }
489

490 491 492 493
        /** Version of lookup which is trusted minimally.
         *  It can only be used to create method handles to
         *  publicly accessible members.
         */
494
        static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, PUBLIC);
495 496

        /** Package-private version of lookup which is trusted. */
497
        static final Lookup IMPL_LOOKUP = new Lookup(Object.class, TRUSTED);
498 499

        private static void checkUnprivilegedlookupClass(Class<?> lookupClass) {
500
            String name = lookupClass.getName();
501
            if (name.startsWith("java.lang.invoke."))
502 503 504
                throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
        }

505
        /**
506
         * Displays the name of the class from which lookups are to be made.
507 508 509
         * (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.)
         * If there are restrictions on the access permitted to this lookup,
         * this is indicated by adding a suffix to the class name, consisting
510 511
         * of a slash and a keyword.  The keyword represents the strongest
         * allowed access, and is chosen as follows:
512 513 514 515 516 517 518 519 520 521
         * <ul>
         * <li>If no access is allowed, the suffix is "/noaccess".
         * <li>If only public access is allowed, the suffix is "/public".
         * <li>If only public and package access are allowed, the suffix is "/package".
         * <li>If only public, package, and private access are allowed, the suffix is "/private".
         * </ul>
         * If none of the above cases apply, it is the case that full
         * access (public, package, private, and protected) is allowed.
         * In this case, no suffix is added.
         * This is true only of an object obtained originally from
522 523
         * {@link java.lang.invoke.MethodHandles#lookup MethodHandles.lookup}.
         * Objects created by {@link java.lang.invoke.MethodHandles.Lookup#in Lookup.in}
524
         * always have restricted access, and will display a suffix.
525 526 527 528 529 530 531
         * <p>
         * (It may seem strange that protected access should be
         * stronger than private access.  Viewed independently from
         * package access, protected access is the first to be lost,
         * because it requires a direct subclass relationship between
         * caller and callee.)
         * @see #in
532
         */
533 534
        @Override
        public String toString() {
535 536
            String cname = lookupClass.getName();
            switch (allowedModes) {
537 538
            case 0:  // no privileges
                return cname + "/noaccess";
539
            case PUBLIC:
540
                return cname + "/public";
541 542
            case PUBLIC|PACKAGE:
                return cname + "/package";
543 544
            case ALL_MODES & ~PROTECTED:
                return cname + "/private";
545 546
            case ALL_MODES:
                return cname;
547 548 549 550 551 552
            case TRUSTED:
                return "/trusted";  // internal only; not exported
            default:  // Should not happen, but it's a bitfield...
                cname = cname + "/" + Integer.toHexString(allowedModes);
                assert(false) : cname;
                return cname;
553
            }
554 555
        }

556 557
        /* Obtain the external caller class, when called from Lookup.<init> or a first-level subroutine. */
        private static Class<?> getCallerClassAtEntryPoint(boolean inSubroutine) {
558
            final int CALLER_DEPTH = 4;
559
            //  Stack for the constructor entry point (inSubroutine=false):
560 561
            // 0: Reflection.getCC, 1: getCallerClassAtEntryPoint,
            // 2: Lookup.<init>, 3: MethodHandles.*, 4: caller
562 563
            //  The stack is slightly different for a subroutine of a Lookup.find* method:
            // 2: Lookup.*, 3: Lookup.find*.*, 4: caller
564
            // Note:  This should be the only use of getCallerClass in this file.
565 566
            assert(Reflection.getCallerClass(CALLER_DEPTH-2) == Lookup.class);
            assert(Reflection.getCallerClass(CALLER_DEPTH-1) == (inSubroutine ? Lookup.class : MethodHandles.class));
567 568 569 570
            return Reflection.getCallerClass(CALLER_DEPTH);
        }

        /**
571
         * Produces a method handle for a static method.
572
         * The type of the method handle will be that of the method.
573 574
         * (Since static methods do not take receivers, there is no
         * additional receiver argument inserted into the method handle type,
575
         * as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
576 577 578
         * The method and all its argument types must be accessible to the lookup class.
         * If the method's class has not yet been initialized, that is done
         * immediately, before the method handle is returned.
579 580 581 582
         * <p>
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
583
         * @param refc the class from which the method is accessed
584 585 586
         * @param name the name of the method
         * @param type the type of the method
         * @return the desired method handle
587
         * @throws NoSuchMethodException if the method does not exist
588 589 590 591
         * @throws IllegalAccessException if access checking fails,
         *                                or if the method is not {@code static},
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
592 593
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
594
         * @throws NullPointerException if any argument is null
595 596
         */
        public
597
        MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
598
            MemberName method = resolveOrFail(REF_invokeStatic, refc, name, type);
599
            checkSecurityManager(refc, method);  // stack walk magic: do not refactor
600
            return getDirectMethod(REF_invokeStatic, refc, method);
601
        }
602 603

        /**
604
         * Produces a method handle for a virtual method.
605
         * The type of the method handle will be that of the method,
606
         * with the receiver type (usually {@code refc}) prepended.
607 608 609 610 611 612 613
         * The method and all its argument types must be accessible to the lookup class.
         * <p>
         * When called, the handle will treat the first argument as a receiver
         * and dispatch on the receiver's type to determine which method
         * implementation to enter.
         * (The dispatching action is identical with that performed by an
         * {@code invokevirtual} or {@code invokeinterface} instruction.)
614
         * <p>
615 616 617 618 619
         * The first argument will be of type {@code refc} if the lookup
         * class has full privileges to access the member.  Otherwise
         * the member must be {@code protected} and the first argument
         * will be restricted in type to the lookup class.
         * <p>
620 621 622
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
623 624 625 626
         * <p>
         * Because of the general equivalence between {@code invokevirtual}
         * instructions and method handles produced by {@code findVirtual},
         * if the class is {@code MethodHandle} and the name string is
627
         * {@code invokeExact} or {@code invoke}, the resulting
628
         * method handle is equivalent to one produced by
629
         * {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or
630
         * {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}
631 632
         * with the same {@code type} argument.
         *
633
         * @param refc the class or interface from which the method is accessed
634 635 636
         * @param name the name of the method
         * @param type the type of the method, with the receiver argument omitted
         * @return the desired method handle
637
         * @throws NoSuchMethodException if the method does not exist
638 639 640 641
         * @throws IllegalAccessException if access checking fails,
         *                                or if the method is {@code static}
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
642 643
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
644
         * @throws NullPointerException if any argument is null
645
         */
646
        public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
647 648 649 650 651 652
            if (refc == MethodHandle.class) {
                MethodHandle mh = findVirtualForMH(name, type);
                if (mh != null)  return mh;
            }
            byte refKind = (refc.isInterface() ? REF_invokeInterface : REF_invokeVirtual);
            MemberName method = resolveOrFail(refKind, refc, name, type);
653
            checkSecurityManager(refc, method);  // stack walk magic: do not refactor
654
            return getDirectMethod(refKind, refc, method);
655
        }
656 657 658 659 660 661 662
        private MethodHandle findVirtualForMH(String name, MethodType type) {
            // these names require special lookups because of the implicit MethodType argument
            if ("invoke".equals(name))
                return invoker(type);
            if ("invokeExact".equals(name))
                return exactInvoker(type);
            return null;
663 664 665
        }

        /**
666
         * Produces a method handle which creates an object and initializes it, using
667 668 669 670 671 672 673 674
         * the constructor of the specified type.
         * The parameter types of the method handle will be those of the constructor,
         * while the return type will be a reference to the constructor's class.
         * The constructor and all its argument types must be accessible to the lookup class.
         * If the constructor's class has not yet been initialized, that is done
         * immediately, before the method handle is returned.
         * <p>
         * Note:  The requested type must have a return type of {@code void}.
675
         * This is consistent with the JVM's treatment of constructor type descriptors.
676 677 678 679
         * <p>
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
680 681 682
         * @param refc the class or interface from which the method is accessed
         * @param type the type of the method, with the receiver argument omitted, and a void return type
         * @return the desired method handle
683 684
         * @throws NoSuchMethodException if the constructor does not exist
         * @throws IllegalAccessException if access checking fails
685 686
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
687 688
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
689
         * @throws NullPointerException if any argument is null
690
         */
691
        public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
692
            String name = "<init>";
693
            MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
694
            checkSecurityManager(refc, ctor);  // stack walk magic: do not refactor
695
            return getDirectConstructor(refc, ctor);
696 697 698
        }

        /**
699
         * Produces an early-bound method handle for a virtual method,
700
         * as if called from an {@code invokespecial}
701
         * instruction from {@code caller}.
702
         * The type of the method handle will be that of the method,
703
         * with a suitably restricted receiver type (such as {@code caller}) prepended.
704
         * The method and all its argument types must be accessible
705 706 707 708 709 710 711 712
         * to the caller.
         * <p>
         * When called, the handle will treat the first argument as a receiver,
         * but will not dispatch on the receiver's type.
         * (This direct invocation action is identical with that performed by an
         * {@code invokespecial} instruction.)
         * <p>
         * If the explicitly specified caller class is not identical with the
713 714
         * lookup class, or if this lookup object does not have private access
         * privileges, the access fails.
715 716 717 718
         * <p>
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
719 720
         * @param refc the class or interface from which the method is accessed
         * @param name the name of the method (which must not be "&lt;init&gt;")
721 722 723
         * @param type the type of the method, with the receiver argument omitted
         * @param specialCaller the proposed calling class to perform the {@code invokespecial}
         * @return the desired method handle
724 725
         * @throws NoSuchMethodException if the method does not exist
         * @throws IllegalAccessException if access checking fails
726 727
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
728 729
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
730
         * @throws NullPointerException if any argument is null
731
         */
732
        public MethodHandle findSpecial(Class<?> refc, String name, MethodType type,
733
                                        Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException {
734
            checkSpecialCaller(specialCaller);
735 736
            Lookup specialLookup = this.in(specialCaller);
            MemberName method = specialLookup.resolveOrFail(REF_invokeSpecial, refc, name, type);
737
            checkSecurityManager(refc, method);  // stack walk magic: do not refactor
738
            return specialLookup.getDirectMethod(REF_invokeSpecial, refc, method);
739
        }
740 741

        /**
742
         * Produces a method handle giving read access to a non-static field.
743 744
         * The type of the method handle will have a return type of the field's
         * value type.
745
         * The method handle's single argument will be the instance containing
746 747
         * the field.
         * Access checking is performed immediately on behalf of the lookup class.
748
         * @param refc the class or interface from which the method is accessed
749 750 751
         * @param name the field's name
         * @param type the field's type
         * @return a method handle which can load values from the field
752 753
         * @throws NoSuchFieldException if the field does not exist
         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
754 755
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
756
         * @throws NullPointerException if any argument is null
757
         */
758
        public MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
759
            MemberName field = resolveOrFail(REF_getField, refc, name, type);
760
            checkSecurityManager(refc, field);  // stack walk magic: do not refactor
761
            return getDirectField(REF_getField, refc, field);
762
        }
763 764

        /**
765
         * Produces a method handle giving write access to a non-static field.
766
         * The type of the method handle will have a void return type.
767
         * The method handle will take two arguments, the instance containing
768
         * the field, and the value to be stored.
769 770
         * The second argument will be of the field's value type.
         * Access checking is performed immediately on behalf of the lookup class.
771
         * @param refc the class or interface from which the method is accessed
772 773 774
         * @param name the field's name
         * @param type the field's type
         * @return a method handle which can store values into the field
775 776
         * @throws NoSuchFieldException if the field does not exist
         * @throws IllegalAccessException if access checking fails, or if the field is {@code static}
777 778
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
779
         * @throws NullPointerException if any argument is null
780
         */
781
        public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
782
            MemberName field = resolveOrFail(REF_putField, refc, name, type);
783
            checkSecurityManager(refc, field);  // stack walk magic: do not refactor
784
            return getDirectField(REF_putField, refc, field);
785
        }
786 787

        /**
788
         * Produces a method handle giving read access to a static field.
789 790 791 792
         * The type of the method handle will have a return type of the field's
         * value type.
         * The method handle will take no arguments.
         * Access checking is performed immediately on behalf of the lookup class.
793
         * @param refc the class or interface from which the method is accessed
794 795 796
         * @param name the field's name
         * @param type the field's type
         * @return a method handle which can load values from the field
797 798
         * @throws NoSuchFieldException if the field does not exist
         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
799 800
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
801
         * @throws NullPointerException if any argument is null
802
         */
803
        public MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
804
            MemberName field = resolveOrFail(REF_getStatic, refc, name, type);
805
            checkSecurityManager(refc, field);  // stack walk magic: do not refactor
806
            return getDirectField(REF_getStatic, refc, field);
807
        }
808 809

        /**
810
         * Produces a method handle giving write access to a static field.
811 812 813
         * The type of the method handle will have a void return type.
         * The method handle will take a single
         * argument, of the field's value type, the value to be stored.
814
         * Access checking is performed immediately on behalf of the lookup class.
815
         * @param refc the class or interface from which the method is accessed
816 817
         * @param name the field's name
         * @param type the field's type
818
         * @return a method handle which can store values into the field
819 820
         * @throws NoSuchFieldException if the field does not exist
         * @throws IllegalAccessException if access checking fails, or if the field is not {@code static}
821 822
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
823
         * @throws NullPointerException if any argument is null
824
         */
825
        public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
826
            MemberName field = resolveOrFail(REF_putStatic, refc, name, type);
827
            checkSecurityManager(refc, field);  // stack walk magic: do not refactor
828
            return getDirectField(REF_putStatic, refc, field);
829
        }
830 831

        /**
832
         * Produces an early-bound method handle for a non-static method.
833 834 835
         * The receiver must have a supertype {@code defc} in which a method
         * of the given name and type is accessible to the lookup class.
         * The method and all its argument types must be accessible to the lookup class.
836 837 838 839 840
         * The type of the method handle will be that of the method,
         * without any insertion of an additional receiver parameter.
         * The given receiver will be bound into the method handle,
         * so that every call to the method handle will invoke the
         * requested method on the given receiver.
841
         * <p>
842 843 844 845 846 847 848 849 850
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the method's variable arity modifier bit ({@code 0x0080}) is set
         * <em>and</em> the trailing array argument is not the only argument.
         * (If the trailing array argument is the only argument,
         * the given receiver value will be bound to it.)
         * <p>
         * This is equivalent to the following code:
         * <blockquote><pre>
851 852 853 854
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle mh0 = lookup().{@link #findVirtual findVirtual}(defc, name, type);
855 856
MethodHandle mh1 = mh0.{@link MethodHandle#bindTo bindTo}(receiver);
MethodType mt1 = mh1.type();
857
if (mh0.isVarargsCollector())
858 859 860
  mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1));
return mh1;
         * </pre></blockquote>
861 862 863
         * where {@code defc} is either {@code receiver.getClass()} or a super
         * type of that class, in which the requested method is accessible
         * to the lookup class.
864
         * (Note that {@code bindTo} does not preserve variable arity.)
865 866 867 868
         * @param receiver the object from which the method is accessed
         * @param name the name of the method
         * @param type the type of the method, with the receiver argument omitted
         * @return the desired method handle
869 870
         * @throws NoSuchMethodException if the method does not exist
         * @throws IllegalAccessException if access checking fails
871 872
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
873 874
         * @exception SecurityException if a security manager is present and it
         *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
875
         * @throws NullPointerException if any argument is null
876
         */
877
        public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
878
            Class<? extends Object> refc = receiver.getClass(); // may get NPE
879
            MemberName method = resolveOrFail(REF_invokeSpecial, refc, name, type);
880
            checkSecurityManager(refc, method);  // stack walk magic: do not refactor
881 882
            MethodHandle mh = getDirectMethodNoRestrict(REF_invokeSpecial, refc, method);
            return mh.bindReceiver(receiver).setVarargs(method);
883 884 885
        }

        /**
886
         * Makes a direct method handle to <i>m</i>, if the lookup class has permission.
887 888 889 890 891 892 893 894
         * If <i>m</i> is non-static, the receiver argument is treated as an initial argument.
         * If <i>m</i> is virtual, overriding is respected on every call.
         * Unlike the Core Reflection API, exceptions are <em>not</em> wrapped.
         * The type of the method handle will be that of the method,
         * with the receiver type prepended (but only if it is non-static).
         * If the method's {@code accessible} flag is not set,
         * access checking is performed immediately on behalf of the lookup class.
         * If <i>m</i> is not public, do not share the resulting handle with untrusted parties.
895 896 897 898
         * <p>
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
899 900
         * @param m the reflected method
         * @return a method handle which can invoke the reflected method
901
         * @throws IllegalAccessException if access checking fails
902 903
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
904
         * @throws NullPointerException if the argument is null
905
         */
906
        public MethodHandle unreflect(Method m) throws IllegalAccessException {
907
            MemberName method = new MemberName(m);
908 909 910
            byte refKind = method.getReferenceKind();
            if (refKind == REF_invokeSpecial)
                refKind = REF_invokeVirtual;
911
            assert(method.isMethod());
912 913
            Lookup lookup = m.isAccessible() ? IMPL_LOOKUP : this;
            return lookup.getDirectMethod(refKind, method.getDeclaringClass(), method);
914 915 916
        }

        /**
917
         * Produces a method handle for a reflected method.
918
         * It will bypass checks for overriding methods on the receiver,
919
         * as if by a {@code invokespecial} instruction from within the {@code specialCaller}.
920
         * The type of the method handle will be that of the method,
921
         * with the special caller type prepended (and <em>not</em> the receiver of the method).
922 923 924
         * If the method's {@code accessible} flag is not set,
         * access checking is performed immediately on behalf of the lookup class,
         * as if {@code invokespecial} instruction were being linked.
925 926 927 928
         * <p>
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the method's variable arity modifier bit ({@code 0x0080}) is set.
929
         * @param m the reflected method
930
         * @param specialCaller the class nominally calling the method
931
         * @return a method handle which can invoke the reflected method
932
         * @throws IllegalAccessException if access checking fails
933 934
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
935
         * @throws NullPointerException if any argument is null
936
         */
937
        public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException {
938
            checkSpecialCaller(specialCaller);
939 940
            Lookup specialLookup = this.in(specialCaller);
            MemberName method = new MemberName(m, true);
941 942
            assert(method.isMethod());
            // ignore m.isAccessible:  this is a new kind of access
943
            return specialLookup.getDirectMethod(REF_invokeSpecial, method.getDeclaringClass(), method);
944 945 946
        }

        /**
947
         * Produces a method handle for a reflected constructor.
948 949
         * The type of the method handle will be that of the constructor,
         * with the return type changed to the declaring class.
950 951 952 953 954
         * The method handle will perform a {@code newInstance} operation,
         * creating a new instance of the constructor's class on the
         * arguments passed to the method handle.
         * <p>
         * If the constructor's {@code accessible} flag is not set,
955
         * access checking is performed immediately on behalf of the lookup class.
956 957 958 959
         * <p>
         * The returned method handle will have
         * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
         * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
960
         * @param c the reflected constructor
961
         * @return a method handle which can invoke the reflected constructor
962
         * @throws IllegalAccessException if access checking fails
963 964
         *                                or if the method's variable arity modifier bit
         *                                is set and {@code asVarargsCollector} fails
965
         * @throws NullPointerException if the argument is null
966
         */
967
        @SuppressWarnings("rawtypes")  // Will be Constructor<?> after JSR 292 MR
968
        public MethodHandle unreflectConstructor(Constructor c) throws IllegalAccessException {
969 970
            MemberName ctor = new MemberName(c);
            assert(ctor.isConstructor());
971 972
            Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this;
            return lookup.getDirectConstructor(ctor.getDeclaringClass(), ctor);
973 974 975
        }

        /**
976
         * Produces a method handle giving read access to a reflected field.
977
         * The type of the method handle will have a return type of the field's
978 979 980 981
         * value type.
         * If the field is static, the method handle will take no arguments.
         * Otherwise, its single argument will be the instance containing
         * the field.
982
         * If the field's {@code accessible} flag is not set,
983 984 985
         * access checking is performed immediately on behalf of the lookup class.
         * @param f the reflected field
         * @return a method handle which can load values from the reflected field
986 987
         * @throws IllegalAccessException if access checking fails
         * @throws NullPointerException if the argument is null
988
         */
989
        public MethodHandle unreflectGetter(Field f) throws IllegalAccessException {
990 991 992 993 994 995 996 997 998
            return unreflectField(f, false);
        }
        private MethodHandle unreflectField(Field f, boolean isSetter) throws IllegalAccessException {
            MemberName field = new MemberName(f, isSetter);
            assert(isSetter
                    ? MethodHandleNatives.refKindIsSetter(field.getReferenceKind())
                    : MethodHandleNatives.refKindIsGetter(field.getReferenceKind()));
            Lookup lookup = f.isAccessible() ? IMPL_LOOKUP : this;
            return lookup.getDirectField(field.getReferenceKind(), f.getDeclaringClass(), field);
999 1000 1001
        }

        /**
1002
         * Produces a method handle giving write access to a reflected field.
1003
         * The type of the method handle will have a void return type.
1004 1005 1006 1007
         * If the field is static, the method handle will take a single
         * argument, of the field's value type, the value to be stored.
         * Otherwise, the two arguments will be the instance containing
         * the field, and the value to be stored.
1008
         * If the field's {@code accessible} flag is not set,
1009 1010 1011
         * access checking is performed immediately on behalf of the lookup class.
         * @param f the reflected field
         * @return a method handle which can store values into the reflected field
1012 1013
         * @throws IllegalAccessException if access checking fails
         * @throws NullPointerException if the argument is null
1014
         */
1015
        public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
1016
            return unreflectField(f, true);
1017 1018
        }

1019
        /// Helper methods, all package-private.
1020

1021
        MemberName resolveOrFail(byte refKind, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException {
1022
            checkSymbolicClass(refc);  // do this before attempting to resolve
1023
            name.getClass(); type.getClass();  // NPE
1024
            return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(),
1025
                                            NoSuchFieldException.class);
1026
        }
1027

1028
        MemberName resolveOrFail(byte refKind, Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
1029
            checkSymbolicClass(refc);  // do this before attempting to resolve
1030
            name.getClass(); type.getClass();  // NPE
1031
            return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(),
1032
                                            NoSuchMethodException.class);
1033
        }
1034

1035
        void checkSymbolicClass(Class<?> refc) throws IllegalAccessException {
1036
            Class<?> caller = lookupClassOrNull();
1037
            if (caller != null && !VerifyAccess.isClassAccessible(refc, caller, allowedModes))
1038
                throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this);
1039 1040
        }

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
        /**
         * Perform necessary <a href="MethodHandles.Lookup.html#secmgr">access checks</a>.
         * This function performs stack walk magic: do not refactor it.
         */
        void checkSecurityManager(Class<?> refc, MemberName m) {
            SecurityManager smgr = System.getSecurityManager();
            if (smgr == null)  return;
            if (allowedModes == TRUSTED)  return;
            // Step 1:
            smgr.checkMemberAccess(refc, Member.PUBLIC);
            // Step 2:
1052 1053 1054 1055 1056 1057 1058
            Class<?> callerClass = ((allowedModes & PRIVATE) != 0
                                    ? lookupClass  // for strong access modes, no extra check
                                    // next line does stack walk magic; do not refactor:
                                    : getCallerClassAtEntryPoint(true));
            if (!VerifyAccess.classLoaderIsAncestor(lookupClass, refc) ||
                (callerClass != lookupClass &&
                 !VerifyAccess.classLoaderIsAncestor(callerClass, refc)))
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
                smgr.checkPackageAccess(VerifyAccess.getPackageName(refc));
            // Step 3:
            if (m.isPublic()) return;
            Class<?> defc = m.getDeclaringClass();
            smgr.checkMemberAccess(defc, Member.DECLARED);  // STACK WALK HERE
            // Step 4:
            if (defc != refc)
                smgr.checkPackageAccess(VerifyAccess.getPackageName(defc));

            // Comment from SM.checkMemberAccess, where which=DECLARED:
            /*
             * stack depth of 4 should be the caller of one of the
             * methods in java.lang.Class that invoke checkMember
             * access. The stack should look like:
             *
             * someCaller                        [3]
             * java.lang.Class.someReflectionAPI [2]
             * java.lang.Class.checkMemberAccess [1]
             * SecurityManager.checkMemberAccess [0]
             *
             */
            // For us it is this stack:
            // someCaller                        [3]
            // Lookup.findSomeMember             [2]
            // Lookup.checkSecurityManager       [1]
            // SecurityManager.checkMemberAccess [0]
        }

1087 1088
        void checkMethod(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
            boolean wantStatic = (refKind == REF_invokeStatic);
1089 1090 1091 1092 1093 1094 1095 1096
            String message;
            if (m.isConstructor())
                message = "expected a method, not a constructor";
            else if (!m.isMethod())
                message = "expected a method";
            else if (wantStatic != m.isStatic())
                message = wantStatic ? "expected a static method" : "expected a non-static method";
            else
1097
                { checkAccess(refKind, refc, m); return; }
1098
            throw m.makeAccessException(message, this);
1099 1100
        }

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
        void checkField(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
            boolean wantStatic = !MethodHandleNatives.refKindHasReceiver(refKind);
            String message;
            if (wantStatic != m.isStatic())
                message = wantStatic ? "expected a static field" : "expected a non-static field";
            else
                { checkAccess(refKind, refc, m); return; }
            throw m.makeAccessException(message, this);
        }

        void checkAccess(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
            assert(m.referenceKindIsConsistentWith(refKind) &&
                   MethodHandleNatives.refKindIsValid(refKind) &&
                   (MethodHandleNatives.refKindIsField(refKind) == m.isField()));
1115 1116 1117
            int allowedModes = this.allowedModes;
            if (allowedModes == TRUSTED)  return;
            int mods = m.getModifiers();
1118 1119 1120
            if (Modifier.isFinal(mods) &&
                    MethodHandleNatives.refKindIsSetter(refKind))
                throw m.makeAccessException("unexpected set of a final field", this);
1121
            if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0)
1122 1123
                return;  // common case
            int requestedModes = fixmods(mods);  // adjust 0 => PACKAGE
1124 1125 1126 1127 1128
            if ((requestedModes & allowedModes) != 0) {
                if (VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(),
                                                    mods, lookupClass(), allowedModes))
                    return;
            } else {
1129
                // Protected members can also be checked as if they were package-private.
1130 1131 1132 1133
                if ((requestedModes & PROTECTED) != 0 && (allowedModes & PACKAGE) != 0
                        && VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass()))
                    return;
            }
1134
            throw m.makeAccessException(accessFailedMessage(refc, m), this);
1135 1136 1137 1138 1139
        }

        String accessFailedMessage(Class<?> refc, MemberName m) {
            Class<?> defc = m.getDeclaringClass();
            int mods = m.getModifiers();
1140 1141 1142 1143 1144
            // check the class first:
            boolean classOK = (Modifier.isPublic(defc.getModifiers()) &&
                               (defc == refc ||
                                Modifier.isPublic(refc.getModifiers())));
            if (!classOK && (allowedModes & PACKAGE) != 0) {
1145
                classOK = (VerifyAccess.isClassAccessible(defc, lookupClass(), ALL_MODES) &&
1146
                           (defc == refc ||
1147
                            VerifyAccess.isClassAccessible(refc, lookupClass(), ALL_MODES)));
1148 1149
            }
            if (!classOK)
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
                return "class is not public";
            if (Modifier.isPublic(mods))
                return "access to public member failed";  // (how?)
            if (Modifier.isPrivate(mods))
                return "member is private";
            if (Modifier.isProtected(mods))
                return "member is protected";
            return "member is private to package";
        }

1160 1161
        private static final boolean ALLOW_NESTMATE_ACCESS = false;

1162 1163
        private void checkSpecialCaller(Class<?> specialCaller) throws IllegalAccessException {
            int allowedModes = this.allowedModes;
1164
            if (allowedModes == TRUSTED)  return;
1165 1166 1167 1168
            if ((allowedModes & PRIVATE) == 0
                || (specialCaller != lookupClass()
                    && !(ALLOW_NESTMATE_ACCESS &&
                         VerifyAccess.isSamePackageMember(specialCaller, lookupClass()))))
1169 1170
                throw new MemberName(specialCaller).
                    makeAccessException("no private access for invokespecial", this);
1171 1172
        }

1173
        private boolean restrictProtectedReceiver(MemberName method) {
1174 1175 1176 1177
            // The accessing class only has the right to use a protected member
            // on itself or a subclass.  Enforce that restriction, from JVMS 5.4.4, etc.
            if (!method.isProtected() || method.isStatic()
                || allowedModes == TRUSTED
1178
                || method.getDeclaringClass() == lookupClass()
1179
                || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass())
1180 1181
                || (ALLOW_NESTMATE_ACCESS &&
                    VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
1182 1183
                return false;
            return true;
1184
        }
1185
        private MethodHandle restrictReceiver(MemberName method, MethodHandle mh, Class<?> caller) throws IllegalAccessException {
1186
            assert(!method.isStatic());
1187 1188
            // receiver type of mh is too wide; narrow to caller
            if (!method.getDeclaringClass().isAssignableFrom(caller)) {
1189
                throw method.makeAccessException("caller class must be a subclass below the method", caller);
1190
            }
1191 1192 1193
            MethodType rawType = mh.type();
            if (rawType.parameterType(0) == caller)  return mh;
            MethodType narrowType = rawType.changeParameterType(0, caller);
1194
            return mh.viewAsType(narrowType);
1195 1196
        }

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
        private MethodHandle getDirectMethod(byte refKind, Class<?> refc, MemberName method) throws IllegalAccessException {
            return getDirectMethodCommon(refKind, refc, method,
                    (refKind == REF_invokeSpecial ||
                        (MethodHandleNatives.refKindHasReceiver(refKind) &&
                            restrictProtectedReceiver(method))));
        }
        private MethodHandle getDirectMethodNoRestrict(byte refKind, Class<?> refc, MemberName method) throws IllegalAccessException {
            return getDirectMethodCommon(refKind, refc, method, false);
        }
        private MethodHandle getDirectMethodCommon(byte refKind, Class<?> refc, MemberName method,
                                                   boolean doRestrict) throws IllegalAccessException {
            checkMethod(refKind, refc, method);
            if (method.isMethodHandleInvoke())
                return fakeMethodHandleInvoke(method);
            MethodHandle mh = DirectMethodHandle.make(refc, method);
            mh = mh.setVarargs(method);
            if (doRestrict)
                mh = restrictReceiver(method, mh, lookupClass());
            return mh;
        }
        private MethodHandle fakeMethodHandleInvoke(MemberName method) {
            return throwException(method.getReturnType(), UnsupportedOperationException.class);
        }
        private MethodHandle getDirectField(byte refKind, Class<?> refc, MemberName field) throws IllegalAccessException {
            checkField(refKind, refc, field);
            MethodHandle mh = DirectMethodHandle.make(refc, field);
            boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(refKind) &&
                                    restrictProtectedReceiver(field));
            if (doRestrict)
                mh = restrictReceiver(field, mh, lookupClass());
            return mh;
        }
        private MethodHandle getDirectConstructor(Class<?> refc, MemberName ctor) throws IllegalAccessException {
            assert(ctor.isConstructor());
            checkAccess(REF_newInvokeSpecial, refc, ctor);
            return DirectMethodHandle.make(ctor).setVarargs(ctor);
1233
        }
1234 1235 1236 1237

        /** Hook called from the JVM (via MethodHandleNatives) to link MH constants:
         */
        /*non-public*/
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
        MethodHandle linkMethodHandleConstant(byte refKind, Class<?> defc, String name, Object type) throws ReflectiveOperationException {
            MemberName resolved = null;
            if (type instanceof MemberName) {
                resolved = (MemberName) type;
                if (!resolved.isResolved())  throw new InternalError("unresolved MemberName");
                assert(name == null || name.equals(resolved.getName()));
            }
            if (MethodHandleNatives.refKindIsField(refKind)) {
                MemberName field = (resolved != null) ? resolved
                        : resolveOrFail(refKind, defc, name, (Class<?>) type);
                return getDirectField(refKind, defc, field);
            } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
                MemberName method = (resolved != null) ? resolved
                        : resolveOrFail(refKind, defc, name, (MethodType) type);
                return getDirectMethod(refKind, defc, method);
            } else if (refKind == REF_newInvokeSpecial) {
                assert(name == null || name.equals("<init>"));
                MemberName ctor = (resolved != null) ? resolved
                        : resolveOrFail(REF_newInvokeSpecial, defc, name, (MethodType) type);
                return getDirectConstructor(defc, ctor);
1258 1259 1260 1261
            }
            // oops
            throw new ReflectiveOperationException("bad MethodHandle constant #"+refKind+" "+name+" : "+type);
        }
1262 1263 1264
    }

    /**
1265
     * Produces a method handle giving read access to elements of an array.
1266 1267 1268 1269 1270
     * The type of the method handle will have a return type of the array's
     * element type.  Its first argument will be the array type,
     * and the second will be {@code int}.
     * @param arrayClass an array type
     * @return a method handle which can load values from the given array type
1271
     * @throws NullPointerException if the argument is null
1272 1273 1274 1275
     * @throws  IllegalArgumentException if arrayClass is not an array type
     */
    public static
    MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException {
1276
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, false);
1277 1278 1279
    }

    /**
1280
     * Produces a method handle giving write access to elements of an array.
1281 1282 1283 1284
     * The type of the method handle will have a void return type.
     * Its last argument will be the array's element type.
     * The first and second arguments will be the array type and int.
     * @return a method handle which can store values into the array type
1285
     * @throws NullPointerException if the argument is null
1286 1287 1288 1289
     * @throws IllegalArgumentException if arrayClass is not an array type
     */
    public static
    MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException {
1290
        return MethodHandleImpl.makeArrayElementAccessor(arrayClass, true);
1291 1292 1293 1294 1295
    }

    /// method handle invocation (reflective style)

    /**
1296
     * Produces a method handle which will invoke any method handle of the
1297 1298
     * given {@code type}, with a given number of trailing arguments replaced by
     * a single trailing {@code Object[]} array.
1299 1300 1301 1302
     * The resulting invoker will be a method handle with the following
     * arguments:
     * <ul>
     * <li>a single {@code MethodHandle} target
1303 1304
     * <li>zero or more leading values (counted by {@code leadingArgCount})
     * <li>an {@code Object[]} array containing trailing arguments
1305
     * </ul>
1306
     * <p>
1307
     * The invoker will invoke its target like a call to {@link MethodHandle#invoke invoke} with
1308 1309
     * the indicated {@code type}.
     * That is, if the target is exactly of the given {@code type}, it will behave
1310
     * like {@code invokeExact}; otherwise it behave as if {@link MethodHandle#asType asType}
1311 1312 1313
     * is used to convert the target to the required {@code type}.
     * <p>
     * The type of the returned invoker will not be the given {@code type}, but rather
1314 1315 1316
     * will have all parameters except the first {@code leadingArgCount}
     * replaced by a single array of type {@code Object[]}, which will be
     * the final parameter.
1317
     * <p>
1318
     * Before invoking its target, the invoker will spread the final array, apply
1319
     * reference casts as necessary, and unbox and widen primitive arguments.
1320 1321 1322
     * <p>
     * This method is equivalent to the following code (though it may be more efficient):
     * <p><blockquote><pre>
1323
MethodHandle invoker = MethodHandles.invoker(type);
1324
int spreadArgCount = type.parameterCount() - leadingArgCount;
1325 1326
invoker = invoker.asSpreader(Object[].class, spreadArgCount);
return invoker;
1327
     * </pre></blockquote>
1328 1329
     * <p>
     * This method throws no reflective or security exceptions.
1330
     * @param type the desired target type
1331
     * @param leadingArgCount number of fixed arguments, to be passed unchanged to the target
1332
     * @return a method handle suitable for invoking any method handle of the given type
1333 1334 1335
     * @throws NullPointerException if {@code type} is null
     * @throws IllegalArgumentException if {@code leadingArgCount} is not in
     *                  the range from 0 to {@code type.parameterCount()} inclusive
1336 1337
     */
    static public
1338 1339 1340 1341
    MethodHandle spreadInvoker(MethodType type, int leadingArgCount) {
        if (leadingArgCount < 0 || leadingArgCount > type.parameterCount())
            throw new IllegalArgumentException("bad argument count "+leadingArgCount);
        return type.invokers().spreadInvoker(leadingArgCount);
1342 1343 1344
    }

    /**
1345
     * Produces a special <em>invoker method handle</em> which can be used to
1346
     * invoke any method handle of the given type, as if by {@link MethodHandle#invokeExact invokeExact}.
1347
     * The resulting invoker will have a type which is
1348 1349 1350
     * exactly equal to the desired type, except that it will accept
     * an additional leading argument of type {@code MethodHandle}.
     * <p>
1351 1352
     * This method is equivalent to the following code (though it may be more efficient):
     * <p><blockquote><pre>
1353
publicLookup().findVirtual(MethodHandle.class, "invokeExact", type)
1354
     * </pre></blockquote>
1355 1356 1357 1358 1359 1360 1361 1362
     *
     * <p style="font-size:smaller;">
     * <em>Discussion:</em>
     * Invoker method handles can be useful when working with variable method handles
     * of unknown types.
     * For example, to emulate an {@code invokeExact} call to a variable method
     * handle {@code M}, extract its type {@code T},
     * look up the invoker method {@code X} for {@code T},
1363
     * and call the invoker method, as {@code X.invoke(T, A...)}.
1364 1365 1366 1367 1368 1369 1370
     * (It would not work to call {@code X.invokeExact}, since the type {@code T}
     * is unknown.)
     * If spreading, collecting, or other argument transformations are required,
     * they can be applied once to the invoker {@code X} and reused on many {@code M}
     * method handle values, as long as they are compatible with the type of {@code X}.
     * <p>
     * <em>(Note:  The invoker method is not available via the Core Reflection API.
1371
     * An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}
1372
     * on the declared {@code invokeExact} or {@code invoke} method will raise an
1373 1374 1375
     * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em>
     * <p>
     * This method throws no reflective or security exceptions.
1376 1377 1378 1379 1380
     * @param type the desired target type
     * @return a method handle suitable for invoking any method handle of the given type
     */
    static public
    MethodHandle exactInvoker(MethodType type) {
1381
        return type.invokers().exactInvoker();
1382 1383
    }

1384 1385
    /**
     * Produces a special <em>invoker method handle</em> which can be used to
1386
     * invoke any method handle compatible with the given type, as if by {@link MethodHandle#invoke invoke}.
1387 1388 1389 1390
     * The resulting invoker will have a type which is
     * exactly equal to the desired type, except that it will accept
     * an additional leading argument of type {@code MethodHandle}.
     * <p>
1391 1392
     * Before invoking its target, if the target differs from the expected type,
     * the invoker will apply reference casts as
1393 1394 1395 1396
     * necessary and box, unbox, or widen primitive values, as if by {@link MethodHandle#asType asType}.
     * Similarly, the return value will be converted as necessary.
     * If the target is a {@linkplain MethodHandle#asVarargsCollector variable arity method handle},
     * the required arity conversion will be made, again as if by {@link MethodHandle#asType asType}.
1397
     * <p>
1398 1399 1400 1401 1402
     * A {@linkplain MethodType#genericMethodType general method type},
     * mentions only {@code Object} arguments and return values.
     * An invoker for such a type is capable of calling any method handle
     * of the same arity as the general type.
     * <p>
1403 1404
     * This method is equivalent to the following code (though it may be more efficient):
     * <p><blockquote><pre>
1405
publicLookup().findVirtual(MethodHandle.class, "invoke", type)
1406 1407 1408 1409 1410 1411 1412
     * </pre></blockquote>
     * <p>
     * This method throws no reflective or security exceptions.
     * @param type the desired target type
     * @return a method handle suitable for invoking any method handle convertible to the given type
     */
    static public
1413 1414 1415 1416
    MethodHandle invoker(MethodType type) {
        return type.invokers().generalInvoker();
    }

1417 1418 1419
    static /*non-public*/
    MethodHandle basicInvoker(MethodType type) {
        return type.form().basicInvoker();
1420 1421
    }

1422
     /// method handle modification (creation from other method handles)
1423 1424

    /**
1425
     * Produces a method handle which adapts the type of the
1426
     * given method handle to a new type by pairwise argument and return type conversion.
1427 1428 1429 1430 1431 1432
     * The original type and new type must have the same number of arguments.
     * The resulting method handle is guaranteed to report a type
     * which is equal to the desired new type.
     * <p>
     * If the original type and new type are equal, returns target.
     * <p>
1433
     * The same conversions are allowed as for {@link MethodHandle#asType MethodHandle.asType},
1434
     * and some additional conversions are also applied if those conversions fail.
1435 1436
     * Given types <em>T0</em>, <em>T1</em>, one of the following conversions is applied
     * if possible, before or instead of any conversions done by {@code asType}:
1437
     * <ul>
1438 1439
     * <li>If <em>T0</em> and <em>T1</em> are references, and <em>T1</em> is an interface type,
     *     then the value of type <em>T0</em> is passed as a <em>T1</em> without a cast.
1440
     *     (This treatment of interfaces follows the usage of the bytecode verifier.)
1441 1442
     * <li>If <em>T0</em> is boolean and <em>T1</em> is another primitive,
     *     the boolean is converted to a byte value, 1 for true, 0 for false.
1443
     *     (This treatment follows the usage of the bytecode verifier.)
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
     * <li>If <em>T1</em> is boolean and <em>T0</em> is another primitive,
     *     <em>T0</em> is converted to byte via Java casting conversion (JLS 5.5),
     *     and the low order bit of the result is tested, as if by {@code (x & 1) != 0}.
     * <li>If <em>T0</em> and <em>T1</em> are primitives other than boolean,
     *     then a Java casting conversion (JLS 5.5) is applied.
     *     (Specifically, <em>T0</em> will convert to <em>T1</em> by
     *     widening and/or narrowing.)
     * <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 casting conversion (JLS 5.5) on the primitive value,
     *     possibly followed by a conversion from byte to boolean by testing
     *     the low-order bit.
     * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive,
     *     and if the reference is null at runtime, a zero value is introduced.
1458 1459 1460
     * </ul>
     * @param target the method handle to invoke after arguments are retyped
     * @param newType the expected type of the new method handle
1461
     * @return a method handle which delegates to the target after performing
1462 1463
     *           any necessary argument conversions, and arranges for any
     *           necessary return value conversions
1464
     * @throws NullPointerException if either argument is null
1465 1466 1467 1468 1469
     * @throws WrongMethodTypeException if the conversion cannot be made
     * @see MethodHandle#asType
     */
    public static
    MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
1470 1471 1472 1473
        if (!target.type().isCastableTo(newType)) {
            throw new WrongMethodTypeException("cannot explicitly cast "+target+" to "+newType);
        }
        return MethodHandleImpl.makePairwiseConvert(target, newType, 2);
1474 1475 1476
    }

    /**
1477
     * Produces a method handle which adapts the calling sequence of the
1478
     * given method handle to a new type, by reordering the arguments.
1479
     * The resulting method handle is guaranteed to report a type
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
     * which is equal to the desired new type.
     * <p>
     * The given array controls the reordering.
     * Call {@code #I} the number of incoming parameters (the value
     * {@code newType.parameterCount()}, and call {@code #O} the number
     * of outgoing parameters (the value {@code target.type().parameterCount()}).
     * Then the length of the reordering array must be {@code #O},
     * and each element must be a non-negative number less than {@code #I}.
     * For every {@code N} less than {@code #O}, the {@code N}-th
     * outgoing argument will be taken from the {@code I}-th incoming
     * argument, where {@code I} is {@code reorder[N]}.
     * <p>
1492 1493
     * No argument or return value conversions are applied.
     * The type of each incoming argument, as determined by {@code newType},
1494 1495
     * must be identical to the type of the corresponding outgoing parameter
     * or parameters in the target method handle.
1496 1497 1498
     * The return type of {@code newType} must be identical to the return
     * type of the original target.
     * <p>
1499 1500 1501 1502
     * The reordering array need not specify an actual permutation.
     * An incoming argument will be duplicated if its index appears
     * more than once in the array, and an incoming argument will be dropped
     * if its index does not appear in the array.
1503 1504 1505 1506
     * As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments},
     * incoming arguments which are not mentioned in the reordering array
     * are may be any type, as determined only by {@code newType}.
     * <blockquote><pre>
1507 1508 1509 1510 1511
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodType intfn1 = methodType(int.class, int.class);
MethodType intfn2 = methodType(int.class, int.class, int.class);
1512 1513
MethodHandle sub = ... {int x, int y => x-y} ...;
assert(sub.type().equals(intfn2));
1514 1515
MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1);
MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0);
1516 1517 1518
assert((int)rsub.invokeExact(1, 100) == 99);
MethodHandle add = ... {int x, int y => x+y} ...;
assert(add.type().equals(intfn2));
1519
MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
1520 1521 1522
assert(twice.type().equals(intfn1));
assert((int)twice.invokeExact(21) == 42);
     * </pre></blockquote>
1523 1524
     * @param target the method handle to invoke after arguments are reordered
     * @param newType the expected type of the new method handle
1525 1526
     * @param reorder an index array which controls the reordering
     * @return a method handle which delegates to the target after it
1527
     *           drops unused arguments and moves and/or duplicates the other arguments
1528
     * @throws NullPointerException if any argument is null
1529 1530 1531 1532 1533
     * @throws IllegalArgumentException if the index array length is not equal to
     *                  the arity of the target, or if any index array element
     *                  not a valid index for a parameter of {@code newType},
     *                  or if two corresponding parameter types in
     *                  {@code target.type()} and {@code newType} are not identical,
1534 1535
     */
    public static
1536
    MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) {
1537 1538
        checkReorder(reorder, newType, target.type());
        return target.permuteArguments(newType, reorder);
1539 1540 1541
    }

    private static void checkReorder(int[] reorder, MethodType newType, MethodType oldType) {
1542 1543 1544
        if (newType.returnType() != oldType.returnType())
            throw newIllegalArgumentException("return types do not match",
                    oldType, newType);
1545 1546 1547
        if (reorder.length == oldType.parameterCount()) {
            int limit = newType.parameterCount();
            boolean bad = false;
1548 1549
            for (int j = 0; j < reorder.length; j++) {
                int i = reorder[j];
1550 1551 1552
                if (i < 0 || i >= limit) {
                    bad = true; break;
                }
1553 1554 1555 1556 1557
                Class<?> src = newType.parameterType(i);
                Class<?> dst = oldType.parameterType(j);
                if (src != dst)
                    throw newIllegalArgumentException("parameter types do not match after reorder",
                            oldType, newType);
1558 1559 1560
            }
            if (!bad)  return;
        }
1561
        throw newIllegalArgumentException("bad reorder array: "+Arrays.toString(reorder));
1562 1563 1564
    }

    /**
1565
     * Produces a method handle of the requested return type which returns the given
1566 1567 1568 1569 1570
     * constant value every time it is invoked.
     * <p>
     * Before the method handle is returned, the passed-in value is converted to the requested type.
     * If the requested type is primitive, widening primitive conversions are attempted,
     * else reference conversions are attempted.
1571
     * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
1572 1573 1574
     * @param type the return type of the desired method handle
     * @param value the value to return
     * @return a method handle of the given return type and no arguments, which always returns the given value
1575 1576 1577
     * @throws NullPointerException if the {@code type} argument is null
     * @throws ClassCastException if the value cannot be converted to the required return type
     * @throws IllegalArgumentException if the given type is {@code void.class}
1578 1579 1580 1581
     */
    public static
    MethodHandle constant(Class<?> type, Object value) {
        if (type.isPrimitive()) {
1582 1583
            if (type == void.class)
                throw newIllegalArgumentException("void type");
1584
            Wrapper w = Wrapper.forPrimitiveType(type);
1585
            return insertArguments(identity(type), 0, w.convert(value, type));
1586 1587 1588 1589 1590 1591
        } else {
            return identity(type).bindTo(type.cast(value));
        }
    }

    /**
1592 1593 1594 1595 1596
     * Produces a method handle which returns its sole argument when invoked.
     * @param type the type of the sole parameter and return value of the desired method handle
     * @return a unary method handle which accepts and returns the given type
     * @throws NullPointerException if the argument is null
     * @throws IllegalArgumentException if the given type is {@code void.class}
1597 1598 1599
     */
    public static
    MethodHandle identity(Class<?> type) {
1600 1601
        if (type == void.class)
            throw newIllegalArgumentException("void type");
1602 1603 1604 1605 1606
        else if (type == Object.class)
            return ValueConversions.identity();
        else if (type.isPrimitive())
            return ValueConversions.identity(Wrapper.forPrimitiveType(type));
        else
1607
            return MethodHandleImpl.makeReferenceIdentity(type);
1608 1609 1610
    }

    /**
1611 1612 1613 1614 1615 1616 1617 1618 1619
     * Provides a target method handle with one or more <em>bound arguments</em>
     * in advance of the method handle's invocation.
     * The formal parameters to the target corresponding to the bound
     * arguments are called <em>bound parameters</em>.
     * Returns a new method handle which saves away the bound arguments.
     * When it is invoked, it receives arguments for any non-bound parameters,
     * binds the saved arguments to their corresponding parameters,
     * and calls the original target.
     * <p>
1620 1621 1622
     * The type of the new method handle will drop the types for the bound
     * parameters from the original target type, since the new method handle
     * will no longer require those arguments to be supplied by its callers.
1623
     * <p>
1624 1625 1626
     * Each given argument object must match the corresponding bound parameter type.
     * If a bound parameter type is a primitive, the argument object
     * must be a wrapper, and will be unboxed to produce the primitive value.
1627
     * <p>
1628 1629 1630 1631
     * The {@code pos} argument selects which parameters are to be bound.
     * It may range between zero and <i>N-L</i> (inclusively),
     * where <i>N</i> is the arity of the target method handle
     * and <i>L</i> is the length of the values array.
1632 1633
     * @param target the method handle to invoke after the argument is inserted
     * @param pos where to insert the argument (zero for the first)
1634
     * @param values the series of arguments to insert
1635
     * @return a method handle which inserts an additional argument,
1636
     *         before calling the original method handle
1637
     * @throws NullPointerException if the target or the {@code values} array is null
1638
     * @see MethodHandle#bindTo
1639 1640
     */
    public static
1641 1642
    MethodHandle insertArguments(MethodHandle target, int pos, Object... values) {
        int insCount = values.length;
1643 1644
        MethodType oldType = target.type();
        int outargs = oldType.parameterCount();
1645 1646 1647 1648
        int inargs  = outargs - insCount;
        if (inargs < 0)
            throw newIllegalArgumentException("too many values to insert");
        if (pos < 0 || pos > inargs)
1649
            throw newIllegalArgumentException("no argument type to append");
1650 1651 1652
        MethodHandle result = target;
        for (int i = 0; i < insCount; i++) {
            Object value = values[i];
1653 1654 1655 1656 1657 1658 1659 1660
            Class<?> ptype = oldType.parameterType(pos+i);
            if (ptype.isPrimitive()) {
                char btype = 'I';
                Wrapper w = Wrapper.forPrimitiveType(ptype);
                switch (w) {
                case LONG:    btype = 'J'; break;
                case FLOAT:   btype = 'F'; break;
                case DOUBLE:  btype = 'D'; break;
1661
                }
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
                // perform unboxing and/or primitive conversion
                value = w.convert(value, ptype);
                result = result.bindArgument(pos, btype, value);
                continue;
            }
            value = ptype.cast(value);  // throw CCE if needed
            if (pos == 0) {
                result = result.bindReceiver(value);
            } else {
                result = result.bindArgument(pos, 'L', value);
1672
            }
1673
        }
1674 1675 1676
        return result;
    }

1677
    /**
1678 1679 1680 1681 1682
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
1683
     * <p>
1684 1685 1686 1687 1688
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
1689 1690 1691
     * <p>
     * <b>Example:</b>
     * <p><blockquote><pre>
1692 1693
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
1694 1695 1696 1697
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
1698 1699 1700 1701 1702 1703 1704 1705 1706
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
     * </pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <p><blockquote><pre>
     * {@link #dropArguments(MethodHandle,int,Class...) dropArguments}(target, pos, valueTypes.toArray(new Class[0]))
1707
     * </pre></blockquote>
1708 1709 1710 1711
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
1712
     *         before calling the original method handle
1713
     * @throws NullPointerException if the target is null,
1714
     *                              or if the {@code valueTypes} list or any of its elements is null
1715 1716 1717
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
1718 1719
     */
    public static
1720
    MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
1721
        MethodType oldType = target.type();  // get NPE
1722 1723 1724
        int dropped = valueTypes.size();
        MethodType.checkSlotCount(dropped);
        if (dropped == 0)  return target;
1725
        int outargs = oldType.parameterCount();
1726
        int inargs  = outargs + dropped;
1727 1728
        if (pos < 0 || pos >= inargs)
            throw newIllegalArgumentException("no argument type to remove");
1729
        ArrayList<Class<?>> ptypes = new ArrayList<>(oldType.parameterList());
1730 1731
        ptypes.addAll(pos, valueTypes);
        MethodType newType = MethodType.methodType(oldType.returnType(), ptypes);
1732
        return target.dropArguments(newType, pos, dropped);
1733 1734
    }

1735
    /**
1736 1737 1738 1739 1740
     * Produces a method handle which will discard some dummy arguments
     * before calling some other specified <i>target</i> method handle.
     * The type of the new method handle will be the same as the target's type,
     * except it will also include the dummy argument types,
     * at some given position.
1741
     * <p>
1742 1743 1744 1745 1746
     * The {@code pos} argument may range between zero and <i>N</i>,
     * where <i>N</i> is the arity of the target.
     * If {@code pos} is zero, the dummy arguments will precede
     * the target's real arguments; if {@code pos} is <i>N</i>
     * they will come after.
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
     * <p>
     * <b>Example:</b>
     * <p><blockquote><pre>
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
MethodHandle d0 = dropArguments(cat, 0, String.class);
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
MethodHandle d1 = dropArguments(cat, 1, String.class);
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
MethodHandle d2 = dropArguments(cat, 2, String.class);
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
     * </pre></blockquote>
     * <p>
     * This method is also equivalent to the following code:
     * <p><blockquote><pre>
1768
     * {@link #dropArguments(MethodHandle,int,List) dropArguments}(target, pos, Arrays.asList(valueTypes))
1769
     * </pre></blockquote>
1770 1771 1772 1773 1774
     * @param target the method handle to invoke after the arguments are dropped
     * @param valueTypes the type(s) of the argument(s) to drop
     * @param pos position of first argument to drop (zero for the leftmost)
     * @return a method handle which drops arguments of the given types,
     *         before calling the original method handle
1775
     * @throws NullPointerException if the target is null,
1776
     *                              or if the {@code valueTypes} array or any of its elements is null
1777 1778 1779
     * @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class},
     *                  or if {@code pos} is negative or greater than the arity of the target,
     *                  or if the new method handle's type would have too many parameters
1780
     */
1781 1782 1783 1784 1785 1786
    public static
    MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) {
        return dropArguments(target, pos, Arrays.asList(valueTypes));
    }

    /**
1787
     * Adapts a target method handle by pre-processing
1788 1789 1790 1791 1792
     * one or more of its arguments, each with its own unary filter function,
     * and then calling the target with each pre-processed argument
     * replaced by the result of its corresponding filter function.
     * <p>
     * The pre-processing is performed by one or more method handles,
1793
     * specified in the elements of the {@code filters} array.
1794 1795 1796 1797 1798
     * The first element of the filter array corresponds to the {@code pos}
     * argument of the target, and so on in sequence.
     * <p>
     * Null arguments in the array are treated as identity functions,
     * and the corresponding arguments left unchanged.
1799
     * (If there are no non-null elements in the array, the original target is returned.)
1800
     * Each filter is applied to the corresponding argument of the adapter.
1801 1802
     * <p>
     * If a filter {@code F} applies to the {@code N}th argument of
1803
     * the target, then {@code F} must be a method handle which
1804 1805 1806 1807 1808 1809
     * takes exactly one argument.  The type of {@code F}'s sole argument
     * replaces the corresponding argument type of the target
     * in the resulting adapted method handle.
     * The return type of {@code F} must be identical to the corresponding
     * parameter type of the target.
     * <p>
1810
     * It is an error if there are elements of {@code filters}
1811
     * (null or not)
1812
     * which do not correspond to argument positions in the target.
1813 1814
     * <b>Example:</b>
     * <p><blockquote><pre>
1815 1816
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
1817 1818 1819 1820 1821
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
MethodHandle upcase = lookup().findVirtual(String.class,
  "toUpperCase", methodType(String.class));
1822
assertEquals("xy", (String) cat.invokeExact("x", "y"));
1823
MethodHandle f0 = filterArguments(cat, 0, upcase);
1824
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
1825
MethodHandle f1 = filterArguments(cat, 1, upcase);
1826
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
1827
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
1828
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
1829 1830 1831 1832 1833 1834 1835 1836
     * </pre></blockquote>
     * <p> Here is pseudocode for the resulting adapter:
     * <blockquote><pre>
     * V target(P... p, A[i]... a[i], B... b);
     * A[i] filter[i](V[i]);
     * T adapter(P... p, V[i]... v[i], B... b) {
     *   return target(p..., f[i](v[i])..., b...);
     * }
1837
     * </pre></blockquote>
1838
     *
1839
     * @param target the method handle to invoke after arguments are filtered
1840
     * @param pos the position of the first argument to filter
1841 1842
     * @param filters method handles to call initially on filtered arguments
     * @return method handle which incorporates the specified argument filtering logic
1843
     * @throws NullPointerException if the target is null
1844 1845
     *                              or if the {@code filters} array is null
     * @throws IllegalArgumentException if a non-null element of {@code filters}
1846
     *          does not match a corresponding argument type of target as described above,
1847
     *          or if the {@code pos+filters.length} is greater than {@code target.type().parameterCount()}
1848 1849
     */
    public static
1850
    MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) {
1851 1852
        MethodType targetType = target.type();
        MethodHandle adapter = target;
1853 1854
        MethodType adapterType = null;
        assert((adapterType = targetType) != null);
1855
        int maxPos = targetType.parameterCount();
1856 1857 1858
        if (pos + filters.length > maxPos)
            throw newIllegalArgumentException("too many filters");
        int curPos = pos-1;  // pre-incremented
1859
        for (MethodHandle filter : filters) {
1860 1861
            curPos += 1;
            if (filter == null)  continue;  // ignore null elements of filters
1862 1863
            adapter = filterArgument(adapter, curPos, filter);
            assert((adapterType = adapterType.changeParameterType(curPos, filter.type().parameterType(0))) != null);
1864
        }
1865
        assert(adapterType.equals(adapter.type()));
1866 1867 1868
        return adapter;
    }

1869 1870 1871 1872 1873 1874 1875
    /*non-public*/ static
    MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) {
        MethodType targetType = target.type();
        MethodType filterType = filter.type();
        if (filterType.parameterCount() != 1
            || filterType.returnType() != targetType.parameterType(pos))
            throw newIllegalArgumentException("target and filter types do not match", targetType, filterType);
1876
        return MethodHandleImpl.makeCollectArguments(target, filter, pos, false);
1877 1878
    }

1879
    /**
1880 1881 1882
     * Adapts a target method handle by post-processing
     * its return value (if any) with a filter (another method handle).
     * The result of the filter is returned from the adapter.
1883
     * <p>
1884 1885 1886 1887 1888
     * If the target returns a value, the filter must accept that value as
     * its only argument.
     * If the target returns void, the filter must accept no arguments.
     * <p>
     * The return type of the filter
1889 1890
     * replaces the return type of the target
     * in the resulting adapted method handle.
1891
     * The argument type of the filter (if any) must be identical to the
1892 1893 1894
     * return type of the target.
     * <b>Example:</b>
     * <p><blockquote><pre>
1895 1896
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
1897 1898 1899 1900 1901 1902 1903 1904
...
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
MethodHandle length = lookup().findVirtual(String.class,
  "length", methodType(int.class));
System.out.println((String) cat.invokeExact("x", "y")); // xy
MethodHandle f0 = filterReturnValue(cat, length);
System.out.println((int) f0.invokeExact("x", "y")); // 2
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
     * </pre></blockquote>
     * <p> Here is pseudocode for the resulting adapter:
     * <blockquote><pre>
     * V target(A...);
     * T filter(V);
     * T adapter(A... a) {
     *   V v = target(a...);
     *   return filter(v);
     * }
     * // and if the target has a void return:
     * void target2(A...);
     * T filter2();
     * T adapter2(A... a) {
     *   target2(a...);
     *   return filter2();
     * }
     * // and if the filter has a void return:
     * V target3(A...);
     * void filter3(V);
     * void adapter3(A... a) {
     *   V v = target3(a...);
     *   filter3(v);
     * }
1928 1929 1930 1931
     * </pre></blockquote>
     * @param target the method handle to invoke before filtering the return value
     * @param filter method handle to call on the return value
     * @return method handle which incorporates the specified return value filtering logic
1932
     * @throws NullPointerException if either argument is null
1933 1934
     * @throws IllegalArgumentException if the argument list of {@code filter}
     *          does not match the return type of target as described above
1935
     */
1936
    public static
1937 1938 1939
    MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
        MethodType targetType = target.type();
        MethodType filterType = filter.type();
1940 1941 1942 1943 1944 1945
        Class<?> rtype = targetType.returnType();
        int filterValues = filterType.parameterCount();
        if (filterValues == 0
                ? (rtype != void.class)
                : (rtype != filterType.parameterType(0)))
            throw newIllegalArgumentException("target and filter types do not match", target, filter);
1946 1947
        // result = fold( lambda(retval, arg...) { filter(retval) },
        //                lambda(        arg...) { target(arg...) } )
1948
        return MethodHandleImpl.makeCollectArguments(filter, target, 0, false);
1949 1950
    }

1951
    /**
1952
     * Adapts a target method handle by pre-processing
1953
     * some of its arguments, and then calling the target with
1954 1955
     * the result of the pre-processing, inserted into the original
     * sequence of arguments.
1956
     * <p>
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
     * The pre-processing is performed by {@code combiner}, a second method handle.
     * Of the arguments passed to the adapter, the first {@code N} arguments
     * are copied to the combiner, which is then called.
     * (Here, {@code N} is defined as the parameter count of the combiner.)
     * After this, control passes to the target, with any result
     * from the combiner inserted before the original {@code N} incoming
     * arguments.
     * <p>
     * If the combiner returns a value, the first parameter type of the target
     * must be identical with the return type of the combiner, and the next
     * {@code N} parameter types of the target must exactly match the parameters
     * of the combiner.
     * <p>
     * If the combiner has a void return, no result will be inserted,
     * and the first {@code N} parameter types of the target
     * must exactly match the parameters of the combiner.
1973 1974
     * <p>
     * The resulting adapter is the same type as the target, except that the
1975 1976
     * first parameter type is dropped,
     * if it corresponds to the result of the combiner.
1977
     * <p>
1978
     * (Note that {@link #dropArguments(MethodHandle,int,List) dropArguments} can be used to remove any arguments
1979
     * that either the combiner or the target does not wish to receive.
1980
     * If some of the incoming arguments are destined only for the combiner,
1981
     * consider using {@link MethodHandle#asCollector asCollector} instead, since those
1982 1983
     * arguments will not need to be live on the stack on entry to the
     * target.)
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
     * <b>Example:</b>
     * <p><blockquote><pre>
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
...
MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
  "println", methodType(void.class, String.class))
    .bindTo(System.out);
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
MethodHandle catTrace = foldArguments(cat, trace);
// also prints "boo":
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
     * </pre></blockquote>
1999 2000
     * <p> Here is pseudocode for the resulting adapter:
     * <blockquote><pre>
2001
     * // there are N arguments in A...
2002 2003 2004 2005 2006 2007
     * T target(V, A[N]..., B...);
     * V combiner(A...);
     * T adapter(A... a, B... b) {
     *   V v = combiner(a...);
     *   return target(v, a..., b...);
     * }
2008 2009 2010 2011 2012 2013 2014
     * // and if the combiner has a void return:
     * T target2(A[N]..., B...);
     * void combiner2(A...);
     * T adapter2(A... a, B... b) {
     *   combiner2(a...);
     *   return target2(a..., b...);
     * }
2015 2016 2017 2018
     * </pre></blockquote>
     * @param target the method handle to invoke after arguments are combined
     * @param combiner method handle to call initially on the incoming arguments
     * @return method handle which incorporates the specified argument folding logic
2019
     * @throws NullPointerException if either argument is null
2020 2021 2022 2023 2024
     * @throws IllegalArgumentException if {@code combiner}'s return type
     *          is non-void and not the same as the first argument type of
     *          the target, or if the initial {@code N} argument types
     *          of the target
     *          (skipping one matching the {@code combiner}'s return type)
2025 2026 2027 2028
     *          are not identical with the argument types of {@code combiner}
     */
    public static
    MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) {
2029
        int pos = 0;
2030 2031
        MethodType targetType = target.type();
        MethodType combinerType = combiner.type();
2032
        int foldPos = pos;
2033
        int foldArgs = combinerType.parameterCount();
2034 2035 2036 2037 2038 2039
        int foldVals = combinerType.returnType() == void.class ? 0 : 1;
        int afterInsertPos = foldPos + foldVals;
        boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs);
        if (ok && !(combinerType.parameterList()
                    .equals(targetType.parameterList().subList(afterInsertPos,
                                                               afterInsertPos + foldArgs))))
2040
            ok = false;
2041
        if (ok && foldVals != 0 && !combinerType.returnType().equals(targetType.parameterType(0)))
2042
            ok = false;
2043 2044
        if (!ok)
            throw misMatchedTypes("target and combiner types", targetType, combinerType);
2045
        MethodType newType = targetType.dropParameterTypes(foldPos, afterInsertPos);
2046
        return MethodHandleImpl.makeCollectArguments(target, combiner, foldPos, true);
2047 2048
    }

2049
    /**
2050
     * Makes a method handle which adapts a target method handle,
2051 2052 2053 2054
     * by guarding it with a test, a boolean-valued method handle.
     * If the guard fails, a fallback handle is called instead.
     * All three method handles must have the same corresponding
     * argument and return types, except that the return type
2055 2056
     * of the test must be boolean, and the test is allowed
     * to have fewer arguments than the other two method handles.
2057 2058 2059
     * <p> Here is pseudocode for the resulting adapter:
     * <blockquote><pre>
     * boolean test(A...);
2060 2061 2062
     * T target(A...,B...);
     * T fallback(A...,B...);
     * T adapter(A... a,B... b) {
2063
     *   if (test(a...))
2064
     *     return target(a..., b...);
2065
     *   else
2066
     *     return fallback(a..., b...);
2067 2068
     * }
     * </pre></blockquote>
2069 2070 2071
     * Note that the test arguments ({@code a...} in the pseudocode) cannot
     * be modified by execution of the test, and so are passed unchanged
     * from the caller to the target or fallback as appropriate.
2072 2073 2074 2075
     * @param test method handle used for test, must return boolean
     * @param target method handle to call if test passes
     * @param fallback method handle to call if test fails
     * @return method handle which incorporates the specified if/then/else logic
2076
     * @throws NullPointerException if any argument is null
2077 2078
     * @throws IllegalArgumentException if {@code test} does not return boolean,
     *          or if all three method types do not match (with the return
2079
     *          type of {@code test} changed to match that of the target).
2080 2081 2082 2083 2084
     */
    public static
    MethodHandle guardWithTest(MethodHandle test,
                               MethodHandle target,
                               MethodHandle fallback) {
2085 2086 2087
        MethodType gtype = test.type();
        MethodType ttype = target.type();
        MethodType ftype = fallback.type();
2088
        if (!ttype.equals(ftype))
2089
            throw misMatchedTypes("target and fallback types", ttype, ftype);
2090 2091 2092 2093 2094 2095 2096
        if (gtype.returnType() != boolean.class)
            throw newIllegalArgumentException("guard type is not a predicate "+gtype);
        List<Class<?>> targs = ttype.parameterList();
        List<Class<?>> gargs = gtype.parameterList();
        if (!targs.equals(gargs)) {
            int gpc = gargs.size(), tpc = targs.size();
            if (gpc >= tpc || !targs.subList(0, gpc).equals(gargs))
2097
                throw misMatchedTypes("target and test types", ttype, gtype);
2098 2099
            test = dropArguments(test, gpc, targs.subList(gpc, tpc));
            gtype = test.type();
2100
        }
2101
        return MethodHandleImpl.makeGuardWithTest(test, target, fallback);
2102 2103
    }

2104 2105 2106 2107
    static RuntimeException misMatchedTypes(String what, MethodType t1, MethodType t2) {
        return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2);
    }

2108
    /**
2109
     * Makes a method handle which adapts a target method handle,
2110 2111 2112 2113
     * by running it inside an exception handler.
     * If the target returns normally, the adapter returns that value.
     * If an exception matching the specified type is thrown, the fallback
     * handle is called instead on the exception, plus the original arguments.
2114
     * <p>
2115 2116 2117 2118
     * The target and handler must have the same corresponding
     * argument and return types, except that handler may omit trailing arguments
     * (similarly to the predicate in {@link #guardWithTest guardWithTest}).
     * Also, the handler must have an extra leading parameter of {@code exType} or a supertype.
2119 2120
     * <p> Here is pseudocode for the resulting adapter:
     * <blockquote><pre>
2121
     * T target(A..., B...);
2122
     * T handler(ExType, A...);
2123
     * T adapter(A... a, B... b) {
2124
     *   try {
2125
     *     return target(a..., b...);
2126 2127 2128
     *   } catch (ExType ex) {
     *     return handler(ex, a...);
     *   }
2129 2130
     * }
     * </pre></blockquote>
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
     * Note that the saved arguments ({@code a...} in the pseudocode) cannot
     * be modified by execution of the target, and so are passed unchanged
     * from the caller to the handler, if the handler is invoked.
     * <p>
     * The target and handler must return the same type, even if the handler
     * always throws.  (This might happen, for instance, because the handler
     * is simulating a {@code finally} clause).
     * To create such a throwing handler, compose the handler creation logic
     * with {@link #throwException throwException},
     * in order to create a method handle of the correct return type.
2141 2142 2143 2144
     * @param target method handle to call
     * @param exType the type of exception which the handler will catch
     * @param handler method handle to call if a matching exception is thrown
     * @return method handle which incorporates the specified try/catch logic
2145
     * @throws NullPointerException if any argument is null
2146 2147 2148 2149
     * @throws IllegalArgumentException if {@code handler} does not accept
     *          the given exception type, or if the method handle types do
     *          not match in their return types and their
     *          corresponding parameters
2150 2151
     */
    public static
2152 2153 2154
    MethodHandle catchException(MethodHandle target,
                                Class<? extends Throwable> exType,
                                MethodHandle handler) {
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
        MethodType ttype = target.type();
        MethodType htype = handler.type();
        if (htype.parameterCount() < 1 ||
            !htype.parameterType(0).isAssignableFrom(exType))
            throw newIllegalArgumentException("handler does not accept exception type "+exType);
        if (htype.returnType() != ttype.returnType())
            throw misMatchedTypes("target and handler return types", ttype, htype);
        List<Class<?>> targs = ttype.parameterList();
        List<Class<?>> hargs = htype.parameterList();
        hargs = hargs.subList(1, hargs.size());  // omit leading parameter from handler
        if (!targs.equals(hargs)) {
            int hpc = hargs.size(), tpc = targs.size();
            if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs))
                throw misMatchedTypes("target and handler types", ttype, htype);
2169
            handler = dropArguments(handler, 1+hpc, targs.subList(hpc, tpc));
2170 2171
            htype = handler.type();
        }
2172
        return MethodHandleImpl.makeGuardWithCatch(target, exType, handler);
2173 2174
    }

2175
    /**
2176
     * Produces a method handle which will throw exceptions of the given {@code exType}.
2177 2178 2179 2180 2181
     * The method handle will accept a single argument of {@code exType},
     * and immediately throw it as an exception.
     * The method type will nominally specify a return of {@code returnType}.
     * The return type may be anything convenient:  It doesn't matter to the
     * method handle's behavior, since it will never return normally.
2182 2183
     * @return method handle which can throw the given exceptions
     * @throws NullPointerException if either argument is null
2184 2185 2186
     */
    public static
    MethodHandle throwException(Class<?> returnType, Class<? extends Throwable> exType) {
2187 2188
        if (!Throwable.class.isAssignableFrom(exType))
            throw new ClassCastException(exType.getName());
2189
        return MethodHandleImpl.throwException(MethodType.methodType(returnType, exType));
2190
    }
2191
}