blob: 4a7fcbe3ab8ab758f5ffa34588cbd1c39db091f3 [file] [log] [blame]
John R Rosefb6164c2009-05-05 22:40:09 -07001/*
John R Roseeedbeda2011-02-11 01:26:24 -08002 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
John R Rosefb6164c2009-05-05 22:40:09 -07003 * 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'Hairfe008ae2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
John R Rosefb6164c2009-05-05 22:40:09 -07008 * particular file as subject to the "Classpath" exception as provided
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
John R Rosefb6164c2009-05-05 22:40:09 -070010 *
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'Hairfe008ae2010-05-25 15:58:33 -070021 * 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 Rosefb6164c2009-05-05 22:40:09 -070024 */
25
John R Rosee7ebd472011-03-18 00:03:24 -070026package java.dyn;
John R Rosefb6164c2009-05-05 22:40:09 -070027
John R Rose4f508ab2010-10-30 21:08:23 -070028import sun.dyn.empty.Empty;
John R Rosee7ebd472011-03-18 00:03:24 -070029import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
John R Rosefb6164c2009-05-05 22:40:09 -070030
31/**
32 * Construction and caching of often-used invokers.
33 * @author jrose
34 */
John R Rosee7ebd472011-03-18 00:03:24 -070035class Invokers {
John R Rosefb6164c2009-05-05 22:40:09 -070036 // 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 Rose2a322bb2010-10-30 21:02:30 -070042 // 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 Rosefb6164c2009-05-05 22:40:09 -070046 // generic (untyped) invoker for the outgoing call
47 private /*lazy*/ MethodHandle genericInvoker;
48
John R Rose020e5502010-01-07 16:16:45 -080049 // generic (untyped) invoker for the outgoing call; accepts a single Object[]
John R Roseeedbeda2011-02-11 01:26:24 -080050 private final /*lazy*/ MethodHandle[] spreadInvokers;
John R Rose020e5502010-01-07 16:16:45 -080051
John R Rose4f508ab2010-10-30 21:08:23 -070052 // invoker for an unbound callsite
53 private /*lazy*/ MethodHandle uninitializedCallSite;
54
John R Rosefb6164c2009-05-05 22:40:09 -070055 /** 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 Roseeedbeda2011-02-11 01:26:24 -080058 /*non-public*/ Invokers(MethodType targetType) {
John R Rosefb6164c2009-05-05 22:40:09 -070059 this.targetType = targetType;
John R Roseeedbeda2011-02-11 01:26:24 -080060 this.spreadInvokers = new MethodHandle[targetType.parameterCount()+1];
John R Rosefb6164c2009-05-05 22:40:09 -070061 }
62
John R Rosee7ebd472011-03-18 00:03:24 -070063 /*non-public*/ static MethodType invokerType(MethodType targetType) {
John R Rose020e5502010-01-07 16:16:45 -080064 return targetType.insertParameterTypes(0, MethodHandle.class);
John R Rosefb6164c2009-05-05 22:40:09 -070065 }
66
John R Rosee7ebd472011-03-18 00:03:24 -070067 /*non-public*/ MethodHandle exactInvoker() {
John R Rosefb6164c2009-05-05 22:40:09 -070068 MethodHandle invoker = exactInvoker;
69 if (invoker != null) return invoker;
John R Rose6fbfeff2010-09-08 18:40:23 -070070 try {
John R Rosee7ebd472011-03-18 00:03:24 -070071 invoker = IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType);
John R Rosef485ab52011-02-11 01:26:32 -080072 } catch (ReflectiveOperationException ex) {
John R Rose6fbfeff2010-09-08 18:40:23 -070073 throw new InternalError("JVM cannot find invoker for "+targetType);
74 }
John R Rosefb6164c2009-05-05 22:40:09 -070075 assert(invokerType(targetType) == invoker.type());
76 exactInvoker = invoker;
77 return invoker;
78 }
79
John R Rosee7ebd472011-03-18 00:03:24 -070080 /*non-public*/ MethodHandle genericInvoker() {
John R Rosefb6164c2009-05-05 22:40:09 -070081 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 Rosee7ebd472011-03-18 00:03:24 -070090 /*non-public*/ MethodHandle erasedInvoker() {
John R Rose2a322bb2010-10-30 21:02:30 -070091 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 Rosee7ebd472011-03-18 00:03:24 -0700103 /*non-public*/ MethodHandle spreadInvoker(int objectArgCount) {
John R Roseeedbeda2011-02-11 01:26:24 -0800104 MethodHandle vaInvoker = spreadInvokers[objectArgCount];
John R Rose020e5502010-01-07 16:16:45 -0800105 if (vaInvoker != null) return vaInvoker;
106 MethodHandle gInvoker = genericInvoker();
John R Rose54e473f2011-02-11 01:26:28 -0800107 vaInvoker = gInvoker.asSpreader(Object[].class, targetType.parameterCount() - objectArgCount);
John R Roseeedbeda2011-02-11 01:26:24 -0800108 spreadInvokers[objectArgCount] = vaInvoker;
John R Rose020e5502010-01-07 16:16:45 -0800109 return vaInvoker;
John R Rosefb6164c2009-05-05 22:40:09 -0700110 }
111
John R Rose4f508ab2010-10-30 21:08:23 -0700112 private static MethodHandle THROW_UCS = null;
113
John R Rosee7ebd472011-03-18 00:03:24 -0700114 /*non-public*/ MethodHandle uninitializedCallSite() {
John R Rose4f508ab2010-10-30 21:08:23 -0700115 MethodHandle invoker = uninitializedCallSite;
116 if (invoker != null) return invoker;
117 if (targetType.parameterCount() > 0) {
118 MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount());
John R Rosee7ebd472011-03-18 00:03:24 -0700119 Invokers invokers0 = type0.invokers();
John R Rose4f508ab2010-10-30 21:08:23 -0700120 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 Rosee7ebd472011-03-18 00:03:24 -0700128 THROW_UCS = IMPL_LOOKUP
John R Rose4f508ab2010-10-30 21:08:23 -0700129 .findStatic(CallSite.class, "uninitializedCallSite",
130 MethodType.methodType(Empty.class));
John R Rosef485ab52011-02-11 01:26:32 -0800131 } catch (ReflectiveOperationException ex) {
John R Rose4f508ab2010-10-30 21:08:23 -0700132 throw new RuntimeException(ex);
133 }
134 }
John R Rosee7ebd472011-03-18 00:03:24 -0700135 invoker = AdapterMethodHandle.makeRetypeRaw(targetType, THROW_UCS);
John R Rose4f508ab2010-10-30 21:08:23 -0700136 assert(invoker.type().equals(targetType));
137 uninitializedCallSite = invoker;
138 return invoker;
139 }
140
John R Rosefb6164c2009-05-05 22:40:09 -0700141 public String toString() {
142 return "Invokers"+targetType;
143 }
144}