Howdy,

I'm wondering if it is possible to use JavaAdapter to provide
constructor arguments to a class when extending it?

I'm not sure how to explain this better but hopefully this code can
explain it:

package dogtest;

import org.mozilla.javascript.*;

public class asdf {

        public String bval = "default value";

        public asdf() {
        }

        public asdf(String bval) {
                this.bval = bval;
        }

        public void implementMe() {
                System.out.println("Original implementMe function.");
                System.out.println("bval = " + bval);
        };

        public static void main(String[] args) {

                Context cx;
                Scriptable scope;

                cx = ContextFactory.getGlobal().enterContext();
                scope = cx.initStandardObjects();

                Object wrappedOut = Context.javaToJS(System.out, scope);
                ScriptableObject.putProperty(scope, "out", wrappedOut);

                System.out.println("Instantiate & call via js JavaAdapter: ");
                String toEval = "var foo = new 
JavaAdapter(Packages.dogtest.asdf,
{implementMe:function(){out.println('New implementMe function.\\nbval
= ' + this.bval);}});"
                                + "foo.implementMe();";

                cx.evaluateString(scope, toEval, "eval", 1, null);

                System.out.println("\nInstantiate & call via java: ");
                asdf.inJava().implementMe();

                System.out.println("\nInstantiate & call via js direct abstract
class: ");
                cx.evaluateString(scope, "var dog = new 
Packages.dogtest.asdf('hi
there');", "eval", 1, null);
                // below line generates compile error
                //cx.evaluateString(scope, "dog.implementMe = function()
{out.println('New implementMe function.\\nbval = ' + this.bval);}",
"eval", 1, null);
                cx.evaluateString(scope, "dog.implementMe();", "eval", 1, null);

        }

        public static asdf inJava() {
                return new asdf("new value") {
                        public void implementMe() {
                                System.out.println("New implementMe function");
                                System.out.println("bval = " + 
this.bval.toString());
                        }
                };
        }
}


(I have put a copy up http://pastebin.com/m2d0045ee incase the
formatting got kludged)

Basically, I am using JavaAdapter to instantiate an abstract class.
Just to make life difficult I would like to instantiate it using a
custom constructor which takes arguments. Is there a way to do this?

Put another way, I want to achieve the functionality of the static
asdf.inJava() function, except in javascript.

_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to