1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * 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  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import java.io.Serializable;
  29 import java.util.Arrays;
  30 
  31 /**
  32  * <p>Methods to facilitate the creation of simple "function objects" that
  33  * implement one or more interfaces by delegation to a provided {@link MethodHandle},
  34  * possibly after type adaptation and partial evaluation of arguments.  These
  35  * methods are typically used as <em>bootstrap methods</em> for {@code invokedynamic}
  36  * call sites, to support the <em>lambda expression</em> and <em>method
  37  * reference expression</em> features of the Java Programming Language.
  38  *
  39  * <p>Indirect access to the behavior specified by the provided {@code MethodHandle}
  40  * proceeds in order through three phases:
  41  * <ul>
  42  *     <li><em>Linkage</em> occurs when the methods in this class are invoked.
  43  *     They take as arguments an interface to be implemented (typically a
  44  *     <em>functional interface</em>, one with a single abstract method), a
  45  *     name and signature of a method from that interface to be implemented, a
  46  *     method handle describing the desired implementation behavior
  47  *     for that method, and possibly other additional metadata, and produce a
  48  *     {@link CallSite} whose target can be used to create suitable function
  49  *     objects.  Linkage may involve dynamically loading a new class that
  50  *     implements the target interface. The {@code CallSite} can be considered a
  51  *     "factory" for function objects and so these linkage methods are referred
  52  *     to as "metafactories".</li>
  53  *
  54  *     <li><em>Capture</em> occurs when the {@code CallSite}'s target is
  55  *     invoked, typically through an {@code invokedynamic} call site,
  56  *     producing a function object.  This may occur many times for
  57  *     a single factory {@code CallSite}.  Capture may involve allocation of a
  58  *     new function object, or may return an existing function object.  The
  59  *     behavior {@code MethodHandle} may have additional parameters beyond those
  60  *     of the specified interface method; these are referred to as <em>captured
  61  *     parameters</em>, which must be provided as arguments to the
  62  *     {@code CallSite} target, and which may be early-bound to the behavior
  63  *     {@code MethodHandle}.  The number of captured parameters and their types
  64  *     are determined during linkage.
  65  *     The identity of a function object produced by invoking the
  66  *     {@code CallSite}'s target is unpredictable, and therefore
  67  *     identity-sensitive operations (such as reference equality, object
  68  *     locking, and {@code System.identityHashCode()} may produce different
  69  *     results in different implementations, or even upon different invocations
  70  *     in the same implementation.</li>
  71  *
  72  *     <li><em>Invocation</em> occurs when an implemented interface method
  73  *     is invoked on a function object.  This may occur many times for a single
  74  *     function object.  The method referenced by the behavior {@code MethodHandle}
  75  *     is invoked with the captured arguments and any additional arguments
  76  *     provided on invocation, as if by {@link MethodHandle#invoke(Object...)}.</li>
  77  * </ul>
  78  *
  79  * <p>It is sometimes useful to restrict the set of inputs or results permitted
  80  * at invocation.  For example, when the generic interface {@code Predicate<T>}
  81  * is parameterized as {@code Predicate<String>}, the input must be a
  82  * {@code String}, even though the method to implement allows any {@code Object}.
  83  * At linkage time, an additional {@link MethodType} parameter describes the
  84  * "instantiated" method type; on invocation, the arguments and eventual result
  85  * are checked against this {@code MethodType}.
  86  *
  87  * <p>This class provides two forms of linkage methods: a standard version
  88  * ({@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)})
  89  * using an optimized protocol, and an alternate version
  90  * {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}).
  91  * The alternate version is a generalization of the standard version, providing
  92  * additional control over the behavior of the generated function objects via
  93  * flags and additional arguments.  The alternate version adds the ability to
  94  * manage the following attributes of function objects:
  95  *
  96  * <ul>
  97  *     <li><em>Bridging.</em>  It is sometimes useful to implement multiple
  98  *     variations of the method signature, involving argument or return type
  99  *     adaptation.  This occurs when multiple distinct VM signatures for a method
 100  *     are logically considered to be the same method by the language.  The
 101  *     flag {@code FLAG_BRIDGES} indicates that a list of additional
 102  *     {@code MethodType}s will be provided, each of which will be implemented
 103  *     by the resulting function object.  These methods will share the same
 104  *     name and instantiated type.</li>
 105  *
 106  *     <li><em>Multiple interfaces.</em>  If needed, more than one interface
 107  *     can be implemented by the function object.  (These additional interfaces
 108  *     are typically marker interfaces with no methods.)  The flag {@code FLAG_MARKERS}
 109  *     indicates that a list of additional interfaces will be provided, each of
 110  *     which should be implemented by the resulting function object.</li>
 111  *
 112  *     <li><em>Serializability.</em>  The generated function objects do not
 113  *     generally support serialization.  If desired, {@code FLAG_SERIALIZABLE}
 114  *     can be used to indicate that the function objects should be serializable.
 115  *     Serializable function objects will use, as their serialized form,
 116  *     instances of the class {@code SerializedLambda}, which requires additional
 117  *     assistance from the capturing class (the class described by the
 118  *     {@link MethodHandles.Lookup} parameter {@code caller}); see
 119  *     {@link SerializedLambda} for details.</li>
 120  * </ul>
 121  *
 122  * <p>Assume the linkage arguments are as follows:
 123  * <ul>
 124  *      <li>{@code invokedType} (describing the {@code CallSite} signature) has
 125  *      K parameters of types (D1..Dk) and return type Rd;</li>
 126  *      <li>{@code samMethodType} (describing the implemented method type) has N
 127  *      parameters, of types (U1..Un) and return type Ru;</li>
 128  *      <li>{@code implMethod} (the {@code MethodHandle} providing the
 129  *      implementation has M parameters, of types (A1..Am) and return type Ra
 130  *      (if the method describes an instance method, the method type of this
 131  *      method handle already includes an extra first argument corresponding to
 132  *      the receiver);</li>
 133  *      <li>{@code instantiatedMethodType} (allowing restrictions on invocation)
 134  *      has N parameters, of types (T1..Tn) and return type Rt.</li>
 135  * </ul>
 136  *
 137  * <p>Then the following linkage invariants must hold:
 138  * <ul>
 139  *     <li>Rd is an interface</li>
 140  *     <li>{@code implMethod} is a <em>direct method handle</em></li>
 141  *     <li>{@code samMethodType} and {@code instantiatedMethodType} have the same
 142  *     arity N, and for i=1..N, Ti and Ui are the same type, or Ti and Ui are
 143  *     both reference types and Ti is a subtype of Ui</li>
 144  *     <li>Either Rt and Ru are the same type, or both are reference types and
 145  *     Rt is a subtype of Ru</li>
 146  *     <li>K + N = M</li>
 147  *     <li>For i=1..K, Di = Ai</li>
 148  *     <li>For i=1..N, Ti is adaptable to Aj, where j=i+k</li>
 149  *     <li>The return type Rt is void, or the return type Ra is not void and is
 150  *     adaptable to Rt</li>
 151  * </ul>
 152  *
 153  * <p>Further, at capture time, if {@code implMethod} corresponds to an instance
 154  * method, and there are any capture arguments ({@code K > 0}), then the first
 155  * capture argument (corresponding to the receiver) must be non-null.
 156  *
 157  * <p>A type Q is considered adaptable to S as follows:
 158  * <table class="striped">
 159  *   <caption style="display:none">adaptable types</caption>
 160  *   <thead>
 161  *     <tr><th scope="col">Q</th><th scope="col">S</th><th scope="col">Link-time checks</th><th scope="col">Invocation-time checks</th></tr>
 162  *   </thead>
 163  *   <tbody>
 164  *     <tr>
 165  *         <th scope="row">Primitive</th><th scope="row">Primitive</th>
 166  *         <td>Q can be converted to S via a primitive widening conversion</td>
 167  *         <td>None</td>
 168  *     </tr>
 169  *     <tr>
 170  *         <th scope="row">Primitive</th><th scope="row">Reference</th>
 171  *         <td>S is a supertype of the Wrapper(Q)</td>
 172  *         <td>Cast from Wrapper(Q) to S</td>
 173  *     </tr>
 174  *     <tr>
 175  *         <th scope="row">Reference</th><th scope="row">Primitive</th>
 176  *         <td>for parameter types: Q is a primitive wrapper and Primitive(Q)
 177  *         can be widened to S
 178  *         <br>for return types: If Q is a primitive wrapper, check that
 179  *         Primitive(Q) can be widened to S</td>
 180  *         <td>If Q is not a primitive wrapper, cast Q to the base Wrapper(S);
 181  *         for example Number for numeric types</td>
 182  *     </tr>
 183  *     <tr>
 184  *         <th scope="row">Reference</th><th scope="row">Reference</th>
 185  *         <td>for parameter types: S is a supertype of Q
 186  *         <br>for return types: none</td>
 187  *         <td>Cast from Q to S</td>
 188  *     </tr>
 189  *   </tbody>
 190  * </table>
 191  *
 192  * @apiNote These linkage methods are designed to support the evaluation
 193  * of <em>lambda expressions</em> and <em>method references</em> in the Java
 194  * Language.  For every lambda expressions or method reference in the source code,
 195  * there is a target type which is a functional interface.  Evaluating a lambda
 196  * expression produces an object of its target type. The recommended mechanism
 197  * for evaluating lambda expressions is to desugar the lambda body to a method,
 198  * invoke an invokedynamic call site whose static argument list describes the
 199  * sole method of the functional interface and the desugared implementation
 200  * method, and returns an object (the lambda object) that implements the target
 201  * type. (For method references, the implementation method is simply the
 202  * referenced method; no desugaring is needed.)
 203  *
 204  * <p>The argument list of the implementation method and the argument list of
 205  * the interface method(s) may differ in several ways.  The implementation
 206  * methods may have additional arguments to accommodate arguments captured by
 207  * the lambda expression; there may also be differences resulting from permitted
 208  * adaptations of arguments, such as casting, boxing, unboxing, and primitive
 209  * widening. (Varargs adaptations are not handled by the metafactories; these are
 210  * expected to be handled by the caller.)
 211  *
 212  * <p>Invokedynamic call sites have two argument lists: a static argument list
 213  * and a dynamic argument list.  The static argument list is stored in the
 214  * constant pool; the dynamic argument is pushed on the operand stack at capture
 215  * time.  The bootstrap method has access to the entire static argument list
 216  * (which in this case, includes information describing the implementation method,
 217  * the target interface, and the target interface method(s)), as well as a
 218  * method signature describing the number and static types (but not the values)
 219  * of the dynamic arguments and the static return type of the invokedynamic site.
 220  *
 221  * @implNote The implementation method is described with a method handle. In
 222  * theory, any method handle could be used. Currently supported are direct method
 223  * handles representing invocation of virtual, interface, constructor and static
 224  * methods.
 225  * @since 1.8
 226  */
 227 public final class LambdaMetafactory {
 228 
 229     private LambdaMetafactory() {}
 230 
 231     /** Flag for alternate metafactories indicating the lambda object
 232      * must be serializable */
 233     public static final int FLAG_SERIALIZABLE = 1 << 0;
 234 
 235     /**
 236      * Flag for alternate metafactories indicating the lambda object implements
 237      * other marker interfaces
 238      * besides Serializable
 239      */
 240     public static final int FLAG_MARKERS = 1 << 1;
 241 
 242     /**
 243      * Flag for alternate metafactories indicating the lambda object requires
 244      * additional bridge methods
 245      */
 246     public static final int FLAG_BRIDGES = 1 << 2;
 247 
 248     private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
 249     private static final MethodType[] EMPTY_MT_ARRAY = new MethodType[0];
 250 
 251     // LambdaMetafactory bootstrap methods are startup sensitive, and may be
 252     // special cased in java.lang.invokeBootstrapMethodInvoker to ensure
 253     // methods are invoked with exact type information to avoid generating
 254     // code for runtime checks. Take care any changes or additions here are
 255     // reflected there as appropriate.
 256 
 257     /**
 258      * Facilitates the creation of simple "function objects" that implement one
 259      * or more interfaces by delegation to a provided {@link MethodHandle},
 260      * after appropriate type adaptation and partial evaluation of arguments.
 261      * Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
 262      * call sites, to support the <em>lambda expression</em> and <em>method
 263      * reference expression</em> features of the Java Programming Language.
 264      *
 265      * <p>This is the standard, streamlined metafactory; additional flexibility
 266      * is provided by {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)}.
 267      * A general description of the behavior of this method is provided
 268      * {@link LambdaMetafactory above}.
 269      *
 270      * <p>When the target of the {@code CallSite} returned from this method is
 271      * invoked, the resulting function objects are instances of a class which
 272      * implements the interface named by the return type of {@code invokedType},
 273      * declares a method with the name given by {@code invokedName} and the
 274      * signature given by {@code samMethodType}.  It may also override additional
 275      * methods from {@code Object}.
 276      *
 277      * @param caller Represents a lookup context with the accessibility
 278      *               privileges of the caller.  Specifically, the lookup context
 279      *               must have
 280      *               <a href="MethodHandles.Lookup.html#privacc">private access</a>
 281      *               privileges.
 282      *               When used with {@code invokedynamic}, this is stacked
 283      *               automatically by the VM.
 284      * @param invokedName The name of the method to implement.  When used with
 285      *                    {@code invokedynamic}, this is provided by the
 286      *                    {@code NameAndType} of the {@code InvokeDynamic}
 287      *                    structure and is stacked automatically by the VM.
 288      * @param invokedType The expected signature of the {@code CallSite}.  The
 289      *                    parameter types represent the types of capture variables;
 290      *                    the return type is the interface to implement.   When
 291      *                    used with {@code invokedynamic}, this is provided by
 292      *                    the {@code NameAndType} of the {@code InvokeDynamic}
 293      *                    structure and is stacked automatically by the VM.
 294      *                    In the event that the implementation method is an
 295      *                    instance method and this signature has any parameters,
 296      *                    the first parameter in the invocation signature must
 297      *                    correspond to the receiver.
 298      * @param samMethodType Signature and return type of method to be implemented
 299      *                      by the function object.
 300      * @param implMethod A direct method handle describing the implementation
 301      *                   method which should be called (with suitable adaptation
 302      *                   of argument types, return types, and with captured
 303      *                   arguments prepended to the invocation arguments) at
 304      *                   invocation time.
 305      * @param instantiatedMethodType The signature and return type that should
 306      *                               be enforced dynamically at invocation time.
 307      *                               This may be the same as {@code samMethodType},
 308      *                               or may be a specialization of it.
 309      * @return a CallSite whose target can be used to perform capture, generating
 310      *         instances of the interface named by {@code invokedType}
 311      * @throws LambdaConversionException If any of the linkage invariants
 312      *                                   described {@link LambdaMetafactory above}
 313      *                                   are violated, or the lookup context
 314      *                                   does not have private access privileges.
 315      */
 316     public static CallSite metafactory(MethodHandles.Lookup caller,
 317                                        String invokedName,
 318                                        MethodType invokedType,
 319                                        MethodType samMethodType,
 320                                        MethodHandle implMethod,
 321                                        MethodType instantiatedMethodType)
 322             throws LambdaConversionException {
 323         AbstractValidatingLambdaMetafactory mf;
 324         mf = new InnerClassLambdaMetafactory(caller, invokedType,
 325                                              invokedName, samMethodType,
 326                                              implMethod, instantiatedMethodType,
 327                                              false, EMPTY_CLASS_ARRAY, EMPTY_MT_ARRAY);
 328         mf.validateMetafactoryArgs();
 329         return mf.buildCallSite();
 330     }
 331 
 332     /**
 333      * Facilitates the creation of simple "function objects" that implement one
 334      * or more interfaces by delegation to a provided {@link MethodHandle},
 335      * after appropriate type adaptation and partial evaluation of arguments.
 336      * Typically used as a <em>bootstrap method</em> for {@code invokedynamic}
 337      * call sites, to support the <em>lambda expression</em> and <em>method
 338      * reference expression</em> features of the Java Programming Language.
 339      *
 340      * <p>This is the general, more flexible metafactory; a streamlined version
 341      * is provided by {@link #metafactory(java.lang.invoke.MethodHandles.Lookup,
 342      * String, MethodType, MethodType, MethodHandle, MethodType)}.
 343      * A general description of the behavior of this method is provided
 344      * {@link LambdaMetafactory above}.
 345      *
 346      * <p>The argument list for this method includes three fixed parameters,
 347      * corresponding to the parameters automatically stacked by the VM for the
 348      * bootstrap method in an {@code invokedynamic} invocation, and an {@code Object[]}
 349      * parameter that contains additional parameters.  The declared argument
 350      * list for this method is:
 351      *
 352      * <pre>{@code
 353      *  CallSite altMetafactory(MethodHandles.Lookup caller,
 354      *                          String invokedName,
 355      *                          MethodType invokedType,
 356      *                          Object... args)
 357      * }</pre>
 358      *
 359      * <p>but it behaves as if the argument list is as follows:
 360      *
 361      * <pre>{@code
 362      *  CallSite altMetafactory(MethodHandles.Lookup caller,
 363      *                          String invokedName,
 364      *                          MethodType invokedType,
 365      *                          MethodType samMethodType,
 366      *                          MethodHandle implMethod,
 367      *                          MethodType instantiatedMethodType,
 368      *                          int flags,
 369      *                          int markerInterfaceCount,  // IF flags has MARKERS set
 370      *                          Class... markerInterfaces, // IF flags has MARKERS set
 371      *                          int bridgeCount,           // IF flags has BRIDGES set
 372      *                          MethodType... bridges      // IF flags has BRIDGES set
 373      *                          )
 374      * }</pre>
 375      *
 376      * <p>Arguments that appear in the argument list for
 377      * {@link #metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)}
 378      * have the same specification as in that method.  The additional arguments
 379      * are interpreted as follows:
 380      * <ul>
 381      *     <li>{@code flags} indicates additional options; this is a bitwise
 382      *     OR of desired flags.  Defined flags are {@link #FLAG_BRIDGES},
 383      *     {@link #FLAG_MARKERS}, and {@link #FLAG_SERIALIZABLE}.</li>
 384      *     <li>{@code markerInterfaceCount} is the number of additional interfaces
 385      *     the function object should implement, and is present if and only if the
 386      *     {@code FLAG_MARKERS} flag is set.</li>
 387      *     <li>{@code markerInterfaces} is a variable-length list of additional
 388      *     interfaces to implement, whose length equals {@code markerInterfaceCount},
 389      *     and is present if and only if the {@code FLAG_MARKERS} flag is set.</li>
 390      *     <li>{@code bridgeCount} is the number of additional method signatures
 391      *     the function object should implement, and is present if and only if
 392      *     the {@code FLAG_BRIDGES} flag is set.</li>
 393      *     <li>{@code bridges} is a variable-length list of additional
 394      *     methods signatures to implement, whose length equals {@code bridgeCount},
 395      *     and is present if and only if the {@code FLAG_BRIDGES} flag is set.</li>
 396      * </ul>
 397      *
 398      * <p>Each class named by {@code markerInterfaces} is subject to the same
 399      * restrictions as {@code Rd}, the return type of {@code invokedType},
 400      * as described {@link LambdaMetafactory above}.  Each {@code MethodType}
 401      * named by {@code bridges} is subject to the same restrictions as
 402      * {@code samMethodType}, as described {@link LambdaMetafactory above}.
 403      *
 404      * <p>When FLAG_SERIALIZABLE is set in {@code flags}, the function objects
 405      * will implement {@code Serializable}, and will have a {@code writeReplace}
 406      * method that returns an appropriate {@link SerializedLambda}.  The
 407      * {@code caller} class must have an appropriate {@code $deserializeLambda$}
 408      * method, as described in {@link SerializedLambda}.
 409      *
 410      * <p>When the target of the {@code CallSite} returned from this method is
 411      * invoked, the resulting function objects are instances of a class with
 412      * the following properties:
 413      * <ul>
 414      *     <li>The class implements the interface named by the return type
 415      *     of {@code invokedType} and any interfaces named by {@code markerInterfaces}</li>
 416      *     <li>The class declares methods with the name given by {@code invokedName},
 417      *     and the signature given by {@code samMethodType} and additional signatures
 418      *     given by {@code bridges}</li>
 419      *     <li>The class may override methods from {@code Object}, and may
 420      *     implement methods related to serialization.</li>
 421      * </ul>
 422      *
 423      * @param caller Represents a lookup context with the accessibility
 424      *               privileges of the caller.  Specifically, the lookup context
 425      *               must have
 426      *               <a href="MethodHandles.Lookup.html#privacc">private access</a>
 427      *               privileges.
 428      *               When used with {@code invokedynamic}, this is stacked
 429      *               automatically by the VM.
 430      * @param invokedName The name of the method to implement.  When used with
 431      *                    {@code invokedynamic}, this is provided by the
 432      *                    {@code NameAndType} of the {@code InvokeDynamic}
 433      *                    structure and is stacked automatically by the VM.
 434      * @param invokedType The expected signature of the {@code CallSite}.  The
 435      *                    parameter types represent the types of capture variables;
 436      *                    the return type is the interface to implement.   When
 437      *                    used with {@code invokedynamic}, this is provided by
 438      *                    the {@code NameAndType} of the {@code InvokeDynamic}
 439      *                    structure and is stacked automatically by the VM.
 440      *                    In the event that the implementation method is an
 441      *                    instance method and this signature has any parameters,
 442      *                    the first parameter in the invocation signature must
 443      *                    correspond to the receiver.
 444      * @param  args       An {@code Object[]} array containing the required
 445      *                    arguments {@code samMethodType}, {@code implMethod},
 446      *                    {@code instantiatedMethodType}, {@code flags}, and any
 447      *                    optional arguments, as described
 448      *                    {@link #altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)} above}
 449      * @return a CallSite whose target can be used to perform capture, generating
 450      *         instances of the interface named by {@code invokedType}
 451      * @throws LambdaConversionException If any of the linkage invariants
 452      *                                   described {@link LambdaMetafactory above}
 453      *                                   are violated, or the lookup context
 454      *                                   does not have private access privileges.
 455      */
 456     public static CallSite altMetafactory(MethodHandles.Lookup caller,
 457                                           String invokedName,
 458                                           MethodType invokedType,
 459                                           Object... args)
 460             throws LambdaConversionException {
 461         MethodType samMethodType = (MethodType)args[0];
 462         MethodHandle implMethod = (MethodHandle)args[1];
 463         MethodType instantiatedMethodType = (MethodType)args[2];
 464         int flags = (Integer) args[3];
 465         Class<?>[] markerInterfaces;
 466         MethodType[] bridges;
 467         int argIndex = 4;
 468         if ((flags & FLAG_MARKERS) != 0) {
 469             int markerCount = (Integer) args[argIndex++];
 470             markerInterfaces = new Class<?>[markerCount];
 471             System.arraycopy(args, argIndex, markerInterfaces, 0, markerCount);
 472             argIndex += markerCount;
 473         }
 474         else
 475             markerInterfaces = EMPTY_CLASS_ARRAY;
 476         if ((flags & FLAG_BRIDGES) != 0) {
 477             int bridgeCount = (Integer) args[argIndex++];
 478             bridges = new MethodType[bridgeCount];
 479             System.arraycopy(args, argIndex, bridges, 0, bridgeCount);
 480             argIndex += bridgeCount;
 481         }
 482         else
 483             bridges = EMPTY_MT_ARRAY;
 484 
 485         boolean isSerializable = ((flags & FLAG_SERIALIZABLE) != 0);
 486         if (isSerializable) {
 487             boolean foundSerializableSupertype = Serializable.class.isAssignableFrom(invokedType.returnType());
 488             for (Class<?> c : markerInterfaces)
 489                 foundSerializableSupertype |= Serializable.class.isAssignableFrom(c);
 490             if (!foundSerializableSupertype) {
 491                 markerInterfaces = Arrays.copyOf(markerInterfaces, markerInterfaces.length + 1);
 492                 markerInterfaces[markerInterfaces.length-1] = Serializable.class;
 493             }
 494         }
 495 
 496         AbstractValidatingLambdaMetafactory mf
 497                 = new InnerClassLambdaMetafactory(caller, invokedType,
 498                                                   invokedName, samMethodType,
 499                                                   implMethod,
 500                                                   instantiatedMethodType,
 501                                                   isSerializable,
 502                                                   markerInterfaces, bridges);
 503         mf.validateMetafactoryArgs();
 504         return mf.buildCallSite();
 505     }
 506 }