From 4e6591e1a9e78542d198a720318ac183529399f9 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 1 May 2024 16:04:10 +0300 Subject: [PATCH] Polishing --- .../expression/spel/IndexingTests.java | 34 ++++--------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java index 44541d8b54..1b50fca17c 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java @@ -26,6 +26,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Set; import com.fasterxml.jackson.databind.JsonNode; @@ -731,7 +732,7 @@ class IndexingTests { .isThrownBy(() -> propertyExpression.getValue(context, birdNameMappings)) .withMessageEndingWith("A problem occurred while attempting to read index '%s' in '%s'", "property", BirdNameToColorMappings.class.getName()) - .havingCause().withMessage("unknown bird color: property"); + .havingCause().withMessage("unknown bird: property"); } static class BirdNameToColorMappings { @@ -742,38 +743,15 @@ class IndexingTests { return switch (name) { case "cardinal" -> Color.RED; case "blue jay" -> Color.BLUE; - default -> throw new RuntimeException("unknown bird color: " + name); + default -> throw new NoSuchElementException("unknown bird: " + name); }; } } - static class BirdNameToColorMappingsIndexAccessor implements IndexAccessor { + static class BirdNameToColorMappingsIndexAccessor extends ReflectiveIndexAccessor { - @Override - public Class[] getSpecificTargetClasses() { - return new Class[] { BirdNameToColorMappings.class }; - } - - @Override - public boolean canRead(EvaluationContext context, Object target, Object index) { - return (target instanceof BirdNameToColorMappings && index instanceof String); - } - - @Override - public TypedValue read(EvaluationContext context, Object target, Object index) { - BirdNameToColorMappings mappings = (BirdNameToColorMappings) target; - String name = (String) index; - return new TypedValue(mappings.get(name)); - } - - @Override - public boolean canWrite(EvaluationContext context, Object target, Object index) { - return false; - } - - @Override - public void write(EvaluationContext context, Object target, Object index, @Nullable Object newValue) { - throw new UnsupportedOperationException(); + BirdNameToColorMappingsIndexAccessor() { + super(BirdNameToColorMappings.class, String.class, "get"); } }