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
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 ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface notified when effectively final analysis detects a problem. -
Constructor Summary
ConstructorsConstructorDescriptionFinalVariableAnalyzer(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 TypeMethodDescriptionprotected SourceUnitProvides access to theSourceUnitfor error reporting during visitation.booleanDetermines if a variable is effectively final, meaning it is not reassigned after initialization.voidVisits anArgumentListExpression, treating it as a tuple expression.voidvisitBinaryExpression(BinaryExpression expression) Visits aBinaryExpression, traversing left and right operand expressions.voidVisits aBlockStatement, invoking the statement hook before parent traversal.voidvisitClosureExpression(ClosureExpression expression) Visits aClosureExpressionwith annotation processing, traversing parameter annotations before delegating to parent traversal.voidvisitIfElse(IfStatement ifElse) Visits anIfStatement, invoking the statement hook before parent traversal.voidvisitPostfixExpression(PostfixExpression expression) Visits aPostfixExpression, traversing the operand expression.voidvisitPrefixExpression(PrefixExpression expression) Visits aPrefixExpression, traversing the operand expression.voidvisitSwitch(SwitchStatement switchS) Visits aSwitchStatement, invoking the statement hook before parent traversal.voidvisitTryCatchFinally(TryCatchStatement statement) Visits aTryCatchStatement, invoking the statement hook before parent traversal.voidvisitVariableExpression(VariableExpression expression) Visits aVariableExpression.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, visitWhileLoopMethods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport
afterSwitchCaseStatementsVisited, 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, visitUnaryPlusExpressionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.GroovyCodeVisitor
visit, visit, visitEmptyExpression, visitListOfExpressions
-
Constructor Details
-
FinalVariableAnalyzer
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 analyzedcallback- the callback to invoke when a variable is found to not be final, may be null
-
-
Method Details
-
getSourceUnit
Provides access to theSourceUnitfor error reporting during visitation. Implementations must override this method.- Specified by:
getSourceUnitin classClassCodeVisitorSupport- Returns:
- the source unit for this visitor
-
isEffectivelyFinal
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
Visits aBlockStatement, invoking the statement hook before parent traversal.- Specified by:
visitBlockStatementin interfaceGroovyCodeVisitor- Overrides:
visitBlockStatementin classClassCodeVisitorSupport- Parameters:
block- the block statement to visit- See Also:
-
visitArgumentlistExpression
Visits anArgumentListExpression, treating it as a tuple expression.- Specified by:
visitArgumentlistExpressionin interfaceGroovyCodeVisitor- Overrides:
visitArgumentlistExpressionin classCodeVisitorSupport- Parameters:
ale- the argument list expression
-
visitBinaryExpression
Visits aBinaryExpression, traversing left and right operand expressions.- Specified by:
visitBinaryExpressionin interfaceGroovyCodeVisitor- Overrides:
visitBinaryExpressionin classCodeVisitorSupport- Parameters:
expression- the binary expression
-
visitClosureExpression
Visits aClosureExpressionwith annotation processing, traversing parameter annotations before delegating to parent traversal.- Specified by:
visitClosureExpressionin interfaceGroovyCodeVisitor- Overrides:
visitClosureExpressionin classClassCodeVisitorSupport- Parameters:
expression- the closure expression to visit
-
visitPrefixExpression
Visits aPrefixExpression, traversing the operand expression.- Specified by:
visitPrefixExpressionin interfaceGroovyCodeVisitor- Overrides:
visitPrefixExpressionin classCodeVisitorSupport- Parameters:
expression- the prefix expression
-
visitPostfixExpression
Visits aPostfixExpression, traversing the operand expression.- Specified by:
visitPostfixExpressionin interfaceGroovyCodeVisitor- Overrides:
visitPostfixExpressionin classCodeVisitorSupport- Parameters:
expression- the postfix expression
-
visitVariableExpression
Visits aVariableExpression. No traversal is performed as variable expressions contain no child expressions.- Specified by:
visitVariableExpressionin interfaceGroovyCodeVisitor- Overrides:
visitVariableExpressionin classCodeVisitorSupport- Parameters:
expression- the variable expression
-
visitIfElse
Visits anIfStatement, invoking the statement hook before parent traversal.- Specified by:
visitIfElsein interfaceGroovyCodeVisitor- Overrides:
visitIfElsein classClassCodeVisitorSupport- Parameters:
ifElse- the if statement to visit
-
visitSwitch
Visits aSwitchStatement, invoking the statement hook before parent traversal.- Specified by:
visitSwitchin interfaceGroovyCodeVisitor- Overrides:
visitSwitchin classClassCodeVisitorSupport- Parameters:
switchS- the switch statement to visit
-
visitTryCatchFinally
Visits aTryCatchStatement, invoking the statement hook before parent traversal.- Specified by:
visitTryCatchFinallyin interfaceGroovyCodeVisitor- Overrides:
visitTryCatchFinallyin classClassCodeVisitorSupport- Parameters:
statement- the try-catch statement to visit
-