6 Apr 2004 17:29
Re: java.beans.XMLEncoder with custom bean not working
I'm wondering if its because the XMLEncoder is using the system classloader somehow - and not realising that the Car class is in a different class loader? Its pretty wierd as its passed the object which has a strong reference to the Class, so I'm not sure why its having difficulties. Might be worth looking at the source of XMLEncoder to try figure this one out. On 6 Apr 2004, at 16:16, Kas Thomas wrote: > Interestingly, XMLEncoder will work correctly inside the console > session > if you try serializing a swing object. For example, this code works > fine > in a console session: > > // begin groovy code > > s=new groovy.swing.SwingBuilder(); > f = s.frame(title: "new frame", bounds:[200,200,400,200]); > f.getContentPane().add( s.button( text:"OK") ); > f.setVisible(true); // show frame onscreen > fname = "d:\\dev\\test.xml"; > enc = new java.beans.XMLEncoder ( new java.io.FileOutputStream ( fname > ) ); > enc.writeObject( f ); enc.close(); // now write it as XML > > // end > > > The above code executes without any problems and produces the expected > XML, namely: > > > <?xml version="1.0" encoding="UTF-8"?> > <java version="1.4.2-beta" class="java.beans.XMLDecoder"> > <object class="javax.swing.JFrame"> > <void property="bounds"> > <object class="java.awt.Rectangle"> > <int>200</int> > <int>200</int> > <int>400</int> > <int>200</int> > </object> > </void> > <void property="contentPane"> > <void method="add"> > <object class="javax.swing.JButton"> > <string>OK</string> > </object> > </void> > </void> > <void property="name"> > <string>frame1</string> > </void> > <void property="title"> > <string>new frame</string> > </void> > </object> > </java> > > > > So, not sure why a SwingBuilder-built widget serializes fine (but a > user bean doesn't, at least inside the console session) ... or what it > means... but I'm glad to know it's just a console-scoped issue. > > Thanks for the comment and for the great work on Groovy. It's awesome. > > --KT > > > > >>>> jastrachan@... 4/6/2004 10:28:19 AM >>> > For some reason the XMLEncoder doesn't like it running inside the > command line shell. Though if I added this script... > > obj = new Car ( make:"Jeep", model: "Laredo" ); > > fname = "cartest.xml"; > enc = new java.beans.XMLEncoder ( new java.io.FileOutputStream ( fname > ) ); > enc.writeObject( obj ); enc.close(); > > > class Car implements java.io.Serializable { > String make > String model > } > > > to a text file and ran it via the groovy command line > > groovy foo.groovy > > then it worked fine, creating this file... > > <?xml version="1.0" encoding="UTF-8"?> > <java version="1.4.2_03" class="java.beans.XMLDecoder"> > <object class="Car"> > <void property="make"> > <string>Jeep</string> > </void> > <void property="model"> > <string>Laredo</string> > </void> > </object> > </java> > > > I confess to not understanding why - I suspect there's a ClassLoader > issue going on here. Maybe we need to set the > Thread.currentThread().getContextClassLoader() or something? > > > On 6 Apr 2004, at 14:31, Kas Thomas wrote: >> Maybe someone can tell me whether I've encountered a bug, a feature, > or >> mere user insanity... ;^) >> >> The following doesn't quite work as expected: >> >> class Car implements java.io.Serializable { >> String make >> String model >> } >> obj = new Car ( make:"Jeep", model: "Laredo" ); >> >> fname = "d:\\dev\\cartest.xml"; >> enc = new java.beans.XMLEncoder ( new java.io.FileOutputStream ( > fname >> ) ); >> enc.writeObject( obj ); enc.close(); >> >> >> >> The above code, when executed in the groovy Swing console, gives a >> ClassNotFoundException on Car, and an XML file containing: >> >> <?xml version="1.0" encoding="UTF-8"?> >> <java version="1.4.2-beta" class="java.beans.XMLDecoder"> >> </java> >> >> >> The ClassNotFoundException seems strange, because the Car instance > is >> there, I can instantiate more Cars in the same console session, I > can >> inspect properties on any instance, etc. >> >> The Groovy doc uses the term "bean" rather generally to describe > user >> objects like Car, and I therefore expected XMLEncoder to work on a > Car >> instance... but no dice. >> >> A nice feature for Groovy would be some kind of very easy, tight, >> seamless support for XMLEncoder/XMLDecoder on beans such that one > can >> just do something like: >> >> obj = new Car ( make:"Jeep", model: "Laredo" ); >> filename = "mycar.xml"; >> groovy.xmlEncode(obj, filename); >> >> >> --KT >> >> _______________________________________________ >> groovy-dev mailing list >> groovy-dev@... >> http://lists.codehaus.org/mailman/listinfo/groovy-dev >> >> > > James > ------- > http://radio.weblogs.com/0112098/ > > _______________________________________________ > groovy-dev mailing list > groovy-dev@... > http://lists.codehaus.org/mailman/listinfo/groovy-dev > _______________________________________________ > groovy-dev mailing list > groovy-dev@... > http://lists.codehaus.org/mailman/listinfo/groovy-dev > > James ------- http://radio.weblogs.com/0112098/
RSS Feed