Skip to content

Commit

Permalink
Merge pull request #34183 from remeio
Browse files Browse the repository at this point in the history
* pr/34183:
  Polish "Add support for multidimensional arrays"
  Add support for multidimensional arrays

Closes gh-34183
  • Loading branch information
snicoll committed Feb 5, 2025
2 parents 819a7c8 + ec037b1 commit fd4dee7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -904,16 +904,7 @@ private PropertyValue createDefaultPropertyValue(PropertyTokenHolder tokens) {
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);
Expand All @@ -937,6 +928,24 @@ else if (Map.class.isAssignableFrom(type)) {
}
}

/**
* Create the array for the given array type.
* @param arrayType the desired type of the target array
* @return a new array instance
*/
private static Object createArray(Class<?> arrayType) {
Assert.notNull(arrayType, "Array type must not be null");
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);
}
}

/**
* Parse the given property name into the corresponding property name tokens.
* @param propertyName the property name to parse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,6 +103,27 @@ void getPropertyValueAutoGrow3dArray() {
assertThat(bean.getThreeDimensionalArray()[1][2][3]).isInstanceOf(Bean.class);
}

@Test
void getPropertyValueAutoGrow3dArrayList() {
assertThat(wrapper.getPropertyValue("threeDimensionalArrayList[1][2][3][4]")).isNotNull();
assertThat(bean.getThreeDimensionalArrayList()).hasSize(2);
assertThat(bean.getThreeDimensionalArrayList().get(1)).hasNumberOfRows(3);
assertThat(bean.getThreeDimensionalArrayList().get(1)[2]).hasNumberOfRows(4);
assertThat(bean.getThreeDimensionalArrayList().get(1)[2][3]).hasSize(5);
assertThat(bean.getThreeDimensionalArrayList().get(1)[2][3][4]).isInstanceOf(Bean.class);
}

@Test
void getPropertyValueAutoGrow3dArrayListForDefault3dArray() {
assertThat(wrapper.getPropertyValue("threeDimensionalArrayList[0]")).isNotNull();
assertThat(bean.getThreeDimensionalArrayList()).hasSize(1);

// Default 3-dimensional array should be [[[]]]
assertThat(bean.getThreeDimensionalArrayList().get(0)).hasNumberOfRows(1);
assertThat(bean.getThreeDimensionalArrayList().get(0)[0]).hasNumberOfRows(1);
assertThat(bean.getThreeDimensionalArrayList().get(0)[0][0]).isEmpty();
}

@Test
void setPropertyValueAutoGrow2dArray() {
Bean newBean = new Bean();
Expand All @@ -123,6 +144,16 @@ void setPropertyValueAutoGrow3dArray() {
.extracting(Bean::getProp).isEqualTo("enigma");
}

@Test
void setPropertyValueAutoGrow3dArrayList() {
Bean newBean = new Bean();
newBean.setProp("enigma");
wrapper.setPropertyValue("threeDimensionalArrayList[0][1][2][3]", newBean);
assertThat(bean.getThreeDimensionalArrayList().get(0)[1][2][3])
.isInstanceOf(Bean.class)
.extracting(Bean::getProp).isEqualTo("enigma");
}

@Test
void getPropertyValueAutoGrowList() {
assertThat(wrapper.getPropertyValue("list[0]")).isNotNull();
Expand Down Expand Up @@ -215,6 +246,8 @@ public static class Bean {

private Bean[][][] threeDimensionalArray;

private List<Bean[][][]> threeDimensionalArrayList;

private List<Bean> list;

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

public List<Bean[][][]> getThreeDimensionalArrayList() {
return threeDimensionalArrayList;
}

public void setThreeDimensionalArrayList(List<Bean[][][]> threeDimensionalArrayList) {
this.threeDimensionalArrayList = threeDimensionalArrayList;
}

public List<Bean> getList() {
return list;
}
Expand Down

0 comments on commit fd4dee7

Please sign in to comment.