Skip to content

Commit 4e6591e

Browse files
committed
Polishing
1 parent 37dd47c commit 4e6591e

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

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

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.HashMap;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.NoSuchElementException;
2930
import java.util.Set;
3031

3132
import com.fasterxml.jackson.databind.JsonNode;
@@ -731,7 +732,7 @@ void readIndexWithStringIndexType() {
731732
.isThrownBy(() -> propertyExpression.getValue(context, birdNameMappings))
732733
.withMessageEndingWith("A problem occurred while attempting to read index '%s' in '%s'",
733734
"property", BirdNameToColorMappings.class.getName())
734-
.havingCause().withMessage("unknown bird color: property");
735+
.havingCause().withMessage("unknown bird: property");
735736
}
736737

737738
static class BirdNameToColorMappings {
@@ -742,38 +743,15 @@ public Color get(String name) {
742743
return switch (name) {
743744
case "cardinal" -> Color.RED;
744745
case "blue jay" -> Color.BLUE;
745-
default -> throw new RuntimeException("unknown bird color: " + name);
746+
default -> throw new NoSuchElementException("unknown bird: " + name);
746747
};
747748
}
748749
}
749750

750-
static class BirdNameToColorMappingsIndexAccessor implements IndexAccessor {
751+
static class BirdNameToColorMappingsIndexAccessor extends ReflectiveIndexAccessor {
751752

752-
@Override
753-
public Class<?>[] getSpecificTargetClasses() {
754-
return new Class<?>[] { BirdNameToColorMappings.class };
755-
}
756-
757-
@Override
758-
public boolean canRead(EvaluationContext context, Object target, Object index) {
759-
return (target instanceof BirdNameToColorMappings && index instanceof String);
760-
}
761-
762-
@Override
763-
public TypedValue read(EvaluationContext context, Object target, Object index) {
764-
BirdNameToColorMappings mappings = (BirdNameToColorMappings) target;
765-
String name = (String) index;
766-
return new TypedValue(mappings.get(name));
767-
}
768-
769-
@Override
770-
public boolean canWrite(EvaluationContext context, Object target, Object index) {
771-
return false;
772-
}
773-
774-
@Override
775-
public void write(EvaluationContext context, Object target, Object index, @Nullable Object newValue) {
776-
throw new UnsupportedOperationException();
753+
BirdNameToColorMappingsIndexAccessor() {
754+
super(BirdNameToColorMappings.class, String.class, "get");
777755
}
778756
}
779757

0 commit comments

Comments
 (0)