import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; class Main { void fooInt() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findStaticGetter(Test.class, "n", int.class); int exactSignature1 = (int) handle.invokeWithArguments(); int exactSignature2 = (int) handle.invoke(); int exactSignature3 = (int) handle.invokeExact(); Object objectResult = handle.invokeWithArguments(); objectResult = handle.invoke(); objectResult = handle.invokeExact(); Object objectResult1 = handle.invokeExact(); int argumentsArray1 = (int) handle.invokeWithArguments(new Object[]{}); int argumentsArray2 = (int) handle.invoke(new Object[]{}); int argumentsArray3 = (int) handle.invokeExact(new Object[]{}); Integer boxedResult1 = (Integer) handle.invokeWithArguments(); Integer boxedResult2 = (Integer) handle.invoke(); Integer boxedResult3 = (Integer) handle.invokeExact(); String incompatibleResult1 = (String) handle.invokeWithArguments(); String incompatibleResult2 = (String) handle.invoke(); String incompatibleResult3 = (String) handle.invokeExact(); int tooManyArguments1 = (int) handle.invokeWithArguments(1, "a"); int tooManyArguments2 = (int) handle.invoke(2, "b"); int tooManyArguments3 = (int) handle.invokeExact(3, "c"); Test instance = new Test(); int instanceArgument1 = (int) handle.invokeWithArguments(instance); int instanceArgument2 = (int) handle.invoke(instance); int instanceArgument3 = (int) handle.invokeExact(instance); } void fooStr() throws Throwable { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final MethodHandle handle = lookup.findStaticGetter(Test.class, "s", String.class); String exactSignature1 = (String) handle.invokeWithArguments(); String exactSignature2 = (String) handle.invoke(); String exactSignature3 = (String) handle.invokeExact(); Object objectResult = handle.invokeWithArguments(); objectResult = handle.invoke(); objectResult = handle.invokeExact(); Object objectResult1 = handle.invokeExact(); String argumentsArray1 = (String) handle.invokeWithArguments(new Object[]{}); String argumentsArray2 = (String) handle.invoke(new Object[]{}); String argumentsArray3 = (String) handle.invokeExact(new Object[]{}); int incompatibleResult1 = (int) handle.invokeWithArguments(); int incompatibleResult2 = (int) handle.invoke(); int incompatibleResult3 = (int) handle.invokeExact(); String tooManyArguments1 = (String) handle.invokeWithArguments("a", 1); String tooManyArguments2 = (String) handle.invoke("b", 2); String tooManyArguments3 = (String) handle.invokeExact("c", 3); final Test instance = new Test(); String instanceArgument1 = (String) handle.invokeWithArguments(instance); String instanceArgument2 = (String) handle.invoke(instance); String instanceArgument3 = (String) handle.invokeExact(instance); CharSequence superclassResult1 = (CharSequence) handle.invokeWithArguments(); CharSequence superclassResult2 = (CharSequence) handle.invoke(); CharSequence superclassResult3 = (CharSequence) handle.invokeExact(); } } class Super {} class Test extends Super { public static int n; public static String s; }