Morphing a graph of objects

Help
2007-11-01
2013-04-03
  • Oswaldo Dantas

    Oswaldo Dantas - 2007-11-01

    Hello,

    I'm using EZMorpher to translate objects I get from a web service to objects from my model and I'd like an opinion if there is a better way to do it.
    To exemplify in a generic scenario, I get objects of type "WA" which has two attributes: a name String and an ArrayList of WB returned by a getB method. I want to translate from WA and WB to the equivalent classes A and B.
    I developed an ArrayListMorpher class containing a HashMap (mapping which origin class gets morphed to which target class) and a reference to the MorpherRegistry to be used to morph the contents of the collection.
    After registering a BeanMorpher for A, another for B and my ArrayListMorpher with the WA->A WB->B mapping, when I do a "morpherRegistry.morph(ArrayList.class, xxx.getWAs())", with xxx.getWAs() returning an ArrayList of WA, I get a new ArrayList (created in my ArrayListMorpher class) containing a number of instances of A, but instead of a collection of B inside it I get a collection of WB.
    To address that, changed the first "if" in the BeanMorpher.setProperty private method from this:

    ...
          if( targetType.isAssignableFrom( sourceType ) ){
             if( value == null && targetType.isPrimitive() ){
                value = morpherRegistry.morph( targetType, value );
             }
             PropertyUtils.setProperty( targetBean, name, value );
          }else{
    ...

    to this:

    ...
           if( targetType.isAssignableFrom( sourceType ) && value == null && targetType.isPrimitive() ){
                value = morpherRegistry.morph( targetType, value );
          }else{
    ...

    so my ArrayListMorpher class could be called in some line after the else.
    After the change I got what I needed with no errors (I haven't tried with circular references like A->B->A->... I probably will need to control it at some point).
    Returning to my question, would there be a better way to morph collections of objects containing collections of other objects? Or the change I made is the way to do it? (if so, maybe this could be merged into the API, after some more thinking in other scenarios like circular references of course)

    Regards,

    Oswaldo

     
    • aalmiray

      aalmiray - 2007-11-06

      Hi Oswaldo,

      This is an scenario I believe I have yet to test, could you provide a failing testcase with basic A,B,WA,WB and ArrayListMorpher?
      I'd like to see what other implications could arise from this change, thanks.

      Andres

       
    • aalmiray

      aalmiray - 2007-11-07

      Reading further into your message, it seems yo laid out the grounds for an ObjectListmorpher, similar to ObjectArrayMorpher, working with Lists instead of arrays. Ideally you'll want to compose an OLM with any Morpher, one of them being your special W* -> * which seems to me that its a good way to manage mixed types in a list without generics information available.

      Cheers,
      Andres

       
    • aalmiray

      aalmiray - 2007-11-07

      Oswaldo, I added ObjectListMorpher which will serve as a base for morphing lists, and SwitchingMorpher that has a similar behavior as described in your ArrayListMorpher. By combining the two you should get the expected result. Please review the tests for each one to learn how to use them.

      Cheers,
      Andres

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks