【问题标题】:Java MethodHandleJava 方法句柄
【发布时间】:2014-11-21 16:22:00
【问题描述】:
@FXML private void handleLeftButton() throws Throwable{

MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType methodType = MethodType.methodType(void.class, ListIterator.class, Text.class);
MethodHandle leftButtonClickMethod = lookup.findVirtual(HomePresenter.class, "leftButtonClick", methodType);

leftButtonClickMethod.invoke(list, menuTitle);

}

为什么会出现这个错误?

java.lang.invoke.WrongMethodTypeException:无法将 MethodHandle(HomePresenter,ListIterator,Text)void 转换为 (ListIterator,Text)void

【问题讨论】:

  • 可以发HomePresenter的相关代码吗?我很难理解为什么您使用这种反射方法来调用该方法,而不是仅仅以通常的方式调用它。

标签: java javafx methodhandle


【解决方案1】:

查看 MethodHandle 中的示例 - 我认为您需要将要调用该方法的实例作为第一个参数传递

leftButtonClickMethod.invoke(this,list, menuTitle);

但因为我从未使用过 java.lang.invoke,所以这可能是完全错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多