You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Sacha Fournier <sa...@isi-corp.biz> on 2005/11/17 16:08:06 UTC

[betwixt] Array of extended class

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 ?

-- 
Sacha Fournier
sacha.fournier@isi-corp.biz
514-258-4096


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


Re: [betwixt] Array of extended class

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
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