-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Closed
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)type: bugA general bugA general bug
Milestone
Description
Sandu Turcan opened SPR-9284 and commented
We use a thirdparty library which has a class that happens to extend java.util.Properties.
When assigning an instance of this class to a property, MapToMapConverter tries to create a new instance, copy all the entries, and if none of the keys or values were converted returns the original value.
The problem is that in this particular case the default constructor fails to create the throwaway instance.
I changed the convert method to the following:
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
boolean copyRequired = !targetType.getType().isInstance(source);
Map<Object, Object> sourceMap = (Map<Object, Object>) source;
if (!copyRequired && (sourceMap.isEmpty() || targetType.equals(sourceType))) {
return sourceMap;
}
Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
Object sourceKey = entry.getKey();
Object sourceValue = entry.getValue();
Object targetKey = convertKey(sourceKey, sourceType, targetType.getMapKeyTypeDescriptor());
Object targetValue = convertValue(sourceValue, sourceType, targetType.getMapValueTypeDescriptor());
targetMap.put(targetKey, targetValue);
if (sourceKey != targetKey || sourceValue != targetValue) {
copyRequired = true;
}
}
return (copyRequired ? targetMap : sourceMap);
}
The actual change:
if (!copyRequired && (sourceMap.isEmpty() || targetType.equals(sourceType))) {
Sub-tasks:
- Backport "Allow MapToMap conversion even without a default constructor" [SPR-9880] #14513 Backport "Allow MapToMap conversion even without a default constructor"
Issue Links:
- CollectionFactory does not chain exceptions [SPR-9285] #13923 CollectionFactory does not chain exceptions
Metadata
Metadata
Assignees
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)type: bugA general bugA general bug