Class AssertStatement

All Implemented Interfaces:
NodeMetaDataHandler

public class AssertStatement extends Statement
Represents an assert statement that enforces a condition with an optional error message. An assert statement evaluates a boolean condition and throws an AssertionError if the condition is false. An optional message expression may be provided to customize the error message. Assert statements are typically used to verify invariants and debug assumptions.

Example: assert i != 0 : "should never be zero"

  • Constructor Details

    • AssertStatement

      public AssertStatement(BooleanExpression booleanExpression)
      Constructs an assert statement with the given condition and no message.
      Parameters:
      booleanExpression - the BooleanExpression condition to assert
    • AssertStatement

      public AssertStatement(BooleanExpression booleanExpression, Expression messageExpression)
      Constructs an assert statement with the given condition and message expression.
      Parameters:
      booleanExpression - the BooleanExpression condition to assert
      messageExpression - the Expression that evaluates to an optional error message; if null or a null expression, no custom message is used
  • Method Details

    • 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
    • getMessageExpression

      public Expression getMessageExpression()
      Returns the message expression that provides an optional error message.
      Returns:
      the Expression that evaluates to the error message, or a null expression if not provided
    • getBooleanExpression

      public BooleanExpression getBooleanExpression()
      Returns the boolean condition to be asserted.
      Returns:
      the BooleanExpression to evaluate
    • setBooleanExpression

      public void setBooleanExpression(BooleanExpression booleanExpression)
      Sets the boolean condition to be asserted.
      Parameters:
      booleanExpression - the BooleanExpression to evaluate
    • setMessageExpression

      public void setMessageExpression(Expression messageExpression)
      Sets the message expression that provides an optional error message.
      Parameters:
      messageExpression - the Expression that evaluates to the error message