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