|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
VariableScopeCodeVisitor.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: VariableScopeCodeVisitor.java,v 1.10 2004/04/01 06:21:53 cpoirier Exp $
|
|
3 |
|
|
4 |
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
|
|
6 |
Redistribution and use of this software and associated documentation
|
|
7 |
("Software"), with or without modification, are permitted provided
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
44 |
|
|
45 |
*/
|
|
46 |
package org.codehaus.groovy.classgen;
|
|
47 |
|
|
48 |
import java.util.Set;
|
|
49 |
|
|
50 |
import org.codehaus.groovy.ast.CodeVisitorSupport;
|
|
51 |
import org.codehaus.groovy.ast.Parameter;
|
|
52 |
import org.codehaus.groovy.ast.VariableScope;
|
|
53 |
import org.codehaus.groovy.ast.expr.BinaryExpression;
|
|
54 |
import org.codehaus.groovy.ast.expr.ClosureExpression;
|
|
55 |
import org.codehaus.groovy.ast.expr.Expression;
|
|
56 |
import org.codehaus.groovy.ast.expr.MethodCallExpression;
|
|
57 |
import org.codehaus.groovy.ast.expr.PostfixExpression;
|
|
58 |
import org.codehaus.groovy.ast.expr.PrefixExpression;
|
|
59 |
import org.codehaus.groovy.ast.expr.VariableExpression;
|
|
60 |
import org.codehaus.groovy.ast.stmt.ForStatement;
|
|
61 |
|
|
62 |
import org.codehaus.groovy.syntax.Types;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* A visitor which figures out which variables are in scope
|
|
66 |
*
|
|
67 |
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
|
|
68 |
* @version $Revision: 1.10 $
|
|
69 |
*/
|
|
70 |
public class VariableScopeCodeVisitor extends CodeVisitorSupport { |
|
71 |
|
|
72 |
private VariableScope scope;
|
|
73 |
|
|
74 | 0 |
public VariableScopeCodeVisitor(VariableScope scope) {
|
75 | 0 |
this.scope = scope;
|
76 |
} |
|
77 |
|
|
78 | 0 |
public Set getReferencedVariables() {
|
79 | 0 |
return scope.getReferencedVariables();
|
80 |
} |
|
81 |
|
|
82 | 0 |
public Set getDeclaredVariables() {
|
83 | 0 |
return scope.getDeclaredVariables();
|
84 |
} |
|
85 |
|
|
86 |
|
|
87 |
|
|
88 | 0 |
public void visitBinaryExpression(BinaryExpression expression) { |
89 | 0 |
Expression leftExpression = expression.getLeftExpression(); |
90 | 0 |
if (expression.getOperation().isA(Types.ASSIGNMENT_OPERATOR) && leftExpression instanceof VariableExpression) { |
91 | 0 |
declareVariable((VariableExpression) leftExpression); |
92 |
} |
|
93 |
else {
|
|
94 | 0 |
leftExpression.visit(this);
|
95 |
} |
|
96 | 0 |
expression.getRightExpression().visit(this);
|
97 |
} |
|
98 |
|
|
99 | 0 |
public void visitForLoop(ForStatement forLoop) { |
100 | 0 |
declareVariable(forLoop.getVariable()); |
101 |
|
|
102 | 0 |
super.visitForLoop(forLoop);
|
103 |
} |
|
104 |
|
|
105 | 0 |
public void visitClosureExpression(ClosureExpression expression) { |
106 | 0 |
VariableScopeCodeVisitor visitor = createClosureVisitor(expression); |
107 | 0 |
expression.getCode().visit(visitor); |
108 |
} |
|
109 |
|
|
110 | 0 |
public void visitVariableExpression(VariableExpression expression) { |
111 |
// check for undeclared variables?
|
|
112 | 0 |
String variable = expression.getVariable(); |
113 |
/*
|
|
114 |
if (!parameterSet.contains(variable)) {
|
|
115 |
referencedVariables.add(variable);
|
|
116 |
}
|
|
117 |
*/
|
|
118 | 0 |
getReferencedVariables().add(variable); |
119 |
} |
|
120 |
|
|
121 |
|
|
122 | 0 |
public void visitPostfixExpression(PostfixExpression expression) { |
123 | 0 |
Expression exp = expression.getExpression(); |
124 | 0 |
if (exp instanceof VariableExpression) { |
125 | 0 |
declareVariable((VariableExpression) exp); |
126 |
} |
|
127 |
else {
|
|
128 | 0 |
exp.visit(this);
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 | 0 |
public void visitPrefixExpression(PrefixExpression expression) { |
133 | 0 |
Expression exp = expression.getExpression(); |
134 | 0 |
if (exp instanceof VariableExpression) { |
135 | 0 |
declareVariable((VariableExpression) exp); |
136 |
} |
|
137 |
else {
|
|
138 | 0 |
exp.visit(this);
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 | 0 |
public void visitMethodCallExpression(MethodCallExpression call) { |
143 | 0 |
if (call.isImplicitThis()) {
|
144 | 0 |
getReferencedVariables().add(call.getMethod()); |
145 |
} |
|
146 | 0 |
super.visitMethodCallExpression(call);
|
147 |
} |
|
148 |
|
|
149 | 0 |
protected void setParameters(Parameter[] parameters) { |
150 |
/*
|
|
151 |
parameterSet.clear();
|
|
152 |
for (int i = 0; i < parameters.length; i++) {
|
|
153 |
parameterSet.add(parameters[i].getName());
|
|
154 |
}
|
|
155 |
*/
|
|
156 |
|
|
157 | 0 |
for (int i = 0; i < parameters.length; i++) { |
158 | 0 |
declareVariable(parameters[i].getName()); |
159 |
} |
|
160 |
} |
|
161 |
|
|
162 | 0 |
protected void declareVariable(VariableExpression varExp) { |
163 | 0 |
String variable = varExp.getVariable(); |
164 | 0 |
declareVariable(variable); |
165 |
} |
|
166 |
|
|
167 | 0 |
protected void declareVariable(String variable) { |
168 |
/*
|
|
169 |
if (!parameterSet.contains(variable)) {
|
|
170 |
declaredVariables.add(variable);
|
|
171 |
getReferencedVariables().add(variable);
|
|
172 |
}
|
|
173 |
*/
|
|
174 | 0 |
getDeclaredVariables().add(variable); |
175 | 0 |
getReferencedVariables().add(variable); |
176 |
} |
|
177 |
|
|
178 | 0 |
protected VariableScopeCodeVisitor createClosureVisitor(ClosureExpression expression) {
|
179 | 0 |
VariableScope closureScope = new VariableScope(scope);
|
180 | 0 |
expression.setVariableScope(closureScope); |
181 | 0 |
VariableScopeCodeVisitor answer = new VariableScopeCodeVisitor(closureScope);
|
182 | 0 |
answer.setParameters(expression.getParameters()); |
183 | 0 |
return answer;
|
184 |
} |
|
185 |
} |
|
186 |
|
|