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