Add support for multidimensional arrays

See gh-34183
This commit is contained in:
xumengqi
2025-01-01 11:06:49 +08:00
committed by Stéphane Nicoll
parent 819a7c86c1
commit 68c1e2ac92
2 changed files with 65 additions and 10 deletions

View File

@@ -904,16 +904,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
try {
if (type.isArray()) {
Class<?> componentType = type.componentType();
// TODO - only handles 2-dimensional arrays
if (componentType.isArray()) {
Object array = Array.newInstance(componentType, 1);
Array.set(array, 0, Array.newInstance(componentType.componentType(), 0));
return array;
}
else {
return Array.newInstance(componentType, 0);
}
return createArray(type);
}
else if (Collection.class.isAssignableFrom(type)) {
TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
@@ -937,6 +928,29 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
}
}
/**
* Create the array for the given array type.
* @param arrayType the desired type of the target array
* @return a new array instance
*/
private Object createArray(Class<?> arrayType) {
Assert.notNull(arrayType, "Array type must not be null");
if (arrayType.isArray()) {
Class<?> componentType = arrayType.componentType();
if (componentType.isArray()) {
Object array = Array.newInstance(componentType, 1);
Array.set(array, 0, createArray(componentType));
return array;
}
else {
return Array.newInstance(componentType, 0);
}
}
else {
throw new IllegalArgumentException("Unsupported Array type: " + arrayType.getName());
}
}
/**
* Parse the given property name into the corresponding property name tokens.
* @param propertyName the property name to parse