Subj : Re: creating JS objects in Java...? To : netscape.public.mozilla.jseng From : Igor Bukanov Date : Fri Feb 18 2005 02:02 am user@domain.invalid wrote: > getting there. > i think i compiled the class, now I have to do something with the bytes > I just got back. Do i need to define my own ClassLoader subclass? No, you can piggyback on the implementation that Rhino uses internally and add something like the following to your code fragment: ClassLoader parentLoader = ThisClass.class.getClassLoader(); GeneratedClassLoader loader = mJsContext.createClassLoader(parentLoader); Class mainClass = null; for (int i = 0; i != objs.length; i += 2) { Class cl = loader.defineClass((String)objs[i], (byte[])objs[i + 1]); if (mainClass == null) mainClass = cl; } The above uses the fact that it is the first class in objs that extends/implements superclass/interfaces. It is also important to make sure that parentLoader in the above code can be used to access both Rhino classes and superclass/interfaces. In your case it would happen automatically, but in general this is a thing to watch. Then you can instantiate MainClass like in: Module module = (Module)mainClass.newInstance(); Regards, Igor > > here's what i've got so far. How's it look? > > > CompilerEnvirons ce = new CompilerEnvirons(); > ce.initFromContext(mJsContext); > ClassCompiler cc = new ClassCompiler(ce); > cc.setMainMethodClass("MyCoolNewClass"); > cc.setTargetExtends(Module.class); > > Class[] interfaces = {NamedObject.class}; > cc.setTargetImplements(interfaces); > > String js = "function > Start(){Packages.java.lang.System.err.println(this);} function > GetName(){return this.toString();} function GetNamedChild(){return null;}"; > Object[] objs = cc.compileToClassFiles(js, "script", 1, "MyCoolNewClass"); > > > -denny- > > > user@domain.invalid wrote: > >> that's exactly perfect! >> Thank you! >> >> I don't really need the class files as actual files, but maybe it's an >> interesting optimization to store them away and use the later... >> >> I've been looking at the javadocs and I can see roughly what's going >> on. Are there any examples anywhere where someone is doing this? I >> did a google search and didn't come up with much. >> >> thanks again - it's perfect. now if I can just figure out how it >> works... >> >> =) >> -denny- >> >> >> >> >> Igor Bukanov wrote: >> >>> 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 >> >> >> > .