Class BinaryExpression

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler
Direct Known Subclasses:
CompareIdentityExpression, CompareToNullExpression, DeclarationExpression

public class BinaryExpression extends Expression
Represents a binary operation between two expressions, such as addition, subtraction, comparison, or array/map access. The operation is specified by a Token indicating the operation type (e.g., +, -, ==, []). Supports safe operations where null objects are handled gracefully, returning null instead of throwing an exception.
See Also:
  • Constructor Details

    • BinaryExpression

      public BinaryExpression(Expression leftExpression, Token operation, Expression rightExpression)
      Creates a binary expression with the given left operand, operation, and right operand.
      Parameters:
      leftExpression - the left operand of the binary operation; must not be null
      operation - the operation token (e.g., Types.PLUS, Types.MINUS); may not be null
      rightExpression - the right operand of the binary operation; must not be null
    • BinaryExpression

      public BinaryExpression(Expression leftExpression, Token operation, Expression rightExpression, boolean safe)
      Creates a binary expression with optional safe navigation support.
      Parameters:
      leftExpression - the left operand of the binary operation; must not be null
      operation - the operation token; may not be null
      rightExpression - the right operand of the binary operation; must not be null
      safe - if true, enables safe navigation (e.g., null-safe array access with ?[])
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.
      Overrides:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node
    • transformExpression

      public Expression transformExpression(ExpressionTransformer transformer)
      Description copied from class: Expression
      Transforms this expression and any nested expressions according to the provided transformer. This method is called during AST transformation phases and must recursively transform any nested expressions to support full AST tree transformation.
      Specified by:
      transformExpression in class Expression
      Parameters:
      transformer - the ExpressionTransformer to apply
      Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)
    • getLeftExpression

      public Expression getLeftExpression()
      Returns the left operand of this binary expression.
      Returns:
      the left operand Expression; never null
    • setLeftExpression

      public void setLeftExpression(Expression leftExpression)
      Sets the left operand of this binary expression.
      Parameters:
      leftExpression - the new left operand; must not be null
    • setRightExpression

      public void setRightExpression(Expression rightExpression)
      Sets the right operand of this binary expression.
      Parameters:
      rightExpression - the new right operand; must not be null
    • getOperation

      public Token getOperation()
      Returns the operation token representing the binary operation.
      Returns:
      the operation Token (e.g., Types.PLUS, Types.EQUAL); never null
    • getRightExpression

      public Expression getRightExpression()
      Returns the right operand of this binary expression.
      Returns:
      the right operand Expression; never null
    • getText

      public String getText()
      Description copied from class: ASTNode
      Returns a human-readable text representation of this AST node. Used for debugging and error messages. Default implementation returns a message indicating the representation is not yet implemented for this node type.
      Overrides:
      getText in class ASTNode
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • isSafe

      public boolean isSafe()
      Indicates whether this binary expression uses safe navigation. Safe operations return null instead of throwing a NullPointerException when the left operand is null (e.g., obj?.property).
      Returns:
      true if safe navigation is enabled; false otherwise
    • setSafe

      public void setSafe(boolean safe)
      Sets whether this binary expression should use safe navigation.
      Parameters:
      safe - true to enable safe navigation; false otherwise
    • newAssignmentExpression

      public static BinaryExpression newAssignmentExpression(Variable variable, Expression rhs)
      Creates an assignment expression in which the specified expression is written into the specified variable. This is a factory method for constructing assignment operations like x = value.
      Parameters:
      variable - the target variable for assignment; must not be null
      rhs - the right-hand side expression to assign; must not be null
      Returns:
      a new BinaryExpression representing the assignment
    • newInitializationExpression

      public static BinaryExpression newInitializationExpression(String variable, ClassNode type, Expression rhs)
      Creates a variable initialization expression in which the specified expression is written into the specified variable name with an optional type annotation. This is a factory method for constructing typed variable declarations like String x = value.
      Parameters:
      variable - the variable name to initialize; must not be null
      type - the type annotation for the variable; may be null for untyped variables
      rhs - the right-hand side expression to assign; must not be null
      Returns:
      a new BinaryExpression representing the initialization