在调试期间调用MethodHandle

时间:2017-10-16 06:02:52

标签: java intellij-idea reflection methodhandle

我试图让我的大脑围绕如何有效地使用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调用。任何见解将不胜感激!

1 个答案:

答案 0 :(得分:0)

这还没有解决IDEA-154967,抱歉。 解决方法是使用invokeWithArguments。