简体   繁体   English

为什么推土机将空源对象传递给我的可配置自定义转换器?

[英]Why is dozer passing in a null source object to my Configurable Custom Converter?

I'm using Dozer version 5.4.0. 我正在使用5.4.0版的推土机。

I have one class with a Map in it, and another class with a List. 我有一类带有Map,另一类带有List。 I'm trying to write a custom converter that will take the values of the map and put it in the List. 我正在尝试编写一个自定义转换器,该转换器将获取地图的值并将其放入列表中。 But the problem is, the converter always gets passed a null source object for the Map, even if the parent source has a populated Map. 但是问题是,即使父源具有填充的Map,转换器也始终会传递Map的空源对象。 I can't figure out why this is happening, I think the converter should be passed a populated Map object. 我不知道为什么会这样,我认为应该将转换器传递给填充的Map对象。

Here is some source code that compiles and shows the problem in action: 这是一些编译并显示实际问题的源代码:

package com.sandbox;

import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.FieldsMappingOptions;

public class Sandbox {

    public static void main(String[] args) {
        DozerBeanMapper mapper = new DozerBeanMapper();
        mapper.addMapping(new MappingConfig());

        ClassWithMap parentSource = new ClassWithMap();
        ClassWithList parentDestination = mapper.map(parentSource, ClassWithList.class);

        int sourceMapSize = parentSource.getMyField().size();
        assert sourceMapSize == 1;
        assert parentDestination.getMyField().size() == 1;    //this assertion fails!
    }

    private static class MappingConfig extends BeanMappingBuilder {

        @Override
        protected void configure() {
            mapping(ClassWithMap.class, ClassWithList.class)
                    .fields("myField", "myField",
                            FieldsMappingOptions.customConverter(MapToListConverter.class, "com.sandbox.MyMapValue"));
        }
    }


}

As you can see, that second assertion fails. 如您所见,第二个断言失败。 Here are the other classes I'm using. 这是我正在使用的其他类。

MapToListConverter.java: MapToListConverter.java:

package com.sandbox;

import org.dozer.DozerConverter;
import org.dozer.Mapper;
import org.dozer.MapperAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapToListConverter extends DozerConverter<Map, List> implements MapperAware {

    private Mapper mapper;

    public MapToListConverter() {
        super(Map.class, List.class);
    }

    @Override
    public List convertTo(Map source, List destination) {   //source is always null, why?!
        List convertedList = new ArrayList();
        if (source != null) {
            for (Object object : source.values()) {
                Object mappedItem = mapper.map(object, getDestinationClass());
                convertedList.add(mappedItem);
            }
        }
        return convertedList;
    }

    private Class<?> getDestinationClass() {
        try {
            return Class.forName(getParameter());
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    public Map convertFrom(List source, Map destination) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }
}

ClassWithMap.java: ClassWithMap.java:

package com.sandbox;

import java.util.HashMap;
import java.util.Map;

public class ClassWithMap {

    private Map<String, MyMapValue> myField;

    public Map<String, MyMapValue> getMyField() {   //this method gets called by dozer, I've tested that with a break point
        if (myField == null) {
            myField = new HashMap<String, MyMapValue>();
            myField.put("1", new MyMapValue());
        }
        return myField; //myField has an entry in it when called by dozer
    }

    public void setMyField(Map<String, MyMapValue> myField) {
        this.myField = myField;
    }

}

ClassWithList.java: ClassWithList.java:

package com.sandbox;

import java.util.List;

public class ClassWithList {

    private List<MyMapValue> myField;

    public List<MyMapValue> getMyField() {
        return myField;
    }

    public void setMyField(List<MyMapValue> myField) {
        this.myField = myField;
    }
}

MyMapValue.java MyMapValue.java

package com.sandbox;

public class MyMapValue {

}

The problem seems to be in the MapFieldMap.getSrcFieldValue method of dozer. 问题似乎出在推土机的MapFieldMap.getSrcFieldValue方法中。 These comments are added by me: 这些评论是我添加的:

  @Override
  public Object getSrcFieldValue(Object srcObj) {
    DozerPropertyDescriptor propDescriptor;
    Object targetObject = srcObj;

    if (getSrcFieldName().equals(DozerConstants.SELF_KEYWORD)) {
      propDescriptor = super.getSrcPropertyDescriptor(srcObj.getClass());
    } else {
      Class<?> actualType = determineActualPropertyType(getSrcFieldName(), isSrcFieldIndexed(), getSrcFieldIndex(), srcObj, false);
      if ((getSrcFieldMapGetMethod() != null)
          || (this.getMapId() == null && MappingUtils.isSupportedMap(actualType) && getSrcHintContainer() == null)) {
        // Need to dig out actual map object by using getter on the field. Use actual map object to get the field value
        targetObject = super.getSrcFieldValue(srcObj);

        String setMethod = MappingUtils.isSupportedMap(actualType) ? "put" : getSrcFieldMapSetMethod();
        String getMethod = MappingUtils.isSupportedMap(actualType) ? "get" : getSrcFieldMapGetMethod();
        String key = getSrcFieldKey() != null ? getSrcFieldKey() : getDestFieldName();

        propDescriptor = new MapPropertyDescriptor(actualType, getSrcFieldName(), isSrcFieldIndexed(), getDestFieldIndex(),
                setMethod, getMethod, key, getSrcDeepIndexHintContainer(), getDestDeepIndexHintContainer());
      } else {
        propDescriptor = super.getSrcPropertyDescriptor(srcObj.getClass());
      }
    }

    Object result = null;
    if (targetObject != null) {
      result = propDescriptor.getPropertyValue(targetObject); //targetObject is my source map, but the result == null
    }

    return result;

  }

I figured out how to fix this. 我想出了解决方法。 Still not sure if it's a bug or not, but I think it is. 仍然不确定是否是错误,但我认为是。 The solution is to change my configuration to say this: 解决的办法是更改我的配置,这样说:

        mapping(ClassWithMap.class, ClassWithList.class, TypeMappingOptions.oneWay())
                .fields("myFields", "myFields"
                        , FieldsMappingOptions.customConverter(MapToListConverter.class, "com.sandbox.MyMapValue")
                );

The fix is in the TypeMappingOptions.oneWay() . 该修复程序位于TypeMappingOptions.oneWay() When it's bidirectional, the dozer MappingsParser tries to use a MapFieldMap which causes my problem: 双向时,推土机MappingsParser尝试使用MapFieldMap导致我的问题:

    // iterate through the fields and see wether or not they should be mapped
    // one way class mappings we do not need to add any fields
    if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
      for (FieldMap fieldMap : fms.toArray(new FieldMap[]{})) {
        fieldMap.validate();

        // If we are dealing with a Map data type, transform the field map into a MapFieldMap type
        // only apply transformation if it is map to non-map mapping.
        if (!(fieldMap instanceof ExcludeFieldMap)) {
          if ((isSupportedMap(classMap.getDestClassToMap())
                  && !isSupportedMap(classMap.getSrcClassToMap()))
              || (isSupportedMap(classMap.getSrcClassToMap())
                  && !isSupportedMap(classMap.getDestClassToMap()))
              || (isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))
                  && !isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))
              || (isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))
                  && !isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))) {
            FieldMap fm = new MapFieldMap(fieldMap);
            classMap.removeFieldMapping(fieldMap);
            classMap.addFieldMapping(fm);
            fieldMap = fm;
          }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM