|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.web.bind;
|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Array;
|
| 20 | +import java.util.Collection; |
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.Map;
|
22 | 23 |
|
23 | 24 | import org.springframework.beans.MutablePropertyValues;
|
24 | 25 | import org.springframework.beans.PropertyValue;
|
| 26 | +import org.springframework.core.CollectionFactory; |
25 | 27 | import org.springframework.validation.DataBinder;
|
26 | 28 | import org.springframework.web.multipart.MultipartFile;
|
27 | 29 |
|
|
40 | 42 | *
|
41 | 43 | * @author Juergen Hoeller
|
42 | 44 | * @author Scott Andrews
|
| 45 | + * @author Brian Clozel |
43 | 46 | * @since 1.2
|
44 | 47 | * @see #registerCustomEditor
|
45 | 48 | * @see #setAllowedFields
|
@@ -243,26 +246,41 @@ protected void checkFieldMarkers(MutablePropertyValues mpvs) {
|
243 | 246 |
|
244 | 247 | /**
|
245 | 248 | * Determine an empty value for the specified field.
|
246 |
| - * <p>Default implementation returns {@code Boolean.FALSE} |
247 |
| - * for boolean fields and an empty array of array types. |
248 |
| - * Else, {@code null} is used as default. |
| 249 | + * <p>Default implementation returns: |
| 250 | + * <ul> |
| 251 | + * <li>{@code Boolean.FALSE} for boolean fields |
| 252 | + * <li>an empty array for array types |
| 253 | + * <li>Collection implementations for Collection types |
| 254 | + * <li>Map implementations for Map types |
| 255 | + * <li>else, {@code null} is used as default |
| 256 | + * </ul> |
249 | 257 | * @param field the name of the field
|
250 | 258 | * @param fieldType the type of the field
|
251 | 259 | * @return the empty value (for most fields: null)
|
252 | 260 | */
|
253 | 261 | protected Object getEmptyValue(String field, Class<?> fieldType) {
|
254 |
| - if (fieldType != null && boolean.class == fieldType || Boolean.class == fieldType) { |
255 |
| - // Special handling of boolean property. |
256 |
| - return Boolean.FALSE; |
257 |
| - } |
258 |
| - else if (fieldType != null && fieldType.isArray()) { |
259 |
| - // Special handling of array property. |
260 |
| - return Array.newInstance(fieldType.getComponentType(), 0); |
261 |
| - } |
262 |
| - else { |
263 |
| - // Default value: try null. |
264 |
| - return null; |
| 262 | + if (fieldType != null) { |
| 263 | + try { |
| 264 | + if (boolean.class == fieldType || Boolean.class == fieldType) { |
| 265 | + // Special handling of boolean property. |
| 266 | + return Boolean.FALSE; |
| 267 | + } |
| 268 | + else if (fieldType.isArray()) { |
| 269 | + // Special handling of array property. |
| 270 | + return Array.newInstance(fieldType.getComponentType(), 0); |
| 271 | + } |
| 272 | + else if (Collection.class.isAssignableFrom(fieldType)) { |
| 273 | + return CollectionFactory.createCollection(fieldType, 0); |
| 274 | + } |
| 275 | + else if (Map.class.isAssignableFrom(fieldType)) { |
| 276 | + return CollectionFactory.createMap(fieldType, 0); |
| 277 | + } |
| 278 | + } catch (IllegalArgumentException exc) { |
| 279 | + return null; |
| 280 | + } |
265 | 281 | }
|
| 282 | + // Default value: try null. |
| 283 | + return null; |
266 | 284 | }
|
267 | 285 |
|
268 | 286 | /**
|
|
0 commit comments