import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; class Main { void fooInt() throws Throwable { MethodHandle handle = MethodHandles.lookup().findSetter(Test.class, "n", int.class); Test instance = new Test(); handle.invokeWithArguments(instance, 1); handle.invoke(instance, 2); handle.invokeExact(instance, 3); Object object = 123; handle.invokeWithArguments(instance, object); handle.invoke(instance, object); handle.invokeExact(instance, object); Object objectResult = handle.invokeWithArguments(instance, 1); objectResult = handle.invoke(instance, 2); objectResult = handle.invokeExact(instance, 3); Object objectResult1 = handle.invokeExact(instance, 3); handle.invokeWithArguments(new Object[]{instance, 1}); handle.invoke(new Object[]{instance, 2}); handle.invokeExact(new Object[]{instance, 3}); handle.invokeWithArguments(instance, Integer.valueOf(1)); handle.invoke(instance, Integer.valueOf(2)); handle.invokeExact(instance, Integer.valueOf(3)); handle.invokeWithArguments(instance, "a"); handle.invoke(instance, "b"); handle.invokeExact(instance, "c"); String incompatibleResult1 = (String) handle.invokeWithArguments(instance, 1); String incompatibleResult2 = (String) handle.invoke(instance, 2); String incompatibleResult3 = (String) handle.invokeExact(instance, 3); handle.invokeWithArguments( null, 1); handle.invoke(null, 2); handle.invokeExact(null, 3); handle.invokeWithArguments(instance, null); handle.invoke(instance, null); handle.invokeExact(instance, null); handle.invokeWithArguments(42, 1); handle.invoke(42, 2); handle.invokeExact(42, 3); handle.invokeWithArguments(1); handle.invoke(2); handle.invokeExact(3); int tooManyArguments1 = (int) handle.invokeWithArguments(instance, 1, "a"); int tooManyArguments2 = (int) handle.invoke(instance, 2, "b"); int tooManyArguments3 = (int) handle.invokeExact(instance, 3, "c"); } void fooStr() throws Throwable { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final MethodHandle handle = lookup.findSetter(Test.class, "s", String.class); final Test instance = new Test(); handle.invokeWithArguments(instance, "a"); handle.invoke(instance, "b"); handle.invokeExact(instance, "c"); Object object = "abc"; handle.invokeWithArguments(instance, object); handle.invoke(instance, object); handle.invokeExact(instance, object); Object objectResult = handle.invokeWithArguments(instance, "a"); objectResult = handle.invoke(instance, "b"); objectResult = handle.invokeExact(instance, "c"); Object objectResult1 = handle.invokeExact(instance, "c"); handle.invokeWithArguments(new Object[]{instance, "a"}); handle.invoke(new Object[]{instance, "b"}); handle.invokeExact(new Object[]{instance, "c"}); handle.invokeWithArguments(instance, 1); handle.invoke(instance, 2); handle.invokeExact(instance, 3); int incompatibleResult1 = (int) handle.invokeWithArguments(instance, "a"); int incompatibleResult2 = (int) handle.invoke(instance, "b"); int incompatibleResult3 = (int) handle.invokeExact(instance, "c"); handle.invokeWithArguments(null, "a"); handle.invoke(null, "b"); handle.invokeExact(null, "c"); handle.invokeWithArguments(instance, null); handle.invoke(instance, null); handle.invokeExact(instance, null); handle.invokeExact(instance, (String)null); handle.invokeWithArguments("x", "a"); handle.invoke("x", "b"); handle.invokeExact("x", "c"); handle.invokeWithArguments(instance); handle.invoke(instance); handle.invokeExact(instance); handle.invokeWithArguments(instance, "a", 1); handle.invoke(instance, "b", 2); handle.invokeExact(instance, "c", 3); handle.invokeWithArguments(instance, charSequence()); handle.invoke(instance, charSequence()); handle.invokeExact(instance, charSequence()); } private static CharSequence charSequence() { return "abc"; } } class Super {} class Test extends Super { public int n; public String s; }