Class LambdaExpr

All Implemented Interfaces:
NodeWithParameters<LambdaExpr>, NodeWithRange<Node>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneable

public class LambdaExpr extends Expression implements NodeWithParameters<LambdaExpr>

A lambda expression

Java 1-7

Does not exist.

Java 8+

(a, b) -> a + b
a -> ...
(Long a) -> { println(a); }

The parameters are on the left side of the ->. If a parameter uses type inference (it has no type specified) then its type is set to UnknownType. If they are in ( ), "isEnclosingParameters" is true.
The body is to the right of the ->. The body is either a BlockStmt when it is in { } braces, or an ExpressionStmt when it is not in braces.

Author:
Raquel Pau
  • Constructor Details

    • LambdaExpr

      public LambdaExpr()
    • LambdaExpr

      public LambdaExpr(Parameter parameter, BlockStmt body)
      Creates a single parameter lambda expression.
    • LambdaExpr

      public LambdaExpr(NodeList<Parameter> parameters, BlockStmt body)
      Creates a zero or multi-parameter lambda expression with its parameters wrapped in ( ).
    • LambdaExpr

      public LambdaExpr(Parameter parameter, Expression body)
      Creates a single parameter lambda expression.
    • LambdaExpr

      public LambdaExpr(NodeList<Parameter> parameters, Expression body)
      Creates a zero or multi-parameter lambda expression with its parameters wrapped in ( ).
    • LambdaExpr

      public LambdaExpr(NodeList<Parameter> parameters, Statement body, boolean isEnclosingParameters)
    • LambdaExpr

      public LambdaExpr(TokenRange tokenRange, NodeList<Parameter> parameters, Statement body, boolean isEnclosingParameters)
      This constructor is used by the parser and is considered private.
  • Method Details

    • getParameters

      public NodeList<Parameter> getParameters()
      Specified by:
      getParameters in interface NodeWithParameters<LambdaExpr>
    • setParameters

      public LambdaExpr setParameters(NodeList<Parameter> parameters)
      Specified by:
      setParameters in interface NodeWithParameters<LambdaExpr>
    • getBody

      public Statement getBody()
      Returns:
      a BlockStatement or an ExpressionStatement. See class Javadoc.
    • setBody

      public LambdaExpr setBody(Statement body)
    • accept

      public <R,​ A> R accept(GenericVisitor<R,​A> v, A arg)
      Description copied from interface: Visitable
      Accept method for visitor support.
      Specified by:
      accept in interface Visitable
      Type Parameters:
      R - the type of the return value of the visitor
      A - the type the user argument passed to the visitor
      Parameters:
      v - the visitor implementation
      arg - the argument passed to the visitor (of type A)
      Returns:
      the result of the visit (of type R)
    • accept

      public <A> void accept(VoidVisitor<A> v, A arg)
      Description copied from interface: Visitable
      Accept method for visitor support.
      Specified by:
      accept in interface Visitable
      Type Parameters:
      A - the type the argument passed for the visitor
      Parameters:
      v - the visitor implementation
      arg - any value relevant for the visitor (of type A)
    • isEnclosingParameters

      public boolean isEnclosingParameters()
    • setEnclosingParameters

      public LambdaExpr setEnclosingParameters(boolean isEnclosingParameters)
    • remove

      public boolean remove(Node node)
      Overrides:
      remove in class Node
    • getExpressionBody

      public Optional<Expression> getExpressionBody()
      Returns:
      if the body of this lambda is a simple expression, return that expression. Otherwise (when the body is a block) return Optional.empty().
    • clone

      public LambdaExpr clone()
      Overrides:
      clone in class Expression
    • getMetaModel

      public LambdaExprMetaModel getMetaModel()
      Overrides:
      getMetaModel in class Expression
      Returns:
      get JavaParser specific node introspection information.
    • replace

      public boolean replace(Node node, Node replacementNode)
      Overrides:
      replace in class Node
    • isLambdaExpr

      public boolean isLambdaExpr()
      Overrides:
      isLambdaExpr in class Expression
    • asLambdaExpr

      public LambdaExpr asLambdaExpr()
      Overrides:
      asLambdaExpr in class Expression
    • ifLambdaExpr

      public void ifLambdaExpr(Consumer<LambdaExpr> action)
      Overrides:
      ifLambdaExpr in class Expression
    • toLambdaExpr

      public Optional<LambdaExpr> toLambdaExpr()
      Overrides:
      toLambdaExpr in class Expression
    • isPolyExpression

      public boolean isPolyExpression()
      Description copied from class: Expression
      See https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.2
      Overrides:
      isPolyExpression in class Expression
      Returns:
      true if the expression is a poly expression