~ubuntu-branches/ubuntu/lucid/groovy/lucid

« back to all changes in this revision

Viewing changes to src/main/org/codehaus/groovy/ast/expr/VariableExpression.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2009-05-19 21:49:37 UTC
  • mfrom: (3.2.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090519214937-56mctwb05t3xd63r
Tags: 1.6.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
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: 12194 $
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 boolean inStaticContext;
38
 
    private boolean isDynamicTyped=false;
39
 
    private Variable accessedVariable;
40
 
    boolean closureShare=false;
41
 
    private ClassNode originType;
42
 
 
43
 
    public Variable getAccessedVariable() {
44
 
        return accessedVariable;
45
 
    }
46
 
 
47
 
    public void setAccessedVariable(Variable origin) {
48
 
        this.accessedVariable = origin;
49
 
    }
50
 
 
51
 
    public VariableExpression(String variable, ClassNode type) {
52
 
        this.variable = variable;
53
 
        originType = type;
54
 
        setType(ClassHelper.getWrapper(type));
55
 
    }
56
 
    
57
 
    public VariableExpression(String variable) {
58
 
        this(variable, ClassHelper.DYNAMIC_TYPE);
59
 
    }
60
 
    
61
 
    public VariableExpression(Variable variable) {
62
 
        this(variable.getName(), variable.getOriginType());
63
 
        setAccessedVariable(variable);
64
 
    }
65
 
 
66
 
    public void visit(GroovyCodeVisitor visitor) {
67
 
        visitor.visitVariableExpression(this);
68
 
    }
69
 
 
70
 
    public Expression transformExpression(ExpressionTransformer transformer) {
71
 
        return this;
72
 
    }
73
 
 
74
 
    public String getText() {
75
 
        return variable;
76
 
    }
77
 
    
78
 
    public String getName() {
79
 
        return variable;
80
 
    }
81
 
 
82
 
    /**
83
 
     * @return true if this variable is dynamically typed
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
 
        return isDynamicTyped;
113
 
    }
114
 
 
115
 
    public boolean isClosureSharedVariable() {
116
 
        if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isClosureSharedVariable();
117
 
        return closureShare;
118
 
    }
119
 
    
120
 
    public void setClosureSharedVariable(boolean inClosure) {
121
 
        closureShare = inClosure;        
122
 
    }
123
 
    
124
 
    public ClassNode getType() {
125
 
        if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.getType();
126
 
        return super.getType();
127
 
    }
128
 
    
129
 
    public ClassNode getOriginType() {
130
 
        return originType;
131
 
    }
132
 
 
133
 
    public boolean isThisExpression() {
134
 
        return "this".equals(variable);
135
 
    }
136
 
 
137
 
    public boolean isSuperExpression() {
138
 
        return "super".equals(variable);
139
 
    }
140
 
}
 
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.&nbsp;"foo".
 
25
 * 
 
26
 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 
27
 * @version $Revision: 12194 $
 
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 boolean inStaticContext;
 
38
    private boolean isDynamicTyped=false;
 
39
    private Variable accessedVariable;
 
40
    boolean closureShare=false;
 
41
    private ClassNode originType;
 
42
 
 
43
    public Variable getAccessedVariable() {
 
44
        return accessedVariable;
 
45
    }
 
46
 
 
47
    public void setAccessedVariable(Variable origin) {
 
48
        this.accessedVariable = origin;
 
49
    }
 
50
 
 
51
    public VariableExpression(String variable, ClassNode type) {
 
52
        this.variable = variable;
 
53
        originType = type;
 
54
        setType(ClassHelper.getWrapper(type));
 
55
    }
 
56
    
 
57
    public VariableExpression(String variable) {
 
58
        this(variable, ClassHelper.DYNAMIC_TYPE);
 
59
    }
 
60
    
 
61
    public VariableExpression(Variable variable) {
 
62
        this(variable.getName(), variable.getOriginType());
 
63
        setAccessedVariable(variable);
 
64
    }
 
65
 
 
66
    public void visit(GroovyCodeVisitor visitor) {
 
67
        visitor.visitVariableExpression(this);
 
68
    }
 
69
 
 
70
    public Expression transformExpression(ExpressionTransformer transformer) {
 
71
        return this;
 
72
    }
 
73
 
 
74
    public String getText() {
 
75
        return variable;
 
76
    }
 
77
    
 
78
    public String getName() {
 
79
        return variable;
 
80
    }
 
81
 
 
82
    /**
 
83
     * @return true if this variable is dynamically typed
 
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
        return isDynamicTyped;
 
113
    }
 
114
 
 
115
    public boolean isClosureSharedVariable() {
 
116
        if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.isClosureSharedVariable();
 
117
        return closureShare;
 
118
    }
 
119
    
 
120
    public void setClosureSharedVariable(boolean inClosure) {
 
121
        closureShare = inClosure;        
 
122
    }
 
123
    
 
124
    public ClassNode getType() {
 
125
        if (accessedVariable!=null && accessedVariable!=this) return accessedVariable.getType();
 
126
        return super.getType();
 
127
    }
 
128
    
 
129
    public ClassNode getOriginType() {
 
130
        return originType;
 
131
    }
 
132
 
 
133
    public boolean isThisExpression() {
 
134
        return "this".equals(variable);
 
135
    }
 
136
 
 
137
    public boolean isSuperExpression() {
 
138
        return "super".equals(variable);
 
139
    }
 
140
}