[英]Java MethodHandle api seems to produce incorrect type
Given this code: 给出以下代码:
MethodType mt = MethodType.methodType(void.class, DomainObject.class);
NOOP_METHOD = RULE_METHOD_LOOKUP.findVirtual(RulesEngine.class, "noOpRule", mt);
the NOOP_METHOD produced is 产生的NOOP_METHOD是
MethodHandle(RulesEngine,DomainObject)void
Why is that first parameter there, that causes failures when i invoke it, like 为什么第一个参数在那里,当我调用它时会导致失败,例如
mh.invoke(domainObject);
as the error message is: 因为错误消息是:
java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(RulesEngine,DomainObject)void to (DomainObject)void
Here is the method in question: 这是有问题的方法:
public void noOpRule(DomainObject d) {
}
The method noOpRule
is an instance method of the RulesEngine
class. 方法noOpRule
是RulesEngine
类的实例方法。
To call it in regular code, you need a RulesEnigne
object as well as a DomainObject
object: 要以常规代码调用它,您需要一个RulesEnigne
对象以及一个DomainObject
对象:
public static void callNoOpRule(RulesEngine rulesEngine, DomainObject domainObject) {
rulesEngine.noOpRule(domainObject);
}
To call it through the MethodHandle
you need both objects as well: 要通过MethodHandle
调用它,还需要两个对象:
mh.invoke(rulesEngine, domainObject);
or, if you are trying to invoking from an instance method of RulesEngine
: 或者,如果您尝试从RulesEngine
的实例方法调用:
mh.invoke(this, domainObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.