Skip to content

Commit f3ab390

Browse files
committed
Merge branch '6.1.x'
2 parents 74b0bb1 + c57c227 commit f3ab390

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ public void setValue(@Nullable Object newValue) {
810810
throw new SpelEvaluationException(getStartPosition(), ex,
811811
SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE, this.name, ex.getMessage());
812812
}
813+
throw new SpelEvaluationException(getStartPosition(),
814+
SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, this.targetObjectTypeDescriptor.toString());
813815
}
814816

815817
@Override

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

+22-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.assertj.core.api.ThrowableTypeAssert;
2425
import org.junit.jupiter.api.Test;
2526

2627
import org.springframework.core.convert.TypeDescriptor;
@@ -83,11 +84,11 @@ void nonExistentPropertiesAndMethods() {
8384
void accessingOnNullObject() {
8485
SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
8586
EvaluationContext context = new StandardEvaluationContext(null);
86-
assertThatExceptionOfType(SpelEvaluationException.class)
87+
assertThatSpelEvaluationException()
8788
.isThrownBy(() -> expr.getValue(context))
8889
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
8990
assertThat(expr.isWritable(context)).isFalse();
90-
assertThatExceptionOfType(SpelEvaluationException.class)
91+
assertThatSpelEvaluationException()
9192
.isThrownBy(() -> expr.setValue(context, "abc"))
9293
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
9394
}
@@ -117,8 +118,7 @@ void addingSpecificPropertyAccessor() {
117118
assertThat((int) i).isEqualTo(99);
118119

119120
// Cannot set it to a string value
120-
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() ->
121-
flibbleexpr.setValue(ctx, "not allowed"));
121+
assertThatSpelEvaluationException().isThrownBy(() -> flibbleexpr.setValue(ctx, "not allowed"));
122122
// message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
123123
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
124124
// System.out.println(e.getMessage());
@@ -173,8 +173,7 @@ void standardGetClassAccess() {
173173
@Test
174174
void noGetClassAccess() {
175175
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
176-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
177-
parser.parseExpression("'a'.class.name").getValue(context));
176+
assertThatSpelEvaluationException().isThrownBy(() -> parser.parseExpression("'a'.class.name").getValue(context));
178177
}
179178

180179
@Test
@@ -187,8 +186,13 @@ void propertyReadOnly() {
187186
target.setName("p2");
188187
assertThat(expr.getValue(context, target)).isEqualTo("p2");
189188

190-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
191-
parser.parseExpression("name='p3'").getValue(context, target));
189+
assertThatSpelEvaluationException()
190+
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target))
191+
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE);
192+
193+
assertThatSpelEvaluationException()
194+
.isThrownBy(() -> parser.parseExpression("['name']='p4'").getValue(context, target))
195+
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE);
192196
}
193197

194198
@Test
@@ -201,8 +205,9 @@ void propertyReadOnlyWithRecordStyle() {
201205
RecordPerson target2 = new RecordPerson("p2");
202206
assertThat(expr.getValue(context, target2)).isEqualTo("p2");
203207

204-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
205-
parser.parseExpression("name='p3'").getValue(context, target2));
208+
assertThatSpelEvaluationException()
209+
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target2))
210+
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE);
206211
}
207212

208213
@Test
@@ -248,7 +253,7 @@ void propertyReadWriteWithRootObject() {
248253
void propertyAccessWithoutMethodResolver() {
249254
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
250255
Person target = new Person("p1");
251-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
256+
assertThatSpelEvaluationException().isThrownBy(() ->
252257
parser.parseExpression("name.substring(1)").getValue(context, target));
253258
}
254259

@@ -274,12 +279,17 @@ void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
274279
void propertyAccessWithArrayIndexOutOfBounds() {
275280
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
276281
Expression expression = parser.parseExpression("stringArrayOfThreeItems[3]");
277-
assertThatExceptionOfType(SpelEvaluationException.class)
282+
assertThatSpelEvaluationException()
278283
.isThrownBy(() -> expression.getValue(context, new Inventor()))
279284
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS);
280285
}
281286

282287

288+
private ThrowableTypeAssert<SpelEvaluationException> assertThatSpelEvaluationException() {
289+
return assertThatExceptionOfType(SpelEvaluationException.class);
290+
}
291+
292+
283293
// This can resolve the property 'flibbles' on any String (very useful...)
284294
private static class StringyPropertyAccessor implements PropertyAccessor {
285295

0 commit comments

Comments
 (0)