admin管理员组文章数量:1435859
In the sun.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.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");
}
});
In the sun.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.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");
}
});
Share
Improve this question
edited Jan 26, 2011 at 22:01
Alec Gorge
asked Jan 26, 2011 at 2:50
Alec GorgeAlec Gorge
17.4k10 gold badges62 silver badges71 bronze badges
2
- It would help if you posted the code that caused that error and the stacktrace. – Stephen C Commented Jan 26, 2011 at 3:16
- i added the offending code per your suggestion. – Alec Gorge Commented Jan 26, 2011 at 22:02
1 Answer
Reset to default 7The 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 ponents 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!
本文标签: javascriptsunorgmozilla Rhino and extending Java abstract classesStack Overflow
版权声明:本文标题:javascript - sun.org.mozilla Rhino and extending Java abstract classes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745657978a2668822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论