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.
| Modifiers | Name | Description |
|---|---|---|
static VariableExpression |
SUPER_EXPRESSION |
|
static VariableExpression |
THIS_EXPRESSION |
| Fields inherited from class | Fields |
|---|---|
class Expression |
EMPTY_ARRAY |
| 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. |
| 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) |
| 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 |
Creates a variable expression with the specified name and type annotation.
name - the variable name; must not be nulltype - the ClassNode representing the declared type of this variable; may be null
for dynamically typed variables or if type inference is neededCreates a variable expression with the specified name. The type is initially set to dynamic.
name - the variable name; must not be nullCreates a copy of this variable expression, preserving all attributes including accessed variable, modifiers, closure sharing state, static context, and metadata.
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 null because variable expressions do not have initial expressions. Initial expressions are associated with Variable instances, not expressions.
Returns the access modifiers for this variable.
Returns the variable name represented by this expression.
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 variable name as a string representation of this expression.
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.
Indicates that this variable expression does not have an initial expression.
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.
Indicates whether this variable or the accessed variable uses dynamic typing. Dynamically typed variables have their types resolved at runtime rather than compile time.
Indicates whether this variable or the accessed variable is accessed in a static context. Static context variables cannot reference instance variables or methods.
Indicates whether this variable expression represents the super keyword.
Indicates whether this variable expression represents the this keyword.
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.
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).
variable - the Variable being accessed; may be null if this is not accessing
an existing variable 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.
inClosure - if true, marks this variable as referenced from a closure; false otherwiseSets whether this variable is accessed in a static context.
inStaticContext - if true, indicates this variable is in a static context; false otherwiseSets the access modifiers for this variable (e.g., public, private, protected).
modifiers - the access modifier flags from java.lang.reflect.ModifierSets 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.
type - the ClassNode representing the variable's type; must not be nullFor 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.
useRef - if true, use the reference directly; false otherwiseCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.