Skip to content

Commit c84cccf

Browse files
author
Keith Donald
committed
revised TypeDescriptor NULL and element/mapKey/mapValue type semantics
1 parent a2a4929 commit c84cccf

File tree

42 files changed

+732
-758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+732
-758
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public <T> T convertIfNecessary(String propertyName, Object oldValue, Object new
141141
// Value not of required type?
142142
if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {
143143
if (requiredType != null && Collection.class.isAssignableFrom(requiredType) && convertedValue instanceof String) {
144-
Class elemType = typeDescriptor.getElementType();
145-
if (elemType != null && Enum.class.isAssignableFrom(elemType)) {
144+
TypeDescriptor elementType = typeDescriptor.getElementType();
145+
if (elementType != null && Enum.class.isAssignableFrom(elementType.getType())) {
146146
convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
147147
}
148148
}
@@ -459,8 +459,8 @@ protected Collection convertToTypedCollection(
459459
return original;
460460
}
461461

462-
Class elementType = typeDescriptor.getElementType();
463-
if (elementType == Object.class && originalAllowed &&
462+
TypeDescriptor elementType = typeDescriptor.getElementType();
463+
if (elementType == null && originalAllowed &&
464464
!this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
465465
return original;
466466
}
@@ -506,7 +506,7 @@ protected Collection convertToTypedCollection(
506506
Object element = it.next();
507507
String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
508508
Object convertedElement = convertIfNecessary(
509-
indexedPropertyName, null, element, elementType, typeDescriptor.getElementTypeDescriptor());
509+
indexedPropertyName, null, element, elementType != null ? elementType.getType() : null , typeDescriptor.getElementType());
510510
try {
511511
convertedCopy.add(convertedElement);
512512
}
@@ -531,9 +531,9 @@ protected Map convertToTypedMap(
531531
return original;
532532
}
533533

534-
Class keyType = typeDescriptor.getMapKeyType();
535-
Class valueType = typeDescriptor.getMapValueType();
536-
if (keyType == Object.class && valueType == Object.class && originalAllowed &&
534+
TypeDescriptor keyType = typeDescriptor.getMapKeyType();
535+
TypeDescriptor valueType = typeDescriptor.getMapValueType();
536+
if (keyType == null && valueType == null && originalAllowed &&
537537
!this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
538538
return original;
539539
}
@@ -579,8 +579,8 @@ protected Map convertToTypedMap(
579579
Object key = entry.getKey();
580580
Object value = entry.getValue();
581581
String keyedPropertyName = buildKeyedPropertyName(propertyName, key);
582-
Object convertedKey = convertIfNecessary(keyedPropertyName, null, key, keyType, typeDescriptor.getMapKeyTypeDescriptor());
583-
Object convertedValue = convertIfNecessary(keyedPropertyName, null, value, valueType, typeDescriptor.getMapValueTypeDescriptor());
582+
Object convertedKey = convertIfNecessary(keyedPropertyName, null, key, keyType != null ? keyType.getType() : null, typeDescriptor.getMapKeyType());
583+
Object convertedValue = convertIfNecessary(keyedPropertyName, null, value, valueType!= null ? valueType.getType() : null, typeDescriptor.getMapValueType());
584584
try {
585585
convertedCopy.put(convertedKey, convertedValue);
586586
}

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ abstract class AbstractDescriptor {
2424
private final Class<?> type;
2525

2626
public AbstractDescriptor(Class<?> type) {
27+
//if (type == null) {
28+
// throw new IllegalArgumentException("type cannot be null");
29+
//}
2730
this.type = type;
2831
}
2932

@@ -33,74 +36,70 @@ public Class<?> getType() {
3336

3437
public TypeDescriptor getElementType() {
3538
if (isCollection()) {
36-
Class<?> elementType = wildcard(getCollectionElementClass());
37-
return new TypeDescriptor(nested(elementType, 0));
39+
Class<?> elementType = resolveCollectionElementType();
40+
return elementType != null ? new TypeDescriptor(nested(elementType, 0)) : null;
3841
} else if (isArray()) {
3942
Class<?> elementType = getType().getComponentType();
4043
return new TypeDescriptor(nested(elementType, 0));
4144
} else {
42-
return TypeDescriptor.NULL;
45+
return null;
4346
}
4447
}
4548

4649
public TypeDescriptor getMapKeyType() {
4750
if (isMap()) {
48-
Class<?> keyType = wildcard(getMapKeyClass());
49-
return new TypeDescriptor(nested(keyType, 0));
51+
Class<?> keyType = resolveMapKeyType();
52+
return keyType != null ? new TypeDescriptor(nested(keyType, 0)) : null;
5053
} else {
51-
return TypeDescriptor.NULL;
54+
return null;
5255
}
5356
}
5457

5558
public TypeDescriptor getMapValueType() {
5659
if (isMap()) {
57-
Class<?> valueType = wildcard(getMapValueClass());
58-
return new TypeDescriptor(nested(valueType, 1));
60+
Class<?> valueType = resolveMapValueType();
61+
return valueType != null ? new TypeDescriptor(nested(valueType, 1)) : null;
5962
} else {
60-
return TypeDescriptor.NULL;
63+
return null;
6164
}
6265
}
6366

6467
public abstract Annotation[] getAnnotations();
6568

6669
public AbstractDescriptor nested() {
6770
if (isCollection()) {
68-
return nested(wildcard(getCollectionElementClass()), 0);
71+
return nested(resolveCollectionElementType(), 0);
6972
} else if (isArray()) {
7073
return nested(getType().getComponentType(), 0);
7174
} else if (isMap()) {
72-
return nested(wildcard(getMapValueClass()), 1);
75+
return nested(resolveMapValueType(), 1);
7376
} else {
7477
throw new IllegalStateException("Not a collection, array, or map: cannot resolve nested value types");
7578
}
7679
}
7780

7881
// subclassing hooks
7982

80-
protected abstract Class<?> getCollectionElementClass();
83+
protected abstract Class<?> resolveCollectionElementType();
8184

82-
protected abstract Class<?> getMapKeyClass();
85+
protected abstract Class<?> resolveMapKeyType();
8386

84-
protected abstract Class<?> getMapValueClass();
87+
protected abstract Class<?> resolveMapValueType();
8588

8689
protected abstract AbstractDescriptor nested(Class<?> type, int typeIndex);
8790

8891
// internal helpers
8992

9093
private boolean isCollection() {
91-
return Collection.class.isAssignableFrom(getType());
94+
return getType() != null && Collection.class.isAssignableFrom(getType());
9295
}
9396

9497
private boolean isArray() {
95-
return getType().isArray();
98+
return getType() != null && getType().isArray();
9699
}
97100

98101
private boolean isMap() {
99-
return Map.class.isAssignableFrom(getType());
100-
}
101-
102-
private Class<?> wildcard(Class<?> type) {
103-
return type != null ? type : Object.class;
102+
return getType() != null && Map.class.isAssignableFrom(getType());
104103
}
105104

106105
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ public Annotation[] getAnnotations() {
5252
}
5353

5454
@Override
55-
protected Class<?> getCollectionElementClass() {
55+
protected Class<?> resolveCollectionElementType() {
5656
return GenericCollectionTypeResolver.getCollectionParameterType(methodParameter);
5757
}
5858

5959
@Override
60-
protected Class<?> getMapKeyClass() {
60+
protected Class<?> resolveMapKeyType() {
6161
return GenericCollectionTypeResolver.getMapKeyParameterType(methodParameter);
6262
}
6363

6464
@Override
65-
protected Class<?> getMapValueClass() {
65+
protected Class<?> resolveMapValueType() {
6666
return GenericCollectionTypeResolver.getMapValueParameterType(methodParameter);
6767
}
6868

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ public Annotation[] getAnnotations() {
2929
}
3030

3131
@Override
32-
protected Class<?> getCollectionElementClass() {
33-
return Object.class;
32+
protected Class<?> resolveCollectionElementType() {
33+
return null;
3434
}
3535

3636
@Override
37-
protected Class<?> getMapKeyClass() {
38-
return Object.class;
37+
protected Class<?> resolveMapKeyType() {
38+
return null;
3939
}
4040

4141
@Override
42-
protected Class<?> getMapValueClass() {
43-
return Object.class;
42+
protected Class<?> resolveMapValueType() {
43+
return null;
4444
}
4545

4646
@Override

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

Lines changed: 0 additions & 116 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface ConversionService {
4141
* @throws ConversionException if an exception occurred
4242
*/
4343
<T> T convert(Object source, Class<T> targetType);
44-
44+
4545
/**
4646
* Returns true if objects of sourceType can be converted to the targetType.
4747
* The TypeDescriptors provide additional context about the field locations where conversion would occur, often object property locations.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ public Annotation[] getAnnotations() {
3636
}
3737

3838
@Override
39-
protected Class<?> getCollectionElementClass() {
39+
protected Class<?> resolveCollectionElementType() {
4040
return GenericCollectionTypeResolver.getCollectionFieldType(this.field, this.nestingLevel);
4141
}
4242

4343
@Override
44-
protected Class<?> getMapKeyClass() {
44+
protected Class<?> resolveMapKeyType() {
4545
return GenericCollectionTypeResolver.getMapKeyFieldType(this.field, this.nestingLevel);
4646
}
4747

4848
@Override
49-
protected Class<?> getMapValueClass() {
49+
protected Class<?> resolveMapValueType() {
5050
return GenericCollectionTypeResolver.getMapValueFieldType(this.field, this.nestingLevel);
5151
}
5252

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ public Annotation[] getAnnotations() {
4343
}
4444

4545
@Override
46-
protected Class<?> getCollectionElementClass() {
46+
protected Class<?> resolveCollectionElementType() {
4747
return GenericCollectionTypeResolver.getCollectionParameterType(methodParameter);
4848
}
4949

5050
@Override
51-
protected Class<?> getMapKeyClass() {
51+
protected Class<?> resolveMapKeyType() {
5252
return GenericCollectionTypeResolver.getMapKeyParameterType(methodParameter);
5353
}
5454

5555
@Override
56-
protected Class<?> getMapValueClass() {
56+
protected Class<?> resolveMapValueType() {
5757
return GenericCollectionTypeResolver.getMapValueParameterType(methodParameter);
5858
}
5959

0 commit comments

Comments
 (0)