Skip to content

Commit 5a1f924

Browse files
philwebbcbeams
authored andcommitted
Resolve Collection element types during conversion
TypeDescriptor.valueOf now uses GenericCollectionTypeResolver to resolve Collection and Map element types whereas previously this information was simply not supported, i.e. null. Issue: SPR-9257
1 parent 99b2f6a commit 5a1f924

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 10 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.
@@ -18,8 +18,11 @@
1818

1919
import java.lang.annotation.Annotation;
2020

21+
import org.springframework.core.GenericCollectionTypeResolver;
22+
2123
/**
2224
* @author Keith Donald
25+
* @author Phillip Webb
2326
* @since 3.1
2427
*/
2528
class ClassDescriptor extends AbstractDescriptor {
@@ -34,18 +37,21 @@ public Annotation[] getAnnotations() {
3437
}
3538

3639
@Override
40+
@SuppressWarnings({ "unchecked", "rawtypes" })
3741
protected Class<?> resolveCollectionElementType() {
38-
return null;
42+
return GenericCollectionTypeResolver.getCollectionType((Class) getType());
3943
}
4044

4145
@Override
46+
@SuppressWarnings({ "unchecked", "rawtypes" })
4247
protected Class<?> resolveMapKeyType() {
43-
return null;
48+
return GenericCollectionTypeResolver.getMapKeyType((Class) getType());
4449
}
4550

4651
@Override
52+
@SuppressWarnings({ "unchecked", "rawtypes" })
4753
protected Class<?> resolveMapValueType() {
48-
return null;
54+
return GenericCollectionTypeResolver.getMapValueType((Class) getType());
4955
}
5056

5157
@Override

spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.Date;
2626
import java.util.HashMap;
27+
import java.util.HashSet;
2728
import java.util.List;
2829
import java.util.Map;
2930

@@ -818,4 +819,26 @@ public void testUpCastNotSuper() throws Exception {
818819
}
819820
}
820821

822+
@Test
823+
public void elementTypeForCollectionSubclass() throws Exception {
824+
@SuppressWarnings("serial")
825+
class CustomSet extends HashSet<String> {
826+
}
827+
828+
assertEquals(TypeDescriptor.valueOf(CustomSet.class).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class));
829+
assertEquals(TypeDescriptor.forObject(new CustomSet()).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class));
830+
}
831+
832+
@Test
833+
public void elementTypeForMapSubclass() throws Exception {
834+
@SuppressWarnings("serial")
835+
class CustomMap extends HashMap<String, Integer> {
836+
}
837+
838+
assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class));
839+
assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
840+
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class));
841+
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
842+
}
843+
821844
}

0 commit comments

Comments
 (0)