Subj : Re: creating JS objects in Java...? To : netscape.public.mozilla.jseng From : Igor Bukanov Date : Thu Feb 17 2005 06:36 pm user@domain.invalid wrote: > So, if I use the JavaAdapter to make c new javascript class that > inherits from another class an implements a few interfaces, if I use the > Rhino API to get a hold of that object in Java, can I use it right then > and there as an object of those interface types? Or do I have to use it > as such only in the JS context? > > Here's what I want to do: I have an engine that takes Module objects. > Many of these Module objects have been overwritten in Java - > EmailModule, etc. I would like to be able to have the user author his > own Module using JavaScript, using, presumably, the JavaAdapter. > > This authoring is done in a little gui. Say that I have an interface > Foo which has an method Bar() that I want to overwrite. So, in this gui > he writes "function Bar(){print("HelloWorld");}". Now, -in Java- how do > I make this Module object with this code overloading the superclass > method Bar() such that I then have a Java Module object that I can pass > around my Java API? > > Basically, I want a Java function (not a JS function) like this: > > public Module BuildSubclass(String[] superclasses, String iBarJSCode, > String iSomeOtherMethodJSCode,...) > { > Module m = new JavaAdapter(superclasses, iBArJSCode, > iSomeOtherMethodJSCode); > > return m; > } > > Now I have a Module object that extends or implements an arbitrary > number of classes or interfaces and who has various overloaded methods. > > > How do I do this? I would be incredibly grateful for some help. Use class compiler, http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/optimizer/ClassCompiler.html to compile you class to bytecode sublassing the desired class, load the generated bytecode into a class loader and instantiate the resulting class. Regards, Igor .