MethodHandle
or the type of a VarHandle
doesn't match the actual method or field.
It also checks that the arguments provided to the MethodHandle.invoke()
, VarHandle.set()
, and so on, match the method signature or field type, otherwise it will result in an exception.
Examples:
MethodHandle mh = MethodHandles.lookup().findVirtual( MyClass.class, "foo", MethodType.methodType(void.class, int.class)); // the argument should be an int value mh.invoke(myObj, "abc");
// the argument should be String.class VarHandle vh = MethodHandles.lookup().findVarHandle( MyClass.class, "text", int.class);
VarHandle vh = MethodHandles.lookup().findVarHandle( MyClass.class, "text", String.class); // the argument should be a String value vh.set(myObj, 42);
New in 2017.2