KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 UselessLabeledBlockRemover
43      Invoke EmptyElseRemover
44      Apply OrAggregatorThree
45 */

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

73     public void normalRetrieving(ASTNode node){
74     if(node instanceof ASTSwitchNode){
75         dealWithSwitchNode((ASTSwitchNode)node);
76         return;
77     }
78
79     //from the Node get the subBodes
80
Iterator sbit = node.get_SubBodies().iterator();
81
82     //onlyASTIfElseNode has 2 subBodies but we need to deal with that
83
int subBodyNumber=0;
84     while (sbit.hasNext()) {
85         Object JavaDoc subBody = sbit.next();
86         Iterator it = ((List) subBody).iterator();
87
88         int nodeNumber=0;
89         //go over the ASTNodes in this subBody and apply
90
while (it.hasNext()){
91         ASTNode temp = (ASTNode) it.next();
92         if(temp instanceof ASTLabeledBlockNode){
93             //check if the label is null
94
ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp;
95             SETNodeLabel label = labelBlock.get_Label();
96             if(label.toString()==null){
97             //uselessLabeledBlock Found REMOVE IT
98
UselessLabeledBlockRemover.removeLabeledBlock(node,labelBlock,subBodyNumber,nodeNumber);
99             if(G.v().ASTTransformations_modified){
100                 return;
101             }
102             }
103         }
104         else if(temp instanceof ASTIfElseNode){
105             //check if there is an empty else body
106
List elseBody = ((ASTIfElseNode)temp).getElseBody();
107             if(elseBody.size()==0){
108             EmptyElseRemover.removeElseBody(node,(ASTIfElseNode)temp,subBodyNumber,nodeNumber);
109             }
110         }
111         else if(temp instanceof ASTIfNode){
112             //check if the next node in the subBody is also an ASTIfNode in which case invoke OrAggregatorThree
113
if(it.hasNext()){//means we can get the nodeNumber+1
114
ASTNode nextNode = (ASTNode)((List)subBody).get(nodeNumber+1);
115             if(nextNode instanceof ASTIfNode){
116                 //found an If followed by another if might match Patter 3.
117
OrAggregatorThree.checkAndTransform(node,(ASTIfNode)temp,(ASTIfNode)nextNode,nodeNumber,subBodyNumber);
118                 if(G.v().ASTTransformations_modified){
119                 //if we modified something we want to stop since the tree is stale
120
//System.out.println("here");
121
return;
122                 }
123                 
124             }
125             }
126         }
127         temp.apply(this);
128         nodeNumber++;
129         }
130         subBodyNumber++;
131     }//end of going over subBodies
132
}
133
134     public void caseASTTryNode(ASTTryNode node){
135     inASTTryNode(node);
136
137     //get try body
138
List tryBody = node.get_TryBody();
139     Iterator it = tryBody.iterator();
140
141     int nodeNumber=0;
142     //go over the ASTNodes and apply
143
while (it.hasNext()){
144         ASTNode temp = (ASTNode) it.next();
145         if(temp instanceof ASTLabeledBlockNode){
146         //check if the label is null
147
ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp;
148         SETNodeLabel label = labelBlock.get_Label();
149         if(label.toString()==null){
150             //uselessLabeledBlock Found REMOVE IT
151

152             List newBody=UselessLabeledBlockRemover.createNewSubBody(tryBody,nodeNumber,labelBlock);
153             if(newBody!=null){
154             //something did not go wrong
155
node.replaceTryBody(newBody);
156             G.v().ASTTransformations_modified = true;
157             //System.out.println("REMOVED LABEL from within trybody");
158
}
159         }
160         }
161         else if(temp instanceof ASTIfElseNode){
162         //check if there is an empty else body
163
List elseBody = ((ASTIfElseNode)temp).getElseBody();
164         if(elseBody.size()==0){
165             //System.out.println("Empty else body found"+temp);
166
List newBody=EmptyElseRemover.createNewNodeBody(tryBody,nodeNumber,(ASTIfElseNode)temp);
167             if(newBody!=null){
168             //something did not go wrong
169
node.replaceTryBody(newBody);
170             G.v().ASTTransformations_modified = true;
171             //System.out.println("REMOVED ELSEBODY from within trybody");
172
return;
173             }
174         }
175         }
176         else if(temp instanceof ASTIfNode){
177         //check if the next node in the subBody is also an ASTIfNode in which case invoke OrAggregatorThree
178
if(it.hasNext()){//means we can get the nodeNumber+1
179
ASTNode nextNode = (ASTNode)tryBody.get(nodeNumber+1);
180             if(nextNode instanceof ASTIfNode){
181             //found an If followed by another if might match Patter 3.
182
List newBody=OrAggregatorThree.createNewNodeBody(tryBody,nodeNumber,(ASTIfNode)temp,(ASTIfNode)nextNode);
183             if(newBody!=null){
184                 //something did not go wrong and pattern was matched
185
node.replaceTryBody(newBody);
186                 G.v().ASTTransformations_modified = true;
187                 //we modified something we want to stop since the tree is stale
188
//System.out.println("here");
189
return;
190                 //System.out.println("OR AGGREGATOR THREE");
191
}
192             }
193         }
194         }
195         temp.apply(this);
196         nodeNumber++;
197     }
198
199
200
201
202     Map exceptionMap = node.get_ExceptionMap();
203     Map paramMap = node.get_ParamMap();
204     //get catch list and apply on the following
205
// a, type of exception caught
206
// b, local of exception
207
// c, catchBody
208
List catchList = node.get_CatchList();
209     Iterator itBody=null;
210         it = catchList.iterator();
211     while (it.hasNext()) {
212         ASTTryNode.container catchBody = (ASTTryNode.container)it.next();
213         
214         SootClass sootClass = ((SootClass)exceptionMap.get(catchBody));
215         Type type = sootClass.getType();
216         
217         //apply on type of exception
218
caseType(type);
219
220         //apply on local of exception
221
Local local = (Local)paramMap.get(catchBody);
222         decideCaseExprOrRef(local);
223
224         //apply on catchBody
225
List body = (List)catchBody.o;
226         itBody = body.iterator();
227
228         nodeNumber=0;
229         //go over the ASTNodes and apply
230
while (itBody.hasNext()){
231         ASTNode temp = (ASTNode) itBody.next();
232         if(temp instanceof ASTLabeledBlockNode){
233             //check if the label is null
234
ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp;
235             SETNodeLabel label = labelBlock.get_Label();
236             if(label.toString()==null){
237             //uselessLabeledBlock Found REMOVE IT
238

239             List newBody=UselessLabeledBlockRemover.createNewSubBody(body,nodeNumber,labelBlock);
240             if(newBody!=null){
241                 //something did not go wrong
242
catchBody.replaceBody(newBody);
243                 G.v().ASTTransformations_modified = true;
244                 //System.out.println("REMOVED LABEL from within catchlist");
245
}
246             
247             }
248         }
249         else if(temp instanceof ASTIfElseNode){
250             //check if there is an empty else body
251
List elseBody = ((ASTIfElseNode)temp).getElseBody();
252             if(elseBody.size()==0){
253             //System.out.println("Empty else body found"+temp);
254
List newBody=EmptyElseRemover.createNewNodeBody(body,nodeNumber,(ASTIfElseNode)temp);
255             if(newBody!=null){
256                 //something did not go wrong
257
catchBody.replaceBody(newBody);
258                 G.v().ASTTransformations_modified = true;
259                 //System.out.println("REMOVED ELSEBODY FROm within catchlist");
260
return;
261             }
262             }
263         }
264         else if(temp instanceof ASTIfNode){
265             //check if the next node in the subBody is also an ASTIfNode in which case invoke OrAggregatorThree
266
if(itBody.hasNext()){//means we can get the nodeNumber+1
267
ASTNode nextNode = (ASTNode)body.get(nodeNumber+1);
268             if(nextNode instanceof ASTIfNode){
269                 //found an If followed by another if might match Patter 3.
270
List newBody=OrAggregatorThree.createNewNodeBody(body,nodeNumber,(ASTIfNode)temp,(ASTIfNode)nextNode);
271                 if(newBody!=null){
272                 //something did not go wrong and pattern was matched
273
catchBody.replaceBody(newBody);
274                 G.v().ASTTransformations_modified = true;
275                 //System.out.println("OR AGGREGATOR THREE");
276
return;
277                 }
278             }
279             }
280         }
281         temp.apply(this);
282         nodeNumber++;
283         }
284     }
285     
286     outASTTryNode(node);
287     }
288
289
290     private void dealWithSwitchNode(ASTSwitchNode node){
291     //do a depthfirst on elements of the switchNode
292

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

304         Iterator itBody = body.iterator();
305         int nodeNumber=0;
306         //go over the ASTNodes and apply
307
while (itBody.hasNext()){
308             ASTNode temp = (ASTNode) itBody.next();
309             if(temp instanceof ASTLabeledBlockNode){
310             //check if the label is null
311
ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp;
312             SETNodeLabel label = labelBlock.get_Label();
313             if(label.toString()==null){
314                 //uselessLabeledBlock Found REMOVE IT
315

316                 List newBody=UselessLabeledBlockRemover.createNewSubBody(body,nodeNumber,labelBlock);
317                 if(newBody!=null){
318                 //something did not go wrong
319

320                 //put this body in the Map
321
index2BodyList.put(currentIndex,newBody);
322                 //replace in actual switchNode
323
node.replaceIndex2BodyList(index2BodyList);
324                 G.v().ASTTransformations_modified = true;
325                 //System.out.println("REMOVED LABEL From Within Switch");
326
}
327             }
328             }
329             else if(temp instanceof ASTIfElseNode){
330             //check if there is an empty else body
331
List elseBody = ((ASTIfElseNode)temp).getElseBody();
332             if(elseBody.size()==0){
333                 //System.out.println("Empty else body found"+temp);
334
List newBody=EmptyElseRemover.createNewNodeBody(body,nodeNumber,(ASTIfElseNode)temp);
335                 if(newBody!=null){
336                 //something did not go wrong
337

338                    //put this body in the Map
339
index2BodyList.put(currentIndex,newBody);
340                 //replace in actual switchNode
341
node.replaceIndex2BodyList(index2BodyList);
342                 G.v().ASTTransformations_modified = true;
343                 //System.out.println("REMOVED ELSEBODY FROM WITHIN SWITCH");
344
return;
345                 }
346             }
347             }
348             else if(temp instanceof ASTIfNode){
349             //check if the next node in the subBody is also an ASTIfNode in which case invoke OrAggregatorThree
350
if(itBody.hasNext()){//means we can get the nodeNumber+1
351
ASTNode nextNode = (ASTNode)body.get(nodeNumber+1);
352                 if(nextNode instanceof ASTIfNode){
353                 //found an If followed by another if might match Patter 3.
354
List newBody=OrAggregatorThree.createNewNodeBody(body,nodeNumber,(ASTIfNode)temp,(ASTIfNode)nextNode);
355                 if(newBody!=null){
356                     //something did not go wrong and pattern was matched
357

358                     //put this body in the Map
359
index2BodyList.put(currentIndex,newBody);
360                     //replace in actual switchNode
361
node.replaceIndex2BodyList(index2BodyList);
362
363                     G.v().ASTTransformations_modified = true;
364                     //System.out.println("OR AGGREGATOR THREE");
365
return;
366                 }
367                 }
368             }
369             }
370             temp.apply(this);
371             nodeNumber++;
372         }
373         }
374     }
375     }
376 }
Popular Tags