Package org.codehaus.groovy.classgen

# Class FinalVariableAnalyzer

java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.classgen.FinalVariableAnalyzer

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

public class FinalVariableAnalyzer extends ClassCodeVisitorSupport
Analyzes variable assignments to determine if variables are effectively final, which is required for use in closures and lambda expressions. This visitor tracks variable states through control flow to detect reassignments that would prevent a variable from being considered effectively final.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `FinalVariableAnalyzer.VariableNotFinalCallback`
  Callback interface notified when effectively final analysis detects a problem.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FinalVariableAnalyzer(SourceUnit sourceUnit)`
  Creates a new final variable analyzer without a callback.
  `FinalVariableAnalyzer(SourceUnit sourceUnit, FinalVariableAnalyzer.VariableNotFinalCallback callback)`
  Creates a new final variable analyzer with a callback for non-final variable detection.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected SourceUnit`
  `getSourceUnit()`
  Provides access to the `SourceUnit` for error reporting during visitation.
  `boolean`
  `isEffectivelyFinal(Variable v)`
  Determines if a variable is effectively final, meaning it is not reassigned after initialization.
  `void`
  `visitArgumentlistExpression(ArgumentListExpression ale)`
  Visits an `ArgumentListExpression`, treating it as a tuple expression.
  `void`
  `visitBinaryExpression(BinaryExpression expression)`
  Visits a `BinaryExpression`, traversing left and right operand expressions.
  `void`
  `visitBlockStatement(BlockStatement block)`
  Visits a `BlockStatement`, invoking the statement hook before parent traversal.
  `void`
  `visitClosureExpression(ClosureExpression expression)`
  Visits a `ClosureExpression` with annotation processing, traversing parameter annotations before delegating to parent traversal.
  `void`
  `visitIfElse(IfStatement ifElse)`
  Visits an `IfStatement`, invoking the statement hook before parent traversal.
  `void`
  `visitPostfixExpression(PostfixExpression expression)`
  Visits a `PostfixExpression`, traversing the operand expression.
  `void`
  `visitPrefixExpression(PrefixExpression expression)`
  Visits a `PrefixExpression`, traversing the operand expression.
  `void`
  `visitSwitch(SwitchStatement switchS)`
  Visits a `SwitchStatement`, invoking the statement hook before parent traversal.
  `void`
  `visitSwitchExpression(SwitchExpression expression)`
  Visits a `SwitchExpression`, traversing the selector, case arms, and default arm (GROOVY-12255).
  `void`
  `visitTryCatchFinally(TryCatchStatement statement)`
  Visits a `TryCatchStatement`, invoking the statement hook before parent traversal.
  `void`
  `visitVariableExpression(VariableExpression expression)`
  Visits a `VariableExpression`.

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

  `addError, visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBreakStatement, visitCaseStatement, visitCatchStatement, visitClass, visitClassCodeContainer, visitConstructor, visitConstructorOrMethod, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitExpressionStatement, visitField, visitForLoop, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitProperty, visitReturnStatement, visitStatement, visitStatementAnnotations, visitSynchronizedStatement, visitThrowStatement, visitWhileLoop, visitYieldStatement`

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

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

  ### 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

  - ### FinalVariableAnalyzer

    public FinalVariableAnalyzer(SourceUnit sourceUnit)
    Creates a new final variable analyzer without a callback.
    Parameters:
      `sourceUnit` - the source unit being analyzed

  - ### FinalVariableAnalyzer

    public FinalVariableAnalyzer(SourceUnit sourceUnit, FinalVariableAnalyzer.VariableNotFinalCallback callback)
    Creates a new final variable analyzer with a callback for non-final variable detection.
    Parameters:
      `sourceUnit` - the source unit being analyzed
      `callback` - the callback to invoke when a variable is found to not be final, may be null

- ## Method Details

  - ### getSourceUnit

    protected SourceUnit getSourceUnit()
    Provides access to the `SourceUnit` for error reporting during visitation. Implementations must override this method.
    Specified by:
      `getSourceUnit` in class `ClassCodeVisitorSupport`
    Returns:
      the source unit for this visitor

  - ### isEffectivelyFinal

    public boolean isEffectivelyFinal(Variable v)
    Determines if a variable is effectively final, meaning it is not reassigned after initialization. Parameters without any state information are considered effectively final by default.
    Parameters:
      `v` - the variable to check
    Returns:
      true if the variable is effectively final, false otherwise

  - ### visitBlockStatement

    public void visitBlockStatement(BlockStatement block)
    Visits a `BlockStatement`, invoking the statement hook before parent traversal.
    Specified by:
      `visitBlockStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBlockStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `block` - the block statement to visit
    See Also:
      - `CodeVisitorSupport.visitForLoop(ForStatement)`
      - `CodeVisitorSupport.visitWhileLoop(WhileStatement)`

  - ### visitArgumentlistExpression

    public void visitArgumentlistExpression(ArgumentListExpression ale)
    Visits an `ArgumentListExpression`, treating it as a tuple expression.
    Specified by:
      `visitArgumentlistExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitArgumentlistExpression` in class `CodeVisitorSupport`
    Parameters:
      `ale` - the argument list expression

  - ### visitBinaryExpression

    public void visitBinaryExpression(BinaryExpression expression)
    Visits a `BinaryExpression`, traversing left and right operand expressions.
    Specified by:
      `visitBinaryExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBinaryExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the binary expression

  - ### visitClosureExpression

    public void visitClosureExpression(ClosureExpression expression)
    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

  - ### visitPrefixExpression

    public void visitPrefixExpression(PrefixExpression expression)
    Visits a `PrefixExpression`, traversing the operand expression.
    Specified by:
      `visitPrefixExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitPrefixExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the prefix expression

  - ### visitPostfixExpression

    public void visitPostfixExpression(PostfixExpression expression)
    Visits a `PostfixExpression`, traversing the operand expression.
    Specified by:
      `visitPostfixExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitPostfixExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the postfix expression

  - ### visitVariableExpression

    public void visitVariableExpression(VariableExpression expression)
    Visits a `VariableExpression`. No traversal is performed as variable expressions contain no child expressions.
    Specified by:
      `visitVariableExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitVariableExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the variable expression

  - ### visitIfElse

    public void visitIfElse(IfStatement ifElse)
    Visits an `IfStatement`, invoking the statement hook before parent traversal.
    Specified by:
      `visitIfElse` in interface `GroovyCodeVisitor`
    Overrides:
      `visitIfElse` in class `ClassCodeVisitorSupport`
    Parameters:
      `ifElse` - the if statement to visit

  - ### visitSwitchExpression

    public void visitSwitchExpression(SwitchExpression expression)
    Visits a `SwitchExpression`, traversing the selector, case arms, and default arm (GROOVY-12255).

    `YieldStatement` completes the expression normally, so only `throw`/`return` skip the join after it (GROOVY-12255).

    Specified by:
      `visitSwitchExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitSwitchExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the switch expression

  - ### visitSwitch

    public void visitSwitch(SwitchStatement switchS)
    Visits a `SwitchStatement`, invoking the statement hook before parent traversal.
    Specified by:
      `visitSwitch` in interface `GroovyCodeVisitor`
    Overrides:
      `visitSwitch` in class `ClassCodeVisitorSupport`
    Parameters:
      `switchS` - the switch statement to visit

  - ### visitTryCatchFinally

    public void visitTryCatchFinally(TryCatchStatement statement)
    Visits a `TryCatchStatement`, invoking the statement hook before parent traversal.
    Specified by:
      `visitTryCatchFinally` in interface `GroovyCodeVisitor`
    Overrides:
      `visitTryCatchFinally` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the try-catch statement to visit