@@ -142,6 +142,10 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
142
142
*/
143
143
protected static final Log logger = LogFactory .getLog (DataBinder .class );
144
144
145
+ /** Internal constant for constructor binding via "[]". */
146
+ private static final int NO_INDEX = -1 ;
147
+
148
+
145
149
@ Nullable
146
150
private Object target ;
147
151
@@ -1056,15 +1060,17 @@ private List<?> createList(
1056
1060
return null ;
1057
1061
}
1058
1062
1059
- int size = (indexes .last () < this .autoGrowCollectionLimit ? indexes .last () + 1 : 0 );
1063
+ int lastIndex = Math .max (indexes .last (), 0 );
1064
+ int size = (lastIndex < this .autoGrowCollectionLimit ? lastIndex + 1 : 0 );
1060
1065
List <?> list = (List <?>) CollectionFactory .createCollection (paramType , size );
1061
1066
for (int i = 0 ; i < size ; i ++) {
1062
1067
list .add (null );
1063
1068
}
1064
1069
1065
1070
for (int index : indexes ) {
1066
- String indexedPath = paramPath + "[" + index + "]" ;
1067
- list .set (index , createIndexedValue (paramPath , paramType , elementType , indexedPath , valueResolver ));
1071
+ String indexedPath = paramPath + "[" + (index != NO_INDEX ? index : "" ) + "]" ;
1072
+ list .set (Math .max (index , 0 ),
1073
+ createIndexedValue (paramPath , paramType , elementType , indexedPath , valueResolver ));
1068
1074
}
1069
1075
1070
1076
return list ;
@@ -1108,12 +1114,14 @@ private <V> V[] createArray(
1108
1114
return null ;
1109
1115
}
1110
1116
1111
- int size = (indexes .last () < this .autoGrowCollectionLimit ? indexes .last () + 1 : 0 );
1117
+ int lastIndex = Math .max (indexes .last (), 0 );
1118
+ int size = (lastIndex < this .autoGrowCollectionLimit ? lastIndex + 1 : 0 );
1112
1119
V [] array = (V []) Array .newInstance (elementType .resolve (), size );
1113
1120
1114
1121
for (int index : indexes ) {
1115
- String indexedPath = paramPath + "[" + index + "]" ;
1116
- array [index ] = createIndexedValue (paramPath , paramType , elementType , indexedPath , valueResolver );
1122
+ String indexedPath = paramPath + "[" + (index != NO_INDEX ? index : "" ) + "]" ;
1123
+ array [Math .max (index , 0 )] =
1124
+ createIndexedValue (paramPath , paramType , elementType , indexedPath , valueResolver );
1117
1125
}
1118
1126
1119
1127
return array ;
@@ -1124,13 +1132,20 @@ private static SortedSet<Integer> getIndexes(String paramPath, ValueResolver val
1124
1132
SortedSet <Integer > indexes = null ;
1125
1133
for (String name : valueResolver .getNames ()) {
1126
1134
if (name .startsWith (paramPath + "[" )) {
1127
- int endIndex = name .indexOf (']' , paramPath .length () + 2 );
1128
- String rawIndex = name .substring (paramPath .length () + 1 , endIndex );
1129
- if (StringUtils .hasLength (rawIndex )) {
1130
- int index = Integer .parseInt (rawIndex );
1131
- indexes = (indexes != null ? indexes : new TreeSet <>());
1132
- indexes .add (index );
1135
+ int index ;
1136
+ if (paramPath .length () + 2 == name .length ()) {
1137
+ if (!name .endsWith ("[]" )) {
1138
+ continue ;
1139
+ }
1140
+ index = NO_INDEX ;
1141
+ }
1142
+ else {
1143
+ int endIndex = name .indexOf (']' , paramPath .length () + 2 );
1144
+ String indexValue = name .substring (paramPath .length () + 1 , endIndex );
1145
+ index = Integer .parseInt (indexValue );
1133
1146
}
1147
+ indexes = (indexes != null ? indexes : new TreeSet <>());
1148
+ indexes .add (index );
1134
1149
}
1135
1150
}
1136
1151
return indexes ;
0 commit comments