Package org.codehaus.groovy.ast
Class VariableScope
java.lang.Object
org.codehaus.groovy.ast.VariableScope
Manages variable scope tracking for a given code block, tracking declared and referenced variables
to determine variable sharing patterns across closure and method boundaries.
Maintains hierarchical scope information with parent references, supports both class-level and
statement-level scopes, and tracks static context for proper variable access semantics.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a root variable scope with no parent.VariableScope(VariableScope parent) Creates a variable scope with a parent scope, enabling hierarchical scope traversal. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a shallow copy of this variable scope with the same parent, class scope, static context, and copies of all variable maps.Returns theClassNodeif this scope corresponds to a class body, or null for method bodies, block statements, or other non-class scopes.getDeclaredVariable(String name) Returns a variable declared in this scope by name, or null if not found.Returns an unmodifiable map of variables declared in this scope.Returns an unmodifiable iterator over declared variables in this scope.Returns the parent scope, or null if this is a root scope.Returns a class variable referenced by this scope by name, or null if not found.Returns an unmodifiable map of class variables referenced by this scope.Returns an unmodifiable iterator over class variables referenced by this scope.Returns a local variable referenced by this scope by name, or null if not found.intReturns the count of local variables referenced by this scope.Returns a modifiable iterator over local variables referenced by this scope.booleanReturns true if this scope corresponds to a class body scope (as opposed to a method, block statement, or other non-class scope).booleanReturns true if this scope is in a static context (static initializers, static methods, etc.).booleanReturns true if the specified variable name is in the referenced class variables map.booleanReturns true if the specified variable name is in the referenced local variables map.booleanisRoot()Returns true if this is a root scope (no parent).voidAdds a variable as declared in this scope.voidAdds a variable as a referenced class variable in this scope.voidAdds a variable as a referenced local variable in this scope.Removes a class variable reference from this scope by name.voidsetClassScope(ClassNode classScope) Sets theClassNodefor this scope if it represents a class body.voidsetInStaticContext(boolean inStaticContext) Marks this scope as being in a static context.
-
Constructor Details
-
VariableScope
public VariableScope()Creates a root variable scope with no parent. -
VariableScope
Creates a variable scope with a parent scope, enabling hierarchical scope traversal.- Parameters:
parent- the parentVariableScope, or null for a root scope
-
-
Method Details
-
getParent
Returns the parent scope, or null if this is a root scope.- Returns:
- the parent
VariableScope, or null
-
isRoot
public boolean isRoot()Returns true if this is a root scope (no parent).- Returns:
- true if this scope has no parent
-
getClassScope
Returns theClassNodeif this scope corresponds to a class body, or null for method bodies, block statements, or other non-class scopes.- Returns:
- the class scope
ClassNode, or null if not a class scope
-
isClassScope
public boolean isClassScope()Returns true if this scope corresponds to a class body scope (as opposed to a method, block statement, or other non-class scope).- Returns:
- true if this is a class scope
-
setClassScope
Sets theClassNodefor this scope if it represents a class body.- Parameters:
classScope- the classClassNode
-
isInStaticContext
public boolean isInStaticContext()Returns true if this scope is in a static context (static initializers, static methods, etc.).- Returns:
- true if in a static context
-
setInStaticContext
public void setInStaticContext(boolean inStaticContext) Marks this scope as being in a static context.- Parameters:
inStaticContext- true if in a static context
-
getDeclaredVariable
Returns a variable declared in this scope by name, or null if not found.- Parameters:
name- the variable name- Returns:
- the
Variable, or null if not declared in this scope
-
getReferencedLocalVariable
Returns a local variable referenced by this scope by name, or null if not found. Referenced local variables are those accessed from parent scopes.- Parameters:
name- the variable name- Returns:
- the
Variable, or null if not referenced
-
getReferencedClassVariable
Returns a class variable referenced by this scope by name, or null if not found. Referenced class variables are those accessed from the class scope.- Parameters:
name- the variable name- Returns:
- the
Variable, or null if not referenced
-
isReferencedLocalVariable
Returns true if the specified variable name is in the referenced local variables map.- Parameters:
name- the variable name- Returns:
- true if referenced locally
-
isReferencedClassVariable
Returns true if the specified variable name is in the referenced class variables map.- Parameters:
name- the variable name- Returns:
- true if referenced from the class scope
-
getDeclaredVariables
Returns an unmodifiable map of variables declared in this scope. The map is empty if no variables are declared.- Returns:
- an unmodifiable map of declared variables
-
getReferencedClassVariables
Returns an unmodifiable map of class variables referenced by this scope. The map is empty if no class variables are referenced.- Returns:
- an unmodifiable map of referenced class variables
-
getReferencedLocalVariablesCount
public int getReferencedLocalVariablesCount()Returns the count of local variables referenced by this scope.- Returns:
- the number of referenced local variables
-
getDeclaredVariablesIterator
Returns an unmodifiable iterator over declared variables in this scope. The remove operation is not supported.- Returns:
- an iterator over declared variables
-
getReferencedLocalVariablesIterator
Returns a modifiable iterator over local variables referenced by this scope. The remove operation is supported and removes the variable from the scope.- Returns:
- an iterator over referenced local variables
-
getReferencedClassVariablesIterator
Returns an unmodifiable iterator over class variables referenced by this scope. The remove operation is not supported.- Returns:
- an iterator over referenced class variables
-
putDeclaredVariable
Adds a variable as declared in this scope. If the declared variables map is not yet initialized, it creates a linked hash map to preserve insertion order.- Parameters:
var- theVariableto declare
-
putReferencedLocalVariable
Adds a variable as a referenced local variable in this scope. Referenced local variables are those accessed from parent or enclosing scopes.- Parameters:
var- theVariableto reference
-
putReferencedClassVariable
Adds a variable as a referenced class variable in this scope. Referenced class variables are those accessed from the class scope.- Parameters:
var- theVariableto reference
-
removeReferencedClassVariable
Removes a class variable reference from this scope by name.- Parameters:
name- the variable name to remove- Returns:
- the removed
Variable, or null if not found
-
copy
Creates a shallow copy of this variable scope with the same parent, class scope, static context, and copies of all variable maps. The returned scope is independent from the original and may be safely modified without affecting the source scope.- Returns:
- a new
VariableScopewith copied configuration
-