Rhino: Access Java interface variables in Javascript implementation

柔情痞子 提交于 2019-12-01 23:18:42

For the interface...

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

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

1) Via the type:

var x = Packages.foo.Iface.X;

2) Via reflection:

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

Tested on Rhino 1.7R4.

¹All variables are implicitly public static final on interfaces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!