Package: org.codehaus.groovy.ast

## [Java] Class VariableScope

- 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:
    Variable
    VariableExpression

- - ### Constructor Summary

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

  - ### Methods Summary

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

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public VariableScope()

      Creates a root variable scope with no parent.

    - #### 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 Detail

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

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

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

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

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

    - #### public VariableScope getParent()

      Returns the parent scope, or null if this is a root scope.

      Returns:
        the parent VariableScope, or null

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

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

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

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

    - #### public int getReferencedLocalVariablesCount()

      Returns the count of local variables referenced by this scope.

      Returns:
        the number of referenced local variables

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

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

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

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

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

    - #### public boolean isRoot()

      Returns true if this is a root scope (no parent).

      Returns:
        true if this scope has no parent

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

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

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

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

    - #### public void setClassScope(ClassNode classScope)

      Sets the ClassNode for this scope if it represents a class body.

      Parameters:
        `classScope` - the class ClassNode

    - #### public void setInStaticContext(boolean inStaticContext)

      Marks this scope as being in a static context.

      Parameters:
        `inStaticContext` - true if in a static context