Skip to content

Commit a3a48a2

Browse files
committed
Disable array allocation in case of no constructor resolution
Closes gh-28808
1 parent 3110487 commit a3a48a2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
243243
intendedArrayType != null ? intendedArrayType.getClass() : null));
244244
}
245245

246+
if (state.getEvaluationContext().getConstructorResolvers().isEmpty()) {
247+
// No constructor resolver -> no array construction either (as of 6.0)
248+
throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND,
249+
type + "[]", "[]");
250+
}
251+
246252
Class<?> componentType;
247253
TypeCode arrayTypeCode = TypeCode.forName(type);
248254
if (arrayTypeCode == TypeCode.OBJECT) {

spring-expression/src/test/java/org/springframework/expression/spel/ArrayConstructorTests.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.expression.EvaluationContext;
2122
import org.springframework.expression.Expression;
2223
import org.springframework.expression.spel.standard.SpelExpressionParser;
24+
import org.springframework.expression.spel.support.SimpleEvaluationContext;
2325
import org.springframework.util.ObjectUtils;
2426

2527
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2629

2730
/**
2831
* Test construction of arrays.
2932
*
3033
* @author Andy Clement
3134
* @author Sam Brannen
35+
* @author Juergen Hoeller
3236
*/
3337
class ArrayConstructorTests extends AbstractExpressionTests {
3438

@@ -97,7 +101,7 @@ void errorCases() {
97101
void typeArrayConstructors() {
98102
evaluate("new String[]{'a','b','c','d'}[1]", "b", String.class);
99103
evaluateAndCheckError("new String[]{'a','b','c','d'}.size()", SpelMessage.METHOD_NOT_FOUND, 30, "size()",
100-
"java.lang.String[]");
104+
"java.lang.String[]");
101105
evaluate("new String[]{'a','b','c','d'}.length", 4, Integer.class);
102106
}
103107

@@ -110,10 +114,18 @@ void basicArray() {
110114
void multiDimensionalArrays() {
111115
evaluate("new String[2][2]", "[Ljava.lang.String;[2]{[2]{null,null},[2]{null,null}}", String[][].class);
112116
evaluate("new String[3][2][1]",
113-
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
114-
String[][][].class);
117+
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
118+
String[][][].class);
115119
}
116120

121+
@Test
122+
void noArrayConstruction() {
123+
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
124+
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
125+
parser.parseExpression("new int[2]").getValue(context));
126+
}
127+
128+
117129
private void evaluateArrayBuildingExpression(String expression, String expectedToString) {
118130
SpelExpressionParser parser = new SpelExpressionParser();
119131
Expression e = parser.parseExpression(expression);

0 commit comments

Comments
 (0)