Rhino:在Javascript实现中访问Java接口变量 [英] Rhino: Access Java interface variables in Javascript implementation

查看:226
本文介绍了Rhino:在Javascript实现中访问Java接口变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rhino:如何在Javascript实现中访问Java接口变量?

Rhino: How to access Java interface variables in Javascript implementation?

我为其他一些方面公开了一个java接口,让它们在javascript中提供相同的实现。

I expose a java interface for some other party to let them provide an implementation for the same in javascript.

public interface APIInterface{

    public static APIUtils util = new APIUtils();

    public ArrayList getAllObjects(Object aTransaction);
}

Javascript实施:

Javascript implementation:

/** Core Interface Method **/
new Object() {

  getAllObjects: function(tran) {
        tran.set(..); //OK  
        tran.set(..); //OK
        util.callSomeFunction(); //Fails here..Rhino doesn't understand util.. 
  }    

}

我希望接口的javascript实现能够理解接口变量 util ,而不必将其作为函数的附加参数传递或添加它到 ScriptEngine 。这在技术上是否可行?

I want the javascript implementation of the interface to understand the interface variable util without having to pass it as an additional argument to the function or by adding it to the ScriptEngine. Is this technically possible?

推荐答案

对于界面......

For the interface...

package foo;
public interface Iface {
  String X = "Hello, World!";
  void invoke();
}

...静态会员¹ X 可以通过两种方式访问​​。

...static member¹ X can be accessed two ways.

1)通过类型:

var x = Packages.foo.Iface.X;

2)通过反思:

var impl = new Packages.foo.Iface({
  invoke : function () {
    var x = this.getClass().getField("X").get(null);
    java.lang.System.out.println(x);
  }
});
impl.invoke();

在Rhino 1.7R4上测试。

Tested on Rhino 1.7R4.

¹接口上的所有变量都隐含 public static final

这篇关于Rhino:在Javascript实现中访问Java接口变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆