2009-05-05 22:40:09 -07:00
|
|
|
/*
|
2011-02-11 01:26:24 -08:00
|
|
|
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
|
2009-05-05 22:40:09 -07:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2009-05-05 22:40:09 -07:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2009-05-05 22:40:09 -07:00
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
package java.dyn;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
import sun.dyn.empty.Empty;
|
2011-03-18 00:03:24 -07:00
|
|
|
import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construction and caching of often-used invokers.
|
|
|
|
* @author jrose
|
|
|
|
*/
|
2011-03-18 00:03:24 -07:00
|
|
|
class Invokers {
|
2009-05-05 22:40:09 -07:00
|
|
|
// exact type (sans leading taget MH) for the outgoing call
|
|
|
|
private final MethodType targetType;
|
|
|
|
|
|
|
|
// exact invoker for the outgoing call
|
|
|
|
private /*lazy*/ MethodHandle exactInvoker;
|
|
|
|
|
2010-10-30 21:02:30 -07:00
|
|
|
// erased (partially untyped but with primitives) invoker for the outgoing call
|
|
|
|
private /*lazy*/ MethodHandle erasedInvoker;
|
|
|
|
/*lazy*/ MethodHandle erasedInvokerWithDrops; // for InvokeGeneric
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
// generic (untyped) invoker for the outgoing call
|
|
|
|
private /*lazy*/ MethodHandle genericInvoker;
|
|
|
|
|
2010-01-07 16:16:45 -08:00
|
|
|
// generic (untyped) invoker for the outgoing call; accepts a single Object[]
|
2011-02-11 01:26:24 -08:00
|
|
|
private final /*lazy*/ MethodHandle[] spreadInvokers;
|
2010-01-07 16:16:45 -08:00
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
// invoker for an unbound callsite
|
|
|
|
private /*lazy*/ MethodHandle uninitializedCallSite;
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
/** Compute and cache information common to all collecting adapters
|
|
|
|
* that implement members of the erasure-family of the given erased type.
|
|
|
|
*/
|
2011-02-11 01:26:24 -08:00
|
|
|
/*non-public*/ Invokers(MethodType targetType) {
|
2009-05-05 22:40:09 -07:00
|
|
|
this.targetType = targetType;
|
2011-02-11 01:26:24 -08:00
|
|
|
this.spreadInvokers = new MethodHandle[targetType.parameterCount()+1];
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ static MethodType invokerType(MethodType targetType) {
|
2010-01-07 16:16:45 -08:00
|
|
|
return targetType.insertParameterTypes(0, MethodHandle.class);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ MethodHandle exactInvoker() {
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodHandle invoker = exactInvoker;
|
|
|
|
if (invoker != null) return invoker;
|
2010-09-08 18:40:23 -07:00
|
|
|
try {
|
2011-03-18 00:03:24 -07:00
|
|
|
invoker = IMPL_LOOKUP.findVirtual(MethodHandle.class, "invokeExact", targetType);
|
2011-02-11 01:26:32 -08:00
|
|
|
} catch (ReflectiveOperationException ex) {
|
2010-09-08 18:40:23 -07:00
|
|
|
throw new InternalError("JVM cannot find invoker for "+targetType);
|
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
assert(invokerType(targetType) == invoker.type());
|
|
|
|
exactInvoker = invoker;
|
|
|
|
return invoker;
|
|
|
|
}
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ MethodHandle genericInvoker() {
|
2009-05-05 22:40:09 -07:00
|
|
|
MethodHandle invoker1 = exactInvoker();
|
|
|
|
MethodHandle invoker = genericInvoker;
|
|
|
|
if (invoker != null) return invoker;
|
|
|
|
MethodType genericType = targetType.generic();
|
|
|
|
invoker = MethodHandles.convertArguments(invoker1, invokerType(genericType));
|
|
|
|
genericInvoker = invoker;
|
|
|
|
return invoker;
|
|
|
|
}
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ MethodHandle erasedInvoker() {
|
2010-10-30 21:02:30 -07:00
|
|
|
MethodHandle invoker1 = exactInvoker();
|
|
|
|
MethodHandle invoker = erasedInvoker;
|
|
|
|
if (invoker != null) return invoker;
|
|
|
|
MethodType erasedType = targetType.erase();
|
|
|
|
if (erasedType == targetType.generic())
|
|
|
|
invoker = genericInvoker();
|
|
|
|
else
|
|
|
|
invoker = MethodHandles.convertArguments(invoker1, invokerType(erasedType));
|
|
|
|
erasedInvoker = invoker;
|
|
|
|
return invoker;
|
|
|
|
}
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ MethodHandle spreadInvoker(int objectArgCount) {
|
2011-02-11 01:26:24 -08:00
|
|
|
MethodHandle vaInvoker = spreadInvokers[objectArgCount];
|
2010-01-07 16:16:45 -08:00
|
|
|
if (vaInvoker != null) return vaInvoker;
|
|
|
|
MethodHandle gInvoker = genericInvoker();
|
2011-02-11 01:26:28 -08:00
|
|
|
vaInvoker = gInvoker.asSpreader(Object[].class, targetType.parameterCount() - objectArgCount);
|
2011-02-11 01:26:24 -08:00
|
|
|
spreadInvokers[objectArgCount] = vaInvoker;
|
2010-01-07 16:16:45 -08:00
|
|
|
return vaInvoker;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
private static MethodHandle THROW_UCS = null;
|
|
|
|
|
2011-03-18 00:03:24 -07:00
|
|
|
/*non-public*/ MethodHandle uninitializedCallSite() {
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodHandle invoker = uninitializedCallSite;
|
|
|
|
if (invoker != null) return invoker;
|
|
|
|
if (targetType.parameterCount() > 0) {
|
|
|
|
MethodType type0 = targetType.dropParameterTypes(0, targetType.parameterCount());
|
2011-03-18 00:03:24 -07:00
|
|
|
Invokers invokers0 = type0.invokers();
|
2010-10-30 21:08:23 -07:00
|
|
|
invoker = MethodHandles.dropArguments(invokers0.uninitializedCallSite(),
|
|
|
|
0, targetType.parameterList());
|
|
|
|
assert(invoker.type().equals(targetType));
|
|
|
|
uninitializedCallSite = invoker;
|
|
|
|
return invoker;
|
|
|
|
}
|
|
|
|
if (THROW_UCS == null) {
|
|
|
|
try {
|
2011-03-18 00:03:24 -07:00
|
|
|
THROW_UCS = IMPL_LOOKUP
|
2010-10-30 21:08:23 -07:00
|
|
|
.findStatic(CallSite.class, "uninitializedCallSite",
|
|
|
|
MethodType.methodType(Empty.class));
|
2011-02-11 01:26:32 -08:00
|
|
|
} catch (ReflectiveOperationException ex) {
|
2010-10-30 21:08:23 -07:00
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 00:03:24 -07:00
|
|
|
invoker = AdapterMethodHandle.makeRetypeRaw(targetType, THROW_UCS);
|
2010-10-30 21:08:23 -07:00
|
|
|
assert(invoker.type().equals(targetType));
|
|
|
|
uninitializedCallSite = invoker;
|
|
|
|
return invoker;
|
|
|
|
}
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
public String toString() {
|
|
|
|
return "Invokers"+targetType;
|
|
|
|
}
|
|
|
|
}
|