The javadoc for the java.dyn.SwitchPoint.guardWithTest method states that "The target and fallback must be of exactly the same method type...", but unfortunately does not describe in javadoc any exceptions which can be thrown when the condition doesn't meet.
Please note that the implementation throws IllegalArgumentException in such case:
$ cat guardWithTestIAE.java
import java.dyn.*;
import static java.dyn.MethodType.*;
import static java.dyn.MethodHandles.*;
public class guardWithTestIAE {
public static void main(String[] args) {
new SwitchPoint().guardWithTest(
exactInvoker(methodType(void.class)),
exactInvoker(methodType(int.class))) ;
}
}
$ javac guardWithTestIAE.java
$ java -showversion -verify -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic guardWithTestIAE
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b135)
Java HotSpot(TM) Server VM (build 21.0-b05, mixed mode)
Exception in thread "main" java.lang.IllegalArgumentException: target and fallback types must match: (MethodHandle)void != (MethodHandle)int
at sun.dyn.MemberName.newIllegalArgumentException(MemberName.java:511)
at java.dyn.MethodHandles.misMatchedTypes(MethodHandles.java:2028)
at java.dyn.MethodHandles.guardWithTest(MethodHandles.java:2012)
at java.dyn.SwitchPoint.guardWithTest(SwitchPoint.java:140)
at guardWithTestIAE.main(guardWithTestIAE.java:8)
It seems that the implementation behaviour should be described in the javadoc with something like:
"Throws: IllegalArgumentException - if the target and fallback are not exactly the same method type."