Class ReflectiveIndexAccessor

java.lang.Object
org.springframework.expression.spel.support.ReflectiveIndexAccessor
All Implemented Interfaces:
Opcodes, IndexAccessor, CompilableIndexAccessor, TargetedAccessor

public class ReflectiveIndexAccessor extends Object implements CompilableIndexAccessor
A flexible IndexAccessor that uses reflection to read from and optionally write to an indexed structure of a target object.

The indexed structure can be accessed through a public read-method (when being read) or a public write-method (when being written). The relationship between the read-method and write-method is based on a convention that is applicable for typical implementations of indexed structures. See the example below for details.

ReflectiveIndexAccessor also implements CompilableIndexAccessor in order to support compilation to bytecode for read access. Note, however, that the configured read-method must be invokable via a public class or public interface for compilation to succeed.

Example

The FruitMap class (the targetType) represents a structure that is indexed via the Color enum (the indexType). The name of the read-method is "getFruit", and that method returns a String (the indexedValueType). The name of the write-method is "setFruit", and that method accepts a Color enum (the indexType) and a String (the indexedValueType which must match the return type of the read-method).

A read-only IndexAccessor for FruitMap can be created via new ReflectiveIndexAccessor(FruitMap.class, Color.class, "getFruit"). With that accessor registered and a FruitMap registered as a variable named #fruitMap, the SpEL expression #fruitMap[T(example.Color).RED] will evaluate to "cherry".

A read-write IndexAccessor for FruitMap can be created via new ReflectiveIndexAccessor(FruitMap.class, Color.class, "getFruit", "setFruit"). With that accessor registered and a FruitMap registered as a variable named #fruitMap, the SpEL expression #fruitMap[T(example.Color).RED] = 'strawberry' can be used to change the fruit mapping for the color red from "cherry" to "strawberry".

 package example;

 public enum Color {
     RED, ORANGE, YELLOW
 }
 public class FruitMap {

     private final Map<Color, String> map = new HashMap<>();

     public FruitMap() {
         this.map.put(Color.RED, "cherry");
         this.map.put(Color.ORANGE, "orange");
         this.map.put(Color.YELLOW, "banana");
     }

     public String getFruit(Color color) {
         return this.map.get(color);
     }

     public void setFruit(Color color, String fruit) {
         this.map.put(color, fruit);
     }
 }
Since:
6.2
Author:
Sam Brannen
See Also:
  • Constructor Details

    • ReflectiveIndexAccessor

      public ReflectiveIndexAccessor(Class<?> targetType, Class<?> indexType, String readMethodName)
      Construct a new ReflectiveIndexAccessor for read-only access.

      See class-level documentation for further details and an example.

      Parameters:
      targetType - the type of indexed structure which serves as the target of index operations
      indexType - the type of index used to read from the indexed structure
      readMethodName - the name of the method used to read from the indexed structure
    • ReflectiveIndexAccessor

      public ReflectiveIndexAccessor(Class<?> targetType, Class<?> indexType, String readMethodName, @Nullable String writeMethodName)
      Construct a new ReflectiveIndexAccessor for read-write access.

      See class-level documentation for further details and an example.

      Parameters:
      targetType - the type of indexed structure which serves as the target of index operations
      indexType - the type of index used to read from or write to the indexed structure
      readMethodName - the name of the method used to read from the indexed structure
      writeMethodName - the name of the method used to write to the indexed structure, or null if writing is not supported
  • Method Details