简体   繁体   English

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

[英]Does node-java allow anonymous functions to be passed to java as parameters?

I want to be able to pass an anonymous function from Javascript to Java using node-java ( https://github.com/joeferner/node-java ). 我希望能够使用node-java( https://github.com/joeferner/node-java )将匿名函数从Javascript传递给Java。 Here's a sample of the Java code: 这是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");
        }
    }
}

Here's how I want to call it from Javascript: 这是我想从Javascript调用的方式:

Example1: 例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);

This throws an error: 这将引发错误:

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)

I have also tried passing in a Javascript module as parameter, and haven't been able to get that to work either. 我也尝试过将Javascript模块作为参数传递,但也无法使其正常工作。

Example 2: 范例2:

In the file "theModule.js": 在文件“ theModule.js”中:

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

Here's the calling Javascript code: 这是调用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);

This second example also fails with an error: 第二个示例也失败并显示错误:

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)

Thoughts? 思考?

Although a little late, I'll add my answer: 虽然有点晚,但我将添加我的答案:

Did you try using a Proxy for the Callback interface? 您是否尝试将代理用于Callback接口? I didn't run this code myself, but it should look something like this: 我没有亲自运行此代码,但它看起来应该像这样:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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