author | shade |
Tue, 17 May 2016 22:28:00 +0300 | |
changeset 38372 | 017d7578731c |
parent 25859 | 3317bb8137f4 |
child 43700 | ee6b5bd26bf9 |
permissions | -rw-r--r-- |
14323 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.lang.invoke; |
|
26 |
||
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 |
|
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
29 |
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
|
30 |
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
|
31 |
import static sun.invoke.util.Wrapper.isWrapperType; |
14323 | 32 |
|
33 |
/** |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
34 |
* 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
|
35 |
* unrolling and input validation. |
14323 | 36 |
* |
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
37 |
* @see LambdaMetafactory |
14323 | 38 |
*/ |
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
39 |
/* package */ abstract class AbstractValidatingLambdaMetafactory { |
14323 | 40 |
|
41 |
/* |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
42 |
* 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
|
43 |
* with their values, given this program: |
14323 | 44 |
* interface II<T> { Object foo(T x); } |
45 |
* interface JJ<R extends Number> extends II<R> { } |
|
46 |
* class CC { String impl(int i) { return "impl:"+i; }} |
|
47 |
* class X { |
|
48 |
* public static void main(String[] args) { |
|
49 |
* JJ<Integer> iii = (new CC())::impl; |
|
50 |
* System.out.printf(">>> %s\n", iii.foo(44)); |
|
51 |
* }} |
|
52 |
*/ |
|
53 |
final Class<?> targetClass; // The class calling the meta-factory via invokedynamic "class X" |
|
54 |
final MethodType invokedType; // The type of the invoked method "(CC)II" |
|
55 |
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
|
56 |
final String samMethodName; // Name of the SAM method "foo" |
14323 | 57 |
final MethodType samMethodType; // Type of the SAM method "(Object)Object" |
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
58 |
final MethodHandle implMethod; // Raw method handle for the implementation method |
14323 | 59 |
final MethodHandleInfo implInfo; // Info about the implementation method handle "MethodHandleInfo[5 CC.impl(int)String]" |
60 |
final int implKind; // Invocation kind for implementation "5"=invokevirtual |
|
61 |
final boolean implIsInstanceMethod; // Is the implementation an instance method "true" |
|
62 |
final Class<?> implDefiningClass; // Type defining the implementation "class CC" |
|
63 |
final MethodType implMethodType; // Type of the implementation method "(int)String" |
|
64 |
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
|
65 |
final boolean isSerializable; // Should the returned instance be serializable |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
66 |
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
|
67 |
final MethodType[] additionalBridges; // Signatures of additional methods to bridge |
14323 | 68 |
|
69 |
||
70 |
/** |
|
71 |
* Meta-factory constructor. |
|
72 |
* |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
73 |
* @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
|
74 |
* 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
|
75 |
* @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
|
76 |
* 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
|
77 |
* 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
|
78 |
* 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
|
79 |
* 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
|
80 |
* 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
|
81 |
* 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
|
82 |
* @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
|
83 |
* 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
|
84 |
* 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
|
85 |
* @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
|
86 |
* 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
|
87 |
* 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
|
88 |
* @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
|
89 |
* (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
|
90 |
* 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
|
91 |
* 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
|
92 |
* are invoked. |
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
93 |
* @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
|
94 |
* 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
|
95 |
* 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
|
96 |
* 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
|
97 |
* @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
|
98 |
* 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
|
99 |
* 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
|
100 |
* @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
|
101 |
* should implement. |
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
102 |
* @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
|
103 |
* 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
|
104 |
* @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
|
105 |
* invariants are violated |
14323 | 106 |
*/ |
107 |
AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller, |
|
108 |
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
|
109 |
String samMethodName, |
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
110 |
MethodType samMethodType, |
14323 | 111 |
MethodHandle implMethod, |
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
112 |
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
|
113 |
boolean isSerializable, |
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
114 |
Class<?>[] markerInterfaces, |
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
115 |
MethodType[] additionalBridges) |
21360
2c2f062cf52f
8019646: Clarify javadoc contract of LambdaMetafactory
briangoetz
parents:
20511
diff
changeset
|
116 |
throws LambdaConversionException { |
22586 | 117 |
if ((caller.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) { |
118 |
throw new LambdaConversionException(String.format( |
|
119 |
"Invalid caller: %s", |
|
120 |
caller.lookupClass().getName())); |
|
121 |
} |
|
14323 | 122 |
this.targetClass = caller.lookupClass(); |
123 |
this.invokedType = invokedType; |
|
124 |
||
125 |
this.samBase = invokedType.returnType(); |
|
126 |
||
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
127 |
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
|
128 |
this.samMethodType = samMethodType; |
14323 | 129 |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
130 |
this.implMethod = implMethod; |
19804 | 131 |
this.implInfo = caller.revealDirect(implMethod); |
20511
852c376528ca
8010433: Remove lambda metafactory work-around to JDK-8005119
rfield
parents:
19804
diff
changeset
|
132 |
this.implKind = implInfo.getReferenceKind(); |
14323 | 133 |
this.implIsInstanceMethod = |
134 |
implKind == MethodHandleInfo.REF_invokeVirtual || |
|
135 |
implKind == MethodHandleInfo.REF_invokeSpecial || |
|
136 |
implKind == MethodHandleInfo.REF_invokeInterface; |
|
137 |
this.implDefiningClass = implInfo.getDeclaringClass(); |
|
138 |
this.implMethodType = implInfo.getMethodType(); |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
this.additionalBridges = additionalBridges; |
18284 | 143 |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
144 |
if (!samBase.isInterface()) { |
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
145 |
throw new LambdaConversionException(String.format( |
18284 | 146 |
"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
|
147 |
samBase.getName())); |
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
148 |
} |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
149 |
|
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
150 |
for (Class<?> c : markerInterfaces) { |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
151 |
if (!c.isInterface()) { |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
152 |
throw new LambdaConversionException(String.format( |
18284 | 153 |
"Marker interface %s is not an interface", |
154 |
c.getName())); |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
155 |
} |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
156 |
} |
14323 | 157 |
} |
158 |
||
159 |
/** |
|
160 |
* Build the CallSite. |
|
161 |
* |
|
162 |
* @return a CallSite, which, when invoked, will return an instance of the |
|
163 |
* functional interface |
|
164 |
* @throws ReflectiveOperationException |
|
165 |
*/ |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
166 |
abstract CallSite buildCallSite() |
21360
2c2f062cf52f
8019646: Clarify javadoc contract of LambdaMetafactory
briangoetz
parents:
20511
diff
changeset
|
167 |
throws LambdaConversionException; |
14323 | 168 |
|
169 |
/** |
|
170 |
* Check the meta-factory arguments for errors |
|
171 |
* @throws LambdaConversionException if there are improper conversions |
|
172 |
*/ |
|
173 |
void validateMetafactoryArgs() throws LambdaConversionException { |
|
174 |
switch (implKind) { |
|
175 |
case MethodHandleInfo.REF_invokeInterface: |
|
176 |
case MethodHandleInfo.REF_invokeVirtual: |
|
177 |
case MethodHandleInfo.REF_invokeStatic: |
|
178 |
case MethodHandleInfo.REF_newInvokeSpecial: |
|
179 |
case MethodHandleInfo.REF_invokeSpecial: |
|
180 |
break; |
|
181 |
default: |
|
182 |
throw new LambdaConversionException(String.format("Unsupported MethodHandle kind: %s", implInfo)); |
|
183 |
} |
|
184 |
||
185 |
// Check arity: optional-receiver + captured + SAM == impl |
|
186 |
final int implArity = implMethodType.parameterCount(); |
|
187 |
final int receiverArity = implIsInstanceMethod ? 1 : 0; |
|
188 |
final int capturedArity = invokedType.parameterCount(); |
|
189 |
final int samArity = samMethodType.parameterCount(); |
|
190 |
final int instantiatedArity = instantiatedMethodType.parameterCount(); |
|
191 |
if (implArity + receiverArity != capturedArity + samArity) { |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
192 |
throw new LambdaConversionException( |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
193 |
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
|
194 |
implIsInstanceMethod ? "instance" : "static", implInfo, |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
195 |
capturedArity, samArity, implArity)); |
14323 | 196 |
} |
197 |
if (instantiatedArity != samArity) { |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
198 |
throw new LambdaConversionException( |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
199 |
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
|
200 |
implIsInstanceMethod ? "instance" : "static", implInfo, |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
201 |
instantiatedArity, samArity)); |
14323 | 202 |
} |
23049 | 203 |
for (MethodType bridgeMT : additionalBridges) { |
204 |
if (bridgeMT.parameterCount() != samArity) { |
|
205 |
throw new LambdaConversionException( |
|
206 |
String.format("Incorrect number of parameters for bridge signature %s; incompatible with %s", |
|
207 |
bridgeMT, samMethodType)); |
|
208 |
} |
|
209 |
} |
|
14323 | 210 |
|
211 |
// If instance: first captured arg (receiver) must be subtype of class where impl method is defined |
|
212 |
final int capturedStart; |
|
213 |
final int samStart; |
|
214 |
if (implIsInstanceMethod) { |
|
215 |
final Class<?> receiverClass; |
|
216 |
||
217 |
// implementation is an instance method, adjust for receiver in captured variables / SAM arguments |
|
218 |
if (capturedArity == 0) { |
|
219 |
// receiver is function parameter |
|
220 |
capturedStart = 0; |
|
221 |
samStart = 1; |
|
222 |
receiverClass = instantiatedMethodType.parameterType(0); |
|
223 |
} else { |
|
224 |
// receiver is a captured variable |
|
225 |
capturedStart = 1; |
|
226 |
samStart = 0; |
|
227 |
receiverClass = invokedType.parameterType(0); |
|
228 |
} |
|
229 |
||
230 |
// check receiver type |
|
231 |
if (!implDefiningClass.isAssignableFrom(receiverClass)) { |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
232 |
throw new LambdaConversionException( |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
233 |
String.format("Invalid receiver type %s; not a subtype of implementation type %s", |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
234 |
receiverClass, implDefiningClass)); |
14323 | 235 |
} |
22609 | 236 |
|
237 |
Class<?> implReceiverClass = implMethod.type().parameterType(0); |
|
238 |
if (implReceiverClass != implDefiningClass && !implReceiverClass.isAssignableFrom(receiverClass)) { |
|
239 |
throw new LambdaConversionException( |
|
240 |
String.format("Invalid receiver type %s; not a subtype of implementation receiver type %s", |
|
241 |
receiverClass, implReceiverClass)); |
|
23049 | 242 |
} |
14323 | 243 |
} else { |
244 |
// no receiver |
|
245 |
capturedStart = 0; |
|
246 |
samStart = 0; |
|
247 |
} |
|
248 |
||
249 |
// Check for exact match on non-receiver captured arguments |
|
250 |
final int implFromCaptured = capturedArity - capturedStart; |
|
251 |
for (int i=0; i<implFromCaptured; i++) { |
|
252 |
Class<?> implParamType = implMethodType.parameterType(i); |
|
253 |
Class<?> capturedParamType = invokedType.parameterType(i + capturedStart); |
|
254 |
if (!capturedParamType.equals(implParamType)) { |
|
255 |
throw new LambdaConversionException( |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
256 |
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
|
257 |
i, capturedParamType, implParamType)); |
14323 | 258 |
} |
259 |
} |
|
260 |
// Check for adaptation match on SAM arguments |
|
261 |
final int samOffset = samStart - implFromCaptured; |
|
262 |
for (int i=implFromCaptured; i<implArity; i++) { |
|
263 |
Class<?> implParamType = implMethodType.parameterType(i); |
|
264 |
Class<?> instantiatedParamType = instantiatedMethodType.parameterType(i + samOffset); |
|
265 |
if (!isAdaptableTo(instantiatedParamType, implParamType, true)) { |
|
266 |
throw new LambdaConversionException( |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
267 |
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
|
268 |
i, instantiatedParamType, implParamType)); |
14323 | 269 |
} |
270 |
} |
|
271 |
||
272 |
// Adaptation match: return type |
|
273 |
Class<?> expectedType = instantiatedMethodType.returnType(); |
|
274 |
Class<?> actualReturnType = |
|
275 |
(implKind == MethodHandleInfo.REF_newInvokeSpecial) |
|
276 |
? implDefiningClass |
|
277 |
: implMethodType.returnType(); |
|
22586 | 278 |
Class<?> samReturnType = samMethodType.returnType(); |
14323 | 279 |
if (!isAdaptableToAsReturn(actualReturnType, expectedType)) { |
280 |
throw new LambdaConversionException( |
|
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
281 |
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
|
282 |
actualReturnType, expectedType)); |
14323 | 283 |
} |
23049 | 284 |
if (!isAdaptableToAsReturnStrict(expectedType, samReturnType)) { |
22586 | 285 |
throw new LambdaConversionException( |
286 |
String.format("Type mismatch for lambda expected return: %s is not convertible to %s", |
|
287 |
expectedType, samReturnType)); |
|
288 |
} |
|
23049 | 289 |
for (MethodType bridgeMT : additionalBridges) { |
290 |
if (!isAdaptableToAsReturnStrict(expectedType, bridgeMT.returnType())) { |
|
291 |
throw new LambdaConversionException( |
|
292 |
String.format("Type mismatch for lambda expected return: %s is not convertible to %s", |
|
293 |
expectedType, bridgeMT.returnType())); |
|
294 |
} |
|
295 |
} |
|
14323 | 296 |
} |
297 |
||
298 |
/** |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
299 |
* 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
|
300 |
* @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
|
301 |
* @param toType Type to convert to |
14323 | 302 |
* @param strict If true, do strict checks, else allow that fromType may be parameterized |
303 |
* @return True if 'fromType' can be passed to an argument of 'toType' |
|
304 |
*/ |
|
305 |
private boolean isAdaptableTo(Class<?> fromType, Class<?> toType, boolean strict) { |
|
306 |
if (fromType.equals(toType)) { |
|
307 |
return true; |
|
308 |
} |
|
309 |
if (fromType.isPrimitive()) { |
|
310 |
Wrapper wfrom = forPrimitiveType(fromType); |
|
311 |
if (toType.isPrimitive()) { |
|
312 |
// both are primitive: widening |
|
313 |
Wrapper wto = forPrimitiveType(toType); |
|
314 |
return wto.isConvertibleFrom(wfrom); |
|
315 |
} else { |
|
316 |
// from primitive to reference: boxing |
|
317 |
return toType.isAssignableFrom(wfrom.wrapperType()); |
|
318 |
} |
|
319 |
} else { |
|
320 |
if (toType.isPrimitive()) { |
|
321 |
// from reference to primitive: unboxing |
|
322 |
Wrapper wfrom; |
|
323 |
if (isWrapperType(fromType) && (wfrom = forWrapperType(fromType)).primitiveType().isPrimitive()) { |
|
324 |
// fromType is a primitive wrapper; unbox+widen |
|
325 |
Wrapper wto = forPrimitiveType(toType); |
|
326 |
return wto.isConvertibleFrom(wfrom); |
|
327 |
} else { |
|
328 |
// must be convertible to primitive |
|
329 |
return !strict; |
|
330 |
} |
|
331 |
} else { |
|
332 |
// 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
|
333 |
return !strict || toType.isAssignableFrom(fromType); |
14323 | 334 |
} |
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/** |
|
18716
9723e722b955
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18284
diff
changeset
|
339 |
* 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
|
340 |
* special handling of void type) and parameterized fromType |
14323 | 341 |
* @return True if 'fromType' can be converted to 'toType' |
342 |
*/ |
|
343 |
private boolean isAdaptableToAsReturn(Class<?> fromType, Class<?> toType) { |
|
344 |
return toType.equals(void.class) |
|
345 |
|| !fromType.equals(void.class) && isAdaptableTo(fromType, toType, false); |
|
346 |
} |
|
23049 | 347 |
private boolean isAdaptableToAsReturnStrict(Class<?> fromType, Class<?> toType) { |
348 |
if (fromType.equals(void.class)) return toType.equals(void.class); |
|
349 |
return isAdaptableTo(fromType, toType, true); |
|
350 |
} |
|
14323 | 351 |
|
352 |
||
16001
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
353 |
/*********** Logging support -- for debugging only, uncomment as needed |
fd4c8d3becf8
8004970: Implement serialization in the lambda metafactory
rfield
parents:
14323
diff
changeset
|
354 |
static final Executor logPool = Executors.newSingleThreadExecutor(); |
14323 | 355 |
protected static void log(final String s) { |
356 |
MethodHandleProxyLambdaMetafactory.logPool.execute(new Runnable() { |
|
357 |
@Override |
|
358 |
public void run() { |
|
359 |
System.out.println(s); |
|
360 |
} |
|
361 |
}); |
|
362 |
} |
|
363 |
||
364 |
protected static void log(final String s, final Throwable e) { |
|
365 |
MethodHandleProxyLambdaMetafactory.logPool.execute(new Runnable() { |
|
366 |
@Override |
|
367 |
public void run() { |
|
368 |
System.out.println(s); |
|
369 |
e.printStackTrace(System.out); |
|
370 |
} |
|
371 |
}); |
|
372 |
} |
|
373 |
***********************/ |
|
374 |
||
375 |
} |