Browse Source

Merge pull request #156 from philwebb/SPR-9257

# By Phillip Webb
* SPR-9257:
  Resolve Collection element types during conversion
pull/99/merge
Chris Beams 13 years ago
parent
commit
9055a7f810
  1. 14
      spring-core/src/main/java/org/springframework/core/convert/ClassDescriptor.java
  2. 23
      spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,8 +18,11 @@ package org.springframework.core.convert; @@ -18,8 +18,11 @@ package org.springframework.core.convert;
import java.lang.annotation.Annotation;
import org.springframework.core.GenericCollectionTypeResolver;
/**
* @author Keith Donald
* @author Phillip Webb
* @since 3.1
*/
class ClassDescriptor extends AbstractDescriptor {
@ -34,18 +37,21 @@ class ClassDescriptor extends AbstractDescriptor { @@ -34,18 +37,21 @@ class ClassDescriptor extends AbstractDescriptor {
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Class<?> resolveCollectionElementType() {
return null;
return GenericCollectionTypeResolver.getCollectionType((Class) getType());
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Class<?> resolveMapKeyType() {
return null;
return GenericCollectionTypeResolver.getMapKeyType((Class) getType());
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Class<?> resolveMapValueType() {
return null;
return GenericCollectionTypeResolver.getMapValueType((Class) getType());
}
@Override

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

@ -24,6 +24,7 @@ import java.util.ArrayList; @@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -818,4 +819,26 @@ public class TypeDescriptorTests { @@ -818,4 +819,26 @@ public class TypeDescriptorTests {
}
}
@Test
public void elementTypeForCollectionSubclass() throws Exception {
@SuppressWarnings("serial")
class CustomSet extends HashSet<String> {
}
assertEquals(TypeDescriptor.valueOf(CustomSet.class).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class));
assertEquals(TypeDescriptor.forObject(new CustomSet()).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class));
}
@Test
public void elementTypeForMapSubclass() throws Exception {
@SuppressWarnings("serial")
class CustomMap extends HashMap<String, Integer> {
}
assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class));
assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class));
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
}
}

Loading…
Cancel
Save