下面列出了怎么用java.lang.invoke.WrongMethodTypeException的API类实例代码及写法,或者点击链接到github查看源代码。
void invoke(Service targetService, byte[] arguments, ExecutionContext context) {
Object argumentsObject = serializeArguments(arguments);
try {
methodHandle.invoke(targetService, argumentsObject, context);
} catch (WrongMethodTypeException | ClassCastException invocationException) {
// Invocation-specific exceptions are thrown as is — they are not thrown
// from the _transaction method_, but from framework code (see mh#invoke spec).
throw invocationException;
} catch (ExecutionException serviceException) {
// 'Service-defined' transaction exceptions
throw serviceException;
} catch (Throwable unexpectedServiceException) {
// Any other _transaction_ exceptions
throw new UnexpectedExecutionException(unexpectedServiceException);
}
}
private static MethodHandle forSpreadCall(MethodHandle mh, MethodType type) {
int expectedParameters = mh.type().parameterCount();
int actualParameters = type.parameterCount();
if (!mh.isVarargsCollector() || !mh.type().parameterType(expectedParameters - 1).equals(Object[].class)) {
throw new WrongMethodTypeException("Not Object[] var-args collector");
}
if (expectedParameters > actualParameters) {
throw new WrongMethodTypeException("Too few arguments");
}
if (expectedParameters < actualParameters) {
int fixedCount = actualParameters - expectedParameters;
int firstFixed = expectedParameters - 1;
List<Class<?>> fixed = type.parameterList().subList(firstFixed, firstFixed + fixedCount);
mh = MethodHandles.collectArguments(mh, firstFixed, combineArraysMH);
mh = MethodHandles.collectArguments(mh, firstFixed, toObjectArray(fixed));
}
return mh.asType(type);
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
protected int resolveInstanceLeaseDurationRewritten(final InstanceInfo info) {
try {
return (int) handlerResolveInstanceLeaseDurationMethod.invokeWithArguments(this, info);
} catch (ClassCastException | WrongMethodTypeException e) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, e);
} catch (RuntimeException re) {
throw re;
} catch (Throwable t) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, t);
}
}
@Override
public void register(InstanceInfo info, int leaseDuration, boolean isReplication) {
try {
register3ArgsMethodHandle.invokeWithArguments(this, info, leaseDuration, isReplication);
handleRegistrationMethod.invokeWithArguments(this, info, leaseDuration, isReplication);
} catch (ClassCastException | WrongMethodTypeException e) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, e);
} catch (RuntimeException re) {
throw re;
} catch (Throwable t) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, t);
}
}
@Override
public void register(final InstanceInfo info, final boolean isReplication) {
try {
register2ArgsMethodHandle.invokeWithArguments(this, info, isReplication);
handleRegistrationMethod.invokeWithArguments(this, info, resolveInstanceLeaseDurationRewritten(info), isReplication);
} catch (ClassCastException | WrongMethodTypeException e) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, e);
} catch (RuntimeException re) {
throw re;
} catch (Throwable t) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, t);
}
}
@Override
public boolean cancel(String appName, String serverId, boolean isReplication) {
try {
final boolean out = (boolean) cancelMethodHandle.invokeWithArguments(this, appName, serverId, isReplication);
handleCancelationMethod.invokeWithArguments(this, appName, serverId, isReplication);
return out;
} catch (ClassCastException | WrongMethodTypeException e) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, e);
} catch (RuntimeException re) {
throw re;
} catch (Throwable t) {
throw new IllegalArgumentException(EXCEPTION_MESSAGE, t);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
MethodHandle mh_VA_h = mh.asSpreader(0, cls, arity-1);
assert(mh_VA.type().parameterType(0) == cls);
assert(mh_VA_h.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA, mh_VA_h);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
MethodHandle mh_VA2_h = mh_CA.asSpreader(0, cls, arity-1);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2, mh_VA2_h);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
/**
* Invokes the override implementation.
*
* @param args passed arguments
* @return the return value from the override call.
* @throws Throwable any occurred exception thrown by the override implementation or by the Java Method Handler.
*/
Object invoke(Object[] args) throws Throwable {
int idxContextParamRing = baseMethod.getIdxContextParamRing();
int idxContextParamSuper = baseMethod.getIdxContextParamSuper();
Object passedArgs[] = args;
int countExtra = 1;
if (idxContextParamRing >= 0)
countExtra++;
if (idxContextParamSuper >= 0)
countExtra++;
// Add extra arguments like this pointer a.s.o.
passedArgs = new Object[args.length + countExtra];
int i = 0;
int j = 0;
while (i < passedArgs.length) {
if (i == 0) {
passedArgs[i] = object;
} else if (i == idxContextParamRing + 1) {
passedArgs[i] = spellRingWithOverride;
} else if (i == idxContextParamSuper + 1) {
passedArgs[i] = new ModuleOverrideSuper(prev);
} else {
passedArgs[i] = args[j];
j++;
}
i++;
}
try {
return baseMethod.getMethodHandle().invokeWithArguments(passedArgs);
} catch (WrongMethodTypeException | ClassCastException e) {
// NOTE: If this happens, then correctness of checks like "areMethodsCompatible()" a.s.o. need to be debugged.
throw new IllegalStateException("Couldn't invoke call. See cause.", e);
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private static void checkForWrongMethodTypeException(MethodHandle mh, MethodType mt) {
try {
MethodHandles.explicitCastArguments(mh, mt);
throw new AssertionError("Expected WrongMethodTypeException is not thrown");
} catch (WrongMethodTypeException wmte) {
if (VERBOSE) {
System.out.printf("Expected exception %s: %s\n",
wmte.getClass(), wmte.getMessage());
}
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
private void testArities(Class<? extends Object[]> cls,
int minArity,
int maxArity,
int iterations) throws Throwable {
boolean verbose = (cls == Object[].class);
for (int arity = minArity; arity <= maxArity; arity++) {
if (verbose) System.out.println("arity="+arity);
MethodHandle mh = MH_hashArguments(cls, arity);
MethodHandle mh_VA = mh.asSpreader(cls, arity);
assert(mh_VA.type().parameterType(0) == cls);
testArities(cls, arity, iterations, verbose, mh, mh_VA);
// mh_CA will collect arguments of a particular type and pass them to mh_VA
MethodHandle mh_CA = mh_VA.asCollector(cls, arity);
MethodHandle mh_VA2 = mh_CA.asSpreader(cls, arity);
assert(mh_CA.type().equals(mh.type()));
assert(mh_VA2.type().equals(mh_VA.type()));
if (cls != Object[].class) {
try {
mh_VA2.invokeWithArguments(new Object[arity]);
throw new AssertionError("should not reach");
} catch (ClassCastException | WrongMethodTypeException ex) {
}
}
int iterations_VA = iterations / 100;
testArities(cls, arity, iterations_VA, false, mh_CA, mh_VA2);
}
}
/**
* Create a method handle that will apply all transformations specified by the current method builder
* and then call the {@code method} method.
* This method uses a cache if the method is a virtual method (either on class or interface)
*
* @param lookup the lookup object used to find the @code method}
* @param method the method called at the end of the transformation.
* @return a new method handle constructed by applying all transformations on the target method.
* @throws NoSuchMethodException throws is a method is not visible.
* @throws NoSuchFieldException throws if a field is not visible.
* @throws IllegalAccessException throws if a type or a member of a type is not visible.
*/
public MethodHandle unreflect(Lookup lookup, Method method) throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException {
MethodHandle target = lookup.unreflect(method);
MethodType targetType = target.type();
if (!targetType.equals(sig)) {
throw new WrongMethodTypeException("target type " + targetType + " is not equals to current type " + sig);
}
int modifiers = method.getModifiers();
if (!Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers)) { // can be virtual
target = new InliningCacheCallSite(target).dynamicInvoker();
}
return call(target);
}
@Test(expected = WrongMethodTypeException.class)
public void givenSumMethodHandleAndIncompatibleArguments_whenInvokingExact_thenException() throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(int.class, int.class, int.class);
MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt);
sumMH.invokeExact(Integer.valueOf(1), 11);
}