John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 1 | /* |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 2 | * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 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 |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. Oracle designates this |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 8 | * particular file as subject to the "Classpath" exception as provided |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 9 | * by Oracle in the LICENSE file that accompanied this code. |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 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 | * |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 26 | package java.dyn; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 27 | |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 28 | import sun.dyn.empty.Empty; |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 29 | import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Construction and caching of often-used invokers. |
| 33 | * @author jrose |
| 34 | */ |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 35 | class Invokers { |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 36 | // exact type (sans leading taget MH) for the outgoing call |
| 37 | private final MethodType targetType; |
| 38 | |
| 39 | // exact invoker for the outgoing call |
| 40 | private /*lazy*/ MethodHandle exactInvoker; |
| 41 | |
John R Rose | 2a322bb | 2010-10-30 21:02:30 -0700 | [diff] [blame] | 42 | // erased (partially untyped but with primitives) invoker for the outgoing call |
| 43 | private /*lazy*/ MethodHandle erasedInvoker; |
| 44 | /*lazy*/ MethodHandle erasedInvokerWithDrops; // for InvokeGeneric |
| 45 | |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 46 | // generic (untyped) invoker for the outgoing call |
| 47 | private /*lazy*/ MethodHandle genericInvoker; |
| 48 | |
John R Rose | 020e550 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 49 | // generic (untyped) invoker for the outgoing call; accepts a single Object[] |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 50 | private final /*lazy*/ MethodHandle[] spreadInvokers; |
John R Rose | 020e550 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 51 | |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 52 | // invoker for an unbound callsite |
| 53 | private /*lazy*/ MethodHandle uninitializedCallSite; |
| 54 | |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 55 | /** Compute and cache information common to all collecting adapters |
| 56 | * that implement members of the erasure-family of the given erased type. |
| 57 | */ |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 58 | /*non-public*/ Invokers(MethodType targetType) { |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 59 | this.targetType = targetType; |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 60 | this.spreadInvokers = new MethodHandle[targetType.parameterCount()+1]; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 61 | } |
| 62 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 63 | /*non-public*/ static MethodType invokerType(MethodType targetType) { |
John R Rose | 020e550 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 64 | return targetType.insertParameterTypes(0, MethodHandle.class); |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 65 | } |
| 66 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 67 | /*non-public*/ MethodHandle exactInvoker() { |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 68 | MethodHandle invoker = exactInvoker; |
| 69 | if (invoker != null) return invoker; |
John R Rose | 6fbfeff | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 70 | try { |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 71 | invoker = IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType); |
John R Rose | f485ab5 | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 72 | } catch (ReflectiveOperationException ex) { |
John R Rose | 6fbfeff | 2010-09-08 18:40:23 -0700 | [diff] [blame] | 73 | throw new InternalError("JVM cannot find invoker for "+targetType); |
| 74 | } |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 75 | assert(invokerType(targetType) == invoker.type()); |
| 76 | exactInvoker = invoker; |
| 77 | return invoker; |
| 78 | } |
| 79 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 80 | /*non-public*/ MethodHandle genericInvoker() { |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 81 | MethodHandle invoker1 = exactInvoker(); |
| 82 | MethodHandle invoker = genericInvoker; |
| 83 | if (invoker != null) return invoker; |
| 84 | MethodType genericType = targetType.generic(); |
| 85 | invoker = MethodHandles.convertArguments(invoker1, invokerType(genericType)); |
| 86 | genericInvoker = invoker; |
| 87 | return invoker; |
| 88 | } |
| 89 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 90 | /*non-public*/ MethodHandle erasedInvoker() { |
John R Rose | 2a322bb | 2010-10-30 21:02:30 -0700 | [diff] [blame] | 91 | MethodHandle invoker1 = exactInvoker(); |
| 92 | MethodHandle invoker = erasedInvoker; |
| 93 | if (invoker != null) return invoker; |
| 94 | MethodType erasedType = targetType.erase(); |
| 95 | if (erasedType == targetType.generic()) |
| 96 | invoker = genericInvoker(); |
| 97 | else |
| 98 | invoker = MethodHandles.convertArguments(invoker1, invokerType(erasedType)); |
| 99 | erasedInvoker = invoker; |
| 100 | return invoker; |
| 101 | } |
| 102 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 103 | /*non-public*/ MethodHandle spreadInvoker(int objectArgCount) { |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 104 | MethodHandle vaInvoker = spreadInvokers[objectArgCount]; |
John R Rose | 020e550 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 105 | if (vaInvoker != null) return vaInvoker; |
| 106 | MethodHandle gInvoker = genericInvoker(); |
John R Rose | 54e473f | 2011-02-11 01:26:28 -0800 | [diff] [blame] | 107 | vaInvoker = gInvoker.asSpreader(Object[].class, targetType.parameterCount() - objectArgCount); |
John R Rose | eedbeda | 2011-02-11 01:26:24 -0800 | [diff] [blame] | 108 | spreadInvokers[objectArgCount] = vaInvoker; |
John R Rose | 020e550 | 2010-01-07 16:16:45 -0800 | [diff] [blame] | 109 | return vaInvoker; |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 112 | private static MethodHandle THROW_UCS = null; |
| 113 | |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 114 | /*non-public*/ MethodHandle uninitializedCallSite() { |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 115 | MethodHandle invoker = uninitializedCallSite; |
| 116 | if (invoker != null) return invoker; |
| 117 | if (targetType.parameterCount() > 0) { |
| 118 | MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount()); |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 119 | Invokers invokers0 = type0.invokers(); |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 120 | invoker = MethodHandles.dropArguments(invokers0.uninitializedCallSite(), |
| 121 | 0, targetType.parameterList()); |
| 122 | assert(invoker.type().equals(targetType)); |
| 123 | uninitializedCallSite = invoker; |
| 124 | return invoker; |
| 125 | } |
| 126 | if (THROW_UCS == null) { |
| 127 | try { |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 128 | THROW_UCS = IMPL_LOOKUP |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 129 | .findStatic(CallSite.class, "uninitializedCallSite", |
| 130 | MethodType.methodType(Empty.class)); |
John R Rose | f485ab5 | 2011-02-11 01:26:32 -0800 | [diff] [blame] | 131 | } catch (ReflectiveOperationException ex) { |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 132 | throw new RuntimeException(ex); |
| 133 | } |
| 134 | } |
John R Rose | e7ebd47 | 2011-03-18 00:03:24 -0700 | [diff] [blame^] | 135 | invoker = AdapterMethodHandle.makeRetypeRaw(targetType, THROW_UCS); |
John R Rose | 4f508ab | 2010-10-30 21:08:23 -0700 | [diff] [blame] | 136 | assert(invoker.type().equals(targetType)); |
| 137 | uninitializedCallSite = invoker; |
| 138 | return invoker; |
| 139 | } |
| 140 | |
John R Rose | fb6164c | 2009-05-05 22:40:09 -0700 | [diff] [blame] | 141 | public String toString() { |
| 142 | return "Invokers"+targetType; |
| 143 | } |
| 144 | } |