26
26
import java .util .HashMap ;
27
27
import java .util .List ;
28
28
import java .util .Map ;
29
+ import java .util .NoSuchElementException ;
29
30
import java .util .Set ;
30
31
31
32
import com .fasterxml .jackson .databind .JsonNode ;
@@ -731,7 +732,7 @@ void readIndexWithStringIndexType() {
731
732
.isThrownBy (() -> propertyExpression .getValue (context , birdNameMappings ))
732
733
.withMessageEndingWith ("A problem occurred while attempting to read index '%s' in '%s'" ,
733
734
"property" , BirdNameToColorMappings .class .getName ())
734
- .havingCause ().withMessage ("unknown bird color : property" );
735
+ .havingCause ().withMessage ("unknown bird: property" );
735
736
}
736
737
737
738
static class BirdNameToColorMappings {
@@ -742,38 +743,15 @@ public Color get(String name) {
742
743
return switch (name ) {
743
744
case "cardinal" -> Color .RED ;
744
745
case "blue jay" -> Color .BLUE ;
745
- default -> throw new RuntimeException ("unknown bird color : " + name );
746
+ default -> throw new NoSuchElementException ("unknown bird: " + name );
746
747
};
747
748
}
748
749
}
749
750
750
- static class BirdNameToColorMappingsIndexAccessor implements IndexAccessor {
751
+ static class BirdNameToColorMappingsIndexAccessor extends ReflectiveIndexAccessor {
751
752
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" );
777
755
}
778
756
}
779
757
0 commit comments