1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.codehaus.groovy.ast.expr;
47
48 import org.codehaus.groovy.ast.ClassHelper;
49 import org.codehaus.groovy.ast.ClassNode;
50 import org.codehaus.groovy.ast.GroovyCodeVisitor;
51 import org.codehaus.groovy.ast.Variable;
52
53 /***
54 * Represents a local variable name, the simplest form of expression. e.g. "foo".
55 *
56 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
57 * @version $Revision: 1.13 $
58 */
59 public class VariableExpression extends Expression implements Variable {
60
61 public static final VariableExpression THIS_EXPRESSION = new VariableExpression("this", ClassHelper.DYNAMIC_TYPE);
62 public static final VariableExpression SUPER_EXPRESSION = new VariableExpression("super", ClassHelper.DYNAMIC_TYPE);
63
64 private String variable;
65 private boolean inStaticContext;
66 private boolean isDynamicTyped=false;
67
68 public VariableExpression(String variable, ClassNode type) {
69 this.variable = variable;
70 super.setType(ClassHelper.getWrapper(type));
71 }
72
73 public VariableExpression(String variable) {
74 this(variable, ClassHelper.DYNAMIC_TYPE);
75 }
76
77 public void visit(GroovyCodeVisitor visitor) {
78 visitor.visitVariableExpression(this);
79 }
80
81 public Expression transformExpression(ExpressionTransformer transformer) {
82 return this;
83 }
84
85 public String getText() {
86 return variable;
87 }
88
89 public String getName() {
90 return variable;
91 }
92
93 /***
94 * @return true if this variable is dynamically typed
95 */
96 public String toString() {
97 return super.toString() + "[variable: " + variable + (this.isDynamicTyped() ? "" : " type: " + getType()) + "]";
98 }
99
100 public Expression getInitialExpression() {
101 return null;
102 }
103
104 public boolean hasInitialExpression() {
105 return false;
106 }
107
108 public boolean isInStaticContext() {
109 return inStaticContext;
110 }
111
112 public void setInStaticContext(boolean inStaticContext) {
113 this.inStaticContext = inStaticContext;
114 }
115
116 public void setType(ClassNode cn){
117 super.setType(cn);
118 isDynamicTyped |= ClassHelper.DYNAMIC_TYPE==cn;
119 }
120
121 public boolean isDynamicTyped() {
122 return isDynamicTyped;
123 }
124 }