diff --git a/src/share/classes/java/dyn/MethodHandle.java b/src/share/classes/java/dyn/MethodHandle.java
index 4904e975306b2a69ce0817c57b5e497e7f535a3a..251d338ce53b069fd642b384068a530b620bbee2 100644
--- a/src/share/classes/java/dyn/MethodHandle.java
+++ b/src/share/classes/java/dyn/MethodHandle.java
@@ -329,6 +329,7 @@ public abstract class MethodHandle
public final Object invokeVarargs(Object... arguments) throws Throwable {
int argc = arguments == null ? 0 : arguments.length;
MethodType type = type();
+ if (type.parameterCount() != argc) throw badParameterCount(type, argc);
if (argc <= 10) {
MethodHandle invoker = MethodHandles.invokers(type).genericInvoker();
switch (argc) {
@@ -377,6 +378,10 @@ public abstract class MethodHandle
return invokeVarargs(arguments.toArray());
}
+ private static WrongMethodTypeException badParameterCount(MethodType type, int argc) {
+ return new WrongMethodTypeException(type+" does not take "+argc+" parameters");
+ }
+
/* --- this is intentionally NOT a javadoc yet ---
* PROVISIONAL API, WORK IN PROGRESS:
* Produce an adapter method handle which adapts the type of the
@@ -446,7 +451,7 @@ public abstract class MethodHandle
* @throws IllegalArgumentException if the conversion cannot be made
* @see MethodHandles#convertArguments
*/
- public final MethodHandle asType(MethodType newType) {
+ public MethodHandle asType(MethodType newType) {
return MethodHandles.convertArguments(this, newType);
}
diff --git a/src/share/classes/java/dyn/MethodHandles.java b/src/share/classes/java/dyn/MethodHandles.java
index b6e8333834094497ecc7c8105a8f5bb5c440cd05..3e1fef7ff14b2ac503dada6e306dfa4ab6e62551 100644
--- a/src/share/classes/java/dyn/MethodHandles.java
+++ b/src/share/classes/java/dyn/MethodHandles.java
@@ -1068,10 +1068,14 @@ public class MethodHandles {
MethodType oldType = target.type();
if (oldType.equals(newType))
return target;
- MethodHandle res = MethodHandleImpl.convertArguments(IMPL_TOKEN, target,
- newType, oldType, null);
+ MethodHandle res = null;
+ try {
+ res = MethodHandleImpl.convertArguments(IMPL_TOKEN, target,
+ newType, oldType, null);
+ } catch (IllegalArgumentException ex) {
+ }
if (res == null)
- throw newIllegalArgumentException("cannot convert to "+newType+": "+target);
+ throw new WrongMethodTypeException("cannot convert to "+newType+": "+target);
return res;
}
@@ -1392,6 +1396,20 @@ public class MethodHandles {
return adapter;
}
+ /** Apply the given filter function to the return value of the given target.
+ */
+ /*public*/ static
+ MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
+ MethodType targetType = target.type();
+ MethodType filterType = filter.type();
+ if (filterType.parameterCount() != 1
+ || filterType.parameterType(0) != targetType.returnType())
+ throw newIllegalArgumentException("target and filter types do not match");
+ // FIXME: Too many nodes here.
+ MethodHandle returner = dropArguments(filter, 0, targetType.parameterList());
+ return foldArguments(returner, exactInvoker(target.type()).bindTo(target));
+ }
+
/**
* PROVISIONAL API, WORK IN PROGRESS:
* Adapt a target method handle {@code target} by pre-processing
@@ -1434,7 +1452,7 @@ public class MethodHandles {
* @return method handle which incorporates the specified argument folding logic
* @throws IllegalArgumentException if the first argument type of
* {@code target} is not the same as {@code combiner}'s return type,
- * or if the next {@code foldArgs} argument types of {@code target}
+ * or if the following argument types of {@code target}
* are not identical with the argument types of {@code combiner}
*/
public static
@@ -1443,12 +1461,39 @@ public class MethodHandles {
MethodType combinerType = combiner.type();
int foldArgs = combinerType.parameterCount();
boolean ok = (targetType.parameterCount() >= 1 + foldArgs);
+ if (ok && !combinerType.parameterList().equals(targetType.parameterList().subList(1, foldArgs+1)))
+ ok = false;
+ if (ok && !combinerType.returnType().equals(targetType.parameterType(0)))
+ ok = false;
if (!ok)
throw misMatchedTypes("target and combiner types", targetType, combinerType);
MethodType newType = targetType.dropParameterTypes(0, 1);
return MethodHandleImpl.foldArguments(IMPL_TOKEN, target, newType, combiner);
}
+ // /**
+ // * PROVISIONAL API, WORK IN PROGRESS:
+ // * Adapt a target method handle {@code target} by pre-processing
+ // * some of its arguments to derive a new target method handle.
+ // * Call the new target on the original arguments.
+ // * @param combined method handle to call initially on the incoming arguments
+ // * @return method handle which incorporates the specified dispatching logic
+ // * @throws IllegalArgumentException if the first argument type of
+ // * {@code combiner}'s return type is not {@link MethodHandle},
+ // * or if the next argument types of {@code target}
+ // * are not identical with the argument types of {@code combiner}
+ // */
+ // public static
+ // MethodHandle dispatchArguments(MethodType targetType, MethodHandle dispatcher) {
+ // MethodType dispatcherType = dispatcher.type();
+ // int foldArgs = dispatcherType.parameterCount();
+ // boolean ok = (targetType.parameterCount() >= foldArgs);
+ // if (!ok)
+ // throw misMatchedTypes("target and dispatcher types", targetType, dispatcherType);
+ // MethodHandle target = exactInvoker(targetType);
+ // return MethodHandleImpl.foldArguments(IMPL_TOKEN, target, targetType, dispatcher);
+ // }
+
/**
* PROVISIONAL API, WORK IN PROGRESS:
* Make a method handle which adapts a target method handle,
@@ -1691,4 +1736,9 @@ public class MethodHandles {
}
return null;
}
+
+ /*non-public*/
+ static MethodHandle withTypeHandler(MethodHandle target, MethodHandle typeHandler) {
+ return MethodHandleImpl.withTypeHandler(IMPL_TOKEN, target, typeHandler);
+ }
}
diff --git a/src/share/classes/sun/dyn/AdapterMethodHandle.java b/src/share/classes/sun/dyn/AdapterMethodHandle.java
index fb9b7fa26c26b3e6a056c24df4b22f58e97b4edb..65bbd372b9c0d9bc08dab3fb778449e874c218ad 100644
--- a/src/share/classes/sun/dyn/AdapterMethodHandle.java
+++ b/src/share/classes/sun/dyn/AdapterMethodHandle.java
@@ -478,6 +478,39 @@ public class AdapterMethodHandle extends BoundMethodHandle {
return new AdapterMethodHandle(target, newType, makeConv(raw ? OP_RETYPE_RAW : OP_RETYPE_ONLY));
}
+ static MethodHandle makeTypeHandler(Access token,
+ MethodHandle target, MethodHandle typeHandler) {
+ Access.check(token);
+ return new WithTypeHandler(target, typeHandler);
+ }
+
+ static class WithTypeHandler extends AdapterMethodHandle {
+ final MethodHandle target, typeHandler;
+ WithTypeHandler(MethodHandle target, MethodHandle typeHandler) {
+ super(target, target.type(), OP_RETYPE_ONLY);
+ this.target = target;
+ this.typeHandler = typeHandler.asType(TYPE_HANDLER_TYPE);
+ }
+
+ public MethodHandle asType(MethodType newType) {
+ if (this.type() == newType)
+ return this;
+ try {
+ MethodHandle retyped = (MethodHandle) typeHandler.invokeExact(target, newType);
+ // Contract: Must return the desired type, or throw WMT
+ if (retyped.type() != newType)
+ throw new WrongMethodTypeException(retyped.toString());
+ return retyped;
+ } catch (Throwable ex) {
+ if (ex instanceof Error) throw (Error)ex;
+ if (ex instanceof RuntimeException) throw (RuntimeException)ex;
+ throw new RuntimeException(ex);
+ }
+ }
+ private static final MethodType TYPE_HANDLER_TYPE
+ = MethodType.methodType(MethodHandle.class, MethodHandle.class, MethodType.class);
+ }
+
/** Can a checkcast adapter validly convert the target to newType?
* The JVM supports all kind of reference casts, even silly ones.
*/
diff --git a/src/share/classes/sun/dyn/BoundMethodHandle.java b/src/share/classes/sun/dyn/BoundMethodHandle.java
index f2ae32efbc95b7f845bb90e2acc55314cf6ff118..4f25262f46ff0dd9362819443f2412204c680a9e 100644
--- a/src/share/classes/sun/dyn/BoundMethodHandle.java
+++ b/src/share/classes/sun/dyn/BoundMethodHandle.java
@@ -103,7 +103,7 @@ public class BoundMethodHandle extends MethodHandle {
super(Access.TOKEN, type);
this.argument = argument;
this.vmargslot = vmargslot;
- assert(this.getClass() == AdapterMethodHandle.class);
+ assert(this instanceof AdapterMethodHandle);
}
/** Initialize the current object as a Java method handle, binding it
diff --git a/src/share/classes/sun/dyn/InvokeGeneric.java b/src/share/classes/sun/dyn/InvokeGeneric.java
new file mode 100644
index 0000000000000000000000000000000000000000..8137bb4a67aa83157903c18d4cc8bb7bd84c3654
--- /dev/null
+++ b/src/share/classes/sun/dyn/InvokeGeneric.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
+ * 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
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+package sun.dyn;
+
+import java.dyn.*;
+import java.lang.reflect.*;
+import sun.dyn.util.*;
+
+/**
+ * Adapters which manage MethodHanndle.invokeGeneric calls.
+ * The JVM calls one of these when the exact type match fails.
+ * @author jrose
+ */
+class InvokeGeneric {
+ // erased type for the call, which originates from an invokeGeneric site
+ private final MethodType erasedCallerType;
+ // an invoker of type (MT, MH; A...) -> R
+ private final MethodHandle initialInvoker;
+
+ /** Compute and cache information for this adapter, so that it can
+ * call out to targets of the erasure-family of the given erased type.
+ */
+ private InvokeGeneric(MethodType erasedCallerType) throws NoAccessException {
+ this.erasedCallerType = erasedCallerType;
+ this.initialInvoker = makeInitialInvoker();
+ assert initialInvoker.type().equals(erasedCallerType
+ .insertParameterTypes(0, MethodType.class, MethodHandle.class))
+ : initialInvoker.type();
+ }
+
+ private static MethodHandles.Lookup lookup() {
+ return MethodHandleImpl.IMPL_LOOKUP;
+ }
+
+ /** Return the adapter information for this type's erasure. */
+ static MethodHandle genericInvokerOf(MethodType type) {
+ MethodTypeImpl form = MethodTypeImpl.of(type);
+ MethodHandle genericInvoker = form.genericInvoker;
+ if (genericInvoker == null) {
+ try {
+ InvokeGeneric gen = new InvokeGeneric(form.erasedType());
+ form.genericInvoker = genericInvoker = gen.initialInvoker;
+ } catch (NoAccessException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ return genericInvoker;
+ }
+
+ private MethodHandle makeInitialInvoker() throws NoAccessException {
+ // postDispatch = #(MH'; MT, MH; A...){MH'(MT, MH; A)}
+ MethodHandle postDispatch = makePostDispatchInvoker();
+ MethodHandle invoker;
+ if (returnConversionPossible()) {
+ invoker = MethodHandles.foldArguments(postDispatch,
+ dispatcher("dispatchWithConversion"));
+ } else {
+ invoker = MethodHandles.foldArguments(postDispatch, dispatcher("dispatch"));
+ }
+ return invoker;
+ }
+
+ private static final Class>[] EXTRA_ARGS = { MethodType.class, MethodHandle.class };
+ private MethodHandle makePostDispatchInvoker() {
+ // Take (MH'; MT, MH; A...) and run MH'(MT, MH; A...).
+ MethodType invokerType = erasedCallerType.insertParameterTypes(0, EXTRA_ARGS);
+ return MethodHandles.exactInvoker(invokerType);
+ }
+ private MethodHandle dropDispatchArguments(MethodHandle targetInvoker) {
+ assert(targetInvoker.type().parameterType(0) == MethodHandle.class);
+ return MethodHandles.dropArguments(targetInvoker, 1, EXTRA_ARGS);
+ }
+
+ private MethodHandle dispatcher(String dispatchName) throws NoAccessException {
+ return lookup().bind(this, dispatchName,
+ MethodType.methodType(MethodHandle.class,
+ MethodType.class, MethodHandle.class));
+ }
+
+ static final boolean USE_AS_TYPE_PATH = true;
+
+ /** Return a method handle to invoke on the callerType, target, and remaining arguments.
+ * The method handle must finish the call.
+ * This is the first look at the caller type and target.
+ */
+ private MethodHandle dispatch(MethodType callerType, MethodHandle target) {
+ MethodType targetType = target.type();
+ if (USE_AS_TYPE_PATH || target instanceof AdapterMethodHandle.WithTypeHandler) {
+ MethodHandle newTarget = target.asType(callerType);
+ targetType = callerType;
+ Invokers invokers = MethodTypeImpl.invokers(Access.TOKEN, targetType);
+ MethodHandle invoker = invokers.erasedInvokerWithDrops;
+ if (invoker == null) {
+ invokers.erasedInvokerWithDrops = invoker =
+ dropDispatchArguments(invokers.erasedInvoker());
+ }
+ return invoker.bindTo(newTarget);
+ }
+ throw new RuntimeException("NYI");
+ }
+
+ private MethodHandle dispatchWithConversion(MethodType callerType, MethodHandle target) {
+ MethodHandle finisher = dispatch(callerType, target);
+ if (returnConversionNeeded(callerType, target))
+ finisher = addReturnConversion(finisher, callerType.returnType()); //FIXME: slow
+ return finisher;
+ }
+
+ private boolean returnConversionPossible() {
+ Class> needType = erasedCallerType.returnType();
+ return !needType.isPrimitive();
+ }
+ private boolean returnConversionNeeded(MethodType callerType, MethodHandle target) {
+ Class> needType = callerType.returnType();
+ if (needType == erasedCallerType.returnType())
+ return false; // no conversions possible, since must be primitive or Object
+ Class> haveType = target.type().returnType();
+ if (VerifyType.isNullConversion(haveType, needType))
+ return false;
+ return true;
+ }
+ private MethodHandle addReturnConversion(MethodHandle target, Class> type) {
+ if (true) throw new RuntimeException("NYI");
+ // FIXME: This is slow because it creates a closure node on every call that requires a return cast.
+ MethodType targetType = target.type();
+ MethodHandle caster = ValueConversions.identity(type);
+ caster = caster.asType(MethodType.methodType(type, targetType.returnType()));
+ // Drop irrelevant arguments, because we only care about the return value:
+ caster = MethodHandles.dropArguments(caster, 1, targetType.parameterList());
+ MethodHandle result = MethodHandles.foldArguments(caster, target);
+ return result.asType(target.type());
+ }
+
+ public String toString() {
+ return "InvokeGeneric"+erasedCallerType;
+ }
+}
diff --git a/src/share/classes/sun/dyn/Invokers.java b/src/share/classes/sun/dyn/Invokers.java
index b3d2823d02423de7fc9767f62ce41125dc91c165..2f2c92a779f1ee7b03b81660ce4fce7ea0aa1b76 100644
--- a/src/share/classes/sun/dyn/Invokers.java
+++ b/src/share/classes/sun/dyn/Invokers.java
@@ -38,6 +38,10 @@ public class Invokers {
// exact invoker for the outgoing call
private /*lazy*/ MethodHandle exactInvoker;
+ // erased (partially untyped but with primitives) invoker for the outgoing call
+ private /*lazy*/ MethodHandle erasedInvoker;
+ /*lazy*/ MethodHandle erasedInvokerWithDrops; // for InvokeGeneric
+
// generic (untyped) invoker for the outgoing call
private /*lazy*/ MethodHandle genericInvoker;
@@ -80,6 +84,19 @@ public class Invokers {
return invoker;
}
+ public MethodHandle erasedInvoker() {
+ 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;
+ }
+
public MethodHandle varargsInvoker(int objectArgCount) {
MethodHandle vaInvoker = varargsInvokers[objectArgCount];
if (vaInvoker != null) return vaInvoker;
diff --git a/src/share/classes/sun/dyn/MethodHandleImpl.java b/src/share/classes/sun/dyn/MethodHandleImpl.java
index caa96b90d69642fc99cc8561d06c1f876f7c5d3c..49a625367a4104ac105c021b84a95785b17f9f7c 100644
--- a/src/share/classes/sun/dyn/MethodHandleImpl.java
+++ b/src/share/classes/sun/dyn/MethodHandleImpl.java
@@ -560,7 +560,9 @@ public abstract class MethodHandleImpl {
MethodHandle bindReceiver(Access token,
MethodHandle target, Object receiver) {
Access.check(token);
- if (target instanceof AdapterMethodHandle) {
+ if (target instanceof AdapterMethodHandle &&
+ ((AdapterMethodHandle)target).conversionOp() == MethodHandleNatives.Constants.OP_RETYPE_ONLY
+ ) {
Object info = MethodHandleNatives.getTargetInfo(target);
if (info instanceof DirectMethodHandle) {
DirectMethodHandle dmh = (DirectMethodHandle) info;
@@ -1257,4 +1259,9 @@ public abstract class MethodHandleImpl {
Access.check(token);
return MethodHandleNatives.getBootstrap(callerClass);
}
+
+ public static MethodHandle withTypeHandler(Access token, MethodHandle target, MethodHandle typeHandler) {
+ Access.check(token);
+ return AdapterMethodHandle.makeTypeHandler(token, target, typeHandler);
+ }
}
diff --git a/src/share/classes/sun/dyn/MethodHandleNatives.java b/src/share/classes/sun/dyn/MethodHandleNatives.java
index 47a9a2d3de124e4dbd3eeedce90f80f32c850111..3d0629dd034455cfe662dbbcda686fe0903835cb 100644
--- a/src/share/classes/sun/dyn/MethodHandleNatives.java
+++ b/src/share/classes/sun/dyn/MethodHandleNatives.java
@@ -316,6 +316,20 @@ class MethodHandleNatives {
return MethodTypeImpl.makeImpl(Access.TOKEN, rtype, ptypes, true);
}
+ /**
+ * The JVM wants to use a MethodType with invokeGeneric. Give the runtime fair warning.
+ */
+ static void notifyGenericMethodType(MethodType type) {
+ try {
+ // Trigger adapter creation.
+ InvokeGeneric.genericInvokerOf(type);
+ } catch (Exception ex) {
+ Error err = new InternalError("Exception while resolving invokeGeneric");
+ err.initCause(ex);
+ throw err;
+ }
+ }
+
/**
* The JVM is resolving a CONSTANT_MethodHandle CP entry. And it wants our help.
* It will make an up-call to this method. (Do not change the name or signature.)
diff --git a/src/share/classes/sun/dyn/MethodTypeImpl.java b/src/share/classes/sun/dyn/MethodTypeImpl.java
index 0a318148a7315835ae18b7ec27a44fc46d4d1768..059f26975426461977f5aab91cbda7d34842c310 100644
--- a/src/share/classes/sun/dyn/MethodTypeImpl.java
+++ b/src/share/classes/sun/dyn/MethodTypeImpl.java
@@ -48,6 +48,7 @@ public class MethodTypeImpl {
final long primCounts; // packed prim & double counts
final int vmslots; // total number of parameter slots
final MethodType erasedType; // the canonical erasure
+
/*lazy*/ MethodType primsAsBoxes; // replace prims by wrappers
/*lazy*/ MethodType primArgsAsBoxes; // wrap args only; make raw return
/*lazy*/ MethodType primsAsInts; // replace prims by int/long
@@ -59,6 +60,7 @@ public class MethodTypeImpl {
/*lazy*/ FromGeneric fromGeneric; // convert cs. w/o prims to with
/*lazy*/ SpreadGeneric[] spreadGeneric; // expand one argument to many
/*lazy*/ FilterGeneric filterGeneric; // convert argument(s) on the fly
+ /*lazy*/ MethodHandle genericInvoker; // hook for invokeGeneric
public MethodType erasedType() {
return erasedType;
diff --git a/src/share/classes/sun/dyn/util/ValueConversions.java b/src/share/classes/sun/dyn/util/ValueConversions.java
index f5ee5cb81fdd4892bb1bbc3b4f38530c12617f0e..aeacb9e0f0fc724d56b9d39689f30f4cc70f028c 100644
--- a/src/share/classes/sun/dyn/util/ValueConversions.java
+++ b/src/share/classes/sun/dyn/util/ValueConversions.java
@@ -377,7 +377,7 @@ public class ValueConversions {
REBOX_CONVERSIONS = newWrapperCaches(2);
/**
- * Becase we normalize primitive types to reduce the number of signatures,
+ * Because we normalize primitive types to reduce the number of signatures,
* primitives are sometimes manipulated under an "erased" type,
* either int (for types other than long/double) or long (for all types).
* When the erased primitive value is then boxed into an Integer or Long,
@@ -475,10 +475,10 @@ public class ValueConversions {
}
private static final EnumMap[]
- ZERO_CONSTANT_FUNCTIONS = newWrapperCaches(1);
+ CONSTANT_FUNCTIONS = newWrapperCaches(2);
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
- EnumMap cache = ZERO_CONSTANT_FUNCTIONS[0];
+ EnumMap cache = CONSTANT_FUNCTIONS[0];
MethodHandle mh = cache.get(wrap);
if (mh != null) {
return mh;
@@ -543,6 +543,24 @@ public class ValueConversions {
return x;
}
+ /**
+ * Identity function on ints.
+ * @param x an arbitrary int value
+ * @return the same value x
+ */
+ static int identity(int x) {
+ return x;
+ }
+
+ /**
+ * Identity function on longs.
+ * @param x an arbitrary long value
+ * @return the same value x
+ */
+ static long identity(long x) {
+ return x;
+ }
+
/**
* Identity function, with reference cast.
* @param t an arbitrary reference type
@@ -553,7 +571,7 @@ public class ValueConversions {
return t.cast(x);
}
- private static final MethodHandle IDENTITY, CAST_REFERENCE, ALWAYS_NULL, ALWAYS_ZERO, ZERO_OBJECT, IGNORE, EMPTY;
+ private static final MethodHandle IDENTITY, IDENTITY_I, IDENTITY_J, CAST_REFERENCE, ALWAYS_NULL, ALWAYS_ZERO, ZERO_OBJECT, IGNORE, EMPTY;
static {
try {
MethodType idType = MethodType.genericMethodType(1);
@@ -562,6 +580,8 @@ public class ValueConversions {
MethodType ignoreType = idType.changeReturnType(void.class);
MethodType zeroObjectType = MethodType.genericMethodType(0);
IDENTITY = IMPL_LOOKUP.findStatic(ValueConversions.class, "identity", idType);
+ IDENTITY_I = IMPL_LOOKUP.findStatic(ValueConversions.class, "identity", MethodType.methodType(int.class, int.class));
+ IDENTITY_J = IMPL_LOOKUP.findStatic(ValueConversions.class, "identity", MethodType.methodType(long.class, long.class));
//CAST_REFERENCE = IMPL_LOOKUP.findVirtual(Class.class, "cast", idType);
CAST_REFERENCE = IMPL_LOOKUP.findStatic(ValueConversions.class, "castReference", castType);
ALWAYS_NULL = IMPL_LOOKUP.findStatic(ValueConversions.class, "alwaysNull", idType);
@@ -613,6 +633,49 @@ public class ValueConversions {
return IDENTITY;
}
+ public static MethodHandle identity(Class> type) {
+ if (type == Object.class)
+ return IDENTITY;
+ else if (!type.isPrimitive())
+ return retype(MethodType.methodType(type, type), IDENTITY);
+ else
+ return identity(Wrapper.forPrimitiveType(type));
+ }
+
+ static MethodHandle identity(Wrapper wrap) {
+ EnumMap cache = CONSTANT_FUNCTIONS[1];
+ MethodHandle mh = cache.get(wrap);
+ if (mh != null) {
+ return mh;
+ }
+ // slow path
+ MethodType type = MethodType.methodType(wrap.primitiveType(), wrap.primitiveType());
+ try {
+ mh = IMPL_LOOKUP.findStatic(ValueConversions.class, "identity", type);
+ } catch (NoAccessException ex) {
+ mh = null;
+ }
+ if (mh == null && wrap == Wrapper.VOID) {
+ mh = EMPTY; // #(){} : #()void
+ }
+ if (mh != null) {
+ cache.put(wrap, mh);
+ return mh;
+ }
+
+ // use a raw conversion
+ if (wrap.isSingleWord() && wrap != Wrapper.INT) {
+ mh = retype(type, identity(Wrapper.INT));
+ } else if (wrap.isDoubleWord() && wrap != Wrapper.LONG) {
+ mh = retype(type, identity(Wrapper.LONG));
+ }
+ if (mh != null) {
+ cache.put(wrap, mh);
+ return mh;
+ }
+ throw new IllegalArgumentException("cannot find identity for " + wrap);
+ }
+
private static MethodHandle retype(MethodType type, MethodHandle mh) {
return AdapterMethodHandle.makeRetypeOnly(IMPL_TOKEN, type, mh);
}
diff --git a/test/java/dyn/InvokeGenericTest.java b/test/java/dyn/InvokeGenericTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..83aa10b020237bf5494cf6380bdd49fb8aa1e277
--- /dev/null
+++ b/test/java/dyn/InvokeGenericTest.java
@@ -0,0 +1,484 @@
+/*
+ * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
+ * 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
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+/* @test
+ * @summary unit tests for java.dyn.MethodHandle.invokeGeneric
+ * @compile -target 7 InvokeGenericTest.java
+ * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles test.java.dyn.InvokeGenericTest
+ */
+
+package test.java.dyn;
+
+import java.dyn.*;
+import static java.dyn.MethodHandles.*;
+import static java.dyn.MethodType.*;
+import java.lang.reflect.*;
+import java.util.*;
+import org.junit.*;
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+
+/**
+ *
+ * @author jrose
+ */
+public class InvokeGenericTest {
+ // How much output?
+ static int verbosity = 0;
+ static {
+ String vstr = System.getProperty("test.java.dyn.InvokeGenericTest.verbosity");
+ if (vstr != null) verbosity = Integer.parseInt(vstr);
+ }
+
+ @Test
+ public void testFirst() throws Throwable {
+ verbosity += 9; try {
+ // left blank for debugging
+ } finally { printCounts(); verbosity -= 9; }
+ }
+
+ public InvokeGenericTest() {
+ }
+
+ @Before
+ public void checkImplementedPlatform() {
+ boolean platformOK = false;
+ Properties properties = System.getProperties();
+ String vers = properties.getProperty("java.vm.version");
+ String name = properties.getProperty("java.vm.name");
+ String arch = properties.getProperty("os.arch");
+ if ((arch.equals("amd64") || arch.equals("i386") || arch.equals("x86") ||
+ arch.equals("sparc") || arch.equals("sparcv9")) &&
+ (name.contains("Client") || name.contains("Server"))
+ ) {
+ platformOK = true;
+ } else {
+ System.err.println("Skipping tests for unsupported platform: "+Arrays.asList(vers, name, arch));
+ }
+ assumeTrue(platformOK);
+ }
+
+ String testName;
+ static int allPosTests, allNegTests;
+ int posTests, negTests;
+ @After
+ public void printCounts() {
+ if (verbosity >= 2 && (posTests | negTests) != 0) {
+ System.out.println();
+ if (posTests != 0) System.out.println("=== "+testName+": "+posTests+" positive test cases run");
+ if (negTests != 0) System.out.println("=== "+testName+": "+negTests+" negative test cases run");
+ allPosTests += posTests;
+ allNegTests += negTests;
+ posTests = negTests = 0;
+ }
+ }
+ void countTest(boolean positive) {
+ if (positive) ++posTests;
+ else ++negTests;
+ }
+ void countTest() { countTest(true); }
+ void startTest(String name) {
+ if (testName != null) printCounts();
+ if (verbosity >= 1)
+ System.out.println(name);
+ posTests = negTests = 0;
+ testName = name;
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ calledLog.clear();
+ calledLog.add(null);
+ nextArgVal = INITIAL_ARG_VAL;
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ int posTests = allPosTests, negTests = allNegTests;
+ if (verbosity >= 2 && (posTests | negTests) != 0) {
+ System.out.println();
+ if (posTests != 0) System.out.println("=== "+posTests+" total positive test cases");
+ if (negTests != 0) System.out.println("=== "+negTests+" total negative test cases");
+ }
+ }
+
+ static List