Subj : Re: Rhino: Wrapping (existing) Java Object (1.5R4) To : Jeremy From : Igor Bukanov Date : Wed Nov 19 2003 11:05 pm Jeremy wrote: > 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)? You can not replace methods of existing objects in JVM and Rhino could not help here. > 2) Is it possible to pass contructors arguments to the JavaAdapter > class? No, but it should not be that hard to extend JavaAdapter to do that: patches are welcome! > ... 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). For workaround you do not need to override each and every method in Java class. You can simply create a subclass that has a default constructor in which it calls the super class constructor using static variables to pass necessary arguments. Then you can use JavaAdapter as is against this class: you will just need to fill static variables with necessary values before constructing adapter objects. If you can not use static variables, then java.lang.ThreadLocal is your friend. Regards, Igor .