Class ReturnStatement

All Implemented Interfaces:
NodeMetaDataHandler

public class ReturnStatement extends Statement
Represents a return statement that terminates a method and optionally returns a value. A return statement evaluates the optional return expression and returns control and the resulting value to the calling code. A return with no expression returns null or void depending on the method signature.
  • Field Details

    • RETURN_NULL_OR_VOID

      public static final ReturnStatement RETURN_NULL_OR_VOID
      Only used for synthetic return statements emitted by the compiler. For comparisons use isReturningNullOrVoid() instead.
  • Constructor Details

    • ReturnStatement

      public ReturnStatement(ExpressionStatement statement)
      Constructs a return statement from an expression statement by extracting its expression.
      Parameters:
      statement - the ExpressionStatement whose expression will be returned
    • ReturnStatement

      public ReturnStatement(Expression expression)
      Constructs a return statement with the given return expression.
      Parameters:
      expression - the Expression to return, or null to return without a value
  • Method Details

    • getExpression

      public Expression getExpression()
      Returns the expression whose value will be returned.
      Returns:
      the Expression to return, or null if returning without a value
    • setExpression

      public void setExpression(Expression expression)
      Sets the expression whose value will be returned.
      Parameters:
      expression - the Expression to return
    • 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
    • isReturningNullOrVoid

      public boolean isReturningNullOrVoid()
      Checks whether this return statement returns null or void. This method is used to identify implicit or synthetic return statements generated by the compiler.
      Returns:
      true if the return expression is a null constant; false otherwise
    • 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