Package org.codehaus.groovy.ast.expr
Class VariableExpression
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.expr.Expression
org.codehaus.groovy.ast.expr.VariableExpression
- All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>,Cloneable,NodeMetaDataHandler,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 Summary
FieldsFields inherited from class org.codehaus.groovy.ast.expr.Expression
EMPTY_ARRAYFields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
ConstructorsConstructorDescriptionVariableExpression(String name) Creates a variable expression with the specified name.VariableExpression(String name, ClassNode type) Creates a variable expression with the specified name and type annotation.Creates a variable expression that references an existingVariable. -
Method Summary
Modifier and TypeMethodDescriptionclone()Creates 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.Returns null because variable expressions do not have initial expressions.intReturns the access modifiers for this variable.getName()Returns the variable name represented by this expression.Returns the original type used when this variable expression was created.getText()Returns the variable name as a string representation of this expression.getType()Returns the type of this variable.booleanIndicates that this variable expression does not have an initial expression.booleanIndicates whether this variable or the accessed variable is shared in a closure context.booleanIndicates whether this variable or the accessed variable uses dynamic typing.booleanIndicates whether this variable or the accessed variable is accessed in a static context.booleanIndicates whether this variable expression represents thesuperkeyword.booleanIndicates whether this variable expression represents thethiskeyword.booleanFor internal compiler use only.voidsetAccessedVariable(Variable variable) Sets the variable that this expression accesses.voidsetClosureSharedVariable(boolean inClosure) Marks this variable as being shared within a closure context.voidsetInStaticContext(boolean inStaticContext) Sets whether this variable is accessed in a static context.voidsetModifiers(int modifiers) Sets the access modifiers for this variable (e.g., public, private, protected).voidSets the declared type for this variable.voidsetUseReferenceDirectly(boolean useRef) For internal compiler use only.toString()transformExpression(ExpressionTransformer transformer) Transforms this expression and any nested expressions according to the provided transformer.voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.expr.Expression
transformExpressions, transformExpressionsMethods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSyntheticMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePositionMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaDataMethods inherited from interface org.codehaus.groovy.ast.Variable
isFinal, isPrivate, isProtected, isPublic, isStatic, isVolatile
-
Field Details
-
THIS_EXPRESSION
-
SUPER_EXPRESSION
-
-
Constructor Details
-
VariableExpression
Creates a variable expression with the specified name and type annotation.- Parameters:
name- the variable name; must not be nulltype- theClassNoderepresenting the declared type of this variable; may be null for dynamically typed variables or if type inference is needed
-
VariableExpression
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
Creates a variable expression that references an existingVariable. This captures the variable's name, type, modifiers, and marks this expression as accessing that variable.- Parameters:
av- theVariableto reference; must not be null
-
-
Method Details
-
clone
Creates a copy of this variable expression, preserving all attributes including accessed variable, modifiers, closure sharing state, static context, and metadata.- Overrides:
clonein classObject- Returns:
- a clone of this
VariableExpressionwith all properties copied
-
setAccessedVariable
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- theVariablebeing accessed; may be null if this is not accessing an existing variable
-
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
Sets the declared type for this variable. If this variableaccessesashared variable, modifying this variable's type is unsafe and may lead to verification errors at compile time. In such cases, set the type on theaccessed variableinstead.- Overrides:
setTypein classExpression- Parameters:
type- theClassNoderepresenting 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
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
Returns null because variable expressions do not have initial expressions. Initial expressions are associated withVariableinstances, not expressions.- Specified by:
getInitialExpressionin interfaceVariable- Returns:
- always null
-
hasInitialExpression
public boolean hasInitialExpression()Indicates that this variable expression does not have an initial expression.- Specified by:
hasInitialExpressionin interfaceVariable- Returns:
- always false
-
getModifiers
public int getModifiers()Returns the access modifiers for this variable.- Specified by:
getModifiersin interfaceVariable- Returns:
- the modifier flags as defined in java.lang.reflect.Modifier
- See Also:
-
Opcodes
-
getName
Returns the variable name represented by this expression. -
getText
Returns the variable name as a string representation of this expression. -
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:
getTypein interfaceVariable- Overrides:
getTypein classExpression- Returns:
- the
ClassNoderepresenting the variable's type; never null (defaults to dynamic type)
-
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:
getOriginTypein interfaceVariable- Returns:
- the original
ClassNoderepresenting the variable's declared type; may be null for dynamically typed variables
-
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:
isDynamicTypedin interfaceVariable- 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:
isInStaticContextin interfaceVariable- Returns:
- true if this variable is in a static context; false otherwise
-
isSuperExpression
public boolean isSuperExpression()Indicates whether this variable expression represents thesuperkeyword.- Returns:
- true if this is a super expression; false otherwise
-
isThisExpression
public boolean isThisExpression()Indicates whether this variable expression represents thethiskeyword.- 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
-
transformExpression
Description copied from class:ExpressionTransforms 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:
transformExpressionin classExpression- Parameters:
transformer- theExpressionTransformerto apply- Returns:
- a transformed copy of this expression (or this expression itself if no changes are needed)
-
visit
Description copied from class:ASTNodeAccepts 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:
visitin classASTNode- Parameters:
visitor- theGroovyCodeVisitorto process this node
-