假设你有MethodHandle
并且已经指定了一些参数,如何在设置之后更改这些参数?
import static java.lang.invoke.MethodType.*;
import static java.lang.invoke.MethodHandles.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
public class SomeTest {
public static void main(String[] args) throws Throwable {
MethodHandle methodHandle = MethodHandles.lookup().findVirtual(SomeTest.class,
"someMethod", methodType(void.class, String.class));
methodHandle = MethodHandles.insertArguments(methodHandle, 1, "Hi");
// this invoke calls with "Hi", which is fine
methodHandle.invoke(new SomeTest());
// here, how to change the arguments to be e.g. "Hello" instead of "Hi"
methodHandle.invoke(new SomeTest());
}
public void someMethod(String a) {
System.out.println("Called with " + a);
}
}
我尝试过使用MethodHandles。filterArguments()
....
methodHandle = MethodHandles.filterArguments(methodHandle, 1,
MethodHandles.lookup().findStatic(SomeTest.class, "returnSomething",
methodType(String.class)));
methodHandle.invoke(new SomeTest());
}
public static String returnSomething() {
return "Hello";
}
但我得到一个例外:
Exception in thread "main" java.lang.IllegalArgumentException: too many filters
at java.lang.invoke.MethodHandleStatics.newIllegalArgumentException(MethodHandleStatics.java:139)
at java.lang.invoke.MethodHandles.filterArgumentsCheckArity(MethodHandles.java:2623)
at java.lang.invoke.MethodHandles.filterArguments(MethodHandles.java:2595)
at test.test.SomeTest.main(SomeTest.java:22)
答案 0 :(得分:0)
2种方法:
重用原始方法句柄并将其绑定到另一个字符串:
MethodHandle methodHandle = MethodHandles.lookup().findVirtual(SomeTest.class,
"someMethod", methodType(void.class, String.class));
MethodHandle hi = methodHandle.insertArguments(1, "Hi");
MethodHandle hello = methodHandle.insertArguments(1, "Hello");
hi.invoke(new SomeTest()); // "Hi"
hello.invoke(new SomeTest()); // "Hello"
将第二个参数绑定到您操作的类成员的getter。您必须使用“exactInvoker”过滤参数,该“exactInvoker”将执行getter以实际获取String值。参见:
public static class StringHolder{
public String toPrint;
StringHolder(String toPrint){
this.toPrint = toPrint;
}
}
public static void main(String[] args) throws Throwable {
MethodHandle toPrintGetter = lookup().findGetter(StringHolder.class, "toPrint", String.class);
MethodHandle someMethod = lookup().findVirtual(SomeTest.class, "someMethod", MethodType.methodType(void.class, String.class));
StringHolder holder = new StringHolder("Hi");
someMethod = MethodHandles.filterArguments(someMethod, 1, MethodHandles.exactInvoker(MethodType.methodType(String.class)));
MethodHandle stringPrinter = MethodHandles.insertArguments(someMethod, 1, toPrintGetter.bindTo(holder));
stringPrinter.invokeExact(new SomeTest()); // prints "Hi"
holder.toPrint = "Hello";
stringPrinter.invokeExact(new SomeTest()); // prints "Hello"
}