Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sun.org.mozilla Rhino and extending Java abstract classes

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");
    }
});
like image 302
Alec Gorge Avatar asked Jan 26 '11 02:01

Alec Gorge


People also ask

Can abstract class extend another class?

An abstract class can extend another abstract class. And any concrete subclasses must ensure that all abstract methods are implemented. Abstract classes can themselves have concrete implementations of methods. These methods are inherited just like a method in a non-abstract class.

WHAT IS interface and abstract class in Java?

The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.

Is abstract cannot be instantiated java interface?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

Can an abstract class have fields?

You can declare both val and var fields in an abstract class (or trait), and those fields can be abstract or have concrete implementations.


1 Answers

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!

like image 174
Ken Avatar answered Oct 06 '22 03:10

Ken