I had the same problem, and currently (like Dozer 5.5.x) there is no easy way, but there is a complicated one.
Note that it relies on the absence of a security manager in the JVM, otherwise you will need to add a few permissions to the security rules. This is because this solution uses reflection to access the private fields of Dozer classes.
You need to extend 2 classes: DozerBeanMapper and MappingProcessor . You will also need to list the direction and interface in order to get the direction from the above classes.
Listing:
public enum Direction { TO, FROM; }
Interface:
public interface DirectionAware { Direction getDirection(); }
Class extending DozerBeanMapper :
public class DirectionAwareDozerBeanMapper extends DozerBeanMapper implements DirectionAware { private Direction direction; public DirectionAwareDozerBeanMapper(Direction direction) { super(); this.direction = direction; } public DirectionAwareDozerBeanMapper(Direction direction, List<String> mappingFiles) { super(mappingFiles); this.direction = direction; } @Override protected Mapper getMappingProcessor() { try { Method m = DozerBeanMapper.class.getDeclaredMethod("initMappings"); m.setAccessible(true); m.invoke(this); } catch (NoSuchMethodException|SecurityException|IllegalAccessException|IllegalArgumentException|InvocationTargetException e) {
Class MappingProcessor :
public class DirectionAwareMappingProcessor extends MappingProcessor implements DirectionAware { private Direction direction; protected DirectionAwareMappingProcessor(ClassMappings arg1, Configuration arg2, CacheManager arg3, StatisticsManager arg4, List<CustomConverter> arg5, DozerEventManager arg6, CustomFieldMapper arg7, Map<String, CustomConverter> arg8, Direction direction) { super(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); this.direction = direction; } public Direction getDirection() { return direction; } }
Now use.
1) Each time you want to map the same primitive type (e.g. String-String), use a DozerConverter with this type for both arguments as a custom converter in your dispenser mapping file. The implementation of such a converter should expand: DozerConverter<String,String> and implement the MapperAware interface. This is important if you have MapperAware , because you have a mapper that you can direct to DirectionAware and then get directions.
For instance:
public class MyMapper extends DozerConverter<String, String> implements MapperAware { private DirectionAware dirAware; public MyMapper(Class<String> cls) { super(cls, cls); } @Override public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<String> destinationClass, Class<String> sourceClass) { if (dirAware.getDirection() == Direction.FROM) { // TODO convert sourceFieldValue for "FROM" direction and return it } else { // TODO convert sourceFieldValue for "TO" direction and return it } } @Override public void setMapper(Mapper mapper) { dirAware = (DirectionAware)mapper; } }
2) You need to create 2 global Maper Dozer objects, one for each display direction. They must be configured with the same mapping files, but with a different direction argument. For instance:
DirectionAwareDozerBeanMapper mapperFrom = DirectionAwareDozerBeanMapper(mappingFiles, Direction.FROM); DirectionAwareDozerBeanMapper mapperTo = DirectionAwareDozerBeanMapper(mappingFiles, Direction.TO);
Of course, you will need to use the proper mapper (from / to) to provide information to the custom mappers which direction you are in.