Subj : Rhino: Wrapping (existing) Java Object (1.5R4) To : netscape.public.mozilla.jseng From : JeremyK@comcast.net (Jeremy) Date : Wed Nov 19 2003 01:09 pm Short Description: 1) In Rhino, is it possible to wrap (with the goal of replacing one or methods) an existing Java object (not class, but existing instance)? 2) Is it possible to pass contructors arguments to the JavaAdapter class? Longer Explanation: I'm trying to adapt/integrate Rhino into an application that uses a lot of Java class factories. Basically, the app has a fair amount of code that looks like this: SomeJavaClass someJavaObject = ClassFactory.newInstance(constructorArg1, constructorArg2) What I'd like to do is to provide some special hook into this class factory, so that by some configuration change, the Java class could be subclassed by JavaScript code. Now this works fine for Java classes which have no argument constructors -- I use the Java Adapter trick. But some objects require parameters on their contructors. Which puts me into the following dilemma: JavaAdapter won't call the contructors with arguments. If, within my JavaScript, I create the Java object, then I can't wrap the existing instance such that I could override one or more of the methods. (I suppose technically I could: I could write a Java object that overrode *every* method in the Java object. Then, I could let the JavaAdapter create a new instance of the class, which would really just be a shell that delegated all calls to JavaScript. The JavaScript could then delegate to the 'real' (original) Java object as required. While this might work, this is probably ugly enough that I'd just resort to writing the desired code in Java). What I can't figure out is how to do all of these at once: a) Get a Java object contructed with parameters b) Return a Java object that must be of class "SomeJavaClass" implementing whatever required interfaces c) Ability to subclass certain methods in JavaScript Obviously, there are bigger changes to the application itself that might make Rhino integration easier, but that's kind of off the table at the moment. One thought I had was that if the JavaAdapter class had another argument, array of arguments to pass to the 'real' object constructor, then it could generate the proper contructor calls, i.e., something like this: AdapterGeneratedClass(javaScriptDelegee, constructorArg1, constructorArg2) { super(constructorArg1, constructorArg2); this.delegee = javaScriptDelegee; } (I'm paraphrasing the class generated dynamically by JavaAdapter, but I think that covers the idea). This seems the cleanest -- no 'extra' objects, etc. Thanks for any input, Jeremy .