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