commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [betwixt] Array of extended class
Date Thu, 17 Nov 2005 22:24:50 GMT
On Thu, 2005-11-17 at 10:08 -0500, Sacha Fournier wrote:
> Hi,
> 
> I have something like this:
> 
> MainClass.java
> class MainClass
> {
>    private MyObject aObject[];
> 
>   public MyObject[] getMyObject()
>  {
>     return this.aObject;
>  }
> }
> 
> MyObjectA.java
> class MyObjectA extends MyObject
> {
>     // Some specific attribute getter...
> }
> 
> MyObjectB.java
> class MyObjectB extends MyObject
> {
>     // Some other specific attribute getter...
> }
> 
> Main.java
> void Main()
> {
>   MainClass oMain = new MainClass();
>   MainClass.addToMyObject(new MyObjectA());
>   MainClass.addToMyObject(new MyObjectB());
> 
>   StringWriter outputWriter = new StringWriter();
>   outputWriter.write("<?xml version='1.0' ?>");
>   BeanWriter beanWriter = new BeanWriter(outputWriter);       
>   beanWriter.write(oMain.getClass().getName(), oMain);
> }
> 
> =============================================
> My xml output is
> <MainClass>
>   <MyObject>{all MyObjectA attributes}</MyObject>
>   <MyObject>{all MyObjectB attributes}</MyObject>
> </MainClass>
> 
> But I was expecting something like that:
> <MainClass>
>   <MyObjectA>{all MyObjectA attributes}</MyObjectA>
>   <MyObjectB>{all MyObjectB attributes}</MyObjectB>
> </MainClass>
> 
> 
> Any ideas ?

this has to do with introspection time verses bind time typing. when
betwiwt introspects the MainClass bean it finds that the property is
typed MyObject. at bind time, the instance actually contains subclasses
of MyObject. by default, betwixt uses bind time mapping.

if you want to be able to round trip (rather than just generate xml),
you'll need to use a polymorphic mapping. this should be covered in
here: http://jakarta.apache.org/commons/betwixt/guide/derived.html.

BTW if you're using the last release please upgrade to a recent nightly
or roll your own since a substantial amount of functionality has been
added in this area.

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message