Package: org.codehaus.groovy.classgen

## [Java] Class FinalVariableAnalyzer

- org.codehaus.groovy.classgen.FinalVariableAnalyzer

-     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
    Modifiers | Name | Description
    --- | --- | ---
    `interface` | `FinalVariableAnalyzer.VariableNotFinalCallback` | Callback interface notified when effectively final analysis detects a problem.

  - ### Constructor Summary

    Constructors
    Constructor and 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.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected SourceUnit` | `getSourceUnit()` * Provides access to the SourceUnit for error reporting during visitation. * Implementations must override this method. * * Returns: the source unit for this visitor
    `` | `public boolean` | `isEffectivelyFinal(Variable v)` Determines if a variable is effectively final, meaning it is not reassigned after initialization.
    `` | `public void` | `visitArgumentlistExpression(ArgumentListExpression ale)` * Visits an ArgumentListExpression, treating it as a tuple expression. * * Parameters: `expression` - the argument list expression
    `` | `public void` | `visitBinaryExpression(BinaryExpression expression)` * Visits a BinaryExpression, traversing left and right operand expressions. * * Parameters: `expression` - the binary expression
    `` | `public void` | `visitBlockStatement(BlockStatement block)` * Visits a BlockStatement, invoking the statement hook before parent traversal. * * Parameters: `statement` - the block statement to visit
    `` | `public void` | `visitClosureExpression(ClosureExpression expression)` * Visits a ClosureExpression with annotation processing, traversing parameter annotations * before delegating to parent traversal. * * Parameters: `expression` - the closure expression to visit
    `` | `public void` | `visitIfElse(IfStatement ifElse)` * Visits an IfStatement, invoking the statement hook before parent traversal. * * Parameters: `statement` - the if statement to visit
    `` | `public void` | `visitPostfixExpression(PostfixExpression expression)` * Visits a PostfixExpression, traversing the operand expression. * * Parameters: `expression` - the postfix expression
    `` | `public void` | `visitPrefixExpression(PrefixExpression expression)` * Visits a PrefixExpression, traversing the operand expression. * * Parameters: `expression` - the prefix expression
    `` | `public void` | `visitSwitch(SwitchStatement switchS)` * Visits a SwitchStatement, invoking the statement hook before parent traversal. * * Parameters: `statement` - the switch statement to visit
    `` | `public void` | `visitSwitchExpression(SwitchExpression expression)` * Visits a SwitchExpression, traversing the selector, case arms, and * default arm (GROOVY-12255). * * Parameters: `expression` - the switch expression * Since: 6.0.0
    `` | `public void` | `visitTryCatchFinally(TryCatchStatement statement)` * Visits a TryCatchStatement, invoking the statement hook before parent traversal. * * Parameters: `statement` - the try-catch statement to visit
    `` | `public void` | `visitVariableExpression(VariableExpression expression)` * Visits a VariableExpression. No traversal is performed as variable expressions contain no child expressions. * * Parameters: `expression` - the variable expression

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class ClassCodeVisitorSupport` | `addError, getSourceUnit, visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCaseStatement, visitCatchStatement, visitClass, visitClassCodeContainer, visitClosureExpression, visitConstructor, visitConstructorOrMethod, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitExpressionStatement, visitField, visitForLoop, visitIfElse, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitProperty, visitReturnStatement, visitStatement, visitStatementAnnotations, visitSwitch, visitSynchronizedStatement, visitThrowStatement, visitTryCatchFinally, visitWhileLoop, visitYieldStatement`
    `class CodeVisitorSupport` | `afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAssertStatement, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBlockStatement, visitBooleanExpression, visitBreakStatement, visitBytecodeExpression, visitCaseStatement, visitCastExpression, visitCatchStatement, visitClassExpression, visitClosureExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitEmptyStatement, visitExpressionStatement, visitFieldExpression, visitForLoop, visitGStringExpression, visitIfElse, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitReturnStatement, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitch, visitSwitchExpression, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop, visitYieldStatement`

- - ### Constructor Detail

    - #### public FinalVariableAnalyzer(SourceUnit sourceUnit)

      Creates a new final variable analyzer without a callback.

      Parameters:
        `sourceUnit` - the source unit being analyzed

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

    - #### protected SourceUnit getSourceUnit()

      * Provides access to the SourceUnit for error reporting during visitation. * Implementations must override this method. * *

      Returns:
        the source unit for this visitor

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

    - #### public void visitArgumentlistExpression(ArgumentListExpression ale)

      * Visits an ArgumentListExpression, treating it as a tuple expression. * *

      Parameters:
        `expression` - the argument list expression

    - #### public void visitBinaryExpression(BinaryExpression expression)

      * Visits a BinaryExpression, traversing left and right operand expressions. * *

      Parameters:
        `expression` - the binary expression

    - #### public void visitBlockStatement(BlockStatement block)

      * Visits a BlockStatement, invoking the statement hook before parent traversal. * *

      Parameters:
        `statement` - the block statement to visit

    - #### public void visitClosureExpression(ClosureExpression expression)

      * Visits a ClosureExpression with annotation processing, traversing parameter annotations * before delegating to parent traversal. * *

      Parameters:
        `expression` - the closure expression to visit

    - #### public void visitIfElse(IfStatement ifElse)

      * Visits an IfStatement, invoking the statement hook before parent traversal. * *

      Parameters:
        `statement` - the if statement to visit

    - #### public void visitPostfixExpression(PostfixExpression expression)

      * Visits a PostfixExpression, traversing the operand expression. * *

      Parameters:
        `expression` - the postfix expression

    - #### public void visitPrefixExpression(PrefixExpression expression)

      * Visits a PrefixExpression, traversing the operand expression. * *

      Parameters:
        `expression` - the prefix expression

    - #### public void visitSwitch(SwitchStatement switchS)

      * Visits a SwitchStatement, invoking the statement hook before parent traversal. * *

      Parameters:
        `statement` - the switch statement to visit

    - #### public void visitSwitchExpression(SwitchExpression expression)

      * Visits a SwitchExpression, traversing the selector, case arms, and * default arm (GROOVY-12255). * *

      Parameters:
        `expression` - the switch expression *
      Since:
        6.0.0

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

    - #### public void visitTryCatchFinally(TryCatchStatement statement)

      * Visits a TryCatchStatement, invoking the statement hook before parent traversal. * *

      Parameters:
        `statement` - the try-catch statement to visit

    - #### public void visitVariableExpression(VariableExpression expression)

      * Visits a VariableExpression. No traversal is performed as variable expressions contain no child expressions. * *

      Parameters:
        `expression` - the variable expression