Skip to content

Commit a33eac3

Browse files
nullzlsbrannen
authored andcommitted
Correctly set auto-growing array's element
Prior to this commit, the implementation of processKeyedProperty() in AbstractNestablePropertyAccessor resulted in a `java.lang.IllegalArgumentException: array element type mismatch` when the property expression had more than one property key and the last key should cause the array to grow automatically. For example, given a property `int[][] multiArray` and property expression `multiArray[1][3]`, the `processKeyedProperty()` method created a new array object and assigned it to `multiArray`; whereas, the new array object should have be assigned to `multiArray[1]`. This commit fixes this issue. Closes gh-26600
1 parent eb68e6a commit a33eac3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ private void processKeyedProperty(PropertyTokenHolder tokens, PropertyValue pv)
305305
Class<?> componentType = propValue.getClass().getComponentType();
306306
Object newArray = Array.newInstance(componentType, arrayIndex + 1);
307307
System.arraycopy(propValue, 0, newArray, 0, length);
308-
setPropertyValue(tokens.actualName, newArray);
309-
propValue = getPropertyValue(tokens.actualName);
308+
int lastKeyIndex = tokens.canonicalName.lastIndexOf('[');
309+
String propName = tokens.canonicalName.substring(0, lastKeyIndex);
310+
setPropertyValue(propName, newArray);
311+
propValue = getPropertyValue(propName);
310312
}
311313
Array.set(propValue, arrayIndex, convertedValue);
312314
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public void getPropertyValueAutoGrowMultiDimensionalArray() {
9999
assertThat(bean.getMultiArray()[0][0]).isInstanceOf(Bean.class);
100100
}
101101

102+
@Test
103+
public void setPropertyValueAutoGrowMultiDimensionalArray() {
104+
wrapper.setPropertyValue("multiArray[2][3]", new Bean());
105+
assertThat(bean.getMultiArray()[2][3]).isInstanceOf(Bean.class);
106+
}
107+
102108
@Test
103109
public void getPropertyValueAutoGrowList() {
104110
assertNotNull(wrapper.getPropertyValue("list[0]"));

0 commit comments

Comments
 (0)