Skip to content

Commit fb97a12

Browse files
committed
Defensive handling of dimensions nullability
1 parent 1ad5b4f commit fb97a12

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -57,7 +57,7 @@
5757
*/
5858
public class ConstructorReference extends SpelNodeImpl {
5959

60-
private boolean isArrayConstructor = false;
60+
private final boolean isArrayConstructor;
6161

6262
@Nullable
6363
private SpelNodeImpl[] dimensions;
@@ -208,14 +208,14 @@ public String toStringAST() {
208208
StringBuilder sb = new StringBuilder("new ");
209209
int index = 0;
210210
sb.append(getChild(index++).toStringAST());
211-
sb.append("(");
211+
sb.append('(');
212212
for (int i = index; i < getChildCount(); i++) {
213213
if (i > index) {
214-
sb.append(",");
214+
sb.append(',');
215215
}
216216
sb.append(getChild(i).toStringAST());
217217
}
218-
sb.append(")");
218+
sb.append(')');
219219
return sb.toString();
220220
}
221221

@@ -234,6 +234,7 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
234234
FormatHelper.formatClassNameForMessage(
235235
intendedArrayType != null ? intendedArrayType.getClass() : null));
236236
}
237+
237238
String type = (String) intendedArrayType;
238239
Class<?> componentType;
239240
TypeCode arrayTypeCode = TypeCode.forName(type);
@@ -243,7 +244,8 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
243244
else {
244245
componentType = arrayTypeCode.getType();
245246
}
246-
Object newArray;
247+
248+
Object newArray = null;
247249
if (!hasInitializer()) {
248250
// Confirm all dimensions were specified (for example [3][][5] is missing the 2nd dimension)
249251
if (this.dimensions != null) {
@@ -252,23 +254,22 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
252254
throw new SpelEvaluationException(getStartPosition(), SpelMessage.MISSING_ARRAY_DIMENSION);
253255
}
254256
}
255-
}
256-
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
257-
258-
// Shortcut for 1 dimensional
259-
if (this.dimensions.length == 1) {
260-
TypedValue o = this.dimensions[0].getTypedValue(state);
261-
int arraySize = ExpressionUtils.toInt(typeConverter, o);
262-
newArray = Array.newInstance(componentType, arraySize);
263-
}
264-
else {
265-
// Multi-dimensional - hold onto your hat!
266-
int[] dims = new int[this.dimensions.length];
267-
for (int d = 0; d < this.dimensions.length; d++) {
268-
TypedValue o = this.dimensions[d].getTypedValue(state);
269-
dims[d] = ExpressionUtils.toInt(typeConverter, o);
257+
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
258+
if (this.dimensions.length == 1) {
259+
// Shortcut for 1-dimensional
260+
TypedValue o = this.dimensions[0].getTypedValue(state);
261+
int arraySize = ExpressionUtils.toInt(typeConverter, o);
262+
newArray = Array.newInstance(componentType, arraySize);
263+
}
264+
else {
265+
// Multi-dimensional - hold onto your hat!
266+
int[] dims = new int[this.dimensions.length];
267+
for (int d = 0; d < this.dimensions.length; d++) {
268+
TypedValue o = this.dimensions[d].getTypedValue(state);
269+
dims[d] = ExpressionUtils.toInt(typeConverter, o);
270+
}
271+
newArray = Array.newInstance(componentType, dims);
270272
}
271-
newArray = Array.newInstance(componentType, dims);
272273
}
273274
}
274275
else {

0 commit comments

Comments
 (0)