On Apr 24, 7:45 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> 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 uphttp://pastebin.com/m2d0045eeincase 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.

Unfortunately the generated adapter class is hardwired to call the
zero-arg base class constructor, so it's not possible in the current
Rhino to construct a base class through JavaAdapter using any
arguments.

As an extension it seems reasonable. I think it'd be possible to add
constructor arguments to the JavaAdapter constructor and pass them
through; the main issue is that the constructor is currently defined
to consider its list of arguments to be classes to extend or
implement, so there would be some work to figure out the right
approach to maintain backwards compatibility.

--N

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

Reply via email to