KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > transformations > ASTCleanerTwo


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair 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 package soot.dava.toolkits.base.AST.transformations;
21
22 import soot.*;
23 import java.util.*;
24 import soot.dava.internal.SET.*;
25 import soot.dava.internal.AST.*;
26 import soot.dava.toolkits.base.AST.analysis.*;
27
28
29 /*
30   Nomair A. Naeem 21-FEB-2005
31
32   In the depthFirstAdaptor children of a ASTNode
33   are gotten in three ways
34   a, ASTStatementSequenceNode uses one way see caseASTStatementSequenceNode in DepthFirstAdapter
35   b, ASTTryNode uses another way see caseASTTryNode in DepthFirstAdapter
36   c, All other nodes use normalRetrieving method to retrieve the children
37
38   TO MAKE CODE EFFECIENT BLOCK THE ANALYSIS TO GOING INTO STATEMENTS
39   this is done by overriding the caseASTStatementSequenceNode
40
41   Current tasks of the cleaner
42      Invoke IfElsebreaker
43
44 */

45
46 public class ASTCleanerTwo extends DepthFirstAdapter{
47
48     public ASTCleanerTwo(){
49     }
50
51     public ASTCleanerTwo(boolean verbose){
52     super(verbose);
53     }
54
55     
56     public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){
57     }
58
59     /*
60       Note the ASTNode in this case can be any of the following:
61       ASTMethodNode ASTSwitchNode ASTIfNode
62       ASTIfElseNode ASTUnconditionalWhileNode ASTWhileNode
63       ASTDoWhileNode ASTForLoopNode ASTLabeledBlockNode
64       ASTSynchronizedBlockNode
65     */

66     public void normalRetrieving(ASTNode node){
67     if(node instanceof ASTSwitchNode){
68         dealWithSwitchNode((ASTSwitchNode)node);
69         return;
70     }
71
72     //from the Node get the subBodes
73
Iterator sbit = node.get_SubBodies().iterator();
74
75     //onlyASTIfElseNode has 2 subBodies but we need to deal with that
76
int subBodyNumber=0;
77     while (sbit.hasNext()) {
78         List subBody = (List)sbit.next();
79         Iterator it = subBody.iterator();
80
81         int nodeNumber=0;
82         //go over the ASTNodes in this subBody and apply
83
while (it.hasNext()){
84         ASTNode temp = (ASTNode) it.next();
85         if(temp instanceof ASTIfElseNode){
86             IfElseBreaker breaker = new IfElseBreaker();
87             boolean success=false;
88             if(breaker.isIfElseBreakingPossiblePatternOne((ASTIfElseNode)temp)){
89             success=true;
90             }
91             else if(breaker.isIfElseBreakingPossiblePatternTwo((ASTIfElseNode)temp)){
92             success=true;
93             }
94             if(G.v().ASTTransformations_modified)
95             return;
96             if(!success){
97             //System.out.println("not successful");
98
}
99             if(success){
100             List newBody = breaker.createNewBody(subBody,nodeNumber);
101
102             if(newBody!= null){
103                 if(node instanceof ASTIfElseNode){
104                 if(subBodyNumber==0){
105                     //the if body was modified
106
List subBodies = node.get_SubBodies();
107                     List ifElseBody = (List)subBodies.get(1);
108                     ((ASTIfElseNode)node).replaceBody(newBody,ifElseBody);
109                     G.v().ASTTransformations_modified = true;
110                     //System.out.println("BROKE IFELSE 1");
111
return;
112                 }
113                 else if(subBodyNumber==1){
114                     //else body was modified
115
List subBodies = node.get_SubBodies();
116                     List ifBody = (List)subBodies.get(0);
117                     ((ASTIfElseNode)node).replaceBody(ifBody,newBody);
118                     G.v().ASTTransformations_modified = true;
119                     //System.out.println("BROKE IFELSE 2");
120
return;
121                 }
122                 else{
123                    throw new RuntimeException JavaDoc("Please report benchmark to programmer");
124                 }
125                 }
126                 else{
127                 if(node instanceof ASTMethodNode){
128                     ((ASTMethodNode)node).replaceBody(newBody);
129                     G.v().ASTTransformations_modified = true;
130                     //System.out.println("BROKE IFELSE 3");
131
return;
132                 }
133                 else if(node instanceof ASTSynchronizedBlockNode){
134                     ((ASTSynchronizedBlockNode)node).replaceBody(newBody);
135                     G.v().ASTTransformations_modified = true;
136                     //System.out.println("BROKE IFELSE 4");
137
return;
138                 }
139                 else if(node instanceof ASTLabeledBlockNode){
140                     ((ASTLabeledBlockNode)node).replaceBody(newBody);
141                     G.v().ASTTransformations_modified = true;
142                     //System.out.println("BROKE IFELSE 5");
143
return;
144                 }
145                 else if(node instanceof ASTUnconditionalLoopNode){
146                     ((ASTUnconditionalLoopNode)node).replaceBody(newBody);
147                     G.v().ASTTransformations_modified = true;
148                     //System.out.println("BROKE IFELSE 6");
149
return;
150                 }
151                 else if(node instanceof ASTIfNode){
152                     ((ASTIfNode)node).replaceBody(newBody);
153                     G.v().ASTTransformations_modified = true;
154                     //System.out.println("BROKE IFELSE 7");
155
return;
156                 }
157                 else if(node instanceof ASTWhileNode){
158                     ((ASTWhileNode)node).replaceBody(newBody);
159                     G.v().ASTTransformations_modified = true;
160                     //System.out.println("BROKE IFELSE 8");
161
return;
162                 }
163                 else if(node instanceof ASTDoWhileNode){
164                     ((ASTDoWhileNode)node).replaceBody(newBody);
165                     G.v().ASTTransformations_modified = true;
166                     //System.out.println("BROKE IFELSE 9");
167
return;
168                 }
169                 else if(node instanceof ASTForLoopNode){
170                     ((ASTForLoopNode)node).replaceBody(newBody);
171                     G.v().ASTTransformations_modified = true;
172                     //System.out.println("BROKE IFELSE 11");
173
return;
174                 }
175                 else {
176                     throw new RuntimeException JavaDoc("Please report benchmark to programmer");
177                 }
178                 }
179             }//newBody was not null
180
}
181         }
182         temp.apply(this);
183         nodeNumber++;
184         }
185         subBodyNumber++;
186     }//end of going over subBodies
187
}
188
189     public void caseASTTryNode(ASTTryNode node){
190     inASTTryNode(node);
191
192     //get try body
193
List tryBody = node.get_TryBody();
194     Iterator it = tryBody.iterator();
195
196     int nodeNumber=0;
197     //go over the ASTNodes and apply
198
while (it.hasNext()){
199         ASTNode temp = (ASTNode) it.next();
200         if(temp instanceof ASTIfElseNode){
201             
202         IfElseBreaker breaker = new IfElseBreaker();
203         boolean success=false;
204         if(breaker.isIfElseBreakingPossiblePatternOne((ASTIfElseNode)temp)){
205             success=true;
206         }
207         else if(breaker.isIfElseBreakingPossiblePatternTwo((ASTIfElseNode)temp)){
208             success=true;
209         }
210         if(G.v().ASTTransformations_modified)
211             return;
212         if(success){
213             List newBody = breaker.createNewBody(tryBody,nodeNumber);
214             
215             if(newBody!= null){
216             //something did not go wrong
217
node.replaceTryBody(newBody);
218             G.v().ASTTransformations_modified = true;
219             //System.out.println("BROKE IFELSE 10");
220
return;
221             }//newBody was not null
222
}
223         }
224         temp.apply(this);
225         nodeNumber++;
226     }
227
228
229
230
231     Map exceptionMap = node.get_ExceptionMap();
232     Map paramMap = node.get_ParamMap();
233     //get catch list and apply on the following
234
// a, type of exception caught
235
// b, local of exception
236
// c, catchBody
237
List catchList = node.get_CatchList();
238     Iterator itBody=null;
239         it = catchList.iterator();
240     while (it.hasNext()) {
241         ASTTryNode.container catchBody = (ASTTryNode.container)it.next();
242         
243         SootClass sootClass = ((SootClass)exceptionMap.get(catchBody));
244         Type type = sootClass.getType();
245         
246         //apply on type of exception
247
caseType(type);
248
249         //apply on local of exception
250
Local local = (Local)paramMap.get(catchBody);
251         decideCaseExprOrRef(local);
252
253         //apply on catchBody
254
List body = (List)catchBody.o;
255         itBody = body.iterator();
256
257         nodeNumber=0;
258         //go over the ASTNodes and apply
259
while (itBody.hasNext()){
260         ASTNode temp = (ASTNode) itBody.next();
261         if(temp instanceof ASTIfElseNode){
262             IfElseBreaker breaker = new IfElseBreaker();
263             boolean success=false;
264             if(breaker.isIfElseBreakingPossiblePatternOne((ASTIfElseNode)temp)){
265             success=true;
266             }
267             else if(breaker.isIfElseBreakingPossiblePatternTwo((ASTIfElseNode)temp)){
268             success=true;
269             }
270             if(G.v().ASTTransformations_modified)
271             return;
272             if(success){
273             List newBody = breaker.createNewBody(body,nodeNumber);
274             
275             if(newBody!= null){
276                 //something did not go wrong
277
catchBody.replaceBody(newBody);
278                 G.v().ASTTransformations_modified = true;
279                 //System.out.println("BROKE IFELSE 11");
280
return;
281             }//newBody was not null
282
}
283         }
284         temp.apply(this);
285         nodeNumber++;
286         }
287     }
288     
289     outASTTryNode(node);
290     }
291
292
293     private void dealWithSwitchNode(ASTSwitchNode node){
294     //do a depthfirst on elements of the switchNode
295

296     List indexList = node.getIndexList();
297     Map index2BodyList = node.getIndex2BodyList();
298
299     Iterator it = indexList.iterator();
300     while (it.hasNext()) {//going through all the cases of the switch statement
301
Object JavaDoc currentIndex = it.next();
302         List body = (List) index2BodyList.get( currentIndex);
303         
304         if (body != null){
305         //this body is a list of ASTNodes
306

307         Iterator itBody = body.iterator();
308         int nodeNumber=0;
309         //go over the ASTNodes and apply
310
while (itBody.hasNext()){
311             ASTNode temp = (ASTNode) itBody.next();
312             if(temp instanceof ASTIfElseNode){
313             IfElseBreaker breaker = new IfElseBreaker();
314             boolean success=false;
315             if(breaker.isIfElseBreakingPossiblePatternOne((ASTIfElseNode)temp)){
316                 success=true;
317             }
318             else if(breaker.isIfElseBreakingPossiblePatternTwo((ASTIfElseNode)temp)){
319                 success=true;
320             }
321             if(G.v().ASTTransformations_modified)
322                 return;
323             if(success){
324                 List newBody = breaker.createNewBody(body,nodeNumber);
325                 
326                 if(newBody!= null){
327                 //put this body in the Map
328
index2BodyList.put(currentIndex,newBody);
329                 //replace in actual switchNode
330
node.replaceIndex2BodyList(index2BodyList);
331                 G.v().ASTTransformations_modified = true;
332                 //System.out.println("BROKE IFELSE 12");
333
return;
334                 }//newBody was not null
335
}
336             }
337             temp.apply(this);
338             nodeNumber++;
339         }
340         }
341     }
342     }
343 }
344
Popular Tags