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