node-java是否允许将匿名函数作为参数传递给java?

时间:2017-05-08 23:18:11

标签: javascript java node.js node-java

我希望能够使用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:

在文件&#34; theModule.js&#34;:

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)

思想?

1 个答案:

答案 0 :(得分:0)

虽然有点晚,但我会补充一下我的答案:

您是否尝试将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);