Convert comma-separated string into list of classes analogous to existing support for class array

Issue: SPR-14415
This commit is contained in:
Juergen Hoeller
2016-06-30 14:02:51 +02:00
parent 89396ff01f
commit c4c941c43f
5 changed files with 60 additions and 35 deletions

View File

@@ -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) {

View File

@@ -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;
}
}
}

View File

@@ -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<Class<?>> 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<Class<?>> getClassList() {
return classList;
}
public void setIntegerArray(Integer[] is) {
intArray = is;
public void setClassList(List<Class<?>> classList) {
this.classList = classList;
}
public IdentityHashMap getIdentityMap() {

View File

@@ -288,15 +288,6 @@
</property>
</bean>
<bean id="classArray" class="org.springframework.tests.sample.beans.HasMap">
<property name="classArray">
<list>
<value>java.lang.String</value>
<value>java.lang.Exception</value>
</list>
</property>
</bean>
<bean id="integerArray" class="org.springframework.tests.sample.beans.HasMap">
<property name="integerArray">
<list>
@@ -307,6 +298,14 @@
</property>
</bean>
<bean id="classArray" class="org.springframework.tests.sample.beans.HasMap">
<property name="classArray" value="java.lang.String,java.lang.Exception"/>
</bean>
<bean id="classList" class="org.springframework.tests.sample.beans.HasMap">
<property name="classList" value="java.lang.String,java.lang.Exception"/>
</bean>
<bean id="listFactory" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list>

View File

@@ -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.
* <p>Returns primitive types as-is.
* <p>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
* <p>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() {