2015-10-29 41 views
1

我试图在Javascript中使用JSR223(https://github.com/openhab/openhab/wiki/Jsr223-Script-Engine)实现openHAB(http://www.openhab.org/)的规则。使用JSR223调用重载静态方法时的异常

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

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中实施的规则或多或少的类似异常。

回答

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)来过滤所有组。