使用JSR223调用重载静态方法时的异常

时间:2015-10-29 16:38:01

标签: javascript nashorn jsr223 openhab

我正在尝试使用JSR223(http://www.openhab.org/)在Javascript中为openHAB(https://github.com/openhab/openhab/wiki/Jsr223-Script-Engine)实现规则。

有人建议跟踪异常的根本原因吗?注意作为参数传递的两个实例都实现了在方法声明中用作参数的接口。

java.lang.RuntimeException: java.lang.NoSuchMethodException: None of the fixed arity signatures [(org.openhab.core.items.Item, org.joda.time.base.AbstractInstant)] of method org.openhab.core.persistence.extensions.PersistenceExtensions.changedSince match the argument types [org.openhab.core.items.GroupItem, org.joda.time.DateTime]
        at jdk.nashorn.javaadapters.java.util.function.Consumer.accept(Unknown Source) ~[na:na]
        at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_31]
        at jdk.nashorn.internal.scripts.Script$\^eval\_.L:13(<eval>:14) ~[na:na]
        at org.openhab.core.jsr223.internal.shared.Rule$$NashornJavaAdapter.execute(Unknown Source) ~[na:na]
        at org.openhab.core.jsr223.internal.engine.RuleExecutionRunnable.run(RuleExecutionRunnable.java:36) ~[na:na]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_31]

以下是已实施的脚本:

'use strict';

load("nashorn:mozilla_compat.js");
importPackage(org.openhab.core.jsr223.internal.shared);
importPackage(org.joda.time);
importPackage(org.joda.time.base);


var autoOffRule = new org.openhab.core.jsr223.internal.shared.Rule() {
    getEventTrigger: function() {
        return [
            new org.openhab.core.jsr223.internal.shared.TimerTrigger("* * * * * ?")
        ];
    }, 
    execute: function(event) {
        for each(var item in ItemRegistry.getItems()) {
            if (item.getState() == org.openhab.core.library.types.OnOffType.ON) {
                var dateTime = org.joda.time.DateTime.now().withFieldAdded(DurationFieldType.seconds(), -5);

                if (!(org.openhab.core.persistence.extensions.PersistenceExtensions.class.static.changedSince(item, var dateTime))) {
                    print("Auto-off for " + item.getName())
                }
            }            
        }
    }
};

function getRules() {
    return new org.openhab.core.jsr223.internal.shared.RuleSet([ autoOffRule ]);
}

被调用的方法已重载并具有以下签名:

org.openhab.core.persistence.extensions.PersistenceExtensions#changedSince(org.openhab.core.items.Item, org.joda.time.base.AbstractInstant)
org.openhab.core.persistence.extensions.PersistenceExtensions#changedSince(org.openhab.core.items.Item, org.joda.time.base.AbstractInstant, java.lang.String)

在jdk1.8.0_31和jdk1.8.0_65上进行了测试和失败。在Groovy中实现了一个规则或多或少的类似异常。

1 个答案:

答案 0 :(得分:0)

我知道在回答这个问题时我是一个死灵法术,但我偶然发现它无法抗拒。

错误消息很明确:您尝试使用GroupItem调用Java方法org.openhab.core.persistence.extensions.PersistenceExtensions.changedSince(org.openhab.core.items.Item, org.joda.time.DateTime)

Which is interesting since a GroupItem extends the GenericItem which implements the Item Interface因此应与方法签名匹配。

使用for each(var item in ItemRegistry.getItems()) {,您可以从OpenHab项目定义中获取所有项目,包括所有组。你可能只想要真正的物品。请尝试if (! item.members )过滤所有群组。