import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; class Main { void fooInt() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findGetter(Test.class, "n", int.class); Test instance = new Test(); int exactSignature1 = (int) handle.invokeWithArguments(instance); int exactSignature2 = (int) handle.invoke(instance); int exactSignature3 = (int) handle.invokeExact(instance); Object objectResult = handle.invokeWithArguments(instance); objectResult = handle.invoke(instance); objectResult = handle.invokeExact(instance); Object objectResult1 = handle.invokeExact(instance); int argumentsArray1 = (int) handle.invokeWithArguments(new Object[]{instance}); int argumentsArray2 = (int) handle.invoke(new Object[]{instance}); int argumentsArray3 = (int) handle.invokeExact(new Object[]{instance}); Integer boxedResult1 = (Integer) handle.invokeWithArguments(instance); Integer boxedResult2 = (Integer) handle.invoke(instance); Integer boxedResult3 = (Integer) handle.invokeExact(instance); String incompatibleResult1 = (String) handle.invokeWithArguments(instance); String incompatibleResult2 = (String) handle.invoke(instance); String incompatibleResult3 = (String) handle.invokeExact(instance); Object nullArg = null; int nullReceiver1 = (int) handle.invokeWithArguments(nullArg); int nullReceiver2 = (int) handle.invoke(nullArg); int nullReceiver3 = (int) handle.invokeExact(nullArg); int incompatibleReceiver1 = (int) handle.invokeWithArguments(42); int incompatibleReceiver2 = (int) handle.invoke(42); int incompatibleReceiver3 = (int) handle.invokeExact(42); int tooFewArguments1 = (int) handle.invokeWithArguments(); int tooFewArguments2 = (int) handle.invoke(); int tooFewArguments3 = (int) handle.invokeExact(); 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 { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final MethodHandle handle = lookup.findGetter(Test.class, "s", String.class); final Test instance = new Test(); String exactSignature1 = (String) handle.invokeWithArguments(instance); String exactSignature2 = (String) handle.invoke(instance); String exactSignature3 = (String) handle.invokeExact(instance); Object objectResult = handle.invokeWithArguments(instance); objectResult = handle.invoke(instance); objectResult = handle.invokeExact(instance); Object objectResult1 = handle.invokeExact(instance); String argumentsArray1 = (String) handle.invokeWithArguments(new Object[]{instance}); String argumentsArray2 = (String) handle.invoke(new Object[]{instance}); String argumentsArray3 = (String) handle.invokeExact(new Object[]{instance}); int incompatibleResult1 = (int) handle.invokeWithArguments(instance); int incompatibleResult2 = (int) handle.invoke(instance); int incompatibleResult3 = (int) handle.invokeExact(instance); Object nullArg = null; String nullReceiver1 = (String) handle.invokeWithArguments(nullArg); String nullReceiver2 = (String) handle.invoke(nullArg); String nullReceiver3 = (String) handle.invokeExact(nullArg); String incompatibleReceiver1 = (String) handle.invokeWithArguments("x"); String incompatibleReceiver2 = (String) handle.invoke("x"); String incompatibleReceiver3 = (String) handle.invokeExact("x"); String tooFewArguments1 = (String) handle.invokeWithArguments(); String tooFewArguments2 = (String) handle.invoke(); String tooFewArguments3 = (String) handle.invokeExact(); String tooManyArguments1 = (String) handle.invokeWithArguments(instance, 1); String tooManyArguments2 = (String) handle.invoke(instance, 2); String tooManyArguments3 = (String) handle.invokeExact(instance, 3); CharSequence superclassResult1 = (CharSequence) handle.invokeWithArguments(instance); CharSequence superclassResult2 = (CharSequence) handle.invoke(instance); CharSequence superclassResult3 = (CharSequence) handle.invokeExact(instance); } } class Super {} class Test extends Super { public int n; public String s; }