rt/emul/compact/src/main/java/java/lang/invoke/LambdaMetafactory.java
branchjdk8-b132
changeset 1646 c880a8a8803b
child 1653 bd151459ee4f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/invoke/LambdaMetafactory.java	Sat Aug 09 11:11:13 2014 +0200
     1.3 @@ -0,0 +1,475 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.invoke;
    1.30 +
    1.31 +import java.io.Serializable;
    1.32 +import java.util.Arrays;
    1.33 +
    1.34 +/**
    1.35 + * <p>Methods to facilitate the creation of simple "function objects" that
    1.36 + * implement one or more interfaces by delegation to a provided {@link MethodHandle},
    1.37 + * possibly after type adaptation and partial evaluation of arguments.  These
    1.38 + * methods are typically used as <em>bootstrap methods</em> for {@code invokedynamic}
    1.39 + * call sites, to support the <em>lambda expression</em> and <em>method
    1.40 + * reference expression</em> features of the Java Programming Language.
    1.41 + *
    1.42 + * <p>Indirect access to the behavior specified by the provided {@code MethodHandle}
    1.43 + * proceeds in order through three phases:
    1.44 + * <ul>
    1.45 + *     <li><em>Linkage</em> occurs when the methods in this class are invoked.
    1.46 + *     They take as arguments an interface to be implemented (typically a
    1.47 + *     <em>functional interface</em>, one with a single abstract method), a
    1.48 + *     name and signature of a method from that interface to be implemented, a
    1.49 + *     method handle describing the desired implementation behavior
    1.50 + *     for that method, and possibly other additional metadata, and produce a
    1.51 + *     {@link CallSite} whose target can be used to create suitable function
    1.52 + *     objects.  Linkage may involve dynamically loading a new class that
    1.53 + *     implements the target interface. The {@code CallSite} can be considered a
    1.54 + *     "factory" for function objects and so these linkage methods are referred
    1.55 + *     to as "metafactories".</li>
    1.56 + *
    1.57 + *     <li><em>Capture</em> occurs when the {@code CallSite}'s target is
    1.58 + *     invoked, typically through an {@code invokedynamic} call site,
    1.59 + *     producing a function object.  This may occur many times for
    1.60 + *     a single factory {@code CallSite}.  Capture may involve allocation of a
    1.61 + *     new function object, or may return an existing function object.  The
    1.62 + *     behavior {@code MethodHandle} may have additional parameters beyond those
    1.63 + *     of the specified interface method; these are referred to as <em>captured
    1.64 + *     parameters</em>, which must be provided as arguments to the
    1.65 + *     {@code CallSite} target, and which may be early-bound to the behavior
    1.66 + *     {@code MethodHandle}.  The number of captured parameters and their types
    1.67 + *     are determined during linkage.</li>
    1.68 + *
    1.69 + *     <li><em>Invocation</em> occurs when an implemented interface method
    1.70 + *     is invoked on a function object.  This may occur many times for a single
    1.71 + *     function object.  The method referenced by the behavior {@code MethodHandle}
    1.72 + *     is invoked with the captured arguments and any additional arguments
    1.73 + *     provided on invocation, as if by {@link MethodHandle#invoke(Object...)}.</li>
    1.74 + * </ul>
    1.75 + *
    1.76 + * <p>It is sometimes useful to restrict the set of inputs or results permitted
    1.77 + * at invocation.  For example, when the generic interface {@code Predicate<T>}
    1.78 + * is parameterized as {@code Predicate<String>}, the input must be a
    1.79 + * {@code String}, even though the method to implement allows any {@code Object}.
    1.80 + * At linkage time, an additional {@link MethodType} parameter describes the
    1.81 + * "instantiated" method type; on invocation, the arguments and eventual result
    1.82 + * are checked against this {@code MethodType}.
    1.83 + *
    1.84 + * <p>This class provides two forms of linkage methods: a standard version
    1.85 + * ({@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)})
    1.86 + * using an optimized protocol, and an alternate version
    1.87 + * {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}).
    1.88 + * The alternate version is a generalization of the standard version, providing
    1.89 + * additional control over the behavior of the generated function objects via
    1.90 + * flags and additional arguments.  The alternate version adds the ability to
    1.91 + * manage the following attributes of function objects:
    1.92 + *
    1.93 + * <ul>
    1.94 + *     <li><em>Bridging.</em>  It is sometimes useful to implement multiple
    1.95 + *     variations of the method signature, involving argument or return type
    1.96 + *     adaptation.  This occurs when multiple distinct VM signatures for a method
    1.97 + *     are logically considered to be the same method by the language.  The
    1.98 + *     flag {@code FLAG_BRIDGES} indicates that a list of additional
    1.99 + *     {@code MethodType}s will be provided, each of which will be implemented
   1.100 + *     by the resulting function object.  These methods will share the same
   1.101 + *     name and instantiated type.</li>
   1.102 + *
   1.103 + *     <li><em>Multiple interfaces.</em>  If needed, more than one interface
   1.104 + *     can be implemented by the function object.  (These additional interfaces
   1.105 + *     are typically marker interfaces with no methods.)  The flag {@code FLAG_MARKERS}
   1.106 + *     indicates that a list of additional interfaces will be provided, each of
   1.107 + *     which should be implemented by the resulting function object.</li>
   1.108 + *
   1.109 + *     <li><em>Serializability.</em>  The generated function objects do not
   1.110 + *     generally support serialization.  If desired, {@code FLAG_SERIALIZABLE}
   1.111 + *     can be used to indicate that the function objects should be serializable.
   1.112 + *     Serializable function objects will use, as their serialized form,
   1.113 + *     instances of the class {@code SerializedLambda}, which requires additional
   1.114 + *     assistance from the capturing class (the class described by the
   1.115 + *     {@link MethodHandles.Lookup} parameter {@code caller}); see
   1.116 + *     {@link SerializedLambda} for details.</li>
   1.117 + * </ul>
   1.118 + *
   1.119 + * <p>Assume the linkage arguments are as follows:
   1.120 + * <ul>
   1.121 + *      <li>{@code invokedType} (describing the {@code CallSite} signature) has
   1.122 + *      K parameters of types (D1..Dk) and return type Rd;</li>
   1.123 + *      <li>{@code samMethodType} (describing the implemented method type) has N
   1.124 + *      parameters, of types (U1..Un) and return type Ru;</li>
   1.125 + *      <li>{@code implMethod} (the {@code MethodHandle} providing the
   1.126 + *      implementation has M parameters, of types (A1..Am) and return type Ra
   1.127 + *      (if the method describes an instance method, the method type of this
   1.128 + *      method handle already includes an extra first argument corresponding to
   1.129 + *      the receiver);</li>
   1.130 + *      <li>{@code instantiatedMethodType} (allowing restrictions on invocation)
   1.131 + *      has N parameters, of types (T1..Tn) and return type Rt.</li>
   1.132 + * </ul>
   1.133 + *
   1.134 + * <p>Then the following linkage invariants must hold:
   1.135 + * <ul>
   1.136 + *     <li>Rd is an interface</li>
   1.137 + *     <li>{@code implMethod} is a <em>direct method handle</em></li>
   1.138 + *     <li>{@code samMethodType} and {@code instantiatedMethodType} have the same
   1.139 + *     arity N, and for i=1..N, Ti and Ui are the same type, or Ti and Ui are
   1.140 + *     both reference types and Ti is a subtype of Ui</li>
   1.141 + *     <li>Either Rt and Ru are the same type, or both are reference types and
   1.142 + *     Rt is a subtype of Ru</li>
   1.143 + *     <li>K + N = M</li>
   1.144 + *     <li>For i=1..K, Di = Ai</li>
   1.145 + *     <li>For i=1..N, Ti is adaptable to Aj, where j=i+k</li>
   1.146 + *     <li>The return type Rt is void, or the return type Ra is not void and is
   1.147 + *     adaptable to Rt</li>
   1.148 + * </ul>
   1.149 + *
   1.150 + * <p>Further, at capture time, if {@code implMethod} corresponds to an instance
   1.151 + * method, and there are any capture arguments ({@code K > 0}), then the first
   1.152 + * capture argument (corresponding to the receiver) must be non-null.
   1.153 + *
   1.154 + * <p>A type Q is considered adaptable to S as follows:
   1.155 + * <table summary="adaptable types">
   1.156 + *     <tr><th>Q</th><th>S</th><th>Link-time checks</th><th>Invocation-time checks</th></tr>
   1.157 + *     <tr>
   1.158 + *         <td>Primitive</td><td>Primitive</td>
   1.159 + *         <td>Q can be converted to S via a primitive widening conversion</td>
   1.160 + *         <td>None</td>
   1.161 + *     </tr>
   1.162 + *     <tr>
   1.163 + *         <td>Primitive</td><td>Reference</td>
   1.164 + *         <td>S is a supertype of the Wrapper(Q)</td>
   1.165 + *         <td>Cast from Wrapper(Q) to S</td>
   1.166 + *     </tr>
   1.167 + *     <tr>
   1.168 + *         <td>Reference</td><td>Primitive</td>
   1.169 + *         <td>for parameter types: Q is a primitive wrapper and Primitive(Q)
   1.170 + *         can be widened to S
   1.171 + *         <br>for return types: If Q is a primitive wrapper, check that
   1.172 + *         Primitive(Q) can be widened to S</td>
   1.173 + *         <td>If Q is not a primitive wrapper, cast Q to the base Wrapper(S);
   1.174 + *         for example Number for numeric types</td>
   1.175 + *     </tr>
   1.176 + *     <tr>
   1.177 + *         <td>Reference</td><td>Reference</td>
   1.178 + *         <td>for parameter types: S is a supertype of Q
   1.179 + *         <br>for return types: none</td>
   1.180 + *         <td>Cast from Q to S</td>
   1.181 + *     </tr>
   1.182 + * </table>
   1.183 + *
   1.184 + * @apiNote These linkage methods are designed to support the evaluation
   1.185 + * of <em>lambda expressions</em> and <em>method references</em> in the Java
   1.186 + * Language.  For every lambda expressions or method reference in the source code,
   1.187 + * there is a target type which is a functional interface.  Evaluating a lambda
   1.188 + * expression produces an object of its target type. The recommended mechanism
   1.189 + * for evaluating lambda expressions is to desugar the lambda body to a method,
   1.190 + * invoke an invokedynamic call site whose static argument list describes the
   1.191 + * sole method of the functional interface and the desugared implementation
   1.192 + * method, and returns an object (the lambda object) that implements the target
   1.193 + * type. (For method references, the implementation method is simply the
   1.194 + * referenced method; no desugaring is needed.)
   1.195 + *
   1.196 + * <p>The argument list of the implementation method and the argument list of
   1.197 + * the interface method(s) may differ in several ways.  The implementation
   1.198 + * methods may have additional arguments to accommodate arguments captured by
   1.199 + * the lambda expression; there may also be differences resulting from permitted
   1.200 + * adaptations of arguments, such as casting, boxing, unboxing, and primitive
   1.201 + * widening. (Varargs adaptations are not handled by the metafactories; these are
   1.202 + * expected to be handled by the caller.)
   1.203 + *
   1.204 + * <p>Invokedynamic call sites have two argument lists: a static argument list
   1.205 + * and a dynamic argument list.  The static argument list is stored in the
   1.206 + * constant pool; the dynamic argument is pushed on the operand stack at capture
   1.207 + * time.  The bootstrap method has access to the entire static argument list
   1.208 + * (which in this case, includes information describing the implementation method,
   1.209 + * the target interface, and the target interface method(s)), as well as a
   1.210 + * method signature describing the number and static types (but not the values)
   1.211 + * of the dynamic arguments and the static return type of the invokedynamic site.
   1.212 + *
   1.213 + * @implNote The implementation method is described with a method handle. In
   1.214 + * theory, any method handle could be used. Currently supported are direct method
   1.215 + * handles representing invocation of virtual, interface, constructor and static
   1.216 + * methods.
   1.217 + */
   1.218 +public class LambdaMetafactory {
   1.219 +
   1.220 +    /** Flag for alternate metafactories indicating the lambda object
   1.221 +     * must be serializable */
   1.222 +    public static final int FLAG_SERIALIZABLE = 1 << 0;
   1.223 +
   1.224 +    /**
   1.225 +     * Flag for alternate metafactories indicating the lambda object implements
   1.226 +     * other marker interfaces
   1.227 +     * besides Serializable
   1.228 +     */
   1.229 +    public static final int FLAG_MARKERS = 1 << 1;
   1.230 +
   1.231 +    /**
   1.232 +     * Flag for alternate metafactories indicating the lambda object requires
   1.233 +     * additional bridge methods
   1.234 +     */
   1.235 +    public static final int FLAG_BRIDGES = 1 << 2;
   1.236 +
   1.237 +    private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
   1.238 +    private static final MethodType[] EMPTY_MT_ARRAY = new MethodType[0];
   1.239 +
   1.240 +    /**
   1.241 +     * Facilitates the creation of simple "function objects" that implement one
   1.242 +     * or more interfaces by delegation to a provided {@link MethodHandle},
   1.243 +     * after appropriate type adaptation and partial evaluation of arguments.
   1.244 +     * Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
   1.245 +     * call sites, to support the <em>lambda expression</em> and <em>method
   1.246 +     * reference expression</em> features of the Java Programming Language.
   1.247 +     *
   1.248 +     * <p>This is the standard, streamlined metafactory; additional flexibility
   1.249 +     * is provided by {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}.
   1.250 +     * A general description of the behavior of this method is provided
   1.251 +     * {@link LambdaMetafactory above}.
   1.252 +     *
   1.253 +     * <p>When the target of the {@code CallSite} returned from this method is
   1.254 +     * invoked, the resulting function objects are instances of a class which
   1.255 +     * implements the interface named by the return type of {@code invokedType},
   1.256 +     * declares a method with the name given by {@code invokedName} and the
   1.257 +     * signature given by {@code samMethodType}.  It may also override additional
   1.258 +     * methods from {@code Object}.
   1.259 +     *
   1.260 +     * @param caller Represents a lookup context with the accessibility
   1.261 +     *               privileges of the caller.  When used with {@code invokedynamic},
   1.262 +     *               this is stacked automatically by the VM.
   1.263 +     * @param invokedName The name of the method to implement.  When used with
   1.264 +     *                    {@code invokedynamic}, this is provided by the
   1.265 +     *                    {@code NameAndType} of the {@code InvokeDynamic}
   1.266 +     *                    structure and is stacked automatically by the VM.
   1.267 +     * @param invokedType The expected signature of the {@code CallSite}.  The
   1.268 +     *                    parameter types represent the types of capture variables;
   1.269 +     *                    the return type is the interface to implement.   When
   1.270 +     *                    used with {@code invokedynamic}, this is provided by
   1.271 +     *                    the {@code NameAndType} of the {@code InvokeDynamic}
   1.272 +     *                    structure and is stacked automatically by the VM.
   1.273 +     *                    In the event that the implementation method is an
   1.274 +     *                    instance method and this signature has any parameters,
   1.275 +     *                    the first parameter in the invocation signature must
   1.276 +     *                    correspond to the receiver.
   1.277 +     * @param samMethodType Signature and return type of method to be implemented
   1.278 +     *                      by the function object.
   1.279 +     * @param implMethod A direct method handle describing the implementation
   1.280 +     *                   method which should be called (with suitable adaptation
   1.281 +     *                   of argument types, return types, and with captured
   1.282 +     *                   arguments prepended to the invocation arguments) at
   1.283 +     *                   invocation time.
   1.284 +     * @param instantiatedMethodType The signature and return type that should
   1.285 +     *                               be enforced dynamically at invocation time.
   1.286 +     *                               This may be the same as {@code samMethodType},
   1.287 +     *                               or may be a specialization of it.
   1.288 +     * @return a CallSite whose target can be used to perform capture, generating
   1.289 +     *         instances of the interface named by {@code invokedType}
   1.290 +     * @throws LambdaConversionException If any of the linkage invariants
   1.291 +     *                                   described {@link LambdaMetafactory above}
   1.292 +     *                                   are violated
   1.293 +     */
   1.294 +    public static CallSite metafactory(MethodHandles.Lookup caller,
   1.295 +                                       String invokedName,
   1.296 +                                       MethodType invokedType,
   1.297 +                                       MethodType samMethodType,
   1.298 +                                       MethodHandle implMethod,
   1.299 +                                       MethodType instantiatedMethodType)
   1.300 +            throws LambdaConversionException {
   1.301 +        AbstractValidatingLambdaMetafactory mf;
   1.302 +        mf = new InnerClassLambdaMetafactory(caller, invokedType,
   1.303 +                                             invokedName, samMethodType,
   1.304 +                                             implMethod, instantiatedMethodType,
   1.305 +                                             false, EMPTY_CLASS_ARRAY, EMPTY_MT_ARRAY);
   1.306 +        mf.validateMetafactoryArgs();
   1.307 +        return mf.buildCallSite();
   1.308 +    }
   1.309 +
   1.310 +    /**
   1.311 +     * Facilitates the creation of simple "function objects" that implement one
   1.312 +     * or more interfaces by delegation to a provided {@link MethodHandle},
   1.313 +     * after appropriate type adaptation and partial evaluation of arguments.
   1.314 +     * Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
   1.315 +     * call sites, to support the <em>lambda expression</em> and <em>method
   1.316 +     * reference expression</em> features of the Java Programming Language.
   1.317 +     *
   1.318 +     * <p>This is the general, more flexible metafactory; a streamlined version
   1.319 +     * is provided by {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}.
   1.320 +     * A general description of the behavior of this method is provided
   1.321 +     * {@link LambdaMetafactory above}.
   1.322 +     *
   1.323 +     * <p>The argument list for this method includes three fixed parameters,
   1.324 +     * corresponding to the parameters automatically stacked by the VM for the
   1.325 +     * bootstrap method in an {@code invokedynamic} invocation, and an {@code Object[]}
   1.326 +     * parameter that contains additional parameters.  The declared argument
   1.327 +     * list for this method is:
   1.328 +     *
   1.329 +     * <pre>{@code
   1.330 +     *  CallSite altMetafactory(MethodHandles.Lookup caller,
   1.331 +     *                          String invokedName,
   1.332 +     *                          MethodType invokedType,
   1.333 +     *                          Object... args)
   1.334 +     * }</pre>
   1.335 +     *
   1.336 +     * <p>but it behaves as if the argument list is as follows:
   1.337 +     *
   1.338 +     * <pre>{@code
   1.339 +     *  CallSite altMetafactory(MethodHandles.Lookup caller,
   1.340 +     *                          String invokedName,
   1.341 +     *                          MethodType invokedType,
   1.342 +     *                          MethodType samMethodType,
   1.343 +     *                          MethodHandle implMethod,
   1.344 +     *                          MethodType instantiatedMethodType,
   1.345 +     *                          int flags,
   1.346 +     *                          int markerInterfaceCount,  // IF flags has MARKERS set
   1.347 +     *                          Class... markerInterfaces, // IF flags has MARKERS set
   1.348 +     *                          int bridgeCount,           // IF flags has BRIDGES set
   1.349 +     *                          MethodType... bridges      // IF flags has BRIDGES set
   1.350 +     *                          )
   1.351 +     * }</pre>
   1.352 +     *
   1.353 +     * <p>Arguments that appear in the argument list for
   1.354 +     * {@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)}
   1.355 +     * have the same specification as in that method.  The additional arguments
   1.356 +     * are interpreted as follows:
   1.357 +     * <ul>
   1.358 +     *     <li>{@code flags} indicates additional options; this is a bitwise
   1.359 +     *     OR of desired flags.  Defined flags are {@link #FLAG_BRIDGES},
   1.360 +     *     {@link #FLAG_MARKERS}, and {@link #FLAG_SERIALIZABLE}.</li>
   1.361 +     *     <li>{@code markerInterfaceCount} is the number of additional interfaces
   1.362 +     *     the function object should implement, and is present if and only if the
   1.363 +     *     {@code FLAG_MARKERS} flag is set.</li>
   1.364 +     *     <li>{@code markerInterfaces} is a variable-length list of additional
   1.365 +     *     interfaces to implement, whose length equals {@code markerInterfaceCount},
   1.366 +     *     and is present if and only if the {@code FLAG_MARKERS} flag is set.</li>
   1.367 +     *     <li>{@code bridgeCount} is the number of additional method signatures
   1.368 +     *     the function object should implement, and is present if and only if
   1.369 +     *     the {@code FLAG_BRIDGES} flag is set.</li>
   1.370 +     *     <li>{@code bridges} is a variable-length list of additional
   1.371 +     *     methods signatures to implement, whose length equals {@code bridgeCount},
   1.372 +     *     and is present if and only if the {@code FLAG_BRIDGES} flag is set.</li>
   1.373 +     * </ul>
   1.374 +     *
   1.375 +     * <p>Each class named by {@code markerInterfaces} is subject to the same
   1.376 +     * restrictions as {@code Rd}, the return type of {@code invokedType},
   1.377 +     * as described {@link LambdaMetafactory above}.  Each {@code MethodType}
   1.378 +     * named by {@code bridges} is subject to the same restrictions as
   1.379 +     * {@code samMethodType}, as described {@link LambdaMetafactory above}.
   1.380 +     *
   1.381 +     * <p>When FLAG_SERIALIZABLE is set in {@code flags}, the function objects
   1.382 +     * will implement {@code Serializable}, and will have a {@code writeReplace}
   1.383 +     * method that returns an appropriate {@link SerializedLambda}.  The
   1.384 +     * {@code caller} class must have an appropriate {@code $deserializeLambda$}
   1.385 +     * method, as described in {@link SerializedLambda}.
   1.386 +     *
   1.387 +     * <p>When the target of the {@code CallSite} returned from this method is
   1.388 +     * invoked, the resulting function objects are instances of a class with
   1.389 +     * the following properties:
   1.390 +     * <ul>
   1.391 +     *     <li>The class implements the interface named by the return type
   1.392 +     *     of {@code invokedType} and any interfaces named by {@code markerInterfaces}</li>
   1.393 +     *     <li>The class declares methods with the name given by {@code invokedName},
   1.394 +     *     and the signature given by {@code samMethodType} and additional signatures
   1.395 +     *     given by {@code bridges}</li>
   1.396 +     *     <li>The class may override methods from {@code Object}, and may
   1.397 +     *     implement methods related to serialization.</li>
   1.398 +     * </ul>
   1.399 +     *
   1.400 +     * @param caller Represents a lookup context with the accessibility
   1.401 +     *               privileges of the caller.  When used with {@code invokedynamic},
   1.402 +     *               this is stacked automatically by the VM.
   1.403 +     * @param invokedName The name of the method to implement.  When used with
   1.404 +     *                    {@code invokedynamic}, this is provided by the
   1.405 +     *                    {@code NameAndType} of the {@code InvokeDynamic}
   1.406 +     *                    structure and is stacked automatically by the VM.
   1.407 +     * @param invokedType The expected signature of the {@code CallSite}.  The
   1.408 +     *                    parameter types represent the types of capture variables;
   1.409 +     *                    the return type is the interface to implement.   When
   1.410 +     *                    used with {@code invokedynamic}, this is provided by
   1.411 +     *                    the {@code NameAndType} of the {@code InvokeDynamic}
   1.412 +     *                    structure and is stacked automatically by the VM.
   1.413 +     *                    In the event that the implementation method is an
   1.414 +     *                    instance method and this signature has any parameters,
   1.415 +     *                    the first parameter in the invocation signature must
   1.416 +     *                    correspond to the receiver.
   1.417 +     * @param  args       An {@code Object[]} array containing the required
   1.418 +     *                    arguments {@code samMethodType}, {@code implMethod},
   1.419 +     *                    {@code instantiatedMethodType}, {@code flags}, and any
   1.420 +     *                    optional arguments, as described
   1.421 +     *                    {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)} above}
   1.422 +     * @return a CallSite whose target can be used to perform capture, generating
   1.423 +     *         instances of the interface named by {@code invokedType}
   1.424 +     * @throws LambdaConversionException If any of the linkage invariants
   1.425 +     *                                   described {@link LambdaMetafactory above}
   1.426 +     *                                   are violated
   1.427 +     */
   1.428 +    public static CallSite altMetafactory(MethodHandles.Lookup caller,
   1.429 +                                          String invokedName,
   1.430 +                                          MethodType invokedType,
   1.431 +                                          Object... args)
   1.432 +            throws LambdaConversionException {
   1.433 +        MethodType samMethodType = (MethodType)args[0];
   1.434 +        MethodHandle implMethod = (MethodHandle)args[1];
   1.435 +        MethodType instantiatedMethodType = (MethodType)args[2];
   1.436 +        int flags = (Integer) args[3];
   1.437 +        Class<?>[] markerInterfaces;
   1.438 +        MethodType[] bridges;
   1.439 +        int argIndex = 4;
   1.440 +        if ((flags & FLAG_MARKERS) != 0) {
   1.441 +            int markerCount = (Integer) args[argIndex++];
   1.442 +            markerInterfaces = new Class<?>[markerCount];
   1.443 +            System.arraycopy(args, argIndex, markerInterfaces, 0, markerCount);
   1.444 +            argIndex += markerCount;
   1.445 +        }
   1.446 +        else
   1.447 +            markerInterfaces = EMPTY_CLASS_ARRAY;
   1.448 +        if ((flags & FLAG_BRIDGES) != 0) {
   1.449 +            int bridgeCount = (Integer) args[argIndex++];
   1.450 +            bridges = new MethodType[bridgeCount];
   1.451 +            System.arraycopy(args, argIndex, bridges, 0, bridgeCount);
   1.452 +            argIndex += bridgeCount;
   1.453 +        }
   1.454 +        else
   1.455 +            bridges = EMPTY_MT_ARRAY;
   1.456 +
   1.457 +        boolean isSerializable = ((flags & FLAG_SERIALIZABLE) != 0);
   1.458 +        if (isSerializable) {
   1.459 +            boolean foundSerializableSupertype = Serializable.class.isAssignableFrom(invokedType.returnType());
   1.460 +            for (Class<?> c : markerInterfaces)
   1.461 +                foundSerializableSupertype |= Serializable.class.isAssignableFrom(c);
   1.462 +            if (!foundSerializableSupertype) {
   1.463 +                markerInterfaces = Arrays.copyOf(markerInterfaces, markerInterfaces.length + 1);
   1.464 +                markerInterfaces[markerInterfaces.length-1] = Serializable.class;
   1.465 +            }
   1.466 +        }
   1.467 +
   1.468 +        AbstractValidatingLambdaMetafactory mf
   1.469 +                = new InnerClassLambdaMetafactory(caller, invokedType,
   1.470 +                                                  invokedName, samMethodType,
   1.471 +                                                  implMethod,
   1.472 +                                                  instantiatedMethodType,
   1.473 +                                                  isSerializable,
   1.474 +                                                  markerInterfaces, bridges);
   1.475 +        mf.validateMetafactoryArgs();
   1.476 +        return mf.buildCallSite();
   1.477 +    }
   1.478 +}