public class StatementWriter
extends Object
Generates bytecode for Groovy statements by visiting AST statement nodes and emitting corresponding JVM instructions via the WriterController. Handles control flow (loops, branches, try/catch), synchronization, assertions, and expression statements.
| Modifiers | Name | Description |
|---|---|---|
protected WriterController |
controller |
The controller coordinating all bytecode writers for the current class. |
| Constructor and description |
|---|
StatementWriter(WriterController controller)Creates a statement writer backed by the given controller. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
protected final BytecodeVariable |
defineLoopIndexVariable(ForStatement statement) |
|
public void |
writeAssert(AssertStatement statement)Generates bytecode for an assert statement. |
|
public void |
writeBlockStatement(BlockStatement block)Generates bytecode for a block statement by visiting each contained statement. |
|
public void |
writeBreak(BreakStatement statement)Generates bytecode for a break statement, applying any intervening finally blocks before the jump. |
|
public void |
writeContinue(ContinueStatement statement)Generates bytecode for a continue statement, applying any intervening finally blocks before the jump. |
|
public void |
writeDoWhileLoop(DoWhileStatement statement)Generates bytecode for a do-while loop. |
|
public void |
writeExpressionStatement(ExpressionStatement statement)Generates bytecode for an expression statement. |
|
protected void |
writeForInLoop(ForStatement statement)Generates bytecode for a for-in loop by calling iterator() on the
collection expression and delegating loop control to
writeForInLoopControlAndBlock. |
|
protected void |
writeForInLoopControlAndBlock(ForStatement statement)Emits the loop-control structure and body for a for-in loop. |
|
protected void |
writeForLoopWithClosureList(ForStatement statement)Generates bytecode for a C-style for(init; cond; incr) loop.
|
|
public void |
writeForStatement(ForStatement statement)Generates bytecode for a for statement. |
|
public void |
writeIfElse(IfStatement statement)Generates bytecode for an if/else statement. |
|
protected void |
writeIteratorHasNext(org.objectweb.asm.MethodVisitor mv)Emits the Iterator.hasNext call via the given visitor. |
|
protected void |
writeIteratorNext(org.objectweb.asm.MethodVisitor mv)Emits the Iterator.next call via the given visitor. |
|
protected final void |
writeLoopBackEdge(org.objectweb.asm.Label continueLabel, boolean bodyMayReachContinue) |
|
public void |
writeReturn(ReturnStatement statement)Generates bytecode for a return statement. |
|
protected void |
writeStatementLabel(Statement statement)Emits bytecode labels for any statement labels attached to statement.
|
|
public void |
writeSwitch(SwitchStatement statement)Generates bytecode for a switch statement. |
|
public void |
writeSynchronized(SynchronizedStatement statement)Generates bytecode for a synchronized statement. |
|
public void |
writeThrow(ThrowStatement statement)Generates bytecode for a throw statement. |
|
public void |
writeTryCatchFinally(TryCatchStatement statement)Generates bytecode for a try/catch/finally statement. |
|
public void |
writeWhileLoop(WhileStatement statement)Generates bytecode for a while loop. |
The controller coordinating all bytecode writers for the current class.
Creates a statement writer backed by the given controller.
controller - the writer controller for the current compilationGenerates bytecode for an assert statement.
statement - the assert statement to compile Generates bytecode for a block statement by visiting each contained statement.
Pushes the block's variable scope, emits the statements, and pops afterward.
Named labels on the block create a breakable region so that break label
within the block jumps to the end of it.
block - the block statement to compileGenerates bytecode for a break statement, applying any intervening finally blocks before the jump.
statement - the break statement to compileGenerates bytecode for a continue statement, applying any intervening finally blocks before the jump.
statement - the continue statement to compileGenerates bytecode for a do-while loop.
statement - the do-while statement to compileGenerates bytecode for an expression statement. Evaluates the expression and discards any value left on the operand stack. Marks method-call and binary expressions so that unused return values are elided rather than boxed.
statement - the expression statement to compile Generates bytecode for a for-in loop by calling iterator() on the
collection expression and delegating loop control to
writeForInLoopControlAndBlock.
statement - the for-in statement to compile Emits the loop-control structure and body for a for-in loop.
Assumes the iterator object is already on the operand stack.
Declares loop variables, emits the hasNext/next check-and-advance,
generates the loop body, and handles index-variable increment when present.
statement - the for-in statement whose control and body should be emitted Generates bytecode for a C-style for(init; cond; incr) loop.
The collection expression is a ClosureListExpression whose middle
element is the boolean condition, lower elements are initializers, and
upper elements are incrementors.
statement - the for statement with a ClosureListExpression collectionGenerates bytecode for a for statement. Delegates to writeForLoopWithClosureList for C-style loops (using a ClosureListExpression), or to writeForInLoop for for-in loops.
statement - the for statement to compileGenerates bytecode for an if/else statement.
statement - the if statement to compileEmits the Iterator.hasNext call via the given visitor. Overrideable so subclasses can substitute a specialized or inlined variant.
mv - the method visitor to write toEmits the Iterator.next call via the given visitor. Overrideable so subclasses can substitute a specialized or inlined variant.
mv - the method visitor to write to Generates bytecode for a return statement.
For void methods emits RETURN after applying any finally blocks.
For value-returning methods evaluates the expression, casts it to the
declared return type, and emits the appropriate typed return instruction.
statement - the return statement to compile Emits bytecode labels for any statement labels attached to statement.
Called before emitting the body of every statement so that named labels
(break foo / continue foo) resolve correctly.
statement - the statement whose labels should be emitted Generates bytecode for a switch statement.
Each case expression is compared using Groovy's isCase operator,
so non-integer switch expressions are supported.
statement - the switch statement to compile Generates bytecode for a synchronized statement.
Stores the monitor object in a local variable, emits
MONITORENTER/MONITOREXIT guards, and registers
a catch-all exception handler that exits the monitor before rethrowing.
statement - the synchronized statement to compile Generates bytecode for a throw statement.
Casts the expression to Throwable and emits ATHROW.
statement - the throw statement to compile Generates bytecode for a try/catch/finally statement.
Handles exception table registration, finally-block inlining at every
exit path, and a catch-all rethrow for exceptions not handled by
any catch clause.
statement - the try/catch/finally statement to compileGenerates bytecode for a while loop.
statement - the while statement to compileCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.