views:

472

answers:

2

Suppose I have the following classes

public class Baz {
  private List<Foo> foos = new ArrayList<Foo>();
}

public class Foo {
  private String string;
}

public class Target {
  private List<String> fooStrings = new ArrayList<String>();
}

Is there any mapping I can use to, given a Baz, map it to the target class and get a list of the strings contained within the foo's in Baz? The following mapping does not work

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos.string</a>
    <b>fooStrings</b>
  </field>
</mapping>

Because string is not a property of foos (which is of type List). I would have thought Dozer would be clever enough to, if it encountered a collection in a deep mapping, and the target was also a collection, to be able to break the deep property name into two and iterate across the collection to get the child part of the deep mapping from the collection members. Apparently not. Is there a solution short of making a feature request of Dozer?

A: 

You could always write your own CustomConverter.

It makes sense why Dozer isn't able to handle this as you expect since at runtime it has no type information about the List foos and can't guarantee that every Object in the list is actually a Foo.

matt b
Its not dozer's job to ensure that every item in a List<Foo> is actually a Foo. In fact it doesn't even have to care about that. All it needs to know that the member items of the list have a property 'string', and its up to me, the developer to ensure that's always the case. Writing a custom converters for an older version of Dozer is what I've been doing. i was hoping in the last 2 major version of Dozer someone would have addressed this fairly obvious (IMHO) use case.
Jherico
A: 

This mapping is not working with dozer 5.5.2 version someField someList[1].someOtherList[0].someOtherField org.dozer.vo.TestObject, org.dozer.vo.AnotherTestObject

giving null pointer exception

java.lang.NullPointerException at java.lang.Class.isAssignableFrom(Native Method) at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:81) at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:57) at org.dozer.factory.DestBeanCreator.create(DestBeanCreator.java:53) at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.writeDeepDestinationValue(GetterSetterPropertyDescriptor.java:190) at org.dozer.propertydescriptor.GetterSetterPropertyDescriptor.setPropertyValue(GetterSetterPropertyDescriptor.java:81) at org.dozer.fieldmap.FieldMap.writeDestValue(FieldMap.java:91) at org.dozer.MappingProcessor.writeDestinationValue(MappingProcessor.java:826) at org.dozer.MappingProcessor.mapFromFieldMap(MappingProcessor.java:310) at org.dozer.MappingProcessor.mapField(MappingProcessor.java:249) at org.dozer.MappingProcessor.map(MappingProcessor.java:218) at org.dozer.MappingProcessor.map(MappingProcessor.java:163) at org.dozer.MappingProcessor.map(MappingProcessor.java:119) at org.dozer.MappingProcessor.map(MappingProcessor.java:114) at org.dozer.DozerBeanMapper.map(DozerBeanMapper.java:92) at com.chase.ccs.mapping.dozer.prototype.TestUtils.main(TestUtils.java:63)

Burhanuddin
What does this have to do with my question?
Jherico