2
* Copyright 2003-2007 the original author or authors.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.codehaus.groovy.ast.expr;
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;
24
* Represents a local variable name, the simplest form of expression. e.g. "foo".
26
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
27
* @version $Revision: 12194 $
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);
36
private String variable;
37
private boolean inStaticContext;
38
private boolean isDynamicTyped=false;
39
private Variable accessedVariable;
40
boolean closureShare=false;
41
private ClassNode originType;
43
public Variable getAccessedVariable() {
44
return accessedVariable;
47
public void setAccessedVariable(Variable origin) {
48
this.accessedVariable = origin;
51
public VariableExpression(String variable, ClassNode type) {
52
this.variable = variable;
54
setType(ClassHelper.getWrapper(type));
57
public VariableExpression(String variable) {
58
this(variable, ClassHelper.DYNAMIC_TYPE);
61
public VariableExpression(Variable variable) {
62
this(variable.getName(), variable.getOriginType());
63
setAccessedVariable(variable);
66
public void visit(GroovyCodeVisitor visitor) {
67
visitor.visitVariableExpression(this);
70
public Expression transformExpression(ExpressionTransformer transformer) {
74
public String getText() {
78
public String getName() {
83
* @return true if this variable is dynamically typed
85
public String toString() {
86
return super.toString() + "[variable: " + variable + (this.isDynamicTyped() ? "" : " type: " + getType()) + "]";
89
public Expression getInitialExpression() {
93
public boolean hasInitialExpression() {
97
public boolean isInStaticContext() {
98
if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isInStaticContext();
99
return inStaticContext;
102
public void setInStaticContext(boolean inStaticContext) {
103
this.inStaticContext = inStaticContext;
106
public void setType(ClassNode cn){
108
isDynamicTyped |= ClassHelper.DYNAMIC_TYPE==cn;
111
public boolean isDynamicTyped() {
112
return isDynamicTyped;
115
public boolean isClosureSharedVariable() {
116
if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isClosureSharedVariable();
120
public void setClosureSharedVariable(boolean inClosure) {
121
closureShare = inClosure;
124
public ClassNode getType() {
125
if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.getType();
126
return super.getType();
129
public ClassNode getOriginType() {
133
public boolean isThisExpression() {
134
return "this".equals(variable);
137
public boolean isSuperExpression() {
138
return "super".equals(variable);
2
* Copyright 2003-2007 the original author or authors.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.codehaus.groovy.ast.expr;
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;
24
* Represents a local variable name, the simplest form of expression. e.g. "foo".
26
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
27
* @version $Revision: 12194 $
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);
36
private String variable;
37
private boolean inStaticContext;
38
private boolean isDynamicTyped=false;
39
private Variable accessedVariable;
40
boolean closureShare=false;
41
private ClassNode originType;
43
public Variable getAccessedVariable() {
44
return accessedVariable;
47
public void setAccessedVariable(Variable origin) {
48
this.accessedVariable = origin;
51
public VariableExpression(String variable, ClassNode type) {
52
this.variable = variable;
54
setType(ClassHelper.getWrapper(type));
57
public VariableExpression(String variable) {
58
this(variable, ClassHelper.DYNAMIC_TYPE);
61
public VariableExpression(Variable variable) {
62
this(variable.getName(), variable.getOriginType());
63
setAccessedVariable(variable);
66
public void visit(GroovyCodeVisitor visitor) {
67
visitor.visitVariableExpression(this);
70
public Expression transformExpression(ExpressionTransformer transformer) {
74
public String getText() {
78
public String getName() {
83
* @return true if this variable is dynamically typed
85
public String toString() {
86
return super.toString() + "[variable: " + variable + (this.isDynamicTyped() ? "" : " type: " + getType()) + "]";
89
public Expression getInitialExpression() {
93
public boolean hasInitialExpression() {
97
public boolean isInStaticContext() {
98
if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isInStaticContext();
99
return inStaticContext;
102
public void setInStaticContext(boolean inStaticContext) {
103
this.inStaticContext = inStaticContext;
106
public void setType(ClassNode cn){
108
isDynamicTyped |= ClassHelper.DYNAMIC_TYPE==cn;
111
public boolean isDynamicTyped() {
112
return isDynamicTyped;
115
public boolean isClosureSharedVariable() {
116
if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isClosureSharedVariable();
120
public void setClosureSharedVariable(boolean inClosure) {
121
closureShare = inClosure;
124
public ClassNode getType() {
125
if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.getType();
126
return super.getType();
129
public ClassNode getOriginType() {
133
public boolean isThisExpression() {
134
return "this".equals(variable);
137
public boolean isSuperExpression() {
138
return "super".equals(variable);