Subj : RE: Integrating Java Maps in Rhino To : netscape.public.mozilla.jseng From : Rod Macpherson Date : Sat May 14 2005 03:07 pm I started off wading through NativeJavaObject which as you know is a full-on reflection of an arbitrary object. Separating the wheat from the chaff was a little rough so I put that aside and started with a clean implementation of Scriptable (plus Wrapper) with all of the methods stubbed out. The Scriptable interface does a good job of spelling out the requirements. After implementing the WrapperFatory.wrapAsJavaObject override to test for Map instances, everything worked as advertised. I think I understand why they did not include this in the core. If they did then anybody trying to use Map or Collection as a POJO would be thwarted and there are many different ways to expose these classes to JavaScript. Thanks for the helpful hints. -----Original Message----- From: Jan Grant [mailto:Jan.Grant@bristol.ac.uk] Sent: Wednesday, May 11, 2005 6:58 AM To: Rod Macpherson Cc: mozilla-jseng@mozilla.org Subject: RE: Integrating Java Maps in Rhino On Wed, 11 May 2005, Rod Macpherson wrote: > Here is another way of stating the problem. > > > > We can dynamically add properties to a JavaScript object at will. How > can we translate the key/value pairs in a Java Map to a set of > properties on a JavaScript Object? The objective being the ability to > use foo.bar syntax in JavaScript versus foo.get( "bar" ). This is reasonably doable. Look at org.mozilla.javascript.WrapFactory: in particular, wrapAsJavaObject. This is called to wrap java objects that don't fall into other categories that are already special-cased, and just slaps a NativeJavaObject around. You can subclass the WrapFactory and override this method; then install the result using org.mozilla.javascript.Context #setWrapFactory(WrapFactory) A NativeJavaMap implementation is pretty straightforward: see NativeJavaObject for hints, and the Rhino documentation on java host objects: there are only a few methods you need to implement, since a java Map is very close indeed to a javascript object. (The trick is to remember to wrap objects on the way out of the map, and to unwrap them on the way back in.) Hope this is a sufficient pointer (don't have time to write the code right now) - Cheers, jan -- jan grant, ILRT, University of Bristol. http://www.ilrt.bris.ac.uk/ Tel +44 (0)117 9287088 (with luck) http://ioctl.org/jan/ It's a sad fact that the word "semantics" seems to have lost all meaning. .