Menu

#14 XmlBeans and List/Set <-> primitive array mapping

open
None
5
2008-12-07
2008-02-20
No

This patch fixes the issues described in RequestID #1897962 (see http://sourceforge.net/tracker/index.php?func=detail&aid=1897962&group_id=133517&atid=727368\), opened by me today, and consists in two small changes in class net.sf.dozer.mapping.MappingProcessor (Dozer version is 4.2)

*** ISSUE #1 (DTO -> XmlBeans)
The issue was solved with a very small fix in method addOrUpdateToList (line 721):

private List addOrUpdateToList(Object srcObj, FieldMap fieldMap, Collection srcCollectionValue, Object destObj, Class destEntryType) {

...

if (field == null) {
result = new ArrayList(srcCollectionValue.size());
} else {
if (CollectionUtils.isList(field.getClass())) {
result = (List) field;
// START Bug-fixing, Bug Request #1897962
} else if (CollectionUtils.isArray(field.getClass()) && !CollectionUtils.isPrimitiveArray(field.getClass())) {// must be array
// END Bug-fixing, Bug Request #1897962
result = new ArrayList(Arrays.asList((Object[]) field));
} else { // assume it is neither - safest way is to create new
// List
result = new ArrayList(srcCollectionValue.size());
}
}

...
}

By adding the check !!CollectionUtils.isPrimitiveArray(field.getClass()), initialization of "result" falls back to the last "else" branch, avoiding the ClassCastException (caused by Arrays.asList((Object[]) field)).

*** ISSUE #2 (XmlBeans -> DTO)
The issue was solved with a small change in method mapCollection (line 510 and following):

private Object mapCollection(Object srcObj, Object srcCollectionValue, FieldMap fieldMap, Object destObj) {

...

// Array to Set
else if (CollectionUtils.isArray(srcFieldType) && CollectionUtils.isSet(destCollectionType)) {
// START Bug-fixing, Bug Request #1897962
Collection srcCollection = null;
if (CollectionUtils.isPrimitiveArray(srcCollectionValue.getClass())) {
srcCollection = CollectionUtils.convertPrimitiveArrayToList(srcCollectionValue);
} else {
srcCollection = Arrays.asList((Object[]) srcCollectionValue);
}
result = addToSet(srcObj, fieldMap, srcCollection, destObj);
// END Bug-fixing, Bug Request #1897962
}

...
}

The fix is really simple: if the source array is a primitive array, I initialize srcCollection using CollectionUtils.convertPrimitiveArrayToList, otherwise I use Arrays.asList. Finally, I call addToSet to initialize result.

The attached zip file DozerPatch.zip contains an Eclipse project with the JUnit tests for this issues/patch.
Tests will fail
By default, tests will fail (because the project uses MappingProcessor from dozer-4.2.jar, included in test/lib). To make them pass, simply refactor class net.sf.dozer.util.mapping.MappingProcessorPatched (under src) to net.sf.dozer.util.mapping.MappingProcessor and re-run the tests.

Hope this helps,

Pierangelo Magli
HP Italy Innovation Center

Discussion

  • Pierangelo Magli

    Logged In: YES
    user_id=2014393
    Originator: YES

    Sorry, I forgot to attach the DozerPatch.zip file before submitting the patch.
    Now I'm trying to attach it, but I get a "Error - Choose a Group" error message. What's the problem?

     
  • Pierangelo Magli

    Logged In: YES
    user_id=2014393
    Originator: YES

    File Added: DozerPatch.zip

     
  • Pierangelo Magli

    Eclipse project with patch / tests

     
  • Pierangelo Magli

    Logged In: YES
    user_id=2014393
    Originator: YES

    Ok, it seems I finally managed to upload the DozerPatch.zip file. It seems that the file was too big, so I removed all the external libraries from test/lib.
    In order to make the project work, you will have to provide by yourself the following dependencies, required at runtime:

    - XmlBeans 2.2.0
    - Commons Beanutils
    - Commons Collections
    - Commons Logging
    - Commons Lang
    - Dozer 4.2

     
  • Franz Garsombke

    Franz Garsombke - 2008-02-20

    Logged In: YES
    user_id=550744
    Originator: NO

    thanks for the patch. we like patches :). i will apply this for the next release.

     
  • Franz Garsombke

    Franz Garsombke - 2008-02-20
    • assigned_to: nobody --> fgarsombke
     
  • Matt Tierney

    Matt Tierney - 2008-12-07
    • assigned_to: fgarsombke --> mhtierney
     

Log in to post a comment.