I\'d like to do something like:
ArrayList objects = new ArrayList();
...
DozerBeanMapper MAPPER = new DozerBeanMapper
What is happening is that you are getting bitten by type erasure. At runtime, java only sees an ArrayList.class
. The type of CustomObject
and NewObject
aren't there, so Dozer is attempting to map a java.util.ArrayList
, not your CustomObject
to NewObject
.
What should work (totally untested):
List<CustomObject> ori = new ArrayList<CustomObject>();
List<NewObject> n = new ArrayList<NewObject>();
for (CustomObject co : ori) {
n.add(MAPPER.map(co, CustomObject.class));
}
I faced a similar issue, and decided on using a generic utility method to avoid iterating every time I needed to perform such mapping.
public static <T, U> List<U> map(final Mapper mapper, final List<T> source, final Class<U> destType) {
final List<U> dest = new ArrayList<>();
for (T element : source) {
dest.add(mapper.map(element, destType));
}
return dest;
}
Usage would then be something like:
final List<CustomObject> accounts.....
final List<NewObject> actual = Util.map(mapper, accounts, NewObject.class);
Possibly this could be simplified further though.