Document SpEL IndexAccessor support in the reference manual
Closes gh-32735
This commit is contained in:
@@ -26,16 +26,20 @@ import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import example.Color;
|
||||
import example.FruitMap;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.IndexAccessor;
|
||||
import org.springframework.expression.Operation;
|
||||
import org.springframework.expression.OperatorOverloader;
|
||||
import org.springframework.expression.common.TemplateParserContext;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.ReflectiveIndexAccessor;
|
||||
import org.springframework.expression.spel.support.SimpleEvaluationContext;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.expression.spel.testresources.Inventor;
|
||||
@@ -254,6 +258,24 @@ class SpelDocumentationTests extends AbstractExpressionTests {
|
||||
assertThat(name).isEqualTo("Nikola Tesla");
|
||||
}
|
||||
|
||||
@Test
|
||||
void indexingIntoCustomStructure() {
|
||||
// Create a ReflectiveIndexAccessor for FruitMap
|
||||
IndexAccessor fruitMapAccessor = new ReflectiveIndexAccessor(
|
||||
FruitMap.class, Color.class, "getFruit", "setFruit");
|
||||
|
||||
// Register the IndexAccessor for FruitMap
|
||||
context.addIndexAccessor(fruitMapAccessor);
|
||||
|
||||
// Register the fruitMap variable
|
||||
context.setVariable("fruitMap", new FruitMap());
|
||||
|
||||
// evaluates to "cherry"
|
||||
String fruit = parser.parseExpression("#fruitMap[T(example.Color).RED]")
|
||||
.getValue(context, String.class);
|
||||
assertThat(fruit).isEqualTo("cherry");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
Reference in New Issue
Block a user