2015-10-29 45 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)來過濾所有組。