Skip to content

Commit 49bd833

Browse files
committed
Merge branch '6.2.x'
2 parents e997e16 + fd4dee7 commit 49bd833

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -893,16 +893,7 @@ private PropertyValue createDefaultPropertyValue(PropertyTokenHolder tokens) {
893893
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
894894
try {
895895
if (type.isArray()) {
896-
Class<?> componentType = type.componentType();
897-
// TODO - only handles 2-dimensional arrays
898-
if (componentType.isArray()) {
899-
Object array = Array.newInstance(componentType, 1);
900-
Array.set(array, 0, Array.newInstance(componentType.componentType(), 0));
901-
return array;
902-
}
903-
else {
904-
return Array.newInstance(componentType, 0);
905-
}
896+
return createArray(type);
906897
}
907898
else if (Collection.class.isAssignableFrom(type)) {
908899
TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
@@ -926,6 +917,24 @@ else if (Map.class.isAssignableFrom(type)) {
926917
}
927918
}
928919

920+
/**
921+
* Create the array for the given array type.
922+
* @param arrayType the desired type of the target array
923+
* @return a new array instance
924+
*/
925+
private static Object createArray(Class<?> arrayType) {
926+
Assert.notNull(arrayType, "Array type must not be null");
927+
Class<?> componentType = arrayType.componentType();
928+
if (componentType.isArray()) {
929+
Object array = Array.newInstance(componentType, 1);
930+
Array.set(array, 0, createArray(componentType));
931+
return array;
932+
}
933+
else {
934+
return Array.newInstance(componentType, 0);
935+
}
936+
}
937+
929938
/**
930939
* Parse the given property name into the corresponding property name tokens.
931940
* @param propertyName the property name to parse

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -103,6 +103,27 @@ void getPropertyValueAutoGrow3dArray() {
103103
assertThat(bean.getThreeDimensionalArray()[1][2][3]).isInstanceOf(Bean.class);
104104
}
105105

106+
@Test
107+
void getPropertyValueAutoGrow3dArrayList() {
108+
assertThat(wrapper.getPropertyValue("threeDimensionalArrayList[1][2][3][4]")).isNotNull();
109+
assertThat(bean.getThreeDimensionalArrayList()).hasSize(2);
110+
assertThat(bean.getThreeDimensionalArrayList().get(1)).hasNumberOfRows(3);
111+
assertThat(bean.getThreeDimensionalArrayList().get(1)[2]).hasNumberOfRows(4);
112+
assertThat(bean.getThreeDimensionalArrayList().get(1)[2][3]).hasSize(5);
113+
assertThat(bean.getThreeDimensionalArrayList().get(1)[2][3][4]).isInstanceOf(Bean.class);
114+
}
115+
116+
@Test
117+
void getPropertyValueAutoGrow3dArrayListForDefault3dArray() {
118+
assertThat(wrapper.getPropertyValue("threeDimensionalArrayList[0]")).isNotNull();
119+
assertThat(bean.getThreeDimensionalArrayList()).hasSize(1);
120+
121+
// Default 3-dimensional array should be [[[]]]
122+
assertThat(bean.getThreeDimensionalArrayList().get(0)).hasNumberOfRows(1);
123+
assertThat(bean.getThreeDimensionalArrayList().get(0)[0]).hasNumberOfRows(1);
124+
assertThat(bean.getThreeDimensionalArrayList().get(0)[0][0]).isEmpty();
125+
}
126+
106127
@Test
107128
void setPropertyValueAutoGrow2dArray() {
108129
Bean newBean = new Bean();
@@ -123,6 +144,16 @@ void setPropertyValueAutoGrow3dArray() {
123144
.extracting(Bean::getProp).isEqualTo("enigma");
124145
}
125146

147+
@Test
148+
void setPropertyValueAutoGrow3dArrayList() {
149+
Bean newBean = new Bean();
150+
newBean.setProp("enigma");
151+
wrapper.setPropertyValue("threeDimensionalArrayList[0][1][2][3]", newBean);
152+
assertThat(bean.getThreeDimensionalArrayList().get(0)[1][2][3])
153+
.isInstanceOf(Bean.class)
154+
.extracting(Bean::getProp).isEqualTo("enigma");
155+
}
156+
126157
@Test
127158
void getPropertyValueAutoGrowList() {
128159
assertThat(wrapper.getPropertyValue("list[0]")).isNotNull();
@@ -215,6 +246,8 @@ public static class Bean {
215246

216247
private Bean[][][] threeDimensionalArray;
217248

249+
private List<Bean[][][]> threeDimensionalArrayList;
250+
218251
private List<Bean> list;
219252

220253
private List<List<Bean>> nestedList;
@@ -269,6 +302,14 @@ public void setThreeDimensionalArray(Bean[][][] threeDimensionalArray) {
269302
this.threeDimensionalArray = threeDimensionalArray;
270303
}
271304

305+
public List<Bean[][][]> getThreeDimensionalArrayList() {
306+
return threeDimensionalArrayList;
307+
}
308+
309+
public void setThreeDimensionalArrayList(List<Bean[][][]> threeDimensionalArrayList) {
310+
this.threeDimensionalArrayList = threeDimensionalArrayList;
311+
}
312+
272313
public List<Bean> getList() {
273314
return list;
274315
}

0 commit comments

Comments
 (0)