Please have a look at the attached files. If I call the "test()" function from the flow, the output is the following:

computePI: 3.141592653589793
"file:test.js", line 8: uncaught JavaScript exception: TypeError: computePI is not a function.

In other words, if I define a public method in a base class, it is not inherited by a derived class, at least according to Rhino.

I was finding this rather baffling (how can you determine if a method is inherited or not?) but looking at the documentation of java.lang.Class.getDeclaredMethods(), I found a possible answer:

"This includes public, protected, default (package) access, and private methods, but excludes inherited methods."

Probaly Rhino uses getDeclaredMethods() instead of getMethods()? Is there any particular reason for this choice?

The problem is I am writing a component to be used from Javascript and I want it to have a polymorphic behavior.Is there a way to somehow tell the derived object to automatically expose all the inherited jsFunction|jsGet|jsSet methods? Or should I stick to a plain Java object and forget about Scriptable?

Thanks in Advance,

Ugo
import org.mozilla.javascript.*;

public class BaseScriptableTest extends ScriptableObject {

    public BaseScriptableTest() {}
    
    public String getClassName() {
        return "BaseScriptableTest";
    }
    
    public static Scriptable jsConstructor(Context cx, Object[] args,
                                            Function ctorObj,
                                            boolean inNewExpr)
        throws Exception {
        return new BaseScriptableTest();
    }

    public double jsFunction_computePI() {
        return Math.PI;
    }
}
import org.mozilla.javascript.*;

public class ScriptableTest extends BaseScriptableTest {
    
    public ScriptableTest() {}
    
    public String getClassName() {
        return "ScriptableTest";
    }
    
    public static Scriptable jsConstructor(Context cx, Object[] args,
                                            Function ctorObj,
                                            boolean inNewExpr)
        throws Exception {
        return new ScriptableTest();
    }
}

Attachment: test.js
Description: JavaScript source

Reply via email to