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