Permalink
Browse files

Fix DeepHierarchyUtils for work with deep mappings with indexed field…

…s on the end
  • Loading branch information...
1 parent a21da6a commit defbcacae1cc4e7a294812be6dbc257c44aec35e @Spikhalskiy Spikhalskiy committed Jan 7, 2013
@@ -18,8 +18,6 @@
import org.dozer.fieldmap.HintContainer;
import org.dozer.propertydescriptor.DozerPropertyDescriptor;
import org.dozer.propertydescriptor.PropertyDescriptorFactory;
-import org.dozer.util.DozerConstants;
-import org.dozer.util.MappingUtils;
import java.util.Collection;
import java.util.StringTokenizer;
@@ -29,7 +27,7 @@
*/
public class DeepHierarchyUtils {
// Copy-paste from GetterSetterPropertyDescriptor
- public static Object getDeepFieldValue(Object srcObj, String fieldName, HintContainer srcDeepIndexHintContainer) {
+ public static Object getDeepFieldValue(Object srcObj, String fieldName, boolean isIndexed, int index, HintContainer srcDeepIndexHintContainer) {
// follow deep field hierarchy. If any values are null along the way, then return null
Object parentObj = srcObj;
Object hierarchyValue = parentObj;
@@ -43,6 +41,10 @@ public static Object getDeepFieldValue(Object srcObj, String fieldName, HintCont
}
}
+ if (isIndexed) {
+ hierarchyValue = MappingUtils.getIndexedValue(hierarchyValue, index);
+ }
+
return hierarchyValue;
}
@@ -37,7 +37,6 @@ public ProtoFieldPropertyDescriptor(Class<?> clazz, String fieldName, boolean is
super(clazz, fieldName, isIndexed, index, srcDeepIndexHintContainer, destDeepIndexHintContainer);
}
-
private Class<?> _propertyType;
@Override
@@ -71,7 +70,7 @@ public ProtoFieldPropertyDescriptor(Class<?> clazz, String fieldName, boolean is
public Object getPropertyValue(Object bean) {
Object result;
if (MappingUtils.isDeepMapping(fieldName)) {
- result = DeepHierarchyUtils.getDeepFieldValue(bean, fieldName, srcDeepIndexHintContainer);
+ result = DeepHierarchyUtils.getDeepFieldValue(bean, fieldName, isIndexed, index, srcDeepIndexHintContainer);
} else {
result = getSimplePropertyValue(bean);
if (isIndexed) {
@@ -100,4 +100,21 @@ public void protoSrc_copyListElement() {
LiteTestObjectContainer result = mapper.map(src, LiteTestObjectContainer.class);
assertEquals(ONE_VALUE, result.getObject().getOne());
}
+
+ @Test
+ public void protoSrc_copyDeepListElement() {
+ final String ONE_VALUE = "smthOne";
+
+ ProtobufWithSimpleCollectionContainer.Builder srcBuilder = ProtobufWithSimpleCollectionContainer.newBuilder();
+ ProtobufWithSimpleCollection.Builder innerBuilder = ProtobufWithSimpleCollection.newBuilder();
+ SimpleProtoTestObject.Builder nestedObjectBuilder1 = SimpleProtoTestObject.newBuilder();
+ nestedObjectBuilder1.setOne(ONE_VALUE);
+ SimpleProtoTestObject.Builder nestedObjectBuilder2 = SimpleProtoTestObject.newBuilder();
+ nestedObjectBuilder2.setOne("smthAnother");
+ innerBuilder.addAllObject(Arrays.asList(nestedObjectBuilder1.build(), nestedObjectBuilder2.build()));
+ srcBuilder.setObject(innerBuilder);
+
+ LiteTestObjectContainer result = mapper.map(srcBuilder.build(), LiteTestObjectContainer.class);
+ assertEquals(ONE_VALUE, result.getObject().getOne());
+ }
}
@@ -34,6 +34,16 @@
</mapping>
<mapping wildcard="false">
+ <class-a>org.dozer.vo.proto.ProtoTestObjects.ProtobufWithSimpleCollectionContainer</class-a>
+ <class-b>org.dozer.vo.proto.LiteTestObjectContainer</class-b>
+
+ <field>
+ <a>object.object[0]</a>
+ <b>object</b>
+ </field>
+ </mapping>
+
+ <mapping wildcard="false">
<class-a>org.dozer.vo.proto.ProtoTestObjects.ProtobufWithSimpleCollection</class-a>
<class-b>org.dozer.vo.proto.LiteTestObjectContainer</class-b>

0 comments on commit defbcac

Please sign in to comment.