I'm trying to use Dozer to convert an instance of
class Source {
private List<Foo> foos = new ArrayList<Foo>();
public List<Foo> getFoos() {
return foos;
}
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
to an instance of:
class Target {
private List<Foo> foos = new ArrayList<Foo>();
public List<Foo> getFoos() {
return foos;
}
}
In Java code I would preform the converstion like this
Source s = new Source();
Target t = new Target();
t.getFoos().addAll(s.getFoos());
Dozer doesn't perform this conversion by default because Target
doesn't have a foos
property (just a getter).
In reality, I have lots of properties like this that I need to map. One option is to tell Dozer to map the private fields directly, but this is not entirely satisfactory because:
- I will need to specify each field to be mapped in this way by name in the Dozer XML config
- accessing private fields is bad
Is there a better way?
Best Answer
There is no easy way to get around this other than the
is-accessible
flag. But you could define a custom converter that uses the getter to do:This would be very heavy handed and alot of work. You would need to define a custom converter (see http://dozer.sourceforge.net/documentation/customconverter.html) between
Source
andTarget
that used the getter instead of the setter:I think, good luck