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.GroovyCodeVisitor;
49 import org.codehaus.groovy.classgen.AsmClassGenerator2;
50 import org.codehaus.groovy.classgen.BytecodeHelper;
51
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.6 $
58 */
59 public class VariableExpression extends Expression {
60
61 public static final VariableExpression THIS_EXPRESSION = new VariableExpression("this", null);
62
63 private String variable;
64
65 public VariableExpression(String variable, String type) {
66 this.variable = variable;
67 if (type == null || type.length() == 0) {
68 isDynamic = true;
69 }
70 else {
71 String boxedType = BytecodeHelper.getObjectTypeForPrimitive(type);
72 boxedType = BytecodeHelper.getObjectArrayTypeForPrimitiveArray(boxedType);
73
74 super.setType(boxedType);
75 isDynamic = false;
76 }
77 }
78
79 public VariableExpression(String variable) {
80 this.variable = variable;
81 }
82
83 public void visit(GroovyCodeVisitor visitor) {
84 visitor.visitVariableExpression(this);
85 }
86
87 public Expression transformExpression(ExpressionTransformer transformer) {
88 return this;
89 }
90
91 protected void resolveType(AsmClassGenerator2 resolver) {
92 resolver.resolve(this);
93 }
94
95 public String getVariable() {
96 return variable;
97 }
98
99 public String getText() {
100 return variable;
101 }
102
103 public String getType() {
104 if (type == null) {
105 return "java.lang.Object";
106 }
107 return type;
108 }
109
110 boolean isDynamic = true;
111 public boolean isDynamic() {
112 return isDynamic;
113 }
114
115 /***
116 * @return true if this variable is dynamically typed
117 */
118
119 public String toString() {
120 return super.toString() + "[variable: " + variable + ((isDynamic()) ? "" : " type: " + type) + "]";
121 }
122
123 public void setDynamic(boolean dynamic) {
124 isDynamic = dynamic;
125 }
126 }