Skip to content

Commit e3136e6

Browse files
philwebbcbeams
authored andcommitted
Allow MapToMap conversion even w/out default ctor
MapToMap conversion now works when the target map does not have a default constructor (as long as a new map copy is not required). Issue: SPR-9284 Backport-Commit: 38c4393
1 parent ced6bb4 commit e3136e6

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.core.convert.support;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collections;
21+
import java.util.List;
2022
import java.util.Map;
2123
import java.util.Set;
2224

@@ -62,18 +64,25 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
6264
if (!copyRequired && sourceMap.isEmpty()) {
6365
return sourceMap;
6466
}
65-
Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
67+
List<MapEntry> targetEntries = new ArrayList<MapEntry>(sourceMap.size());
6668
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
6769
Object sourceKey = entry.getKey();
6870
Object sourceValue = entry.getValue();
6971
Object targetKey = convertKey(sourceKey, sourceType, targetType.getMapKeyTypeDescriptor());
7072
Object targetValue = convertValue(sourceValue, sourceType, targetType.getMapValueTypeDescriptor());
71-
targetMap.put(targetKey, targetValue);
73+
targetEntries.add(new MapEntry(targetKey, targetValue));
7274
if (sourceKey != targetKey || sourceValue != targetValue) {
7375
copyRequired = true;
7476
}
7577
}
76-
return (copyRequired ? targetMap : sourceMap);
78+
if(!copyRequired) {
79+
return sourceMap;
80+
}
81+
Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
82+
for (MapEntry entry : targetEntries) {
83+
entry.addToMap(targetMap);
84+
}
85+
return targetMap;
7786
}
7887

7988
// internal helpers
@@ -102,4 +111,19 @@ private Object convertValue(Object sourceValue, TypeDescriptor sourceType, TypeD
102111
return this.conversionService.convert(sourceValue, sourceType.getMapValueTypeDescriptor(sourceValue), targetType);
103112
}
104113

114+
private static class MapEntry {
115+
116+
private Object key;
117+
private Object value;
118+
119+
public MapEntry(Object key, Object value) {
120+
this.key = key;
121+
this.value = value;
122+
}
123+
124+
public void addToMap(Map<Object, Object> map) {
125+
map.put(key, value);
126+
}
127+
}
128+
105129
}

org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.convert.support;
1818

1919
import java.util.Arrays;
20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.LinkedHashMap;
2223
import java.util.List;
@@ -200,4 +201,26 @@ public void emptyMapDifferentTargetImplType() throws Exception {
200201

201202
public LinkedHashMap<String, String> emptyMapDifferentTarget;
202203

204+
@Test
205+
public void noDefaultConstructorCopyNotRequired() throws Exception {
206+
// SPR-9284
207+
NoDefaultConstructorMap<String, Integer> map = new NoDefaultConstructorMap<String,Integer>(
208+
Collections.<String, Integer> singletonMap("1", 1));
209+
TypeDescriptor sourceType = TypeDescriptor.map(NoDefaultConstructorMap.class,
210+
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
211+
TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class,
212+
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
213+
assertTrue(conversionService.canConvert(sourceType, targetType));
214+
@SuppressWarnings("unchecked")
215+
Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType);
216+
assertEquals(map, result);
217+
assertEquals(NoDefaultConstructorMap.class, result.getClass());
218+
}
219+
220+
public static class NoDefaultConstructorMap<K, V> extends HashMap<K, V> {
221+
public NoDefaultConstructorMap(Map<? extends K, ? extends V> m) {
222+
super(m);
223+
}
224+
}
225+
203226
}

0 commit comments

Comments
 (0)