Skip to content

Commit bfbd25a

Browse files
committed
BeanWrapper auto-growing support for EnumSet / EnumMap
Issue: SPR-12483
1 parent 7635e7b commit bfbd25a

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,17 @@ private Object setDefaultValue(PropertyTokenHolder tokens) {
613613
}
614614

615615
private PropertyValue createDefaultPropertyValue(PropertyTokenHolder tokens) {
616-
Class<?> type = getPropertyTypeDescriptor(tokens.canonicalName).getType();
616+
TypeDescriptor desc = getPropertyTypeDescriptor(tokens.canonicalName);
617+
Class<?> type = desc.getType();
617618
if (type == null) {
618619
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + tokens.canonicalName,
619620
"Could not determine property type for auto-growing a default value");
620621
}
621-
Object defaultValue = newValue(type, tokens.canonicalName);
622+
Object defaultValue = newValue(type, desc, tokens.canonicalName);
622623
return new PropertyValue(tokens.canonicalName, defaultValue);
623624
}
624625

625-
private Object newValue(Class<?> type, String name) {
626+
private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
626627
try {
627628
if (type.isArray()) {
628629
Class<?> componentType = type.getComponentType();
@@ -637,17 +638,20 @@ private Object newValue(Class<?> type, String name) {
637638
}
638639
}
639640
else if (Collection.class.isAssignableFrom(type)) {
640-
return CollectionFactory.createCollection(type, 16);
641+
TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
642+
return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
641643
}
642644
else if (Map.class.isAssignableFrom(type)) {
643-
return CollectionFactory.createMap(type, 16);
645+
TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
646+
return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
644647
}
645648
else {
646649
return type.newInstance();
647650
}
648651
}
649652
catch (Exception ex) {
650-
// TODO Root cause exception context is lost here... should we throw another exception type that preserves context instead?
653+
// TODO: Root cause exception context is lost here; just exception message preserved.
654+
// Should we throw another exception type that preserves context instead?
651655
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
652656
"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
653657
}
@@ -860,7 +864,7 @@ private Object growArrayIfNecessary(Object array, int index, String name) {
860864
Object newArray = Array.newInstance(componentType, index + 1);
861865
System.arraycopy(array, 0, newArray, 0, length);
862866
for (int i = length; i < Array.getLength(newArray); i++) {
863-
Array.set(newArray, i, newValue(componentType, name));
867+
Array.set(newArray, i, newValue(componentType, null, name));
864868
}
865869
// TODO this is not efficient because conversion may create a copy ... set directly because we know it is assignable.
866870
setPropertyValue(name, newArray);
@@ -882,7 +886,7 @@ private void growCollectionIfNecessary(Collection<Object> collection, int index,
882886
Class<?> elementType = GenericCollectionTypeResolver.getCollectionReturnType(pd.getReadMethod(), nestingLevel);
883887
if (elementType != null) {
884888
for (int i = collection.size(); i < index + 1; i++) {
885-
collection.add(newValue(elementType, name));
889+
collection.add(newValue(elementType, null, name));
886890
}
887891
}
888892
}

spring-beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java

Lines changed: 53 additions & 1 deletion
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-2014 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.
@@ -16,8 +16,12 @@
1616

1717
package org.springframework.beans;
1818

19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
1922
import org.junit.Test;
2023

24+
import org.springframework.core.convert.support.DefaultConversionService;
2125
import org.springframework.tests.sample.beans.CustomEnum;
2226
import org.springframework.tests.sample.beans.GenericBean;
2327

@@ -121,4 +125,52 @@ public void testCustomEnumSetWithGetterSetterMismatch() {
121125
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2));
122126
}
123127

128+
@Test
129+
public void testStandardEnumSetWithMultipleValues() {
130+
GenericBean<?> gb = new GenericBean<Object>();
131+
BeanWrapper bw = new BeanWrapperImpl(gb);
132+
bw.setConversionService(new DefaultConversionService());
133+
assertNull(gb.getStandardEnumSet());
134+
bw.setPropertyValue("standardEnumSet", new String[] {"VALUE_1", "VALUE_2"});
135+
assertEquals(2, gb.getStandardEnumSet().size());
136+
assertTrue(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1));
137+
assertTrue(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2));
138+
}
139+
140+
@Test
141+
public void testStandardEnumSetWithAutoGrowing() {
142+
GenericBean<?> gb = new GenericBean<Object>();
143+
BeanWrapper bw = new BeanWrapperImpl(gb);
144+
bw.setAutoGrowNestedPaths(true);
145+
assertNull(gb.getStandardEnumSet());
146+
bw.getPropertyValue("standardEnumSet.class");
147+
assertEquals(0, gb.getStandardEnumSet().size());
148+
}
149+
150+
@Test
151+
public void testStandardEnumMapWithMultipleValues() {
152+
GenericBean<?> gb = new GenericBean<Object>();
153+
BeanWrapper bw = new BeanWrapperImpl(gb);
154+
bw.setConversionService(new DefaultConversionService());
155+
assertNull(gb.getStandardEnumMap());
156+
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
157+
map.put("VALUE_1", 1);
158+
map.put("VALUE_2", 2);
159+
bw.setPropertyValue("standardEnumMap", map);
160+
assertEquals(2, gb.getStandardEnumMap().size());
161+
assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1));
162+
assertEquals(new Integer(2), gb.getStandardEnumMap().get(CustomEnum.VALUE_2));
163+
}
164+
165+
@Test
166+
public void testStandardEnumMapWithAutoGrowing() {
167+
GenericBean<?> gb = new GenericBean<Object>();
168+
BeanWrapper bw = new BeanWrapperImpl(gb);
169+
bw.setAutoGrowNestedPaths(true);
170+
assertNull(gb.getStandardEnumMap());
171+
bw.setPropertyValue("standardEnumMap[VALUE_1]", 1);
172+
assertEquals(1, gb.getStandardEnumMap().size());
173+
assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1));
174+
}
175+
124176
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.Collection;
2121
import java.util.Collections;
22+
import java.util.EnumMap;
23+
import java.util.EnumSet;
2224
import java.util.HashMap;
2325
import java.util.HashSet;
2426
import java.util.Iterator;
@@ -67,6 +69,10 @@ public class GenericBean<T> {
6769

6870
private Set<CustomEnum> customEnumSet;
6971

72+
private EnumSet<CustomEnum> standardEnumSet;
73+
74+
private EnumMap<CustomEnum, Integer> standardEnumMap;
75+
7076
private T genericProperty;
7177

7278
private List<T> genericListProperty;
@@ -267,6 +273,22 @@ public void setCustomEnumSetMismatch(Set<String> customEnumSet) {
267273
}
268274
}
269275

276+
public EnumSet<CustomEnum> getStandardEnumSet() {
277+
return standardEnumSet;
278+
}
279+
280+
public void setStandardEnumSet(EnumSet<CustomEnum> standardEnumSet) {
281+
this.standardEnumSet = standardEnumSet;
282+
}
283+
284+
public EnumMap<CustomEnum, Integer> getStandardEnumMap() {
285+
return standardEnumMap;
286+
}
287+
288+
public void setStandardEnumMap(EnumMap<CustomEnum, Integer> standardEnumMap) {
289+
this.standardEnumMap = standardEnumMap;
290+
}
291+
270292
public static GenericBean createInstance(Set<Integer> integerSet) {
271293
return new GenericBean(integerSet);
272294
}

spring-core/src/main/java/org/springframework/core/CollectionFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ public static <E> Collection<E> createCollection(Class<?> collectionClass, int c
140140
* Create the most appropriate collection for the given collection type.
141141
* @param collectionClass the desired type of the target Collection
142142
* @param elementType the collection's element type, or {@code null} if not known
143+
* (note: only relevant for {@link EnumSet} creation)
143144
* @param capacity the initial capacity
144145
* @return the new Collection instance
146+
* @since 4.1.3
145147
* @see java.util.LinkedHashSet
146148
* @see java.util.TreeSet
147149
* @see java.util.EnumSet
@@ -229,8 +231,10 @@ public static <K, V> Map<K, V> createMap(Class<?> mapClass, int capacity) {
229231
* Create the most approximate map for the given map.
230232
* @param mapClass the desired type of the target Map
231233
* @param keyType the map's key type, or {@code null} if not known
234+
* (note: only relevant for {@link EnumMap} creation)
232235
* @param capacity the initial capacity
233236
* @return the new Map instance
237+
* @since 4.1.3
234238
* @see java.util.LinkedHashMap
235239
* @see java.util.TreeMap
236240
* @see java.util.EnumMap

0 commit comments

Comments
 (0)