sun.org.mozilla Rhino 和扩展 Java 抽象类
而不是任何其他类型的类:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorExcep
tion: JavaAdapter: first arg should be interface Class (<Unknown source>#11) in
<Unknown source> at line number 11
根据以下错误消息,在 sun.org.mozilla 版本的 Rhino 中,JavaAdapter
仅将接口作为其第一个参数, 通过 Rhino 扩展抽象类(或普通类)?
这是有问题的代码:
var j = new JavaAdapter(foo.bar.abstractClass, {
field : "test",
method : function () {
print("on enable");
}
});
In the sun.org.mozilla version of Rhino, JavaAdapter
only takes interfaces as its first argument instead of any other kind of class according to this error message:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorExcep
tion: JavaAdapter: first arg should be interface Class (<Unknown source>#11) in
<Unknown source> at line number 11
Is there any way, no matter how hacky, to extend an abstract class (or a normal class for that matter) via Rhino?
Here is the offending code:
var j = new JavaAdapter(foo.bar.abstractClass, {
field : "test",
method : function () {
print("on enable");
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 Rhino 的 Sun 版本,另一个答案是正确的。从问题的措辞来看,尚不完全清楚切换到原始(Mozilla)Rhino 是否适合您。
具体来说,当 Sun 将 Rhino 添加到 Java 时,“一些组件已被排除,因为出于足迹和安全原因”,其中之一是 Mozilla 的 JavaAdapter。 Sun 编写了自己的“JavaAdapter”,但它比 Mozilla 的小得多、简单得多,而且只能用于实现单个 Java 接口。 Mozilla 最初的 JavaAdapter 没有这样的限制:我一直用它来实现抽象类。
它与一些模糊的哲学差异无关,例如“JavaScript 实际上并不像 Java 那样是‘OO’”。 Sun 认为“使用 JavaAdapter 来扩展 Java 类或实现多个接口的情况非常罕见”(同上),并决定删除此功能。
如果可以接受将 850KB 的 jar 文件与您的代码一起发送,那么就使用 Mozilla Rhino 并实现您想要的所有抽象类!
The other answer is correct for the Sun version of Rhino. It's not entirely clear from the phrasing of the question if switching to the original (Mozilla) Rhino is an option for you or not.
Specifically, when Sun added Rhino to Java, "a few components have been excluded due to footprint and security reasons", and one of them was Mozilla's JavaAdapter. Sun wrote their own "JavaAdapter" but it is much smaller and simpler than the Mozilla one, and it can only be used to implement a single Java interface. Mozilla's original JavaAdapter has no such restriction: I use it to implement abstract classes all the time.
It has nothing to do with some vague philosophical difference like "JavaScript isn't actually 'OO' in the same way as Java". Sun thought that "The uses of JavaAdapter to extend a Java class or to implement multiple interfaces are very rare" (ibid) and decided to remove this feature.
If it's acceptable to ship an 850KB jar file with your code, then grab Mozilla Rhino and implement all the abstract classes you want!