我正在努力思考如何有效地使用 MethodHandles,让我失望的一件事是尝试在调试期间执行 MethodHandles。 这是一些示例代码,说明了我的问题。
public class MetricianTest {
public static void main(String[] args) throws Throwable {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
final MethodType mt = MethodType.methodType(String.class);
final MethodHandle mh = lookup.findVirtual(MHTestClass.class, "testMethod", mt);
System.out.println(mh.invoke(new MHTestClass()));
}
public static class MHTestClass {
public int testField = 1;
public MHTestClass() {
}
public String testMethod() {
return "method-value";
}
}
}
代码在正常运行时可以工作,但停止 IntelliJ 调试器并尝试调用 MethodHandle 会抛出 UnsupportedOperationException。查看 Javadocs,我可以看到 MethodHandles 无法反射调用,但我不确定我是否理解原因,或者如何在程序中调试 MethodHandle 调用。任何见解将不胜感激!
最佳答案
此问题尚未修复 IDEA-154967 , 对不起。 解决方法是使用 invokeWithArguments。
关于java - 调试期间调用 MethodHandle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763824/