Package: org.codehaus.groovy.ast.expr

## [Java] Class VariableExpression

- org.codehaus.groovy.ast.expr.VariableExpression

- All Implemented Interfaces and Traits:
    Cloneable, Variable

      public class VariableExpression
      extends Expression
      implements Cloneable, Variable

  Represents a local variable reference, the simplest form of expression (e.g., "foo"). Variables can refer to local variables, parameters, fields, or special variables like `this` and `super`. A variable expression may reference an accessed variable (either a local variable or field), track closure sharing and context information, and support dynamic typing. Each user-defined variable expression maintains its own instance to preserve line information for accurate error reporting.

  See Also:
    Expression
    Variable
    BinaryExpression
    ClosureExpression

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static VariableExpression` | `SUPER_EXPRESSION` |
    `static VariableExpression` | `THIS_EXPRESSION` |

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class Expression` | `EMPTY_ARRAY`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `VariableExpression(String name, ClassNode type)` Creates a variable expression with the specified name and type annotation.
    `VariableExpression(String name)` Creates a variable expression with the specified name.
    `VariableExpression(Variable av)` Creates a variable expression that references an existing Variable.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public VariableExpression` | `clone()` Creates a copy of this variable expression, preserving all attributes including accessed variable, modifiers, closure sharing state, static context, and metadata.
    `` | `public Variable` | `getAccessedVariable()` Returns the variable that this expression accesses.
    `` | `public Expression` | `getInitialExpression()` Returns null because variable expressions do not have initial expressions.
    `` | `public int` | `getModifiers()` Returns the access modifiers for this variable.
    `` | `public String` | `getName()` Returns the variable name represented by this expression.
    `` | `public ClassNode` | `getOriginType()` Returns the original type used when this variable expression was created.
    `` | `public String` | `getText()` Returns the variable name as a string representation of this expression.
    `` | `public ClassNode` | `getType()` Returns the type of this variable.
    `` | `public boolean` | `hasInitialExpression()` Indicates that this variable expression does not have an initial expression.
    `` | `public boolean` | `isClosureSharedVariable()` Indicates whether this variable or the accessed variable is shared in a closure context.
    `` | `public boolean` | `isDynamicTyped()` Indicates whether this variable or the accessed variable uses dynamic typing.
    `` | `public boolean` | `isInStaticContext()` Indicates whether this variable or the accessed variable is accessed in a static context.
    `` | `public boolean` | `isSuperExpression()` Indicates whether this variable expression represents the `super` keyword.
    `` | `public boolean` | `isThisExpression()` Indicates whether this variable expression represents the `this` keyword.
    `` | `public boolean` | `isUseReferenceDirectly()` For internal compiler use only.
    `` | `public void` | `setAccessedVariable(Variable variable)` Sets the variable that this expression accesses.
    `` | `public void` | `setClosureSharedVariable(boolean inClosure)` Marks this variable as being shared within a closure context.
    `` | `public void` | `setInStaticContext(boolean inStaticContext)` Sets whether this variable is accessed in a static context.
    `` | `public void` | `setModifiers(int modifiers)` Sets the access modifiers for this variable (e.g., public, private, protected).
    `` | `public void` | `setType(ClassNode type)` Sets the declared type for this variable.
    `` | `public void` | `setUseReferenceDirectly(boolean useRef)` For internal compiler use only.
    `` | `public String` | `toString()`
    `` | `public Expression` | `transformExpression(ExpressionTransformer transformer)`
    `` | `public void` | `visit(GroovyCodeVisitor visitor)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Expression` | `getType, setType, transformExpression, transformExpressions, transformExpressions`
    `class AnnotatedNode` | `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`
    `class ASTNode` | `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

- - ### Field Detail

    - #### public static final VariableExpression SUPER_EXPRESSION

    - #### public static final VariableExpression THIS_EXPRESSION

  - ### Constructor Detail

    - #### public VariableExpression(String name, ClassNode type)

      Creates a variable expression with the specified name and type annotation.

      Parameters:
        `name` - the variable name; must not be null
        `type` - the ClassNode representing the declared type of this variable; may be null for dynamically typed variables or if type inference is needed

    - #### public VariableExpression(String name)

      Creates a variable expression with the specified name. The type is initially set to dynamic.

      Parameters:
        `name` - the variable name; must not be null

    - #### public VariableExpression(Variable av)

      Creates a variable expression that references an existing Variable. This captures the variable's name, type, modifiers, and marks this expression as accessing that variable.

      Parameters:
        `av` - the Variable to reference; must not be null

  - ### Method Detail

    - #### public VariableExpression clone()

      Creates a copy of this variable expression, preserving all attributes including accessed variable, modifiers, closure sharing state, static context, and metadata.

      Returns:
        a clone of this VariableExpression with all properties copied

    - #### public Variable getAccessedVariable()

      Returns the variable that this expression accesses. If not explicitly set, returns null, indicating this expression represents a standalone variable reference without a linked definition.

      Returns:
        the accessed Variable, or null if this is not accessing another variable

    - #### public Expression getInitialExpression()

      Returns null because variable expressions do not have initial expressions. Initial expressions are associated with Variable instances, not expressions.

      Returns:
        always null

    - #### public int getModifiers()

      Returns the access modifiers for this variable.

      Returns:
        the modifier flags as defined in java.lang.reflect.Modifier

    - #### public String getName()

      Returns the variable name represented by this expression.

      Returns:
        the variable name; never null

    - #### public ClassNode getOriginType()

      Returns the original type used when this variable expression was created. For example, getType() may return a boxed type while this method returns the primitive type. If this variable accesses another variable, returns the accessed variable's origin type.

      Returns:
        the original ClassNode representing the variable's declared type; may be null for dynamically typed variables

    - #### public String getText()

      Returns the variable name as a string representation of this expression.

      Returns:
        the variable name

    - #### public ClassNode getType()

      Returns the type of this variable. If this variable accesses another variable, returns the accessed variable's type. Otherwise returns the type set on this expression.

      Returns:
        the ClassNode representing the variable's type; never null (defaults to dynamic type)

    - #### public boolean hasInitialExpression()

      Indicates that this variable expression does not have an initial expression.

      Returns:
        always false

    - #### public boolean isClosureSharedVariable()

      Indicates whether this variable or the accessed variable is shared in a closure context. Closure-shared variables are accessible from within closure bodies. Example: in the code `def str = 'Hello'; def cl = { println str }`, this would return true for the "str" variable expression inside the closure.

      Returns:
        true if this variable is accessed from a closure; false otherwise

    - #### public boolean isDynamicTyped()

      Indicates whether this variable or the accessed variable uses dynamic typing. Dynamically typed variables have their types resolved at runtime rather than compile time.

      Returns:
        true if this variable is dynamically typed; false otherwise

    - #### public boolean isInStaticContext()

      Indicates whether this variable or the accessed variable is accessed in a static context. Static context variables cannot reference instance variables or methods.

      Returns:
        true if this variable is in a static context; false otherwise

    - #### public boolean isSuperExpression()

      Indicates whether this variable expression represents the `super` keyword.

      Returns:
        true if this is a super expression; false otherwise

    - #### public boolean isThisExpression()

      Indicates whether this variable expression represents the `this` keyword.

      Returns:
        true if this is a this expression; false otherwise

    - #### public boolean isUseReferenceDirectly()

      For internal compiler use only. Returns whether to use the variable reference directly without dereferencing. This flag is used by compiler internals and should probably be converted to node metadata in the future.

      Returns:
        true if using reference directly; false otherwise

    - #### public void setAccessedVariable(Variable variable)

      Sets the variable that this expression accesses. This is typically used to link a variable expression to the actual variable definition (local variable, field, or parameter).

      Parameters:
        `variable` - the Variable being accessed; may be null if this is not accessing an existing variable

    - #### public void setClosureSharedVariable(boolean inClosure)

      Marks this variable as being shared within a closure context. Closure-shared variables are accessible from within closure bodies and must be handled specially during compilation. Example: in the code `def str = 'Hello'; def cl = { println str }`, the "str" variable is closure-shared because it is referenced inside the closure.

      Parameters:
        `inClosure` - if true, marks this variable as referenced from a closure; false otherwise

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

      Sets whether this variable is accessed in a static context.

      Parameters:
        `inStaticContext` - if true, indicates this variable is in a static context; false otherwise

    - #### public void setModifiers(int modifiers)

      Sets the access modifiers for this variable (e.g., public, private, protected).

      Parameters:
        `modifiers` - the access modifier flags from java.lang.reflect.Modifier

    - #### public void setType(ClassNode type)

      Sets the declared type for this variable. If this variable accesses a shared variable, modifying this variable's type is unsafe and may lead to verification errors at compile time. In such cases, set the type on the accessed variable instead.

      Parameters:
        `type` - the ClassNode representing the variable's type; must not be null

    - #### public void setUseReferenceDirectly(boolean useRef)

      For internal compiler use only. This flag indicates whether to use the variable reference directly without dereferencing. This is used by compiler internals and should probably be converted to node metadata in the future.

      Parameters:
        `useRef` - if true, use the reference directly; false otherwise

    - #### public String toString()

    - #### public Expression transformExpression(ExpressionTransformer transformer)

    - #### public void visit(GroovyCodeVisitor visitor)