【问题标题】:Does node-java allow anonymous functions to be passed to java as parameters?node-java 是否允许将匿名函数作为参数传递给 java?
【发布时间】:2017-05-08 23:18:10
【问题描述】:

我希望能够使用 node-java (https://github.com/joeferner/node-java) 将匿名函数从 Javascript 传递到 Java。以下是 Java 代码示例:

public class Example {
    public Example() {
    }

    public interface Callback {
        public void f();
    }

    public void method1(boolean flag, Callback c) {
        System.out.println("flag value: " + flag);
        if (flag) {
            System.out.println("About to call callback");
            c.f();
            System.out.println("Called callback");
        }
        else {
            System.out.println("Didn't call callback");
        }
    }
}

这是我想从 Javascript 中调用它的方式:

示例 1:

var java = require('java');
java.classpath.push('test8');

var Example = java.import('Example');

var anonF = () => {
    console.log("Hello from callback!");
}

var e = new Example();
e.method1Sync(true, anonF);

这会引发错误:

flag value: true
About to call callback
/Users/chris.prince/Desktop/Tests/test.js:11
e.method1Sync(true, anonF);
  ^

Error: Error running instance method
java.lang.NullPointerException
    at Example.method1(Example.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

    at Error (native)
    at Object.<anonymous> (/Users/chris.prince/Desktop/Tests/test.js:11:3)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)

我也尝试过传入一个 Javascript 模块作为参数,但也无法让它工作。

示例 2:

在文件“theModule.js”中:

exports.f = function() {
    console.log("Hello From The Module!");
};

这里是调用 Javascript 代码:

var theModule = require('./theModule');
var java = require('java');
java.classpath.push('test8');

var Example = java.import('Example');

var e = new Example();
e.method1Sync(true, theModule);

第二个示例也因错误而失败:

flag value: true
About to call callback
/Users/chris.prince/Desktop/Tests/test.js:8
e.method1Sync(true, theModule);
  ^

Error: Error running instance method
java.lang.NullPointerException
    at Example.method1(Example.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

    at Error (native)
    at Object.<anonymous> (/Users/chris.prince/Desktop/Tests/test.js:8:3)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)

想法?

【问题讨论】:

  • 你是说在第一个例子中,即使你传递了false,也会调用回调?你正在传递true,所以它当然会调用回调。顺便说一句,您可以使用 java.lang.Runnable 而不是 Callback 并且您不需要在 Javascript 箭头函数的主体周围使用大括号。
  • 我的错。这是该示例的早期版本,即使我通过了false,也会调用回调。具体来说,当我调用e.method1(true, anonF);e.method1(false, anonF); 时,anonF 会被调用。我已经编辑了我的第一个示例。

标签: javascript java node.js node-java


【解决方案1】:

虽然有点晚,但我会添加我的答案:

您是否尝试将Proxy 用于Callback 接口? 我自己没有运行这段代码,但它应该看起来像这样:

var java = require('java');
java.classpath.push('test8');

var Example = java.import('Example');
var myCallbackProxy = java.newProxy('Callback', {
  f: function () {
    console.log("Hello from callback!");
  }
});

var e = new Example();
e.method1Sync(true, myCallbackProxy);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 2019-11-23
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多