Skip to content

Commit

Permalink
Disable array allocation in case of no constructor resolution
Browse files Browse the repository at this point in the history
Closes gh-28808
  • Loading branch information
jhoeller committed Oct 18, 2022
1 parent 3110487 commit a3a48a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
intendedArrayType != null ? intendedArrayType.getClass() : null));
}

if (state.getEvaluationContext().getConstructorResolvers().isEmpty()) {
// No constructor resolver -> no array construction either (as of 6.0)
throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND,
type + "[]", "[]");
}

Class<?> componentType;
TypeCode arrayTypeCode = TypeCode.forName(type);
if (arrayTypeCode == TypeCode.OBJECT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@

import org.junit.jupiter.api.Test;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.util.ObjectUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* Test construction of arrays.
*
* @author Andy Clement
* @author Sam Brannen
* @author Juergen Hoeller
*/
class ArrayConstructorTests extends AbstractExpressionTests {

Expand Down Expand Up @@ -97,7 +101,7 @@ void errorCases() {
void typeArrayConstructors() {
evaluate("new String[]{'a','b','c','d'}[1]", "b", String.class);
evaluateAndCheckError("new String[]{'a','b','c','d'}.size()", SpelMessage.METHOD_NOT_FOUND, 30, "size()",
"java.lang.String[]");
"java.lang.String[]");
evaluate("new String[]{'a','b','c','d'}.length", 4, Integer.class);
}

Expand All @@ -110,10 +114,18 @@ void basicArray() {
void multiDimensionalArrays() {
evaluate("new String[2][2]", "[Ljava.lang.String;[2]{[2]{null,null},[2]{null,null}}", String[][].class);
evaluate("new String[3][2][1]",
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
String[][][].class);
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
String[][][].class);
}

@Test
void noArrayConstruction() {
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
parser.parseExpression("new int[2]").getValue(context));
}


private void evaluateArrayBuildingExpression(String expression, String expectedToString) {
SpelExpressionParser parser = new SpelExpressionParser();
Expression e = parser.parseExpression(expression);
Expand Down

0 comments on commit a3a48a2

Please sign in to comment.