Skip to content

Commit 5e3a520

Browse files
author
Keith Donald
committed
restored TypeDescriptor getElementType, getMapKeyType, and getMapValueType compatibility; StringToCollection and Array Converters are now conditional and check targetElementType if present; TypeDesciptor#isAssignable no longer bothers with element type and map key/value types in checking assignability for consistency elsewhere; improved javadoc
1 parent a1a7c32 commit 5e3a520

File tree

21 files changed

+244
-230
lines changed

21 files changed

+244
-230
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public <T> T convertIfNecessary(String propertyName, Object oldValue, Object new
147147
// Value not of required type?
148148
if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {
149149
if (requiredType != null && Collection.class.isAssignableFrom(requiredType) && convertedValue instanceof String) {
150-
TypeDescriptor elementType = typeDescriptor.getElementType();
150+
TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();
151151
if (elementType != null && Enum.class.isAssignableFrom(elementType.getType())) {
152152
convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
153153
}
@@ -465,7 +465,7 @@ protected Collection convertToTypedCollection(
465465
return original;
466466
}
467467
typeDescriptor = typeDescriptor.narrow(original);
468-
TypeDescriptor elementType = typeDescriptor.getElementType();
468+
TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();
469469
if (elementType == null && originalAllowed &&
470470
!this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
471471
return original;
@@ -512,7 +512,7 @@ protected Collection convertToTypedCollection(
512512
Object element = it.next();
513513
String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
514514
Object convertedElement = convertIfNecessary(
515-
indexedPropertyName, null, element, elementType != null ? elementType.getType() : null , typeDescriptor.getElementType());
515+
indexedPropertyName, null, element, elementType != null ? elementType.getType() : null , typeDescriptor.getElementTypeDescriptor());
516516
try {
517517
convertedCopy.add(convertedElement);
518518
}
@@ -537,8 +537,8 @@ protected Map convertToTypedMap(
537537
return original;
538538
}
539539
typeDescriptor = typeDescriptor.narrow(original);
540-
TypeDescriptor keyType = typeDescriptor.getMapKeyType();
541-
TypeDescriptor valueType = typeDescriptor.getMapValueType();
540+
TypeDescriptor keyType = typeDescriptor.getMapKeyTypeDescriptor();
541+
TypeDescriptor valueType = typeDescriptor.getMapValueTypeDescriptor();
542542
if (keyType == null && valueType == null && originalAllowed &&
543543
!this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
544544
return original;
@@ -585,8 +585,8 @@ protected Map convertToTypedMap(
585585
Object key = entry.getKey();
586586
Object value = entry.getValue();
587587
String keyedPropertyName = buildKeyedPropertyName(propertyName, key);
588-
Object convertedKey = convertIfNecessary(keyedPropertyName, null, key, keyType != null ? keyType.getType() : null, typeDescriptor.getMapKeyType());
589-
Object convertedValue = convertIfNecessary(keyedPropertyName, null, value, valueType!= null ? valueType.getType() : null, typeDescriptor.getMapValueType());
588+
Object convertedKey = convertIfNecessary(keyedPropertyName, null, key, keyType != null ? keyType.getType() : null, typeDescriptor.getMapKeyTypeDescriptor());
589+
Object convertedValue = convertIfNecessary(keyedPropertyName, null, value, valueType!= null ? valueType.getType() : null, typeDescriptor.getMapValueTypeDescriptor());
590590
try {
591591
convertedCopy.put(convertedKey, convertedValue);
592592
}

org.springframework.core/src/main/java/org/springframework/core/convert/AbstractDescriptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public Class<?> getType() {
3434
return type;
3535
}
3636

37-
public TypeDescriptor getElementType() {
37+
public TypeDescriptor getElementTypeDescriptor() {
3838
if (isCollection()) {
3939
Class<?> elementType = resolveCollectionElementType();
4040
return elementType != null ? new TypeDescriptor(nested(elementType, 0)) : null;
@@ -46,7 +46,7 @@ public TypeDescriptor getElementType() {
4646
}
4747
}
4848

49-
public TypeDescriptor getMapKeyType() {
49+
public TypeDescriptor getMapKeyTypeDescriptor() {
5050
if (isMap()) {
5151
Class<?> keyType = resolveMapKeyType();
5252
return keyType != null ? new TypeDescriptor(nested(keyType, 0)) : null;
@@ -55,7 +55,7 @@ public TypeDescriptor getMapKeyType() {
5555
}
5656
}
5757

58-
public TypeDescriptor getMapValueType() {
58+
public TypeDescriptor getMapValueTypeDescriptor() {
5959
if (isMap()) {
6060
Class<?> valueType = resolveMapValueType();
6161
return valueType != null ? new TypeDescriptor(nested(valueType, 1)) : null;

org.springframework.core/src/main/java/org/springframework/core/convert/FieldDescriptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private FieldDescriptor(Class<?> type, Field field, int nestingLevel, int typeIn
6161
super(type);
6262
this.field = field;
6363
this.nestingLevel = nestingLevel;
64+
// TODO typeIndex is not preserved at current nestingLevel is not preserved: see SPR-8394
6465
}
6566

6667
}

org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 100 additions & 117 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
5555
}
5656
int length = Array.getLength(source);
5757
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), length);
58-
if (targetType.getElementType() == null) {
58+
if (targetType.getElementTypeDescriptor() == null) {
5959
for (int i = 0; i < length; i++) {
6060
Object sourceElement = Array.get(source, i);
6161
target.add(sourceElement);
6262
}
6363
} else {
6464
for (int i = 0; i < length; i++) {
6565
Object sourceElement = Array.get(source, i);
66-
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementType(sourceElement), targetType.getElementType());
66+
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor());
6767
target.add(targetElement);
6868
}
6969
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
5353
return null;
5454
}
5555
Collection<?> sourceCollection = (Collection<?>) source;
56-
Object array = Array.newInstance(targetType.getElementType().getType(), sourceCollection.size());
56+
Object array = Array.newInstance(targetType.getElementTypeDescriptor().getType(), sourceCollection.size());
5757
int i = 0;
5858
for (Object sourceElement : sourceCollection) {
59-
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementType(sourceElement), targetType.getElementType());
59+
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor());
6060
Array.set(array, i++, targetElement);
6161
}
6262
return array;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
5555
}
5656
Collection<?> sourceCollection = (Collection<?>) source;
5757
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
58-
if (targetType.getElementType() == null) {
58+
if (targetType.getElementTypeDescriptor() == null) {
5959
for (Object element : sourceCollection) {
6060
target.add(element);
6161
}
6262
} else {
6363
for (Object sourceElement : sourceCollection) {
64-
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementType(sourceElement), targetType.getElementType());
64+
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor());
6565
target.add(targetElement);
6666
}
6767
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
5151
return null;
5252
}
5353
Object firstElement = sourceCollection.iterator().next();
54-
return this.conversionService.convert(firstElement, sourceType.elementType(firstElement), targetType);
54+
return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
5555
}
5656

5757
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
5858
if (i > 0) {
5959
sb.append(DELIMITER);
6060
}
61-
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementType(sourceElement), targetType);
61+
Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType);
6262
sb.append(targetElement);
6363
i++;
6464
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
5858
for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
5959
Object sourceKey = entry.getKey();
6060
Object sourceValue = entry.getValue();
61-
Object targetKey = convertKey(sourceKey, sourceType, targetType.getMapKeyType());
62-
Object targetValue = convertValue(sourceValue, sourceType, targetType.getMapValueType());
61+
Object targetKey = convertKey(sourceKey, sourceType, targetType.getMapKeyTypeDescriptor());
62+
Object targetValue = convertValue(sourceValue, sourceType, targetType.getMapValueTypeDescriptor());
6363
targetMap.put(targetKey, targetValue);
6464
}
6565
return targetMap;
@@ -71,14 +71,14 @@ private Object convertKey(Object sourceKey, TypeDescriptor sourceType, TypeDescr
7171
if (targetType == null) {
7272
return sourceKey;
7373
}
74-
return this.conversionService.convert(sourceKey, sourceType.mapKeyType(sourceKey), targetType);
74+
return this.conversionService.convert(sourceKey, sourceType.mapKeyTypeDescriptor(sourceKey), targetType);
7575
}
7676

7777
private Object convertValue(Object sourceValue, TypeDescriptor sourceType, TypeDescriptor targetType) {
7878
if (targetType == null) {
7979
return sourceValue;
8080
}
81-
return this.conversionService.convert(sourceValue, sourceType.mapValueType(sourceValue), targetType);
81+
return this.conversionService.convert(sourceValue, sourceType.mapValueTypeDescriptor(sourceValue), targetType);
8282
}
8383

8484
}

0 commit comments

Comments
 (0)