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.
| Modifiers | Name | Description |
|---|---|---|
interface |
FinalVariableAnalyzer.VariableNotFinalCallback |
Callback interface notified when effectively final analysis detects a problem. |
| 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. |
| 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. * *
|
|
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. * *
|
|
public void |
visitBinaryExpression(BinaryExpression expression)* Visits a BinaryExpression, traversing left and right operand expressions. * *
|
|
public void |
visitBlockStatement(BlockStatement block)* Visits a BlockStatement, invoking the statement hook before parent traversal. * *
|
|
public void |
visitClosureExpression(ClosureExpression expression)* Visits a ClosureExpression with annotation processing, traversing parameter annotations * before delegating to parent traversal. * *
|
|
public void |
visitIfElse(IfStatement ifElse)* Visits an IfStatement, invoking the statement hook before parent traversal. * *
|
|
public void |
visitPostfixExpression(PostfixExpression expression)* Visits a PostfixExpression, traversing the operand expression. * *
|
|
public void |
visitPrefixExpression(PrefixExpression expression)* Visits a PrefixExpression, traversing the operand expression. * *
|
|
public void |
visitSwitch(SwitchStatement switchS)* Visits a SwitchStatement, invoking the statement hook before parent traversal. * *
|
|
public void |
visitTryCatchFinally(TryCatchStatement statement)* Visits a TryCatchStatement, invoking the statement hook before parent traversal. * *
|
|
public void |
visitVariableExpression(VariableExpression expression)* Visits a VariableExpression. No traversal is performed as variable expressions contain no child expressions. * *
|
Creates a new final variable analyzer without a callback.
sourceUnit - the source unit being analyzedCreates a new final variable analyzer with a callback for non-final variable detection.
sourceUnit - the source unit being analyzedcallback - the callback to invoke when a variable is found to not be final, may be null* Provides access to the SourceUnit for error reporting during visitation. * Implementations must override this method. * *
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.
v - the variable to check* Visits an ArgumentListExpression, treating it as a tuple expression. * *
expression - the argument list expression* Visits a BinaryExpression, traversing left and right operand expressions. * *
expression - the binary expression* Visits a BlockStatement, invoking the statement hook before parent traversal. * *
statement - the block statement to visit* Visits a ClosureExpression with annotation processing, traversing parameter annotations * before delegating to parent traversal. * *
expression - the closure expression to visit* Visits an IfStatement, invoking the statement hook before parent traversal. * *
statement - the if statement to visit* Visits a PostfixExpression, traversing the operand expression. * *
expression - the postfix expression* Visits a PrefixExpression, traversing the operand expression. * *
expression - the prefix expression* Visits a SwitchStatement, invoking the statement hook before parent traversal. * *
statement - the switch statement to visit* Visits a TryCatchStatement, invoking the statement hook before parent traversal. * *
statement - the try-catch statement to visit* Visits a VariableExpression. No traversal is performed as variable expressions contain no child expressions. * *
expression - the variable expressionCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.