我正在努力思考如何有效地使用 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 调用。任何见解将不胜感激!