简体   繁体   中英

Exception when calling overloaded static method using JSR223

I'm trying to implement a rule for openHAB ( http://www.openhab.org/ ) in Javascript using JSR223 ( https://github.com/openhab/openhab/wiki/Jsr223-Script-Engine ).

Anyone having a suggestion of the root cause of following exception? Remark that both instances passed as argument implement the interfaces used as parameters in the method declaration.

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]

Following is the implemented script:

'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 ]);
}

The called method is overloaded and has following signatures:

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)

Tested and failing both on jdk1.8.0_31 and jdk1.8.0_65. Bumped into a more or less a similar exception having a rule implemented in Groovy.

I know that I'm a necromantic when answering this question but I stumbled upon it and couldn't resist.

The error message is clear: You try to invoke the Java Method org.openhab.core.persistence.extensions.PersistenceExtensions.changedSince(org.openhab.core.items.Item, org.joda.time.DateTime) with a GroupItem .

Which is interesting since a GroupItem extends the GenericItem which implements the Item Interface and therefore should match the method signature.

With for each(var item in ItemRegistry.getItems()) { you get all items from your OpenHab Item Definition including all groups. You probably only want the real items. Try if (! item.members ) to filter all groups.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM