diff --git a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java index 9b1eda75eef..9928530dbd4 100644 --- a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -183,10 +183,14 @@ class TypeConverterDelegate { // Value not of required type? if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) { - if (requiredType != null && Collection.class.isAssignableFrom(requiredType) && convertedValue instanceof String) { - TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor(); - if (elementType != null && Enum.class.isAssignableFrom(elementType.getType())) { - convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue); + if (typeDescriptor != null && requiredType != null && Collection.class.isAssignableFrom(requiredType) && + convertedValue instanceof String) { + TypeDescriptor elementTypeDesc = typeDescriptor.getElementTypeDescriptor(); + if (elementTypeDesc != null) { + Class elementType = elementTypeDesc.getType(); + if (Class.class == elementType || Enum.class.isAssignableFrom(elementType)) { + convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue); + } } } if (editor == null) { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java index 53c951ebf1c..c7ac691035a 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 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. @@ -29,6 +29,7 @@ import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; +import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; @@ -52,14 +53,16 @@ import static org.junit.Assert.*; */ public class XmlBeanCollectionTests { - private final DefaultListableBeanFactory beanFactory; + private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - public XmlBeanCollectionTests() { - this.beanFactory = new DefaultListableBeanFactory(); + + @Before + public void loadBeans() { new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions( new ClassPathResource("collections.xml", getClass())); } + @Test public void testCollectionFactoryDefaults() throws Exception { ListFactoryBean listFactory = new ListFactoryBean(); @@ -327,6 +330,15 @@ public class XmlBeanCollectionTests { assertTrue(hasMap.getObjectArray()[1].equals(this.beanFactory.getBean("jenny"))); } + @Test + public void testIntegerArray() throws Exception { + HasMap hasMap = (HasMap) this.beanFactory.getBean("integerArray"); + assertTrue(hasMap.getIntegerArray().length == 3); + assertTrue(hasMap.getIntegerArray()[0].intValue() == 0); + assertTrue(hasMap.getIntegerArray()[1].intValue() == 1); + assertTrue(hasMap.getIntegerArray()[2].intValue() == 2); + } + @Test public void testClassArray() throws Exception { HasMap hasMap = (HasMap) this.beanFactory.getBean("classArray"); @@ -336,12 +348,11 @@ public class XmlBeanCollectionTests { } @Test - public void testIntegerArray() throws Exception { - HasMap hasMap = (HasMap) this.beanFactory.getBean("integerArray"); - assertTrue(hasMap.getIntegerArray().length == 3); - assertTrue(hasMap.getIntegerArray()[0].intValue() == 0); - assertTrue(hasMap.getIntegerArray()[1].intValue() == 1); - assertTrue(hasMap.getIntegerArray()[2].intValue() == 2); + public void testClassList() throws Exception { + HasMap hasMap = (HasMap) this.beanFactory.getBean("classList"); + assertTrue(hasMap.getClassList().size()== 2); + assertTrue(hasMap.getClassList().get(0).equals(String.class)); + assertTrue(hasMap.getClassList().get(1).equals(Exception.class)); } @Test @@ -447,4 +458,5 @@ public class XmlBeanCollectionTests { return obj; } } + } diff --git a/spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java b/spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java index 4639050b2a6..51c5415b674 100644 --- a/spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java +++ b/spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -17,6 +17,7 @@ package org.springframework.tests.sample.beans; import java.util.IdentityHashMap; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -38,9 +39,11 @@ public class HasMap { private Object[] objectArray; + private Integer[] intArray; + private Class[] classArray; - private Integer[] intArray; + private List> classList; private IdentityHashMap identityMap; @@ -81,6 +84,14 @@ public class HasMap { this.objectArray = objectArray; } + public Integer[] getIntegerArray() { + return intArray; + } + + public void setIntegerArray(Integer[] is) { + intArray = is; + } + public Class[] getClassArray() { return classArray; } @@ -89,12 +100,12 @@ public class HasMap { this.classArray = classArray; } - public Integer[] getIntegerArray() { - return intArray; + public List> getClassList() { + return classList; } - public void setIntegerArray(Integer[] is) { - intArray = is; + public void setClassList(List> classList) { + this.classList = classList; } public IdentityHashMap getIdentityMap() { diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml index 73294dfbaed..9047d26b097 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml @@ -288,15 +288,6 @@ - - - - java.lang.String - java.lang.Exception - - - - @@ -307,6 +298,14 @@ + + + + + + + + diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 931cb288916..dd5f180180d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -145,10 +145,9 @@ public class TypeDescriptor implements Serializable { /** * The type of the backing class, method parameter, field, or property * described by this TypeDescriptor. - *

Returns primitive types as-is. - *

See {@link #getObjectType()} for a variation of this operation that - * resolves primitive types to their corresponding Object types if necessary. - * @return the type, or {@code null} if it cannot be determined + *

Returns primitive types as-is. See {@link #getObjectType()} for a + * variation of this operation that resolves primitive types to their + * corresponding Object types if necessary. * @see #getObjectType() */ public Class getType() {