src/java.base/share/classes/java/lang/invoke/AbstractValidatingLambdaMetafactory.java
author mchung
Tue, 29 Oct 2019 12:52:03 -0700
changeset 58849 67a3f50b14ae
parent 47216 71c04702a3d5
permissions -rw-r--r--
8173975: Lookup::in should not allow target class be primitive or array class Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22609
diff changeset
     2
 * Copyright (c) 2012, 2013, 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
package java.lang.invoke;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    26
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    27
import sun.invoke.util.Wrapper;
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    28
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
    29
import static java.lang.invoke.MethodHandleInfo.*;
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    30
import static sun.invoke.util.Wrapper.forPrimitiveType;
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    31
import static sun.invoke.util.Wrapper.forWrapperType;
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    32
import static sun.invoke.util.Wrapper.isWrapperType;
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    33
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    34
/**
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    35
 * Abstract implementation of a lambda metafactory which provides parameter
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    36
 * unrolling and input validation.
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    37
 *
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
    38
 * @see LambdaMetafactory
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    39
 */
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
    40
/* package */ abstract class AbstractValidatingLambdaMetafactory {
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    41
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    42
    /*
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    43
     * For context, the comments for the following fields are marked in quotes
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    44
     * with their values, given this program:
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    45
     * interface II<T> {  Object foo(T x); }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    46
     * interface JJ<R extends Number> extends II<R> { }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    47
     * class CC {  String impl(int i) { return "impl:"+i; }}
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    48
     * class X {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    49
     *     public static void main(String[] args) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    50
     *         JJ<Integer> iii = (new CC())::impl;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    51
     *         System.out.printf(">>> %s\n", iii.foo(44));
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    52
     * }}
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    53
     */
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    54
    final Class<?> targetClass;               // The class calling the meta-factory via invokedynamic "class X"
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    55
    final MethodType invokedType;             // The type of the invoked method "(CC)II"
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    56
    final Class<?> samBase;                   // The type of the returned instance "interface JJ"
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    57
    final String samMethodName;               // Name of the SAM method "foo"
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    58
    final MethodType samMethodType;           // Type of the SAM method "(Object)Object"
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
    59
    final MethodHandle implMethod;            // Raw method handle for the implementation method
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
    60
    final MethodType implMethodType;          // Type of the implMethod MethodHandle "(CC,int)String"
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    61
    final MethodHandleInfo implInfo;          // Info about the implementation method handle "MethodHandleInfo[5 CC.impl(int)String]"
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    62
    final int implKind;                       // Invocation kind for implementation "5"=invokevirtual
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    63
    final boolean implIsInstanceMethod;       // Is the implementation an instance method "true"
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
    64
    final Class<?> implClass;                 // Class for referencing the implementation method "class CC"
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    65
    final MethodType instantiatedMethodType;  // Instantiated erased functional interface method type "(Integer)Object"
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
    66
    final boolean isSerializable;             // Should the returned instance be serializable
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
    67
    final Class<?>[] markerInterfaces;        // Additional marker interfaces to be implemented
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    68
    final MethodType[] additionalBridges;     // Signatures of additional methods to bridge
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    69
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    70
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    71
    /**
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    72
     * Meta-factory constructor.
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
    73
     *
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    74
     * @param caller Stacked automatically by VM; represents a lookup context
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    75
     *               with the accessibility privileges of the caller.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    76
     * @param invokedType Stacked automatically by VM; the signature of the
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    77
     *                    invoked method, which includes the expected static
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    78
     *                    type of the returned lambda object, and the static
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    79
     *                    types of the captured arguments for the lambda.  In
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    80
     *                    the event that the implementation method is an
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    81
     *                    instance method, the first argument in the invocation
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    82
     *                    signature will correspond to the receiver.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    83
     * @param samMethodName Name of the method in the functional interface to
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    84
     *                      which the lambda or method reference is being
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    85
     *                      converted, represented as a String.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    86
     * @param samMethodType Type of the method in the functional interface to
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    87
     *                      which the lambda or method reference is being
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    88
     *                      converted, represented as a MethodType.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    89
     * @param implMethod The implementation method which should be called
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    90
     *                   (with suitable adaptation of argument types, return
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    91
     *                   types, and adjustment for captured arguments) when
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    92
     *                   methods of the resulting functional interface instance
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    93
     *                   are invoked.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    94
     * @param instantiatedMethodType The signature of the primary functional
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    95
     *                               interface method after type variables are
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    96
     *                               substituted with their instantiation from
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    97
     *                               the capture site
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    98
     * @param isSerializable Should the lambda be made serializable?  If set,
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
    99
     *                       either the target type or one of the additional SAM
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   100
     *                       types must extend {@code Serializable}.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   101
     * @param markerInterfaces Additional interfaces which the lambda object
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   102
     *                       should implement.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   103
     * @param additionalBridges Method types for additional signatures to be
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   104
     *                          bridged to the implementation method
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   105
     * @throws LambdaConversionException If any of the meta-factory protocol
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   106
     * invariants are violated
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   107
     */
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   108
    AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller,
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   109
                                       MethodType invokedType,
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   110
                                       String samMethodName,
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   111
                                       MethodType samMethodType,
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   112
                                       MethodHandle implMethod,
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   113
                                       MethodType instantiatedMethodType,
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   114
                                       boolean isSerializable,
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   115
                                       Class<?>[] markerInterfaces,
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   116
                                       MethodType[] additionalBridges)
21360
2c2f062cf52f 8019646: Clarify javadoc contract of LambdaMetafactory
briangoetz
parents: 20511
diff changeset
   117
            throws LambdaConversionException {
22586
dd49926cdfae 8032711: Issue with Lambda in handling
rfield
parents: 21360
diff changeset
   118
        if ((caller.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) {
dd49926cdfae 8032711: Issue with Lambda in handling
rfield
parents: 21360
diff changeset
   119
            throw new LambdaConversionException(String.format(
dd49926cdfae 8032711: Issue with Lambda in handling
rfield
parents: 21360
diff changeset
   120
                    "Invalid caller: %s",
dd49926cdfae 8032711: Issue with Lambda in handling
rfield
parents: 21360
diff changeset
   121
                    caller.lookupClass().getName()));
dd49926cdfae 8032711: Issue with Lambda in handling
rfield
parents: 21360
diff changeset
   122
        }
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   123
        this.targetClass = caller.lookupClass();
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   124
        this.invokedType = invokedType;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   125
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   126
        this.samBase = invokedType.returnType();
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   127
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   128
        this.samMethodName = samMethodName;
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   129
        this.samMethodType  = samMethodType;
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   130
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   131
        this.implMethod = implMethod;
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   132
        this.implMethodType = implMethod.type();
19804
83170920c07b 8008688: Make MethodHandleInfo public
rfield
parents: 18716
diff changeset
   133
        this.implInfo = caller.revealDirect(implMethod);
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   134
        switch (implInfo.getReferenceKind()) {
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   135
            case REF_invokeVirtual:
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   136
            case REF_invokeInterface:
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   137
                this.implClass = implMethodType.parameterType(0);
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   138
                // reference kind reported by implInfo may not match implMethodType's first param
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   139
                // Example: implMethodType is (Cloneable)String, implInfo is for Object.toString
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   140
                this.implKind = implClass.isInterface() ? REF_invokeInterface : REF_invokeVirtual;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   141
                this.implIsInstanceMethod = true;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   142
                break;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   143
            case REF_invokeSpecial:
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   144
                // JDK-8172817: should use referenced class here, but we don't know what it was
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   145
                this.implClass = implInfo.getDeclaringClass();
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   146
                this.implKind = REF_invokeSpecial;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   147
                this.implIsInstanceMethod = true;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   148
                break;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   149
            case REF_invokeStatic:
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   150
            case REF_newInvokeSpecial:
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   151
                // JDK-8172817: should use referenced class here for invokestatic, but we don't know what it was
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   152
                this.implClass = implInfo.getDeclaringClass();
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   153
                this.implKind = implInfo.getReferenceKind();
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   154
                this.implIsInstanceMethod = false;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   155
                break;
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   156
            default:
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   157
                throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", implInfo));
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   158
        }
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   159
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   160
        this.instantiatedMethodType = instantiatedMethodType;
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   161
        this.isSerializable = isSerializable;
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   162
        this.markerInterfaces = markerInterfaces;
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   163
        this.additionalBridges = additionalBridges;
18284
e281a0a2583e 8017044: anti-delta fix for 8015402
chegar
parents: 18182
diff changeset
   164
43700
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   165
        if (samMethodName.isEmpty() ||
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   166
                samMethodName.indexOf('.') >= 0 ||
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   167
                samMethodName.indexOf(';') >= 0 ||
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   168
                samMethodName.indexOf('[') >= 0 ||
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   169
                samMethodName.indexOf('/') >= 0 ||
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   170
                samMethodName.indexOf('<') >= 0 ||
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   171
                samMethodName.indexOf('>') >= 0) {
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   172
            throw new LambdaConversionException(String.format(
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   173
                    "Method name '%s' is not legal",
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   174
                    samMethodName));
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   175
        }
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   176
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   177
        if (!samBase.isInterface()) {
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   178
            throw new LambdaConversionException(String.format(
18284
e281a0a2583e 8017044: anti-delta fix for 8015402
chegar
parents: 18182
diff changeset
   179
                    "Functional interface %s is not an interface",
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   180
                    samBase.getName()));
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   181
        }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   182
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   183
        for (Class<?> c : markerInterfaces) {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   184
            if (!c.isInterface()) {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   185
                throw new LambdaConversionException(String.format(
18284
e281a0a2583e 8017044: anti-delta fix for 8015402
chegar
parents: 18182
diff changeset
   186
                        "Marker interface %s is not an interface",
e281a0a2583e 8017044: anti-delta fix for 8015402
chegar
parents: 18182
diff changeset
   187
                        c.getName()));
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   188
            }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   189
        }
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   190
    }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   191
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   192
    /**
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   193
     * Build the CallSite.
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   194
     *
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   195
     * @return a CallSite, which, when invoked, will return an instance of the
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   196
     * functional interface
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   197
     * @throws ReflectiveOperationException
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   198
     */
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   199
    abstract CallSite buildCallSite()
21360
2c2f062cf52f 8019646: Clarify javadoc contract of LambdaMetafactory
briangoetz
parents: 20511
diff changeset
   200
            throws LambdaConversionException;
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   201
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   202
    /**
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   203
     * Check the meta-factory arguments for errors
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   204
     * @throws LambdaConversionException if there are improper conversions
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   205
     */
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   206
    void validateMetafactoryArgs() throws LambdaConversionException {
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   207
        // Check arity: captured + SAM == impl
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   208
        final int implArity = implMethodType.parameterCount();
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   209
        final int capturedArity = invokedType.parameterCount();
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   210
        final int samArity = samMethodType.parameterCount();
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   211
        final int instantiatedArity = instantiatedMethodType.parameterCount();
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   212
        if (implArity != capturedArity + samArity) {
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   213
            throw new LambdaConversionException(
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   214
                    String.format("Incorrect number of parameters for %s method %s; %d captured parameters, %d functional interface method parameters, %d implementation parameters",
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   215
                                  implIsInstanceMethod ? "instance" : "static", implInfo,
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   216
                                  capturedArity, samArity, implArity));
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   217
        }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   218
        if (instantiatedArity != samArity) {
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   219
            throw new LambdaConversionException(
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   220
                    String.format("Incorrect number of parameters for %s method %s; %d instantiated parameters, %d functional interface method parameters",
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   221
                                  implIsInstanceMethod ? "instance" : "static", implInfo,
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   222
                                  instantiatedArity, samArity));
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   223
        }
23049
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   224
        for (MethodType bridgeMT : additionalBridges) {
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   225
            if (bridgeMT.parameterCount() != samArity) {
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   226
                throw new LambdaConversionException(
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   227
                        String.format("Incorrect number of parameters for bridge signature %s; incompatible with %s",
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   228
                                      bridgeMT, samMethodType));
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   229
            }
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   230
        }
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   231
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   232
        // If instance: first captured arg (receiver) must be subtype of class where impl method is defined
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   233
        final int capturedStart; // index of first non-receiver capture parameter in implMethodType
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   234
        final int samStart; // index of first non-receiver sam parameter in implMethodType
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   235
        if (implIsInstanceMethod) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   236
            final Class<?> receiverClass;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   237
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   238
            // implementation is an instance method, adjust for receiver in captured variables / SAM arguments
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   239
            if (capturedArity == 0) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   240
                // receiver is function parameter
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   241
                capturedStart = 0;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   242
                samStart = 1;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   243
                receiverClass = instantiatedMethodType.parameterType(0);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   244
            } else {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   245
                // receiver is a captured variable
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   246
                capturedStart = 1;
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   247
                samStart = capturedArity;
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   248
                receiverClass = invokedType.parameterType(0);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   249
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   250
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   251
            // check receiver type
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   252
            if (!implClass.isAssignableFrom(receiverClass)) {
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   253
                throw new LambdaConversionException(
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   254
                        String.format("Invalid receiver type %s; not a subtype of implementation type %s",
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   255
                                      receiverClass, implClass));
23049
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   256
            }
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   257
        } else {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   258
            // no receiver
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   259
            capturedStart = 0;
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   260
            samStart = capturedArity;
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   261
        }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   262
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   263
        // Check for exact match on non-receiver captured arguments
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   264
        for (int i=capturedStart; i<capturedArity; i++) {
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   265
            Class<?> implParamType = implMethodType.parameterType(i);
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   266
            Class<?> capturedParamType = invokedType.parameterType(i);
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   267
            if (!capturedParamType.equals(implParamType)) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   268
                throw new LambdaConversionException(
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   269
                        String.format("Type mismatch in captured lambda parameter %d: expecting %s, found %s",
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   270
                                      i, capturedParamType, implParamType));
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   271
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   272
        }
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   273
        // Check for adaptation match on non-receiver SAM arguments
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   274
        for (int i=samStart; i<implArity; i++) {
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   275
            Class<?> implParamType = implMethodType.parameterType(i);
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   276
            Class<?> instantiatedParamType = instantiatedMethodType.parameterType(i - capturedArity);
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   277
            if (!isAdaptableTo(instantiatedParamType, implParamType, true)) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   278
                throw new LambdaConversionException(
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   279
                        String.format("Type mismatch for lambda argument %d: %s is not convertible to %s",
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   280
                                      i, instantiatedParamType, implParamType));
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   281
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   282
        }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   283
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   284
        // Adaptation match: return type
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   285
        Class<?> expectedType = instantiatedMethodType.returnType();
43789
43068ea5965e 8174399: LambdaMetafactory should use types in implMethod.type()
dlsmith
parents: 43703
diff changeset
   286
        Class<?> actualReturnType = implMethodType.returnType();
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   287
        if (!isAdaptableToAsReturn(actualReturnType, expectedType)) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   288
            throw new LambdaConversionException(
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   289
                    String.format("Type mismatch for lambda return: %s is not convertible to %s",
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   290
                                  actualReturnType, expectedType));
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   291
        }
43700
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   292
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   293
        // Check descriptors of generated methods
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   294
        checkDescriptor(samMethodType);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   295
        for (MethodType bridgeMT : additionalBridges) {
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   296
            checkDescriptor(bridgeMT);
22586
dd49926cdfae 8032711: Issue with Lambda in handling
rfield
parents: 21360
diff changeset
   297
        }
43700
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   298
    }
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   299
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   300
    /** Validate that the given descriptor's types are compatible with {@code instantiatedMethodType} **/
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   301
    private void checkDescriptor(MethodType descriptor) throws LambdaConversionException {
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   302
        for (int i = 0; i < instantiatedMethodType.parameterCount(); i++) {
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   303
            Class<?> instantiatedParamType = instantiatedMethodType.parameterType(i);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   304
            Class<?> descriptorParamType = descriptor.parameterType(i);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   305
            if (!descriptorParamType.isAssignableFrom(instantiatedParamType)) {
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   306
                String msg = String.format("Type mismatch for instantiated parameter %d: %s is not a subtype of %s",
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   307
                                           i, instantiatedParamType, descriptorParamType);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   308
                throw new LambdaConversionException(msg);
23049
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   309
            }
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   310
        }
43700
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   311
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   312
        Class<?> instantiatedReturnType = instantiatedMethodType.returnType();
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   313
        Class<?> descriptorReturnType = descriptor.returnType();
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   314
        if (!isAdaptableToAsReturnStrict(instantiatedReturnType, descriptorReturnType)) {
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   315
            String msg = String.format("Type mismatch for lambda expected return: %s is not convertible to %s",
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   316
                                       instantiatedReturnType, descriptorReturnType);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   317
            throw new LambdaConversionException(msg);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   318
        }
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   319
    }
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   320
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   321
    /**
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   322
     * Check type adaptability for parameter types.
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   323
     * @param fromType Type to convert from
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   324
     * @param toType Type to convert to
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   325
     * @param strict If true, do strict checks, else allow that fromType may be parameterized
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   326
     * @return True if 'fromType' can be passed to an argument of 'toType'
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   327
     */
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   328
    private boolean isAdaptableTo(Class<?> fromType, Class<?> toType, boolean strict) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   329
        if (fromType.equals(toType)) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   330
            return true;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   331
        }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   332
        if (fromType.isPrimitive()) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   333
            Wrapper wfrom = forPrimitiveType(fromType);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   334
            if (toType.isPrimitive()) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   335
                // both are primitive: widening
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   336
                Wrapper wto = forPrimitiveType(toType);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   337
                return wto.isConvertibleFrom(wfrom);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   338
            } else {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   339
                // from primitive to reference: boxing
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   340
                return toType.isAssignableFrom(wfrom.wrapperType());
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   341
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   342
        } else {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   343
            if (toType.isPrimitive()) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   344
                // from reference to primitive: unboxing
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   345
                Wrapper wfrom;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   346
                if (isWrapperType(fromType) && (wfrom = forWrapperType(fromType)).primitiveType().isPrimitive()) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   347
                    // fromType is a primitive wrapper; unbox+widen
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   348
                    Wrapper wto = forPrimitiveType(toType);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   349
                    return wto.isConvertibleFrom(wfrom);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   350
                } else {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   351
                    // must be convertible to primitive
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   352
                    return !strict;
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   353
                }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   354
            } else {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   355
                // both are reference types: fromType should be a superclass of toType.
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   356
                return !strict || toType.isAssignableFrom(fromType);
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   357
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   358
        }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   359
    }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   360
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   361
    /**
18716
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   362
     * Check type adaptability for return types --
9723e722b955 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18284
diff changeset
   363
     * special handling of void type) and parameterized fromType
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   364
     * @return True if 'fromType' can be converted to 'toType'
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   365
     */
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   366
    private boolean isAdaptableToAsReturn(Class<?> fromType, Class<?> toType) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   367
        return toType.equals(void.class)
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   368
               || !fromType.equals(void.class) && isAdaptableTo(fromType, toType, false);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   369
    }
23049
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   370
    private boolean isAdaptableToAsReturnStrict(Class<?> fromType, Class<?> toType) {
43700
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   371
        if (fromType.equals(void.class) || toType.equals(void.class)) return fromType.equals(toType);
ee6b5bd26bf9 8173587: LambdaMetafactory needs to validate descriptors and method name
dlsmith
parents: 25859
diff changeset
   372
        else return isAdaptableTo(fromType, toType, true);
23049
c66d7e7bcded 8035776: Consistent Lambda construction
rfield
parents: 23010
diff changeset
   373
    }
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   374
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   375
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   376
    /*********** Logging support -- for debugging only, uncomment as needed
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents: 14323
diff changeset
   377
    static final Executor logPool = Executors.newSingleThreadExecutor();
14323
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   378
    protected static void log(final String s) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   379
        MethodHandleProxyLambdaMetafactory.logPool.execute(new Runnable() {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   380
            @Override
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   381
            public void run() {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   382
                System.out.println(s);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   383
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   384
        });
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   385
    }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   386
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   387
    protected static void log(final String s, final Throwable e) {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   388
        MethodHandleProxyLambdaMetafactory.logPool.execute(new Runnable() {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   389
            @Override
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   390
            public void run() {
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   391
                System.out.println(s);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   392
                e.printStackTrace(System.out);
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   393
            }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   394
        });
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   395
    }
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   396
    ***********************/
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   397
5acca3d1f124 8000806: Implement runtime lambda metafactory
rfield
parents:
diff changeset
   398
}