#66 Mapping from object to array

closed
None
5
2014-11-13
2006-12-15
No

I've submitted a post on the developer forum. Does not seem to be much activity there, and this place is probably the right place to submit it.

Given the two classes (with appropriate get'ers and set'ers):

public class Source {
String a;
String b;
// get'ers and set'ers follows
}

public class Destination {
String a;
String b[];
// get'ers and set'ers follows
}

The mapping looks like this:

<mappings>
<configuration>
<stop-on-errors>true</stop-on-errors>
</configuration>
<mapping map-null="false">
<class-a>Source</class-a>
<class-b>Destination</class-b>
<field>
<a>a</a>
<b>a</b>
</field>
<field>
<a>b</a>
<b>b[0]</b>
<b-hint>java.lang.String</b-hint>
</field>
</mapping>
</mappings>

If an object of class Source is:

Source.a = "This is a"
Source.b = "This is b"

one would assume that the resulting destination was:

Destination.a = "This is a"
Destination.b = { "This is b" }

while it in fact becomes

Destination.a = "This is a"
Destination.b = { "This is b", null }

The mapping works if the destination mapping is <b>b[i]</b> where i is greater than 0.

The reason for this behavior seems to be the class net.sf.dozer.util.mapping.propertydescriptor.BruteForcePropertyDescriptor. The method writeIndexedValue has the following code snippet:

if (returnType.isArray()) {
o = Array.newInstance(returnType.getComponentType(), 1);
} else if (collectionUtils.isSet(returnType)) {
o = new HashSet();
} else { // default
o = new ArrayList();
}

It creates either an empty ArrayList, an empty HashSet or an array containing a null object. By changing it to call Array.newInstance( returnType.getComponentType(), 0) instead, the above mapping works as one would assume. As far as I have been able to test none of the unit tests breaks by doing this.

The patched source file is attached for inclusion if found appropriate.

Discussion

  • Jarle Sætre

    Jarle Sætre - 2006-12-15

    Patched source code for net.sf.dozer.util.mapping.propertydescriptor.BruteForcePropertyDescriptor

     
  • Franz Garsombke

    Franz Garsombke - 2006-12-15

    Logged In: YES
    user_id=550744
    Originator: NO

    Thanks for the patch. I will test and apply to the 2.5 codebase.

    Thanks for your help and time.

    Franz

     
  • Franz Garsombke

    Franz Garsombke - 2006-12-15
    • assigned_to: nobody --> fgarsombke
     
  • Franz Garsombke

    Franz Garsombke - 2007-02-03
    • status: open --> pending
     
  • Franz Garsombke

    Franz Garsombke - 2007-02-03

    Logged In: YES
    user_id=550744
    Originator: NO

    Patch applied and all tests work.

     
  • Matt Tierney

    Matt Tierney - 2007-02-09
    • status: pending --> closed
     

Log in to post a comment.

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

Sign up for the SourceForge newsletter:





No, thanks