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

    • getSpecificTargetClasses

      public Class<?>[] getSpecificTargetClasses()
      Return an array containing the targetType configured via the constructor.
      Specified by:
      getSpecificTargetClasses in interface TargetedAccessor
      Returns:
      an array of classes that this accessor is suitable for (or null or an empty array if a generic accessor)
    • canRead

      public boolean canRead(EvaluationContext context, Object target, Object index)
      Return true if the supplied target and index can be assigned to the targetType and indexType configured via the constructor.

      Considers primitive wrapper classes as assignable to the corresponding primitive types.

      Specified by:
      canRead in interface IndexAccessor
      Parameters:
      context - the evaluation context in which the access is being attempted
      target - the target object upon which the index is being accessed
      index - the index being accessed
      Returns:
      true if this index accessor is able to read the index
    • read

      public TypedValue read(EvaluationContext context, Object target, Object index)
      Invoke the configured read-method via reflection and return the result wrapped in a TypedValue.
      Specified by:
      read in interface IndexAccessor
      Parameters:
      context - the evaluation context in which the access is being attempted
      target - the target object upon which the index is being accessed
      index - the index being accessed
      Returns:
      a TypedValue object wrapping the index value read and a type descriptor for the value
    • canWrite

      public boolean canWrite(EvaluationContext context, Object target, Object index)
      Return true if a write-method has been configured and canRead(EvaluationContext, Object, Object) returns true for the same arguments.
      Specified by:
      canWrite in interface IndexAccessor
      Parameters:
      context - the evaluation context in which the access is being attempted
      target - the target object upon which the index is being accessed
      index - the index being accessed
      Returns:
      true if this index accessor is able to write to the index
    • write

      public void write(EvaluationContext context, Object target, Object index, @Nullable Object newValue)
      Invoke the configured write-method via reflection.

      Should only be invoked if canWrite(EvaluationContext, Object, Object) returns true for the same arguments.

      Specified by:
      write in interface IndexAccessor
      Parameters:
      context - the evaluation context in which the access is being attempted
      target - the target object upon which the index is being accessed
      index - the index being accessed
      newValue - the new value for the index
    • isCompilable

      public boolean isCompilable()
      Description copied from interface: CompilableIndexAccessor
      Determine if this IndexAccessor is currently suitable for compilation.

      May only be known once the index has been read.

      Specified by:
      isCompilable in interface CompilableIndexAccessor
      See Also:
    • getIndexedValueType

      public Class<?> getIndexedValueType()
      Get the return type of the configured read-method.
      Specified by:
      getIndexedValueType in interface CompilableIndexAccessor
      See Also:
    • generateCode

      public void generateCode(SpelNode index, MethodVisitor mv, CodeFlow cf)
      Description copied from interface: CompilableIndexAccessor
      Generate bytecode that performs the operation for reading the index.

      Bytecode should be generated into the supplied MethodVisitor using context information from the CodeFlow where necessary.

      The supplied indexNode should be used to generate the appropriate bytecode to load the index onto the stack. For example, given the expression book.authors[0], invoking codeFlow.generateCodeForArgument(methodVisitor, indexNode, int.class) will ensure that the index (0) is available on the stack as a primitive int.

      Will only be invoked if CompilableIndexAccessor.isCompilable() returns true.

      Specified by:
      generateCode in interface CompilableIndexAccessor
      Parameters:
      index - the SpelNode that represents the index being accessed
      mv - the ASM MethodVisitor into which code should be generated
      cf - the current state of the expression compiler