Skip to content

Commit c4c941c

Browse files
committed
Convert comma-separated string into list of classes analogous to existing support for class array
Issue: SPR-14415
1 parent 89396ff commit c4c941c

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -183,10 +183,14 @@ public <T> T convertIfNecessary(String propertyName, Object oldValue, Object new
183183

184184
// Value not of required type?
185185
if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {
186-
if (requiredType != null && Collection.class.isAssignableFrom(requiredType) && convertedValue instanceof String) {
187-
TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();
188-
if (elementType != null && Enum.class.isAssignableFrom(elementType.getType())) {
189-
convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
186+
if (typeDescriptor != null && requiredType != null && Collection.class.isAssignableFrom(requiredType) &&
187+
convertedValue instanceof String) {
188+
TypeDescriptor elementTypeDesc = typeDescriptor.getElementTypeDescriptor();
189+
if (elementTypeDesc != null) {
190+
Class<?> elementType = elementTypeDesc.getType();
191+
if (Class.class == elementType || Enum.class.isAssignableFrom(elementType)) {
192+
convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
193+
}
190194
}
191195
}
192196
if (editor == null) {

spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -29,6 +29,7 @@
2929
import java.util.TreeMap;
3030
import java.util.TreeSet;
3131

32+
import org.junit.Before;
3233
import org.junit.Test;
3334

3435
import org.springframework.beans.factory.BeanCreationException;
@@ -52,14 +53,16 @@
5253
*/
5354
public class XmlBeanCollectionTests {
5455

55-
private final DefaultListableBeanFactory beanFactory;
56+
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
5657

57-
public XmlBeanCollectionTests() {
58-
this.beanFactory = new DefaultListableBeanFactory();
58+
59+
@Before
60+
public void loadBeans() {
5961
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
6062
new ClassPathResource("collections.xml", getClass()));
6163
}
6264

65+
6366
@Test
6467
public void testCollectionFactoryDefaults() throws Exception {
6568
ListFactoryBean listFactory = new ListFactoryBean();
@@ -327,6 +330,15 @@ public void testObjectArray() throws Exception {
327330
assertTrue(hasMap.getObjectArray()[1].equals(this.beanFactory.getBean("jenny")));
328331
}
329332

333+
@Test
334+
public void testIntegerArray() throws Exception {
335+
HasMap hasMap = (HasMap) this.beanFactory.getBean("integerArray");
336+
assertTrue(hasMap.getIntegerArray().length == 3);
337+
assertTrue(hasMap.getIntegerArray()[0].intValue() == 0);
338+
assertTrue(hasMap.getIntegerArray()[1].intValue() == 1);
339+
assertTrue(hasMap.getIntegerArray()[2].intValue() == 2);
340+
}
341+
330342
@Test
331343
public void testClassArray() throws Exception {
332344
HasMap hasMap = (HasMap) this.beanFactory.getBean("classArray");
@@ -336,12 +348,11 @@ public void testClassArray() throws Exception {
336348
}
337349

338350
@Test
339-
public void testIntegerArray() throws Exception {
340-
HasMap hasMap = (HasMap) this.beanFactory.getBean("integerArray");
341-
assertTrue(hasMap.getIntegerArray().length == 3);
342-
assertTrue(hasMap.getIntegerArray()[0].intValue() == 0);
343-
assertTrue(hasMap.getIntegerArray()[1].intValue() == 1);
344-
assertTrue(hasMap.getIntegerArray()[2].intValue() == 2);
351+
public void testClassList() throws Exception {
352+
HasMap hasMap = (HasMap) this.beanFactory.getBean("classList");
353+
assertTrue(hasMap.getClassList().size()== 2);
354+
assertTrue(hasMap.getClassList().get(0).equals(String.class));
355+
assertTrue(hasMap.getClassList().get(1).equals(Exception.class));
345356
}
346357

347358
@Test
@@ -447,4 +458,5 @@ public Object getObject() {
447458
return obj;
448459
}
449460
}
461+
450462
}

spring-beans/src/test/java/org/springframework/tests/sample/beans/HasMap.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.tests.sample.beans;
1818

1919
import java.util.IdentityHashMap;
20+
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Properties;
2223
import java.util.Set;
@@ -38,9 +39,11 @@ public class HasMap {
3839

3940
private Object[] objectArray;
4041

42+
private Integer[] intArray;
43+
4144
private Class<?>[] classArray;
4245

43-
private Integer[] intArray;
46+
private List<Class<?>> classList;
4447

4548
private IdentityHashMap identityMap;
4649

@@ -81,6 +84,14 @@ public void setObjectArray(Object[] objectArray) {
8184
this.objectArray = objectArray;
8285
}
8386

87+
public Integer[] getIntegerArray() {
88+
return intArray;
89+
}
90+
91+
public void setIntegerArray(Integer[] is) {
92+
intArray = is;
93+
}
94+
8495
public Class<?>[] getClassArray() {
8596
return classArray;
8697
}
@@ -89,12 +100,12 @@ public void setClassArray(Class<?>[] classArray) {
89100
this.classArray = classArray;
90101
}
91102

92-
public Integer[] getIntegerArray() {
93-
return intArray;
103+
public List<Class<?>> getClassList() {
104+
return classList;
94105
}
95106

96-
public void setIntegerArray(Integer[] is) {
97-
intArray = is;
107+
public void setClassList(List<Class<?>> classList) {
108+
this.classList = classList;
98109
}
99110

100111
public IdentityHashMap getIdentityMap() {

spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml

+8-9
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,6 @@
288288
</property>
289289
</bean>
290290

291-
<bean id="classArray" class="org.springframework.tests.sample.beans.HasMap">
292-
<property name="classArray">
293-
<list>
294-
<value>java.lang.String</value>
295-
<value>java.lang.Exception</value>
296-
</list>
297-
</property>
298-
</bean>
299-
300291
<bean id="integerArray" class="org.springframework.tests.sample.beans.HasMap">
301292
<property name="integerArray">
302293
<list>
@@ -307,6 +298,14 @@
307298
</property>
308299
</bean>
309300

301+
<bean id="classArray" class="org.springframework.tests.sample.beans.HasMap">
302+
<property name="classArray" value="java.lang.String,java.lang.Exception"/>
303+
</bean>
304+
305+
<bean id="classList" class="org.springframework.tests.sample.beans.HasMap">
306+
<property name="classList" value="java.lang.String,java.lang.Exception"/>
307+
</bean>
308+
310309
<bean id="listFactory" class="org.springframework.beans.factory.config.ListFactoryBean">
311310
<property name="sourceList">
312311
<list>

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -145,10 +145,9 @@ public Class<?> getObjectType() {
145145
/**
146146
* The type of the backing class, method parameter, field, or property
147147
* described by this TypeDescriptor.
148-
* <p>Returns primitive types as-is.
149-
* <p>See {@link #getObjectType()} for a variation of this operation that
150-
* resolves primitive types to their corresponding Object types if necessary.
151-
* @return the type, or {@code null} if it cannot be determined
148+
* <p>Returns primitive types as-is. See {@link #getObjectType()} for a
149+
* variation of this operation that resolves primitive types to their
150+
* corresponding Object types if necessary.
152151
* @see #getObjectType()
153152
*/
154153
public Class<?> getType() {

0 commit comments

Comments
 (0)