Class SpelCompiler

java.lang.Object
org.springframework.expression.spel.standard.SpelCompiler
All Implemented Interfaces:
Opcodes

public final class SpelCompiler extends Object implements Opcodes
A SpelCompiler will take a regular parsed expression and create (and load) a class containing byte code that does the same thing as that expression. The compiled form of an expression will evaluate far faster than the interpreted form.

The SpelCompiler is not currently handling all expression types but covers many of the common cases. The framework is extensible to cover more cases in the future. For absolute maximum speed there is *no checking* in the compiled code. The compiled version of the expression uses information learned during interpreted runs of the expression when it generates the byte code. For example if it knows that a particular property dereference always seems to return a Map then it will generate byte code that expects the result of the property dereference to be a Map. This ensures maximal performance but should the dereference result in something other than a map, the compiled expression will fail - like a ClassCastException would occur if passing data of an unexpected type in a regular Java program.

Due to the lack of checking there are likely some expressions that should never be compiled, for example if an expression is continuously dealing with different types of data. Due to these cases the compiler is something that must be selectively turned on for an associated SpelExpressionParser (through the SpelParserConfiguration object), it is not on by default.

Individual expressions can be compiled by calling SpelCompiler.compile(expression).

Since:
4.1
Author:
Andy Clement, Juergen Hoeller
  • Method Details

    • compile

      @Nullable public CompiledExpression compile(SpelNodeImpl expression)
      Attempt compilation of the supplied expression. A check is made to see if it is compilable before compilation proceeds. The check involves visiting all the nodes in the expression AST and ensuring enough state is known about them that bytecode can be generated for them.
      Parameters:
      expression - the expression to compile
      Returns:
      an instance of the class implementing the compiled expression, or null if compilation is not possible
    • getCompiler

      public static SpelCompiler getCompiler(@Nullable ClassLoader classLoader)
      Factory method for compiler instances. The returned SpelCompiler will attach a class loader as the child of the given class loader and this child will be used to load compiled expressions.
      Parameters:
      classLoader - the ClassLoader to use as the basis for compilation
      Returns:
      a corresponding SpelCompiler instance
    • compile

      public static boolean compile(Expression expression)
      Request that an attempt is made to compile the specified expression. It may fail if components of the expression are not suitable for compilation or the data types involved are not suitable for compilation. Used for testing.
      Parameters:
      expression - the expression to compile
      Returns:
      true if the expression was successfully compiled, false otherwise
    • revertToInterpreted

      public static void revertToInterpreted(Expression expression)
      Request to revert to the interpreter for expression evaluation. Any compiled form is discarded but can be recreated by later recompiling again.
      Parameters:
      expression - the expression