Skip to content

Commit

Permalink
Defensive handling of dimensions nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Sep 14, 2021
1 parent 3baaced commit b8b85a6
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
*/
public class ConstructorReference extends SpelNodeImpl {

private boolean isArrayConstructor = false;
private final boolean isArrayConstructor;

@Nullable
private SpelNodeImpl[] dimensions;
Expand Down Expand Up @@ -234,6 +234,7 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
FormatHelper.formatClassNameForMessage(
intendedArrayType != null ? intendedArrayType.getClass() : null));
}

String type = (String) intendedArrayType;
Class<?> componentType;
TypeCode arrayTypeCode = TypeCode.forName(type);
Expand All @@ -243,7 +244,8 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
else {
componentType = arrayTypeCode.getType();
}
Object newArray;

Object newArray = null;
if (!hasInitializer()) {
// Confirm all dimensions were specified (for example [3][][5] is missing the 2nd dimension)
if (this.dimensions != null) {
Expand All @@ -252,23 +254,22 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
throw new SpelEvaluationException(getStartPosition(), SpelMessage.MISSING_ARRAY_DIMENSION);
}
}
}
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();

// Shortcut for 1 dimensional
if (this.dimensions.length == 1) {
TypedValue o = this.dimensions[0].getTypedValue(state);
int arraySize = ExpressionUtils.toInt(typeConverter, o);
newArray = Array.newInstance(componentType, arraySize);
}
else {
// Multi-dimensional - hold onto your hat!
int[] dims = new int[this.dimensions.length];
for (int d = 0; d < this.dimensions.length; d++) {
TypedValue o = this.dimensions[d].getTypedValue(state);
dims[d] = ExpressionUtils.toInt(typeConverter, o);
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
if (this.dimensions.length == 1) {
// Shortcut for 1-dimensional
TypedValue o = this.dimensions[0].getTypedValue(state);
int arraySize = ExpressionUtils.toInt(typeConverter, o);
newArray = Array.newInstance(componentType, arraySize);
}
else {
// Multi-dimensional - hold onto your hat!
int[] dims = new int[this.dimensions.length];
for (int d = 0; d < this.dimensions.length; d++) {
TypedValue o = this.dimensions[d].getTypedValue(state);
dims[d] = ExpressionUtils.toInt(typeConverter, o);
}
newArray = Array.newInstance(componentType, dims);
}
newArray = Array.newInstance(componentType, dims);
}
}
else {
Expand Down

0 comments on commit b8b85a6

Please sign in to comment.