public class VariableScope
extends Object
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.
| Constructor and description |
|---|
VariableScope()Creates a root variable scope with no parent. |
VariableScope(VariableScope parent)Creates a variable scope with a parent scope, enabling hierarchical scope traversal. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public VariableScope |
copy()Creates a shallow copy of this variable scope with the same parent, class scope, static context, and copies of all variable maps. |
|
public ClassNode |
getClassScope()Returns the ClassNode if this scope corresponds to a class body, or null for method bodies, block statements, or other non-class scopes. |
|
public Variable |
getDeclaredVariable(String name)Returns a variable declared in this scope by name, or null if not found. |
|
public Map<String, Variable> |
getDeclaredVariables()Returns an unmodifiable map of variables declared in this scope. |
|
public Iterator<Variable> |
getDeclaredVariablesIterator()Returns an unmodifiable iterator over declared variables in this scope. |
|
public VariableScope |
getParent()Returns the parent scope, or null if this is a root scope. |
|
public Variable |
getReferencedClassVariable(String name)Returns a class variable referenced by this scope by name, or null if not found. |
|
public Map<String, Variable> |
getReferencedClassVariables()Returns an unmodifiable map of class variables referenced by this scope. |
|
public Iterator<Variable> |
getReferencedClassVariablesIterator()Returns an unmodifiable iterator over class variables referenced by this scope. |
|
public Variable |
getReferencedLocalVariable(String name)Returns a local variable referenced by this scope by name, or null if not found. |
|
public int |
getReferencedLocalVariablesCount()Returns the count of local variables referenced by this scope. |
|
public Iterator<Variable> |
getReferencedLocalVariablesIterator()Returns a modifiable iterator over local variables referenced by this scope. |
|
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). |
|
public boolean |
isInStaticContext()Returns true if this scope is in a static context (static initializers, static methods, etc.). |
|
public boolean |
isReferencedClassVariable(String name)Returns true if the specified variable name is in the referenced class variables map. |
|
public boolean |
isReferencedLocalVariable(String name)Returns true if the specified variable name is in the referenced local variables map. |
|
public boolean |
isRoot()Returns true if this is a root scope (no parent). |
|
public void |
putDeclaredVariable(Variable var)Adds a variable as declared in this scope. |
|
public void |
putReferencedClassVariable(Variable var)Adds a variable as a referenced class variable in this scope. |
|
public void |
putReferencedLocalVariable(Variable var)Adds a variable as a referenced local variable in this scope. |
|
public Object |
removeReferencedClassVariable(String name)Removes a class variable reference from this scope by name. |
|
public void |
setClassScope(ClassNode classScope)Sets the ClassNode for this scope if it represents a class body. |
|
public void |
setInStaticContext(boolean inStaticContext)Marks this scope as being in a static context. |
Creates a root variable scope with no parent.
Creates a variable scope with a parent scope, enabling hierarchical scope traversal.
parent - the parent VariableScope, or null for a root scopeCreates 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 the ClassNode if this scope corresponds to a class body, or null for method bodies, block statements, or other non-class scopes.
Returns a variable declared in this scope by name, or null if not found.
name - the variable nameReturns an unmodifiable map of variables declared in this scope. The map is empty if no variables are declared.
Returns an unmodifiable iterator over declared variables in this scope. The remove operation is not supported.
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. Referenced class variables are those accessed from the class scope.
name - the variable nameReturns an unmodifiable map of class variables referenced by this scope. The map is empty if no class variables are referenced.
Returns an unmodifiable iterator over class variables referenced by this scope. The remove operation is not supported.
Returns a local variable referenced by this scope by name, or null if not found. Referenced local variables are those accessed from parent scopes.
name - the variable nameReturns the count of local variables referenced by this scope.
Returns a modifiable iterator over local variables referenced by this scope. The remove operation is supported and removes the variable from the scope.
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 scope is in a static context (static initializers, static methods, etc.).
Returns true if the specified variable name is in the referenced class variables map.
name - the variable nameReturns true if the specified variable name is in the referenced local variables map.
name - the variable nameReturns true if this is a root scope (no parent).
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.
var - the Variable to declareAdds a variable as a referenced class variable in this scope. Referenced class variables are those accessed from the class scope.
var - the Variable to referenceAdds a variable as a referenced local variable in this scope. Referenced local variables are those accessed from parent or enclosing scopes.
var - the Variable to referenceRemoves a class variable reference from this scope by name.
name - the variable name to removeSets the ClassNode for this scope if it represents a class body.
classScope - the class ClassNodeMarks this scope as being in a static context.
inStaticContext - true if in a static contextCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.