KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > traversals > ASTParentNodeFinder


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair A. Naeem
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Maintained by Nomair A. Naeem
22  */

23
24 /*
25  * Change log: * November 22nd 2005: Moved this class from structuredAnalysis
26  * package to traversals package. Since this is a traversal not an analysis
27  *
28  */

29
30
31 package soot.dava.toolkits.base.AST.traversals;
32
33
34 import java.util.*;
35 import soot.jimple.*;
36 import soot.dava.internal.AST.*;
37 import soot.dava.internal.javaRep.*;
38 import soot.dava.toolkits.base.AST.analysis.*;
39
40
41 /*
42  * This traversal class is responsible to gather information
43  * regarding the different nodes and statements in the AST.
44  * The class produces a HashMap between the node/statement given as
45  * key and the parent of this construct (value)
46  *
47  * November 23rd, 2005. (Nomair) It is used for instance in the CopyPropagation algorithm
48  * to be able to remove a particular copy
49  * stmt for instance from its parent.
50  */

51
52 public class ASTParentNodeFinder extends DepthFirstAdapter{
53
54     HashMap parentOf;
55     Stack parentStack;
56
57     public ASTParentNodeFinder(){
58     parentOf = new HashMap();
59     parentStack = new Stack();
60     }
61
62     public ASTParentNodeFinder(boolean verbose){
63     super(verbose);
64     parentOf = new HashMap();
65     parentStack = new Stack();
66     }
67
68     public void inASTMethodNode(ASTMethodNode node){
69     parentOf.put(node,null);
70     parentStack.push(node);
71     }
72
73     public void outASTMethodNode(ASTMethodNode node){
74     parentStack.pop();
75     }
76
77
78     public void inASTSynchronizedBlockNode(ASTSynchronizedBlockNode node){
79     parentOf.put(node,parentStack.peek());
80     parentStack.push(node);
81     }
82     public void outASTSynchronizedBlockNode(ASTSynchronizedBlockNode node){
83     parentStack.pop();
84     }
85
86
87     public void inASTLabeledBlockNode (ASTLabeledBlockNode node){
88     parentOf.put(node,parentStack.peek());
89     parentStack.push(node);
90     }
91     public void outASTLabeledBlockNode (ASTLabeledBlockNode node){
92     parentStack.pop();
93     }
94
95
96
97     public void inASTUnconditionalLoopNode (ASTUnconditionalLoopNode node){
98     parentOf.put(node,parentStack.peek());
99     parentStack.push(node);
100     }
101     public void outASTUnconditionalLoopNode (ASTUnconditionalLoopNode node){
102     parentStack.pop();
103     }
104
105
106
107     public void inASTSwitchNode(ASTSwitchNode node){
108     parentOf.put(node,parentStack.peek());
109     parentStack.push(node);
110     }
111     public void outASTSwitchNode(ASTSwitchNode node){
112     parentStack.pop();
113     }
114
115
116
117
118     public void inASTIfNode(ASTIfNode node){
119     parentOf.put(node,parentStack.peek());
120     parentStack.push(node);
121     }
122     public void outASTIfNode(ASTIfNode node){
123     parentStack.pop();
124     }
125
126
127
128
129     public void inASTIfElseNode(ASTIfElseNode node){
130     parentOf.put(node,parentStack.peek());
131     parentStack.push(node);
132     }
133     public void outASTIfElseNode(ASTIfElseNode node){
134     parentStack.pop();
135     }
136
137
138
139
140     public void inASTWhileNode(ASTWhileNode node){
141     parentOf.put(node,parentStack.peek());
142     parentStack.push(node);
143     }
144     public void outASTWhileNode(ASTWhileNode node){
145     parentStack.pop();
146     }
147
148
149
150
151
152
153
154     public void inASTForLoopNode(ASTForLoopNode node){
155     parentOf.put(node,parentStack.peek());
156     parentStack.push(node);
157     }
158     public void outASTForLoopNode(ASTForLoopNode node){
159     parentStack.pop();
160     }
161
162
163
164
165     public void inASTDoWhileNode(ASTDoWhileNode node){
166     parentOf.put(node,parentStack.peek());
167     parentStack.push(node);
168     }
169     public void outASTDoWhileNode(ASTDoWhileNode node){
170     parentStack.pop();
171     }
172
173
174
175
176
177
178
179     public void inASTTryNode(ASTTryNode node){
180     parentOf.put(node,parentStack.peek());
181     parentStack.push(node);
182     }
183     public void outASTTryNode(ASTTryNode node){
184     parentStack.pop();
185     }
186
187
188
189
190
191
192     public void inASTStatementSequenceNode(ASTStatementSequenceNode node){
193     parentOf.put(node,parentStack.peek());
194     parentStack.push(node);
195     }
196     public void outASTStatementSequenceNode(ASTStatementSequenceNode node){
197     parentStack.pop();
198     }
199
200
201
202
203     public void inDefinitionStmt(DefinitionStmt s){
204     parentOf.put(s,parentStack.peek());
205     }
206
207     public void inReturnStmt(ReturnStmt s){
208     parentOf.put(s,parentStack.peek());
209     }
210
211     public void inInvokeStmt(InvokeStmt s){
212     parentOf.put(s,parentStack.peek());
213     }
214
215
216
217
218     public void inThrowStmt(ThrowStmt s){
219     parentOf.put(s,parentStack.peek());
220     }
221
222     public void inDVariableDeclarationStmt(DVariableDeclarationStmt s){
223     parentOf.put(s,parentStack.peek());
224     }
225
226
227     public void inStmt(Stmt s){
228     parentOf.put(s,parentStack.peek());
229     }
230
231
232     /*
233      * This is the method which should be invoked by classes needing parent information.
234      * When the method is invoked with a statement or node as input it returns the parent
235      * of that object. The parent can safely be casted to ASTNode as long as the parent
236      * returned is non null
237      */

238     public Object JavaDoc getParentOf(Object JavaDoc statementOrNode){
239     return parentOf.get(statementOrNode);
240     }
241 }
Popular Tags