javascript - sun.org.mozilla Rhino and extending Java abstract classes - Stack Overflow

In the sun.mozilla version of Rhino, JavaAdapter only takes interfaces as its first argument instead of

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
Add a ment  | 

1 Answer 1

Reset to default 7

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 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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745657978a4638657.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信