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