From c63599f88e38ba47dff888a38984b4df9d3e617a Mon Sep 17 00:00:00 2001 From: nullzl Date: Wed, 24 Feb 2021 11:19:58 +0800 Subject: [PATCH 1/2] Correctly set auto-growing array's element AbstractNestablePropertyAccessor.processKeyedProperty implementations throw IllegalArgumentException, when the property expression has more than one property key and the last key causes array to grow automatically. This commit fix it. --- .../beans/AbstractNestablePropertyAccessor.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java index 16ab258a14e9..25ab5f447b6b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java @@ -305,8 +305,10 @@ private void processKeyedProperty(PropertyTokenHolder tokens, PropertyValue pv) Class componentType = propValue.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, arrayIndex + 1); System.arraycopy(propValue, 0, newArray, 0, length); - setPropertyValue(tokens.actualName, newArray); - propValue = getPropertyValue(tokens.actualName); + int lastKeyIndex = tokens.canonicalName.lastIndexOf('['); + String propName = tokens.canonicalName.substring(0, lastKeyIndex); + setPropertyValue(propName, newArray); + propValue = getPropertyValue(propName); } Array.set(propValue, arrayIndex, convertedValue); } From 157aa90d6e3befb2a7db66de920010f0fc06e3bf Mon Sep 17 00:00:00 2001 From: nullzl Date: Wed, 24 Feb 2021 23:44:18 +0800 Subject: [PATCH 2/2] Add unit test for setting auto-growing array's element --- .../springframework/beans/BeanWrapperAutoGrowingTests.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java index c17e2c7359b0..a31410e7444b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperAutoGrowingTests.java @@ -99,6 +99,12 @@ public void getPropertyValueAutoGrowMultiDimensionalArray() { assertThat(bean.getMultiArray()[0][0]).isInstanceOf(Bean.class); } + @Test + public void setPropertyValueAutoGrowMultiDimensionalArray() { + wrapper.setPropertyValue("multiArray[2][3]", new Bean()); + assertThat(bean.getMultiArray()[2][3]).isInstanceOf(Bean.class); + } + @Test public void getPropertyValueAutoGrowList() { assertNotNull(wrapper.getPropertyValue("list[0]"));