Package org.codehaus.groovy.ast.stmt
Class BlockStatement
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.stmt.Statement
org.codehaus.groovy.ast.stmt.BlockStatement
- All Implemented Interfaces:
NodeMetaDataHandler
Represents a compound statement consisting of an ordered sequence of
Statements
within a specific VariableScope. A block statement is the fundamental grouping
mechanism in the Groovy AST and is used to represent the body of methods, classes, loops, conditionals,
try-catch blocks, and any other scoped code region.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty BlockStatement with a new default scope.BlockStatement(List<Statement> statements, VariableScope scope) Constructs a BlockStatement with a list of statements and a variable scope.BlockStatement(Statement[] statements, VariableScope scope) Constructs a BlockStatement with an array of statements and a variable scope. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddStatement(Statement statement) Appends aStatementto this block.voidaddStatements(List<Statement> listOfStatements) Appends allStatements from the provided list to this block.Returns the list ofStatements in this block.getText()Returns a human-readable text representation of this AST node.Returns theVariableScopeassociated with this block, which contains variable declarations, type information, and scope metadata.booleanisEmpty()Checks whether this block contains no statements.voidsetVariableScope(VariableScope scope) Sets theVariableScopefor this block.toString()voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.stmt.Statement
addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, setStatementLabelMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Constructor Details
-
BlockStatement
public BlockStatement()Constructs an empty BlockStatement with a new default scope. -
BlockStatement
Constructs a BlockStatement with an array of statements and a variable scope. The provided array is copied internally; subsequent modifications to the array will not affect this BlockStatement.- Parameters:
statements- an array ofStatements to include in this block; must not be null or a NullPointerException will be raisedscope- theVariableScopefor this block, typically containing local variable declarations and type information
-
BlockStatement
Constructs a BlockStatement with a list of statements and a variable scope. A reference to the provided list is maintained; modifications to the list after construction will be reflected in this BlockStatement.- Parameters:
statements- aListofStatements to include in this block; pass an empty list, not null, to avoid NullPointerException laterscope- theVariableScopefor this block, containing variable binding and type metadata
-
-
Method Details
-
visit
Description copied from class:ASTNodeAccepts 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:
visitin classASTNode- Parameters:
visitor- theGroovyCodeVisitorto process this node
-
getStatements
Returns the list ofStatements in this block.- Returns:
- an unmodifiable view or direct reference to the statements list
-
addStatement
Appends aStatementto this block.- Parameters:
statement- theStatementto add; must not be null
-
addStatements
Appends allStatements from the provided list to this block. -
getText
Description copied from class:ASTNodeReturns 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. -
toString
-
isEmpty
public boolean isEmpty()Checks whether this block contains no statements. -
getVariableScope
Returns theVariableScopeassociated with this block, which contains variable declarations, type information, and scope metadata.- Returns:
- the
VariableScopefor this block
-
setVariableScope
Sets theVariableScopefor this block.- Parameters:
scope- theVariableScopeto associate with this block
-