import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; class Main { void fooInt() throws Throwable { MethodHandle handle = MethodHandles.lookup().findStatic(Test.class, "foo", MethodType.methodType(int.class, int.class)); int exactSignature1 = (int) handle.invokeWithArguments(1); int exactSignature2 = (int) handle.invoke(2); int exactSignature3 = (int) handle.invokeExact(3); Object object = 123; int objectArgument1 = (int) handle.invokeWithArguments(object); int objectArgument2 = (int) handle.invoke(object); int objectArgument3 = (int) handle.invokeExact(object); Object objectResult = handle.invokeWithArguments(1); objectResult = handle.invoke(2); objectResult = handle.invokeExact(3); Object objectResult1 = handle.invokeExact(3); int argumentsArray1 = (int) handle.invokeWithArguments(new Object[]{1}); int argumentsArray2 = (int) handle.invoke(new Object[]{2}); int argumentsArray3 = (int) handle.invokeExact(new Object[]{3}); int boxedArgument1 = (int) handle.invokeWithArguments(Integer.valueOf(1)); int boxedArgument2 = (int) handle.invoke(Integer.valueOf(2)); int boxedArgument3 = (int) handle.invokeExact(Integer.valueOf(3)); Integer boxedResult1 = (Integer) handle.invokeWithArguments(1); Integer boxedResult2 = (Integer) handle.invoke(2); Integer boxedResult3 = (Integer) handle.invokeExact(3); int incompatibleArgument1 = (int) handle.invokeWithArguments("a"); int incompatibleArgument2 = (int) handle.invoke("b"); int incompatibleArgument3 = (int) handle.invokeExact("c"); String incompatibleResult1 = (String) handle.invokeWithArguments(1); String incompatibleResult2 = (String) handle.invoke(2); String incompatibleResult3 = (String) handle.invokeExact(3); Object nullArg = null; int nullArgument1 = (int) handle.invokeWithArguments(nullArg); int nullArgument2 = (int) handle.invoke(null); int nullArgument3 = (int) handle.invokeExact(null); int tooFewArguments1 = (int) handle.invokeWithArguments(); int tooFewArguments2 = (int) handle.invoke(); int tooFewArguments3 = (int) handle.invokeExact(); Test instance = new Test(); int tooManyArguments1 = (int) handle.invokeWithArguments(instance, 1); int tooManyArguments2 = (int) handle.invoke(instance, 2); int tooManyArguments3 = (int) handle.invokeExact(instance, 3); } void fooStr() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findStatic(Test.class, "foo", MethodType.methodType(String.class, String.class)); String exactSignature1 = (String) handle.invokeWithArguments("a"); String exactSignature2 = (String) handle.invoke("b"); String exactSignature3 = (String) handle.invokeExact("c"); Object object = "abc"; String objectArgument1 = (String) handle.invokeWithArguments(object); String objectArgument2 = (String) handle.invoke(object); String objectArgument3 = (String) handle.invokeExact(object); Object objectResult = handle.invokeWithArguments("a"); objectResult = handle.invoke("b"); objectResult = handle.invokeExact("c"); Object objectResult1 = handle.invokeExact("c"); String argumentsArray1 = (String) handle.invokeWithArguments(new Object[]{"a"}); String argumentsArray2 = (String) handle.invoke(new Object[]{"b"}); String argumentsArray3 = (String) handle.invokeExact(new Object[]{"c"}); String incompatibleArgument1 = (String) handle.invokeWithArguments(1); String incompatibleArgument2 = (String) handle.invoke(2); String incompatibleArgument3 = (String) handle.invokeExact(3); int incompatibleResult1 = (int) handle.invokeWithArguments("a"); int incompatibleResult2 = (int) handle.invoke("b"); int incompatibleResult3 = (int) handle.invokeExact("c"); Object nullArg = null; String nullArgument1 = (String) handle.invokeWithArguments(nullArg); String nullArgument2 = (String) handle.invoke(null); String nullArgument3 = (String) handle.invokeExact(null); String tooFewArguments1 = (String) handle.invokeWithArguments(); String tooFewArguments2 = (String) handle.invoke(); String tooFewArguments3 = (String) handle.invokeExact(); Test instance = new Test(); String tooManyArguments1 = (String) handle.invokeWithArguments(instance, "a"); String tooManyArguments2 = (String) handle.invoke(instance, "b"); String tooManyArguments3 = (String) handle.invokeExact(instance, "c"); String superclassArgument1 = (String) handle.invokeWithArguments(charSequence()); String superclassArgument2 = (String) handle.invoke(charSequence()); String superclassArgument3 = (String) handle.invokeExact(charSequence()); CharSequence superclassResult1 = (CharSequence) handle.invokeWithArguments("a"); CharSequence superclassResult2 = (CharSequence) handle.invoke("b"); CharSequence superclassResult3 = (CharSequence) handle.invokeExact("c"); } private static CharSequence charSequence() { return "abc"; } } class Test { public static int foo(int n) {return n;} public static String foo(String s) {return s;} }