Class VariableScope

java.lang.Object
org.codehaus.groovy.ast.VariableScope

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.
See Also:
  • Constructor Details

    • VariableScope

      public VariableScope()
      Creates a root variable scope with no parent.
    • VariableScope

      public VariableScope(VariableScope parent)
      Creates a variable scope with a parent scope, enabling hierarchical scope traversal.
      Parameters:
      parent - the parent VariableScope, or null for a root scope
  • Method Details

    • getParent

      public VariableScope 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

      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.
      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

      public void setClassScope(ClassNode classScope)
      Sets the ClassNode for this scope if it represents a class body.
      Parameters:
      classScope - the class ClassNode
    • 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

      public Variable getDeclaredVariable(String name)
      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

      public Variable getReferencedLocalVariable(String name)
      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

      public Variable getReferencedClassVariable(String name)
      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

      public boolean isReferencedLocalVariable(String name)
      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

      public boolean isReferencedClassVariable(String name)
      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

      public Map<String,Variable> 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

      public Map<String,Variable> 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

      public Iterator<Variable> getDeclaredVariablesIterator()
      Returns an unmodifiable iterator over declared variables in this scope. The remove operation is not supported.
      Returns:
      an iterator over declared variables
    • getReferencedLocalVariablesIterator

      public Iterator<Variable> 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

      public Iterator<Variable> 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

      public void putDeclaredVariable(Variable var)
      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 - the Variable to declare
    • putReferencedLocalVariable

      public void putReferencedLocalVariable(Variable var)
      Adds a variable as a referenced local variable in this scope. Referenced local variables are those accessed from parent or enclosing scopes.
      Parameters:
      var - the Variable to reference
    • putReferencedClassVariable

      public void putReferencedClassVariable(Variable var)
      Adds a variable as a referenced class variable in this scope. Referenced class variables are those accessed from the class scope.
      Parameters:
      var - the Variable to reference
    • removeReferencedClassVariable

      public Object removeReferencedClassVariable(String name)
      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

      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. The returned scope is independent from the original and may be safely modified without affecting the source scope.
      Returns:
      a new VariableScope with copied configuration