View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.ast.expr;
17  
18  import org.codehaus.groovy.ast.ClassHelper;
19  import org.codehaus.groovy.ast.ClassNode;
20  import org.codehaus.groovy.ast.GroovyCodeVisitor;
21  import org.codehaus.groovy.ast.Variable;
22  
23  /**
24   * Represents a local variable name, the simplest form of expression. e.g. "foo".
25   * 
26   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
27   * @version $Revision: 21216 $
28   */
29  public class VariableExpression extends Expression implements Variable {
30      // The following fields are only used internally; every occurrence of a user-defined expression of the same kind
31      // has its own instance so as to preserve line information. Consequently, to test for such an expression, don't
32      // compare against the field but call isXXXExpression() instead.
33      public static final VariableExpression THIS_EXPRESSION = new VariableExpression("this", ClassHelper.DYNAMIC_TYPE);
34      public static final VariableExpression SUPER_EXPRESSION = new VariableExpression("super", ClassHelper.DYNAMIC_TYPE);
35  
36      private String variable;
37      private int modifiers;
38      private boolean inStaticContext;
39      private boolean isDynamicTyped=false;
40      private Variable accessedVariable;
41      boolean closureShare=false;
42      boolean useRef=false;
43      private ClassNode originType;
44  
45      public Variable getAccessedVariable() {
46          return accessedVariable;
47      }
48  
49      public void setAccessedVariable(Variable origin) {
50          this.accessedVariable = origin;
51      }
52  
53      public VariableExpression(String variable, ClassNode type) {
54          this.variable = variable;
55          originType = type;
56          setType(ClassHelper.getWrapper(type));
57      }
58      
59      public VariableExpression(String variable) {
60          this(variable, ClassHelper.DYNAMIC_TYPE);
61      }
62      
63      public VariableExpression(Variable variable) {
64          this(variable.getName(), variable.getOriginType());
65          setAccessedVariable(variable);
66          setModifiers(variable.getModifiers());
67      }
68  
69      public void visit(GroovyCodeVisitor visitor) {
70          visitor.visitVariableExpression(this);
71      }
72  
73      public Expression transformExpression(ExpressionTransformer transformer) {
74          return this;
75      }
76  
77      public String getText() {
78          return variable;
79      }
80      
81      public String getName() {
82          return variable;
83      }
84  
85      public String toString() {
86          return super.toString() + "[variable: " + variable + (this.isDynamicTyped() ? "" : " type: " + getType()) + "]";
87      }
88  
89      public Expression getInitialExpression() {
90          return null;
91      }
92  
93      public boolean hasInitialExpression() {
94          return false;
95      }
96      
97      public boolean isInStaticContext() {
98          if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isInStaticContext();
99          return inStaticContext;
100     }
101     
102     public void setInStaticContext(boolean inStaticContext) {
103         this.inStaticContext = inStaticContext;
104     }
105 
106     public void setType(ClassNode cn){
107         super.setType(cn);
108         isDynamicTyped |= ClassHelper.DYNAMIC_TYPE==cn;
109     }
110     
111     public boolean isDynamicTyped() {
112         if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isDynamicTyped();
113         return isDynamicTyped;
114     }
115 
116     public boolean isClosureSharedVariable() {
117         if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isClosureSharedVariable();
118         return closureShare;
119     }
120     
121     public void setClosureSharedVariable(boolean inClosure) {
122         closureShare = inClosure;        
123     }
124 
125     public int getModifiers() {
126         return modifiers;
127     }
128 
129     public void setUseReferenceDirectly(boolean useRef) {
130         this.useRef = useRef;        
131     }
132     
133     public boolean isUseReferenceDirectly() {
134         return useRef;
135     }
136     
137     public ClassNode getType() {
138         if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.getType();
139         return super.getType();
140     }
141 
142     public ClassNode getOriginType() {
143         if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.getOriginType();
144         return originType;
145     }
146 
147     public boolean isThisExpression() {
148         return "this".equals(variable);
149     }
150 
151     public boolean isSuperExpression() {
152         return "super".equals(variable);
153     }
154 
155     public void setModifiers(int modifiers) {
156         this.modifiers = modifiers;
157     }
158 }