最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论