Package org.codehaus.groovy.control

# Class LabelVerifier

java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.control.LabelVerifier

All Implemented Interfaces:
  `GroovyClassVisitor`, `GroovyCodeVisitor`, `ErrorCollecting`

public class LabelVerifier extends ClassCodeVisitorSupport
This class checks the handling of labels in the AST

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `LabelVerifier(SourceUnit src)`
  Creates a verifier for label usage in one source unit.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `assertNoLabelsMissed()`
  Reports any labeled break or continue statements that could not be matched.
  `protected SourceUnit`
  `getSourceUnit()`
  Returns the source unit currently being verified.
  `protected void`
  `postVisitStatement(Statement s)`
  Removes any labels that go out of scope after visiting the current statement.
  `void`
  `visitAssertStatement(AssertStatement statement)`
  Visits an assert statement and then runs post-visit bookkeeping.
  `void`
  `visitBlockStatement(BlockStatement statement)`
  Visits a block statement and then runs post-visit bookkeeping.
  `void`
  `visitBreakStatement(BreakStatement breakStatement)`
  Verifies that a `break` statement appears in a valid context.
  `void`
  `visitCaseStatement(CaseStatement statement)`
  Visits a case statement and then runs post-visit bookkeeping.
  `void`
  `visitCatchStatement(CatchStatement statement)`
  Visits a catch statement and then runs post-visit bookkeeping.
  `protected void`
  `visitClassCodeContainer(Statement code)`
  Resets label-tracking state before verifying a class code block.
  `void`
  `visitClosureExpression(ClosureExpression expression)`
  Visits a `ClosureExpression` with annotation processing, traversing parameter annotations before delegating to parent traversal.
  `void`
  `visitContinueStatement(ContinueStatement continueStatement)`
  Verifies that a `continue` statement appears in a valid loop context.
  `void`
  `visitDoWhileLoop(DoWhileStatement loop)`
  Verifies labels inside a `do`/`while` loop.
  `void`
  `visitEmptyStatement(EmptyStatement statement)`
  Visits an empty statement and then runs post-visit bookkeeping.
  `void`
  `visitExpressionStatement(ExpressionStatement statement)`
  Visits an expression statement and then runs post-visit bookkeeping.
  `void`
  `visitForLoop(ForStatement loop)`
  Verifies labels inside a `for` loop.
  `void`
  `visitIfElse(IfStatement cond)`
  Verifies labels inside an `if`/`else` statement.
  `void`
  `visitLambdaExpression(LambdaExpression expression)`
  Visits a `LambdaExpression`, treating it as a closure expression.
  `void`
  `visitReturnStatement(ReturnStatement statement)`
  Visits a return statement and then runs post-visit bookkeeping.
  `protected void`
  `visitStatement(Statement s)`
  Registers any labels introduced by the current statement.
  `void`
  `visitSwitch(SwitchStatement switchStatement)`
  Verifies labels inside a `switch` statement.
  `void`
  `visitSwitchExpression(SwitchExpression expression)`
  Verifies labels and `yield` inside a switch expression (GROOVY-12255).
  `void`
  `visitSynchronizedStatement(SynchronizedStatement statement)`
  Visits a synchronized statement and then runs post-visit bookkeeping.
  `void`
  `visitThrowStatement(ThrowStatement statement)`
  Visits a throw statement and then runs post-visit bookkeeping.
  `void`
  `visitTryCatchFinally(TryCatchStatement statement)`
  Visits a try-catch-finally statement and then runs post-visit bookkeeping.
  `void`
  `visitWhileLoop(WhileStatement loop)`
  Verifies labels inside a `while` loop.
  `void`
  `visitYieldStatement(YieldStatement statement)`
  Verifies that a `yield` statement targets the innermost switch expression and does not cross a closure or lambda that was entered after that switch (GROOVY-12255 / JEP 361).

  ### Methods inherited from class org.codehaus.groovy.ast.ClassCodeVisitorSupport

  `addError, visitAnnotation, visitAnnotations, visitAnnotations, visitClass, visitConstructor, visitConstructorOrMethod, visitDeclarationExpression, visitField, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitProperty, visitStatementAnnotations`

  ### Methods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport

  `afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBooleanExpression, visitBytecodeExpression, visitCastExpression, visitClassExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitFieldExpression, visitGStringExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitTernaryExpression, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.GroovyCodeVisitor

  `visit, visit, visitEmptyExpression, visitListOfExpressions`

- ## Constructor Details

  - ### LabelVerifier

    public LabelVerifier(SourceUnit src)
    Creates a verifier for label usage in one source unit.
    Parameters:
      `src` - the source unit to report errors against

- ## Method Details

  - ### getSourceUnit

    protected SourceUnit getSourceUnit()
    Returns the source unit currently being verified.
    Specified by:
      `getSourceUnit` in class `ClassCodeVisitorSupport`
    Returns:
      the active source unit

  - ### assertNoLabelsMissed

    protected void assertNoLabelsMissed()
    Reports any labeled break or continue statements that could not be matched.

  - ### visitStatement

    protected void visitStatement(Statement s)
    Registers any labels introduced by the current statement.
    Parameters:
      `s` - the statement being entered

  - ### postVisitStatement

    protected void postVisitStatement(Statement s)
    Removes any labels that go out of scope after visiting the current statement.
    Parameters:
      `s` - the statement being exited

  - ### visitClassCodeContainer

    protected void visitClassCodeContainer(Statement code)
    Resets label-tracking state before verifying a class code block.
    Overrides:
      `visitClassCodeContainer` in class `ClassCodeVisitorSupport`
    Parameters:
      `code` - the statement container to verify

  - ### visitIfElse

    public void visitIfElse(IfStatement cond)
    Verifies labels inside an `if`/`else` statement.
    Specified by:
      `visitIfElse` in interface `GroovyCodeVisitor`
    Parameters:
      `cond` - the conditional statement to inspect

  - ### visitForLoop

    public void visitForLoop(ForStatement loop)
    Verifies labels inside a `for` loop.
    Specified by:
      `visitForLoop` in interface `GroovyCodeVisitor`
    Parameters:
      `loop` - the loop to inspect

  - ### visitWhileLoop

    public void visitWhileLoop(WhileStatement loop)
    Verifies labels inside a `while` loop.
    Specified by:
      `visitWhileLoop` in interface `GroovyCodeVisitor`
    Parameters:
      `loop` - the loop to inspect

  - ### visitDoWhileLoop

    public void visitDoWhileLoop(DoWhileStatement loop)
    Verifies labels inside a `do`/`while` loop.
    Specified by:
      `visitDoWhileLoop` in interface `GroovyCodeVisitor`
    Parameters:
      `loop` - the loop to inspect

  - ### visitSwitch

    public void visitSwitch(SwitchStatement switchStatement)
    Verifies labels inside a `switch` statement.
    Specified by:
      `visitSwitch` in interface `GroovyCodeVisitor`
    Parameters:
      `switchStatement` - the switch statement to inspect

  - ### visitSwitchExpression

    public void visitSwitchExpression(SwitchExpression expression)
    Verifies labels and `yield` inside a switch expression (GROOVY-12255). Per JEP 361, `break` and `continue` cannot transfer control out of a switch expression, so the enclosing loop, switch, and label context does not flow into the arms; loops and labels declared inside an arm remain valid targets.
    Specified by:
      `visitSwitchExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitSwitchExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the switch expression to inspect
    Since:
      6.0.0

  - ### visitYieldStatement

    public void visitYieldStatement(YieldStatement statement)
    Verifies that a `yield` statement targets the innermost switch expression and does not cross a closure or lambda that was entered after that switch (GROOVY-12255 / JEP 361). A switch expression that itself lives inside a closure (for example a GINQ query) is allowed.
    Specified by:
      `visitYieldStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitYieldStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the yield statement to inspect
    Since:
      6.0.0

  - ### visitClosureExpression

    public void visitClosureExpression(ClosureExpression expression)
    Description copied from class: `ClassCodeVisitorSupport`
    Visits a `ClosureExpression` with annotation processing, traversing parameter annotations before delegating to parent traversal.
    Specified by:
      `visitClosureExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitClosureExpression` in class `ClassCodeVisitorSupport`
    Parameters:
      `expression` - the closure expression to visit

  - ### visitLambdaExpression

    public void visitLambdaExpression(LambdaExpression expression)
    Description copied from class: `CodeVisitorSupport`
    Visits a `LambdaExpression`, treating it as a closure expression.
    Specified by:
      `visitLambdaExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitLambdaExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the lambda expression

  - ### visitBreakStatement

    public void visitBreakStatement(BreakStatement breakStatement)
    Verifies that a `break` statement appears in a valid context.
    Specified by:
      `visitBreakStatement` in interface `GroovyCodeVisitor`
    Parameters:
      `breakStatement` - the break statement to inspect

  - ### visitContinueStatement

    public void visitContinueStatement(ContinueStatement continueStatement)
    Verifies that a `continue` statement appears in a valid loop context.
    Specified by:
      `visitContinueStatement` in interface `GroovyCodeVisitor`
    Parameters:
      `continueStatement` - the continue statement to inspect

  - ### visitAssertStatement

    public void visitAssertStatement(AssertStatement statement)
    Visits an assert statement and then runs post-visit bookkeeping.
    Specified by:
      `visitAssertStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitAssertStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitBlockStatement

    public void visitBlockStatement(BlockStatement statement)
    Visits a block statement and then runs post-visit bookkeeping.
    Specified by:
      `visitBlockStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBlockStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit
    See Also:
      - `CodeVisitorSupport.visitForLoop(ForStatement)`
      - `CodeVisitorSupport.visitWhileLoop(WhileStatement)`

  - ### visitCaseStatement

    public void visitCaseStatement(CaseStatement statement)
    Visits a case statement and then runs post-visit bookkeeping.
    Specified by:
      `visitCaseStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitCaseStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitCatchStatement

    public void visitCatchStatement(CatchStatement statement)
    Visits a catch statement and then runs post-visit bookkeeping.
    Specified by:
      `visitCatchStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitCatchStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitEmptyStatement

    public void visitEmptyStatement(EmptyStatement statement)
    Visits an empty statement and then runs post-visit bookkeeping.
    Specified by:
      `visitEmptyStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitEmptyStatement` in class `CodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitExpressionStatement

    public void visitExpressionStatement(ExpressionStatement statement)
    Visits an expression statement and then runs post-visit bookkeeping.
    Specified by:
      `visitExpressionStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitExpressionStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitReturnStatement

    public void visitReturnStatement(ReturnStatement statement)
    Visits a return statement and then runs post-visit bookkeeping.
    Specified by:
      `visitReturnStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitReturnStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitSynchronizedStatement

    public void visitSynchronizedStatement(SynchronizedStatement statement)
    Visits a synchronized statement and then runs post-visit bookkeeping.
    Specified by:
      `visitSynchronizedStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitSynchronizedStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitThrowStatement

    public void visitThrowStatement(ThrowStatement statement)
    Visits a throw statement and then runs post-visit bookkeeping.
    Specified by:
      `visitThrowStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitThrowStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit

  - ### visitTryCatchFinally

    public void visitTryCatchFinally(TryCatchStatement statement)
    Visits a try-catch-finally statement and then runs post-visit bookkeeping.
    Specified by:
      `visitTryCatchFinally` in interface `GroovyCodeVisitor`
    Overrides:
      `visitTryCatchFinally` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the statement to visit