Class ELCompiler

java.lang.Object
io.micronaut.el.processor.compiler.ELCompiler

@Internal public final class ELCompiler extends Object
Lowers an expression syntax tree to the Micronaut SourceGen model.

Statically known accesses and operators become direct Java operations. The remainder uses the runtime resolution described in sections 1.5 and 1.6 of the specification.

Since:
1.0
Author:
Denis Stepanov
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    The base and property of an lvalue.
    static final record 
    A compiled expression and its statically known type, when available.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    List<io.micronaut.sourcegen.model.MethodDef>
    Returns the private helpers accumulated while compiling the current expression class.
    void
    beginClass(String className)
    Starts compiling one generated expression class.
    Returns the equality form of a generated expression, replacing declared function names with the methods bound to them as required by the Jakarta EL expression equality contract.
    io.micronaut.sourcegen.model.ExpressionDef
    compile(ELNode node, io.micronaut.sourcegen.model.ExpressionDef elContext)
    Compiles an expression.
    io.micronaut.sourcegen.model.StatementDef
    compileEvaluation(ELNode node, io.micronaut.sourcegen.model.ExpressionDef elContext, Function<io.micronaut.sourcegen.model.ExpressionDef, ELCompiler.Typed> body)
    Compiles the evaluation of an expression as the body of a method returning its result.
    compileLValue(ELNode node, io.micronaut.sourcegen.model.ExpressionDef elContext)
    Compiles the base object and the property of an lvalue, as described in the section 1.13 of the specification.
    compileTyped(ELNode node, io.micronaut.sourcegen.model.ExpressionDef ctx)
    Compiles an expression, with its static type.
    boolean
    evaluatesTo(io.micronaut.inject.ast.ClassElement expected)
     
    @Nullable io.micronaut.inject.ast.ClassElement
     
    static boolean
     
    io.micronaut.inject.ast.ClassElement
    The expected type inferred for an expression whose declaration omits it: the static type of the result, a primitive as the wrapper the evaluation returns, Object when the compiler does not know the type.
    io.micronaut.inject.ast.ClassElement
    The static type of an expression, for the return type of a method expression whose declaration omits it.
    io.micronaut.sourcegen.model.ExpressionDef
    invokeRuntime(io.micronaut.sourcegen.model.ClassTypeDef owner, String name, io.micronaut.sourcegen.model.TypeDef returning, io.micronaut.sourcegen.model.ExpressionDef... values)
    Invokes a static method of the runtime with the signature the runtime actually declares, for the writers.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ELCompiler

      public ELCompiler(CompilationContext context)
      Parameters:
      context - The compilation context
  • Method Details

    • beginClass

      public void beginClass(String className)
      Starts compiling one generated expression class.
      Parameters:
      className - The generated class name
    • accessMethods

      public List<io.micronaut.sourcegen.model.MethodDef> accessMethods()
      Returns the private helpers accumulated while compiling the current expression class.
      Returns:
      The generated access helpers
    • compile

      public io.micronaut.sourcegen.model.ExpressionDef compile(ELNode node, io.micronaut.sourcegen.model.ExpressionDef elContext)
      Compiles an expression.
      Parameters:
      node - The parsed expression
      elContext - The expression holding the jakarta.el.ELContext
      Returns:
      The compiled expression
    • compileEvaluation

      public io.micronaut.sourcegen.model.StatementDef compileEvaluation(ELNode node, io.micronaut.sourcegen.model.ExpressionDef elContext, Function<io.micronaut.sourcegen.model.ExpressionDef, ELCompiler.Typed> body)
      Compiles the evaluation of an expression as the body of a method returning its result.

      Repeated stable identifiers share a local. Lambda bodies retain their own context, and an outer context captured by a generated Java lambda is first bound to a local for both source and bytecode generation.

      Parameters:
      node - The parsed expression
      elContext - The expression holding the jakarta.el.ELContext
      body - Compiles the result after shared identifiers are in place
      Returns:
      The body of the method
    • evaluationType

      public @Nullable io.micronaut.inject.ast.ClassElement evaluationType()
      Returns:
      The static type of the result of the last compileEvaluation(ELNode, ExpressionDef, Function), or null when it is not known
    • inferredEvaluationType

      public io.micronaut.inject.ast.ClassElement inferredEvaluationType()
      The expected type inferred for an expression whose declaration omits it: the static type of the result, a primitive as the wrapper the evaluation returns, Object when the compiler does not know the type.
      Returns:
      The inferred type
    • inferredType

      public io.micronaut.inject.ast.ClassElement inferredType(ELNode node)
      The static type of an expression, for the return type of a method expression whose declaration omits it.
      Parameters:
      node - The parsed expression
      Returns:
      The inferred type
    • evaluatesTo

      public boolean evaluatesTo(io.micronaut.inject.ast.ClassElement expected)
      Parameters:
      expected - The expected type of a value expression
      Returns:
      Whether the result of the last compileEvaluation(ELNode, ExpressionDef, Function) is known to be of the expected type, so that the coercion of the section 1.23 of the specification is the identity
    • compileLValue

      public @Nullable ELCompiler.LValue compileLValue(ELNode node, io.micronaut.sourcegen.model.ExpressionDef elContext)
      Compiles the base object and the property of an lvalue, as described in the section 1.13 of the specification.
      Parameters:
      node - The parsed expression
      elContext - The expression holding the jakarta.el.ELContext
      Returns:
      The lvalue or null when the expression is not an lvalue
    • invokeRuntime

      public io.micronaut.sourcegen.model.ExpressionDef invokeRuntime(io.micronaut.sourcegen.model.ClassTypeDef owner, String name, io.micronaut.sourcegen.model.TypeDef returning, io.micronaut.sourcegen.model.ExpressionDef... values)
      Invokes a static method of the runtime with the signature the runtime actually declares, for the writers.
      Parameters:
      owner - The declaring type
      name - The method name
      returning - The type the result is used as
      values - The arguments
      Returns:
      The invocation
      See Also:
      • runtime(ClassTypeDef, String, TypeDef, ExpressionDef...)
    • compileTyped

      public ELCompiler.Typed compileTyped(ELNode node, io.micronaut.sourcegen.model.ExpressionDef ctx)
      Compiles an expression, with its static type.
      Parameters:
      node - The parsed expression
      ctx - The expression holding the jakarta.el.ELContext
      Returns:
      The compiled expression and its type
    • unresolvedIdentifiers

      public Set<String> unresolvedIdentifiers()
      Returns:
      The identifiers the compiled code resolves dynamically because nothing declares them: not a variable, not a lambda parameter, not an import
    • canonical

      public String canonical(ELNode node)
      Returns the equality form of a generated expression, replacing declared function names with the methods bound to them as required by the Jakarta EL expression equality contract.
      Parameters:
      node - The parsed expression
      Returns:
      The equality form
    • hasAssignments

      public static boolean hasAssignments(ELNode node)
      Parameters:
      node - A parsed expression
      Returns:
      Whether the expression assigns or sequences, which makes what an identifier denotes dynamic