Class VariableExpression

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, Cloneable, NodeMetaDataHandler, 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:
  • Field Details

  • Constructor Details

    • VariableExpression

      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
    • VariableExpression

      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
    • VariableExpression

      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 Details

    • clone

      public VariableExpression clone()
      Creates a copy of this variable expression, preserving all attributes including accessed variable, modifiers, closure sharing state, static context, and metadata.
      Overrides:
      clone in class Object
      Returns:
      a clone of this VariableExpression with all properties copied
    • setAccessedVariable

      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
    • setClosureSharedVariable

      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.
      Specified by:
      setClosureSharedVariable in interface Variable
      Parameters:
      inClosure - if true, marks this variable as referenced from a closure; false otherwise
    • setInStaticContext

      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
    • setModifiers

      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
    • setType

      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.
      Overrides:
      setType in class Expression
      Parameters:
      type - the ClassNode representing the variable's type; must not be null
    • setUseReferenceDirectly

      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
    • getAccessedVariable

      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
    • getInitialExpression

      public Expression getInitialExpression()
      Returns null because variable expressions do not have initial expressions. Initial expressions are associated with Variable instances, not expressions.
      Specified by:
      getInitialExpression in interface Variable
      Returns:
      always null
    • hasInitialExpression

      public boolean hasInitialExpression()
      Indicates that this variable expression does not have an initial expression.
      Specified by:
      hasInitialExpression in interface Variable
      Returns:
      always false
    • getModifiers

      public int getModifiers()
      Returns the access modifiers for this variable.
      Specified by:
      getModifiers in interface Variable
      Returns:
      the modifier flags as defined in java.lang.reflect.Modifier
      See Also:
      • Opcodes
    • getName

      public String getName()
      Returns the variable name represented by this expression.
      Specified by:
      getName in interface Variable
      Returns:
      the variable name; never null
    • getText

      public String getText()
      Returns the variable name as a string representation of this expression.
      Overrides:
      getText in class ASTNode
      Returns:
      the variable name
    • getType

      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.
      Specified by:
      getType in interface Variable
      Overrides:
      getType in class Expression
      Returns:
      the ClassNode representing the variable's type; never null (defaults to dynamic type)
    • getOriginType

      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.
      Specified by:
      getOriginType in interface Variable
      Returns:
      the original ClassNode representing the variable's declared type; may be null for dynamically typed variables
    • isClosureSharedVariable

      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.
      Specified by:
      isClosureSharedVariable in interface Variable
      Returns:
      true if this variable is accessed from a closure; false otherwise
    • isDynamicTyped

      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.
      Specified by:
      isDynamicTyped in interface Variable
      Returns:
      true if this variable is dynamically typed; false otherwise
    • isInStaticContext

      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.
      Specified by:
      isInStaticContext in interface Variable
      Returns:
      true if this variable is in a static context; false otherwise
    • isSuperExpression

      public boolean isSuperExpression()
      Indicates whether this variable expression represents the super keyword.
      Returns:
      true if this is a super expression; false otherwise
    • isThisExpression

      public boolean isThisExpression()
      Indicates whether this variable expression represents the this keyword.
      Returns:
      true if this is a this expression; false otherwise
    • isUseReferenceDirectly

      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
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • transformExpression

      public Expression transformExpression(ExpressionTransformer transformer)
      Description copied from class: Expression
      Transforms this expression and any nested expressions according to the provided transformer. This method is called during AST transformation phases and must recursively transform any nested expressions to support full AST tree transformation.
      Specified by:
      transformExpression in class Expression
      Parameters:
      transformer - the ExpressionTransformer to apply
      Returns:
      a transformed copy of this expression (or this expression itself if no changes are needed)
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.
      Overrides:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node