From 5a1f924ac328827c31ced745a65867d4a1feca17 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 28 Sep 2012 15:03:15 -0700 Subject: [PATCH] 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 --- .../core/convert/ClassDescriptor.java | 14 +++++++---- .../core/convert/TypeDescriptorTests.java | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/ClassDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/ClassDescriptor.java index b73d4770180..87f8e066292 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/ClassDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/ClassDescriptor.java @@ -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; 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 { } @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 diff --git a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java index 3e83b173ae0..21d87d9b15e 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java @@ -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 { } } + @Test + public void elementTypeForCollectionSubclass() throws Exception { + @SuppressWarnings("serial") + class CustomSet extends HashSet { + } + + 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 { + } + + 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)); + } + }