1 /* |
|
2 * Copyright (c) 2010, 2013, 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 |
|
26 package jdk.nashorn.internal.codegen; |
|
27 |
|
28 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; |
|
29 import static jdk.nashorn.internal.codegen.types.Type.BOOLEAN; |
|
30 import static jdk.nashorn.internal.codegen.types.Type.INT; |
|
31 import static jdk.nashorn.internal.lookup.Lookup.MH; |
|
32 |
|
33 import java.lang.invoke.CallSite; |
|
34 import java.lang.invoke.MethodHandle; |
|
35 import java.lang.invoke.MethodHandles; |
|
36 import java.lang.invoke.MethodType; |
|
37 import java.lang.invoke.MutableCallSite; |
|
38 import java.util.HashMap; |
|
39 import java.util.Map; |
|
40 import jdk.nashorn.internal.codegen.CompilerConstants.Call; |
|
41 import jdk.nashorn.internal.codegen.types.Type; |
|
42 import jdk.nashorn.internal.ir.RuntimeNode; |
|
43 import jdk.nashorn.internal.ir.RuntimeNode.Request; |
|
44 import jdk.nashorn.internal.lookup.Lookup; |
|
45 import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
46 import jdk.nashorn.internal.runtime.linker.Bootstrap; |
|
47 |
|
48 /** |
|
49 * Optimistic call site that assumes its Object arguments to be of a boxed type. |
|
50 * Gradually reverts to wider boxed types if the assumption for the RuntimeNode |
|
51 * is proven wrong. Finally reverts to the generic ScriptRuntime method. |
|
52 * |
|
53 * This is used from the CodeGenerator when we have a runtime node, but 1 or more |
|
54 * primitive arguments. This class generated appropriate specializations, for example |
|
55 * {@code Object a === int b} is a good idea to specialize to {@code ((Integer)a).intValue() == b} |
|
56 * surrounded by catch blocks that will try less narrow specializations |
|
57 */ |
|
58 public final class RuntimeCallSite extends MutableCallSite { |
|
59 static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "runtimeBootstrap", CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class); |
|
60 |
|
61 private static final MethodHandle NEXT = findOwnMH_V("next", MethodHandle.class, String.class); |
|
62 |
|
63 private final RuntimeNode.Request request; |
|
64 |
|
65 /** |
|
66 * A specialized runtime node, i.e. on where we know at least one more specific type than object |
|
67 */ |
|
68 static final class SpecializedRuntimeNode { |
|
69 private static final char REQUEST_SEPARATOR = ':'; |
|
70 |
|
71 private final RuntimeNode.Request request; |
|
72 |
|
73 private final Type[] parameterTypes; |
|
74 |
|
75 private final Type returnType; |
|
76 |
|
77 /** |
|
78 * Constructor. |
|
79 * |
|
80 * @param request runtime node request to specialize |
|
81 * @param parameterTypes parameter types of the call site |
|
82 * @param returnType return type of the call site |
|
83 */ |
|
84 SpecializedRuntimeNode(final RuntimeNode.Request request, final Type[] parameterTypes, final Type returnType) { |
|
85 this.request = request; |
|
86 this.parameterTypes = parameterTypes; |
|
87 this.returnType = returnType; |
|
88 } |
|
89 |
|
90 /** |
|
91 * The first type to try to use for this generated runtime node |
|
92 * |
|
93 * @return a type |
|
94 */ |
|
95 public Type firstTypeGuess() { |
|
96 Type widest = Type.UNKNOWN; |
|
97 for (final Type type : parameterTypes) { |
|
98 if (type.isObject()) { |
|
99 continue; |
|
100 } |
|
101 widest = Type.widest(type, widest); |
|
102 } |
|
103 widest = Type.widest(widest, firstTypeGuessForObject(request)); |
|
104 |
|
105 return widest; |
|
106 } |
|
107 |
|
108 private static Type firstTypeGuessForObject(final Request request) { |
|
109 switch (request) { |
|
110 case ADD: |
|
111 return INT; |
|
112 default: |
|
113 return BOOLEAN; |
|
114 } |
|
115 } |
|
116 |
|
117 Request getRequest() { |
|
118 return request; |
|
119 } |
|
120 |
|
121 Type[] getParameterTypes() { |
|
122 return parameterTypes; |
|
123 } |
|
124 |
|
125 Type getReturnType() { |
|
126 return returnType; |
|
127 } |
|
128 |
|
129 private static char descFor(final Type type) { |
|
130 if (type.isObject()) { |
|
131 return 'O'; |
|
132 } |
|
133 return type.getDescriptor().charAt(0); |
|
134 } |
|
135 |
|
136 @Override |
|
137 public boolean equals(final Object other) { |
|
138 if (other instanceof SpecializedRuntimeNode) { |
|
139 final SpecializedRuntimeNode otherNode = (SpecializedRuntimeNode)other; |
|
140 |
|
141 if (!otherNode.getReturnType().equals(getReturnType())) { |
|
142 return false; |
|
143 } |
|
144 |
|
145 if (getParameterTypes().length != otherNode.getParameterTypes().length) { |
|
146 return false; |
|
147 } |
|
148 |
|
149 for (int i = 0; i < getParameterTypes().length; i++) { |
|
150 if (!Type.areEquivalent(getParameterTypes()[i], otherNode.getParameterTypes()[i])) { |
|
151 return false; |
|
152 } |
|
153 } |
|
154 |
|
155 return otherNode.getRequest().equals(getRequest()); |
|
156 } |
|
157 |
|
158 return false; |
|
159 } |
|
160 |
|
161 @Override |
|
162 public int hashCode() { |
|
163 int hashCode = getRequest().toString().hashCode(); |
|
164 hashCode ^= getReturnType().hashCode(); |
|
165 for (final Type type : getParameterTypes()) { |
|
166 hashCode ^= type.hashCode(); |
|
167 } |
|
168 return hashCode; |
|
169 } |
|
170 |
|
171 @Override |
|
172 public String toString() { |
|
173 final StringBuilder sb = new StringBuilder(); |
|
174 sb.append(getRequest().toString()); |
|
175 sb.append(REQUEST_SEPARATOR); |
|
176 sb.append(descFor(getReturnType())); |
|
177 |
|
178 for (final Type type : getParameterTypes()) { |
|
179 sb.append(descFor(type)); |
|
180 } |
|
181 |
|
182 return sb.toString(); |
|
183 } |
|
184 |
|
185 String getName(final Type extraType) { |
|
186 return toString() + "_" + descFor(extraType); |
|
187 } |
|
188 |
|
189 String getInitialName() { |
|
190 return getName(firstTypeGuess()); |
|
191 } |
|
192 } |
|
193 |
|
194 |
|
195 /** |
|
196 * Constructor |
|
197 * |
|
198 * @param type method type for call site |
|
199 * @param name name of runtime call |
|
200 */ |
|
201 public RuntimeCallSite(final MethodType type, final String name) { |
|
202 super(type); |
|
203 this.request = Request.valueOf(name.substring(0, name.indexOf(SpecializedRuntimeNode.REQUEST_SEPARATOR))); |
|
204 setTarget(makeMethod(name)); |
|
205 } |
|
206 |
|
207 private String nextName(final String requestName) { |
|
208 if (requestName.equals(request.toString())) { |
|
209 return null; |
|
210 } |
|
211 |
|
212 final char[] c = requestName.toCharArray(); |
|
213 final int last = c.length - 1; |
|
214 |
|
215 if (c[last - 1] != '_') { |
|
216 return null; |
|
217 } |
|
218 |
|
219 switch (c[last]) { |
|
220 case 'Z': |
|
221 c[last] = 'I'; |
|
222 break; |
|
223 case 'I': |
|
224 c[last] = 'J'; |
|
225 break; |
|
226 case 'J': |
|
227 c[last] = 'D'; |
|
228 break; |
|
229 case 'D': |
|
230 default: |
|
231 return request.toString(); |
|
232 } |
|
233 |
|
234 return new String(c); |
|
235 } |
|
236 |
|
237 private boolean isSpecialized(final String requestName) { |
|
238 return nextName(requestName) != null; |
|
239 } |
|
240 |
|
241 private MethodHandle makeMethod(final String requestName) { |
|
242 MethodHandle mh; |
|
243 |
|
244 if (isSpecialized(requestName)) { |
|
245 final Class<?> boxedType; |
|
246 final Class<?> primitiveType; |
|
247 |
|
248 switch (requestName.charAt(requestName.length() - 1)) { |
|
249 case 'Z': |
|
250 boxedType = Boolean.class; |
|
251 primitiveType = int.class; |
|
252 break; |
|
253 case 'I': |
|
254 boxedType = Integer.class; |
|
255 primitiveType = int.class; |
|
256 break; |
|
257 case 'J': |
|
258 boxedType = Long.class; |
|
259 primitiveType = long.class; |
|
260 break; |
|
261 case 'D': |
|
262 boxedType = Number.class; |
|
263 primitiveType = double.class; |
|
264 break; |
|
265 default: |
|
266 throw new RuntimeException("should not reach here"); |
|
267 } |
|
268 |
|
269 final boolean isStrictCmp = (request == Request.EQ_STRICT || request == Request.NE_STRICT); |
|
270 |
|
271 if (isStrictCmp && |
|
272 (boxedType != Boolean.class && |
|
273 (type().parameterType(0) == boolean.class || |
|
274 type().parameterType(1) == boolean.class))) { |
|
275 // number and boolean are never strictly equal, e.g. 0 !== false |
|
276 mh = MH.dropArguments(MH.constant(boolean.class, request == Request.NE_STRICT), 0, type().parameterArray()); |
|
277 } else { |
|
278 mh = METHODS.get(request.nonStrictName() + primitiveType.getSimpleName()); |
|
279 // unbox objects |
|
280 |
|
281 for (int i = 0; i < type().parameterCount(); i++) { |
|
282 if (!type().parameterType(i).isPrimitive()) { |
|
283 mh = MH.filterArguments(mh, i, UNBOX.get(boxedType)); |
|
284 } |
|
285 } |
|
286 |
|
287 mh = Lookup.filterReturnType(mh, type().returnType()); |
|
288 mh = MH.explicitCastArguments(mh, type()); |
|
289 } |
|
290 |
|
291 final MethodHandle fallback = MH.foldArguments(MethodHandles.exactInvoker(type()), MH.insertArguments(NEXT, 0, this, requestName)); |
|
292 |
|
293 MethodHandle guard; |
|
294 if (type().parameterType(0).isPrimitive()) { |
|
295 guard = MH.insertArguments( |
|
296 MH.dropArguments(CHECKCAST, 1, type().parameterType(0)), 0, boxedType); |
|
297 } else if (type().parameterType(1).isPrimitive()) { |
|
298 guard = MH.insertArguments( |
|
299 MH.dropArguments(CHECKCAST, 2, type().parameterType(1)), 0, boxedType); |
|
300 } else { |
|
301 assert !type().parameterType(0).isPrimitive() && !type().parameterType(1).isPrimitive(); |
|
302 guard = MH.insertArguments(CHECKCAST2, 0, boxedType); |
|
303 } |
|
304 |
|
305 if (request == Request.ADD && boxedType == Integer.class) { |
|
306 // int add needs additional overflow check |
|
307 MethodHandle addcheck = ADDCHECK; |
|
308 for (int i = 0; i < type().parameterCount(); i++) { |
|
309 if (!type().parameterType(i).isPrimitive()) { |
|
310 addcheck = MH.filterArguments(addcheck, i, UNBOX.get(boxedType)); |
|
311 } |
|
312 } |
|
313 addcheck = MH.explicitCastArguments(addcheck, type().changeReturnType(boolean.class)); |
|
314 guard = MH.guardWithTest(upcastGuard(guard), addcheck, |
|
315 MH.dropArguments(MH.constant(boolean.class, false), 0, type().parameterArray())); |
|
316 } |
|
317 |
|
318 return MH.guardWithTest(upcastGuard(guard), mh, fallback); |
|
319 } |
|
320 |
|
321 // generic fallback |
|
322 return MH.explicitCastArguments(Lookup.filterReturnType(GENERIC_METHODS.get(request.name()), type().returnType()), type()); |
|
323 } |
|
324 |
|
325 private MethodHandle upcastGuard(final MethodHandle guard) { |
|
326 return MH.asType(guard, type().changeReturnType(boolean.class)); |
|
327 } |
|
328 |
|
329 /** |
|
330 * This is public just so that the generated specialization code can |
|
331 * use it to get the next wider typed method |
|
332 * |
|
333 * Do not call directly |
|
334 * |
|
335 * @param name current name (with type) of runtime call at the call site |
|
336 * @return next wider specialization method for this RuntimeCallSite |
|
337 */ |
|
338 public MethodHandle next(final String name) { |
|
339 final MethodHandle next = makeMethod(nextName(name)); |
|
340 setTarget(next); |
|
341 return next; |
|
342 } |
|
343 |
|
344 /** Method cache */ |
|
345 private static final Map<String, MethodHandle> METHODS; |
|
346 |
|
347 /** Generic method cache */ |
|
348 private static final Map<String, MethodHandle> GENERIC_METHODS; |
|
349 |
|
350 /** Unbox cache */ |
|
351 private static final Map<Class<?>, MethodHandle> UNBOX; |
|
352 |
|
353 private static final MethodHandle CHECKCAST = findOwnMH_S("checkcast", boolean.class, Class.class, Object.class); |
|
354 private static final MethodHandle CHECKCAST2 = findOwnMH_S("checkcast", boolean.class, Class.class, Object.class, Object.class); |
|
355 private static final MethodHandle ADDCHECK = findOwnMH_S("ADDcheck", boolean.class, int.class, int.class); |
|
356 |
|
357 /** |
|
358 * Build maps of correct boxing operations |
|
359 */ |
|
360 static { |
|
361 UNBOX = new HashMap<>(); |
|
362 UNBOX.put(Boolean.class, findOwnMH_S("unboxZ", int.class, Object.class)); |
|
363 UNBOX.put(Integer.class, findOwnMH_S("unboxI", int.class, Object.class)); |
|
364 UNBOX.put(Long.class, findOwnMH_S("unboxJ", long.class, Object.class)); |
|
365 UNBOX.put(Number.class, findOwnMH_S("unboxD", double.class, Object.class)); |
|
366 |
|
367 METHODS = new HashMap<>(); |
|
368 |
|
369 for (final Request req : Request.values()) { |
|
370 if (req.canSpecialize()) { |
|
371 if (req.name().endsWith("_STRICT")) { |
|
372 continue; |
|
373 } |
|
374 |
|
375 final boolean isCmp = Request.isComparison(req); |
|
376 |
|
377 METHODS.put(req.name() + "int", findOwnMH_S(req.name(), (isCmp ? boolean.class : int.class), int.class, int.class)); |
|
378 METHODS.put(req.name() + "long", findOwnMH_S(req.name(), (isCmp ? boolean.class : long.class), long.class, long.class)); |
|
379 METHODS.put(req.name() + "double", findOwnMH_S(req.name(), (isCmp ? boolean.class : double.class), double.class, double.class)); |
|
380 } |
|
381 } |
|
382 |
|
383 GENERIC_METHODS = new HashMap<>(); |
|
384 for (final Request req : Request.values()) { |
|
385 if (req.canSpecialize()) { |
|
386 GENERIC_METHODS.put(req.name(), MH.findStatic(MethodHandles.lookup(), ScriptRuntime.class, req.name(), |
|
387 MH.type(req.getReturnType().getTypeClass(), Object.class, Object.class))); |
|
388 } |
|
389 } |
|
390 } |
|
391 |
|
392 /** |
|
393 * Specialized version of != operator for two int arguments. Do not call directly. |
|
394 * @param a int |
|
395 * @param b int |
|
396 * @return a != b |
|
397 */ |
|
398 public static boolean NE(final int a, final int b) { |
|
399 return a != b; |
|
400 } |
|
401 |
|
402 /** |
|
403 * Specialized version of != operator for two double arguments. Do not call directly. |
|
404 * @param a double |
|
405 * @param b double |
|
406 * @return a != b |
|
407 */ |
|
408 public static boolean NE(final double a, final double b) { |
|
409 return a != b; |
|
410 } |
|
411 |
|
412 /** |
|
413 * Specialized version of != operator for two long arguments. Do not call directly. |
|
414 * @param a long |
|
415 * @param b long |
|
416 * @return a != b |
|
417 */ |
|
418 public static boolean NE(final long a, final long b) { |
|
419 return a != b; |
|
420 } |
|
421 |
|
422 /** |
|
423 * Specialized version of == operator for two int arguments. Do not call directly. |
|
424 * @param a int |
|
425 * @param b int |
|
426 * @return a == b |
|
427 */ |
|
428 public static boolean EQ(final int a, final int b) { |
|
429 return a == b; |
|
430 } |
|
431 |
|
432 /** |
|
433 * Specialized version of == operator for two double arguments. Do not call directly. |
|
434 * @param a double |
|
435 * @param b double |
|
436 * @return a == b |
|
437 */ |
|
438 public static boolean EQ(final double a, final double b) { |
|
439 return a == b; |
|
440 } |
|
441 |
|
442 /** |
|
443 * Specialized version of == operator for two long arguments. Do not call directly. |
|
444 * @param a long |
|
445 * @param b long |
|
446 * @return a == b |
|
447 */ |
|
448 public static boolean EQ(final long a, final long b) { |
|
449 return a == b; |
|
450 } |
|
451 |
|
452 /** |
|
453 * Specialized version of {@literal <} operator for two int arguments. Do not call directly. |
|
454 * @param a int |
|
455 * @param b int |
|
456 * @return a {@code <} b |
|
457 */ |
|
458 public static boolean LT(final int a, final int b) { |
|
459 return a < b; |
|
460 } |
|
461 |
|
462 /** |
|
463 * Specialized version of {@literal <} operator for two double arguments. Do not call directly. |
|
464 * @param a double |
|
465 * @param b double |
|
466 * @return a {@literal <} b |
|
467 */ |
|
468 public static boolean LT(final double a, final double b) { |
|
469 return a < b; |
|
470 } |
|
471 |
|
472 /** |
|
473 * Specialized version of {@literal <} operator for two long arguments. Do not call directly. |
|
474 * @param a long |
|
475 * @param b long |
|
476 * @return a {@literal <} b |
|
477 */ |
|
478 public static boolean LT(final long a, final long b) { |
|
479 return a < b; |
|
480 } |
|
481 |
|
482 /** |
|
483 * Specialized version of {@literal <=} operator for two int arguments. Do not call directly. |
|
484 * @param a int |
|
485 * @param b int |
|
486 * @return a {@literal <=} b |
|
487 */ |
|
488 public static boolean LE(final int a, final int b) { |
|
489 return a <= b; |
|
490 } |
|
491 |
|
492 /** |
|
493 * Specialized version of {@literal <=} operator for two double arguments. Do not call directly. |
|
494 * @param a double |
|
495 * @param b double |
|
496 * @return a {@literal <=} b |
|
497 */ |
|
498 public static boolean LE(final double a, final double b) { |
|
499 return a <= b; |
|
500 } |
|
501 |
|
502 /** |
|
503 * Specialized version of {@literal <=} operator for two long arguments. Do not call directly. |
|
504 * @param a long |
|
505 * @param b long |
|
506 * @return a {@literal <=} b |
|
507 */ |
|
508 public static boolean LE(final long a, final long b) { |
|
509 return a <= b; |
|
510 } |
|
511 |
|
512 /** |
|
513 * Specialized version of {@literal >} operator for two int arguments. Do not call directly. |
|
514 * @param a int |
|
515 * @param b int |
|
516 * @return a {@literal >} b |
|
517 */ |
|
518 public static boolean GT(final int a, final int b) { |
|
519 return a > b; |
|
520 } |
|
521 |
|
522 /** |
|
523 * Specialized version of {@literal >} operator for two double arguments. Do not call directly. |
|
524 * @param a double |
|
525 * @param b double |
|
526 * @return a {@literal >} b |
|
527 */ |
|
528 public static boolean GT(final double a, final double b) { |
|
529 return a > b; |
|
530 } |
|
531 |
|
532 /** |
|
533 * Specialized version of {@literal >} operator for two long arguments. Do not call directly. |
|
534 * @param a long |
|
535 * @param b long |
|
536 * @return a {@literal >} b |
|
537 */ |
|
538 public static boolean GT(final long a, final long b) { |
|
539 return a > b; |
|
540 } |
|
541 |
|
542 /** |
|
543 * Specialized version of {@literal >=} operator for two int arguments. Do not call directly. |
|
544 * @param a int |
|
545 * @param b int |
|
546 * @return a {@literal >=} b |
|
547 */ |
|
548 public static boolean GE(final int a, final int b) { |
|
549 return a >= b; |
|
550 } |
|
551 |
|
552 /** |
|
553 * Specialized version of {@literal >=} operator for two double arguments. Do not call directly. |
|
554 * @param a double |
|
555 * @param b double |
|
556 * @return a {@literal >=} b |
|
557 */ |
|
558 public static boolean GE(final double a, final double b) { |
|
559 return a >= b; |
|
560 } |
|
561 |
|
562 /** |
|
563 * Specialized version of {@literal >=} operator for two long arguments. Do not call directly. |
|
564 * @param a long |
|
565 * @param b long |
|
566 * @return a {@code >=} b |
|
567 */ |
|
568 public static boolean GE(final long a, final long b) { |
|
569 return a >= b; |
|
570 } |
|
571 |
|
572 /** |
|
573 * Specialized version of + operator for two int arguments. Do not call directly. |
|
574 * @param a int |
|
575 * @param b int |
|
576 * @return a + b |
|
577 */ |
|
578 public static int ADD(final int a, final int b) { |
|
579 return a + b; |
|
580 } |
|
581 |
|
582 /** |
|
583 * Specialized version of + operator for two long arguments. Do not call directly. |
|
584 * @param a long |
|
585 * @param b long |
|
586 * @return a + b |
|
587 */ |
|
588 public static long ADD(final long a, final long b) { |
|
589 return a + b; |
|
590 } |
|
591 |
|
592 /** |
|
593 * Specialized version of + operator for two double arguments. Do not call directly. |
|
594 * @param a double |
|
595 * @param b double |
|
596 * @return a + b |
|
597 */ |
|
598 public static double ADD(final double a, final double b) { |
|
599 return a + b; |
|
600 } |
|
601 |
|
602 /** |
|
603 * Check that ints are addition compatible, i.e. their sum is equal to the sum |
|
604 * of them cast to long. Otherwise the addition will overflow. Do not call directly. |
|
605 * |
|
606 * @param a int |
|
607 * @param b int |
|
608 * |
|
609 * @return true if addition does not overflow |
|
610 */ |
|
611 public static boolean ADDcheck(final int a, final int b) { |
|
612 return (a + b == (long)a + (long)b); |
|
613 } |
|
614 |
|
615 /** |
|
616 * Checkcast used for specialized ops. Do not call directly |
|
617 * |
|
618 * @param type to to check against |
|
619 * @param obj object to check for type |
|
620 * |
|
621 * @return true if type check holds |
|
622 */ |
|
623 public static boolean checkcast(final Class<?> type, final Object obj) { |
|
624 return type.isInstance(obj); |
|
625 } |
|
626 |
|
627 /** |
|
628 * Checkcast used for specialized ops. Do not call directly |
|
629 * |
|
630 * @param type type to check against |
|
631 * @param objA first object to check against type |
|
632 * @param objB second object to check against type |
|
633 * |
|
634 * @return true if type check holds for both objects |
|
635 */ |
|
636 public static boolean checkcast(final Class<?> type, final Object objA, final Object objB) { |
|
637 return type.isInstance(objA) && type.isInstance(objB); |
|
638 } |
|
639 |
|
640 /** |
|
641 * Unbox a java.lang.Boolean. Do not call directly |
|
642 * @param obj object to cast to int and unbox |
|
643 * @return an int value for the boolean, 1 is true, 0 is false |
|
644 */ |
|
645 public static int unboxZ(final Object obj) { |
|
646 return (boolean)obj ? 1 : 0; |
|
647 } |
|
648 |
|
649 /** |
|
650 * Unbox a java.lang.Integer. Do not call directly |
|
651 * @param obj object to cast to int and unbox |
|
652 * @return an int |
|
653 */ |
|
654 public static int unboxI(final Object obj) { |
|
655 return (int)obj; |
|
656 } |
|
657 |
|
658 /** |
|
659 * Unbox a java.lang.Long. Do not call directly |
|
660 * @param obj object to cast to long and unbox |
|
661 * @return a long |
|
662 */ |
|
663 public static long unboxJ(final Object obj) { |
|
664 return (long)obj; |
|
665 } |
|
666 |
|
667 /** |
|
668 * Unbox a java.lang.Number. Do not call directly |
|
669 * @param obj object to cast to Number and unbox |
|
670 * @return a double |
|
671 */ |
|
672 public static double unboxD(final Object obj) { |
|
673 return ((Number)obj).doubleValue(); |
|
674 } |
|
675 |
|
676 private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { |
|
677 return MH.findStatic(MethodHandles.lookup(), RuntimeCallSite.class, name, MH.type(rtype, types)); |
|
678 } |
|
679 |
|
680 private static MethodHandle findOwnMH_V(final String name, final Class<?> rtype, final Class<?>... types) { |
|
681 return MH.findVirtual(MethodHandles.lookup(), RuntimeCallSite.class, name, MH.type(rtype, types)); |
|
682 } |
|
683 } |
|