0

鉴于此代码:

MethodType mt = MethodType.methodType(void.class, DomainObject.class);
NOOP_METHOD = RULE_METHOD_LOOKUP.findVirtual(RulesEngine.class, "noOpRule", mt);

产生的 NOOP_METHOD 是

MethodHandle(RulesEngine,DomainObject)void 

为什么第一个参数在那里,当我调用它时会导致失败,比如

mh.invoke(domainObject);

因为错误消息是:

 java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(RulesEngine,DomainObject)void to (DomainObject)void

这是有问题的方法:

public void noOpRule(DomainObject d) {
}
4

1 回答 1

2

该方法noOpRuleRulesEngine该类的实例方法。

要在常规代码中调用它,您需要一个RulesEnigne对象和一个DomainObject对象:

public static void callNoOpRule(RulesEngine rulesEngine, DomainObject domainObject) {
    rulesEngine.noOpRule(domainObject);
}

要通过 调用它,MethodHandle您还需要两个对象:

mh.invoke(rulesEngine, domainObject);

或者,如果您尝试从以下实例方法调用RulesEngine

mh.invoke(this, domainObject);
于 2018-06-23T15:20:38.173 回答