Top Inners Fields Constructors Methods
org.codehaus.groovy.classgen

public Class FinalVariableAnalyzer

extends ClassCodeVisitorSupport
Class Inheritance
Imports
org.codehaus.groovy.ast.ClassCodeVisitorSupport, .Parameter, .Variable, org.codehaus.groovy.ast.expr.ArgumentListExpression, .BinaryExpression, .ClosureExpression, .DeclarationExpression, .EmptyExpression, .Expression, .PostfixExpression, .PrefixExpression, .TupleExpression, .VariableExpression, org.codehaus.groovy.ast.stmt.BlockStatement, .BreakStatement, .CaseStatement, .CatchStatement, .ContinueStatement, .EmptyStatement, .IfStatement, .ReturnStatement, .Statement, .SwitchStatement, .ThrowStatement, .TryCatchStatement, org.codehaus.groovy.control.SourceUnit, org.codehaus.groovy.runtime.DefaultGroovyMethods, org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport, java.lang.reflect.Modifier, java.util.ArrayList, .Deque, .HashMap, .HashSet, .Iterator, .LinkedList, .List, .Map, .Set

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static interface
private static enum

Field Summary

Modifier and TypeField and Description
private final Deque<Map<Variable, FinalVariableAnalyzer.VariableState>>
private final FinalVariableAnalyzer.VariableNotFinalCallback
private Set<Variable>
private boolean
private boolean
private final SourceUnit

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
private void
checkPrePostfixOperation(final Expression variable, final Expression originalExpression)

private void
private boolean

Returns:

true if the block falls through, i.e. no break/return
fallsThrough
(Statement statement)

private void
protected SourceUnit
private Map<Variable, FinalVariableAnalyzer.VariableState>
private static Variable
public boolean
private Map<Variable, FinalVariableAnalyzer.VariableState>
private Map<Variable, FinalVariableAnalyzer.VariableState>
private void
recordAssignment(Variable var, boolean isDeclaration, boolean uninitialized, boolean forceVariable, Expression expression)

private void
recordAssignments(BinaryExpression expression, boolean isDeclaration, Expression leftExpression, Expression rightExpression)

private void
recordFinalVars(Expression leftExpression)

private boolean

Returns:

true if the block's last statement is a return or throw
returningBlock
(Statement block)

public void
public void
public void
private void
public void
public void
public void
public void
public void
public void
public void
Inherited from org.codehaus.groovy.ast.ClassCodeVisitorSupport:
addErrorvisitAnnotationvisitAnnotationsvisitAssertStatementvisitBreakStatementvisitCaseStatementvisitCatchStatementvisitClassvisitClassCodeContainervisitConstructorvisitConstructorOrMethodvisitContinueStatementvisitDeclarationExpressionvisitDoWhileLoopvisitExpressionStatementvisitFieldvisitForLoopvisitImportsvisitMethodvisitObjectInitializerStatementsvisitPackagevisitPropertyvisitReturnStatementvisitStatementvisitSynchronizedStatementvisitThrowStatementvisitWhileLoop

Field Detail

assignmentTrackerback to summary
private final Deque<Map<Variable, FinalVariableAnalyzer.VariableState>> assignmentTracker
callbackback to summary
private final FinalVariableAnalyzer.VariableNotFinalCallback callback
declaredFinalVariablesback to summary
private Set<Variable> declaredFinalVariables
inArgumentListback to summary
private boolean inArgumentList
inAssignmentRHSback to summary
private boolean inAssignmentRHS
sourceUnitback to summary
private final SourceUnit sourceUnit

Constructor Detail

FinalVariableAnalyzerback to summary
public FinalVariableAnalyzer(final SourceUnit sourceUnit)
FinalVariableAnalyzerback to summary
public FinalVariableAnalyzer(final SourceUnit sourceUnit, final FinalVariableAnalyzer.VariableNotFinalCallback callback)

Method Detail

checkPrePostfixOperationback to summary
private void checkPrePostfixOperation(final Expression variable, final Expression originalExpression)
cleanLocalVarsback to summary
private void cleanLocalVars(Map<Variable, FinalVariableAnalyzer.VariableState> origState, Map<Variable, FinalVariableAnalyzer.VariableState> state)
fallsThroughback to summary
private boolean fallsThrough(Statement statement)
Returns:boolean

true if the block falls through, i.e. no break/return

fixVarback to summary
private void fixVar(Variable var)
getSourceUnitback to summary
protected SourceUnit getSourceUnit()

Implements abstract org.codehaus.groovy.ast.ClassCodeVisitorSupport.getSourceUnit.

Annotations
@Override
getStateback to summary
private Map<Variable, FinalVariableAnalyzer.VariableState> getState()
getTargetback to summary
private static Variable getTarget(Variable v)
isEffectivelyFinalback to summary
public boolean isEffectivelyFinal(Variable v)
popStateback to summary
private Map<Variable, FinalVariableAnalyzer.VariableState> popState()
pushStateback to summary
private Map<Variable, FinalVariableAnalyzer.VariableState> pushState()
recordAssignmentback to summary
private void recordAssignment(Variable var, boolean isDeclaration, boolean uninitialized, boolean forceVariable, Expression expression)
recordAssignmentsback to summary
private void recordAssignments(BinaryExpression expression, boolean isDeclaration, Expression leftExpression, Expression rightExpression)
recordFinalVarsback to summary
private void recordFinalVars(Expression leftExpression)
returningBlockback to summary
private boolean returningBlock(Statement block)
Returns:boolean

true if the block's last statement is a return or throw

visitArgumentlistExpressionback to summary
public void visitArgumentlistExpression(ArgumentListExpression ale)

Overrides org.codehaus.groovy.ast.CodeVisitorSupport.visitArgumentlistExpression.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitArgumentlistExpression.

Annotations
@Override
visitBinaryExpressionback to summary
public void visitBinaryExpression(final BinaryExpression expression)

Overrides org.codehaus.groovy.ast.CodeVisitorSupport.visitBinaryExpression.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitBinaryExpression.

Annotations
@Override
visitBlockStatementback to summary
public void visitBlockStatement(final BlockStatement block)

Overrides org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitBlockStatement.

Annotations
@Override
visitCatchFinallyback to summary
private void visitCatchFinally(Map<Variable, FinalVariableAnalyzer.VariableState> initialVarState, List<Map<Variable, FinalVariableAnalyzer.VariableState>> afterTryCatchStates, CatchStatement catchStatement, Statement finallyStatement)
visitClosureExpressionback to summary
public void visitClosureExpression(final ClosureExpression expression)

Overrides org.codehaus.groovy.ast.CodeVisitorSupport.visitClosureExpression.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitClosureExpression.

Annotations
@Override
visitIfElseback to summary
public void visitIfElse(final IfStatement ifElse)

Overrides org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitIfElse.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitIfElse.

Annotations
@Override
visitPostfixExpressionback to summary
public void visitPostfixExpression(final PostfixExpression expression)

Overrides org.codehaus.groovy.ast.CodeVisitorSupport.visitPostfixExpression.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitPostfixExpression.

Annotations
@Override
visitPrefixExpressionback to summary
public void visitPrefixExpression(final PrefixExpression expression)

Overrides org.codehaus.groovy.ast.CodeVisitorSupport.visitPrefixExpression.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitPrefixExpression.

Annotations
@Override
visitSwitchback to summary
public void visitSwitch(SwitchStatement switchS)

Overrides org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitSwitch.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitSwitch.

Annotations
@Override
visitTryCatchFinallyback to summary
public void visitTryCatchFinally(final TryCatchStatement statement)

Overrides org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitTryCatchFinally.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitTryCatchFinally.

Annotations
@Override
visitVariableExpressionback to summary
public void visitVariableExpression(final VariableExpression expression)

Overrides org.codehaus.groovy.ast.CodeVisitorSupport.visitVariableExpression.

Implements org.codehaus.groovy.ast.GroovyCodeVisitor.visitVariableExpression.

Annotations
@Override
org.codehaus.groovy.classgen back to summary

private Class FinalVariableAnalyzer.StateMap

extends HashMap<Variable, FinalVariableAnalyzer.VariableState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public FinalVariableAnalyzer.VariableState
get(final Object
the key whose associated value is to be returned
key
)

Overrides java.util.HashMap.get.

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
public FinalVariableAnalyzer.VariableState
put(final Variable
key with which the specified value is to be associated
key
,
final FinalVariableAnalyzer.VariableState
value to be associated with the specified key
value
)

Overrides java.util.HashMap.put.

Implements java.util.Map.put.

Associates the specified value with the specified key in this map.
Inherited from java.util.HashMap:
clearclonecomputecomputeIfAbsentcomputeIfPresentcontainsKeycontainsValueentrySetforEachgetOrDefaultisEmptykeySetmergenewHashMapputAllputIfAbsentremoveremovereplacereplacereplaceAllsizevalues

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.HashMap.serialVersionUID.

Constructor Detail

StateMapback to summary
private StateMap()

Method Detail

getback to summary
public FinalVariableAnalyzer.VariableState get(final Object key)

Overrides java.util.HashMap.get.

Implements java.util.Map.get.

Doc from java.util.HashMap.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Parameters
key:Object

Doc from java.util.Map.get.

the key whose associated value is to be returned

Returns:FinalVariableAnalyzer.VariableState

Doc from java.util.Map.get.

the value to which the specified key is mapped, or null if this map contains no mapping for the key

Annotations
@Override
putback to summary
public FinalVariableAnalyzer.VariableState put(final Variable key, final FinalVariableAnalyzer.VariableState value)

Overrides java.util.HashMap.put.

Implements java.util.Map.put.

Doc from java.util.HashMap.put.

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

Parameters
key:Variable

key with which the specified value is to be associated

value:FinalVariableAnalyzer.VariableState

value to be associated with the specified key

Returns:FinalVariableAnalyzer.VariableState

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

Annotations
@Override
org.codehaus.groovy.classgen back to summary

public Interface FinalVariableAnalyzer.VariableNotFinalCallback


Method Summary

Modifier and TypeMethod and Description
public void
variableNotAlwaysInitialized(VariableExpression
the variable detected as potentially uninitialized
var
)

Callback used whenever a variable is declared as final, but can remain in an uninitialized state

public void
variableNotFinal(Variable
the variable detected as not final
var
,
Expression
the expression responsible for the contract to be broken
bexp
)

Callback called whenever an assignment transforms an effectively final variable into a non final variable (aka, breaks the "final" modifier contract)

Method Detail

variableNotAlwaysInitializedback to summary
public void variableNotAlwaysInitialized(VariableExpression var)

Callback used whenever a variable is declared as final, but can remain in an uninitialized state

Parameters
var:VariableExpression

the variable detected as potentially uninitialized

variableNotFinalback to summary
public void variableNotFinal(Variable var, Expression bexp)

Callback called whenever an assignment transforms an effectively final variable into a non final variable (aka, breaks the "final" modifier contract)

Parameters
var:Variable

the variable detected as not final

bexp:Expression

the expression responsible for the contract to be broken

org.codehaus.groovy.classgen back to summary

private final Enum FinalVariableAnalyzer.VariableState

extends Enum<FinalVariableAnalyzer.VariableState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final FinalVariableAnalyzer.VariableState
public static final FinalVariableAnalyzer.VariableState
public static final FinalVariableAnalyzer.VariableState
public static final FinalVariableAnalyzer.VariableState
private final boolean

Constructor Summary

AccessConstructor and Description
private
VariableState(final boolean isFinal)

Method Summary

Modifier and TypeMethod and Description
public FinalVariableAnalyzer.VariableState
public boolean
public static FinalVariableAnalyzer.VariableState
public static FinalVariableAnalyzer.VariableState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

is_ambiguousback to summary
public static final FinalVariableAnalyzer.VariableState is_ambiguous
is_finalback to summary
public static final FinalVariableAnalyzer.VariableState is_final
is_uninitializedback to summary
public static final FinalVariableAnalyzer.VariableState is_uninitialized
is_varback to summary
public static final FinalVariableAnalyzer.VariableState is_var
isFinalback to summary
private final boolean isFinal

Constructor Detail

VariableStateback to summary
private VariableState(final boolean isFinal)

Method Detail

getNextback to summary
public FinalVariableAnalyzer.VariableState getNext()
isFinalback to summary
public boolean isFinal()
valueOfback to summary
public static FinalVariableAnalyzer.VariableState valueOf(String name)
valuesback to summary
public static FinalVariableAnalyzer.VariableState[] values()