KickJava   Java API By Example, From Geeks To Geeks.

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


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 */

33
34 public class EmptyElseRemover{
35
36     public static void removeElseBody(ASTNode node,ASTIfElseNode ifElseNode ,int subBodyNumber, int nodeNumber){
37     if(!(node instanceof ASTIfElseNode)){
38         //these are the nodes which always have one subBody
39
List subBodies = node.get_SubBodies();
40         if(subBodies.size()!=1){
41         //there is something wrong
42
throw new RuntimeException JavaDoc("Please report this benchmark to the programmer");
43         }
44         List onlySubBody = (List)subBodies.get(0);
45
46         /*
47           The onlySubBody contains the ASTIfElseNode whose elsebody has to be removed
48           at location given by the nodeNumber variable
49         */

50         List newBody = createNewNodeBody(onlySubBody,nodeNumber,ifElseNode);
51         if(newBody==null){
52         //something went wrong
53
return;
54         }
55         if(node instanceof ASTMethodNode){
56         ((ASTMethodNode)node).replaceBody(newBody);
57         G.v().ASTTransformations_modified = true;
58         //System.out.println("REMOVED ELSE BODY");
59
}
60         else if(node instanceof ASTSynchronizedBlockNode){
61         ((ASTSynchronizedBlockNode)node).replaceBody(newBody);
62         G.v().ASTTransformations_modified = true;
63         //System.out.println("REMOVED ELSE BODY");
64
}
65         else if(node instanceof ASTLabeledBlockNode){
66         ((ASTLabeledBlockNode)node).replaceBody(newBody);
67         G.v().ASTTransformations_modified = true;
68         //System.out.println("REMOVED ELSE BODY");
69
}
70         else if(node instanceof ASTUnconditionalLoopNode){
71         ((ASTUnconditionalLoopNode)node).replaceBody(newBody);
72         G.v().ASTTransformations_modified = true;
73         //System.out.println("REMOVED ELSE BODY");
74
}
75         else if(node instanceof ASTIfNode){
76         ((ASTIfNode)node).replaceBody(newBody);
77         G.v().ASTTransformations_modified = true;
78         //System.out.println("REMOVED ELSE BODY");
79
}
80         else if(node instanceof ASTWhileNode){
81         ((ASTWhileNode)node).replaceBody(newBody);
82         G.v().ASTTransformations_modified = true;
83         //System.out.println("REMOVED ELSE BODY");
84
}
85         else if(node instanceof ASTDoWhileNode){
86         ((ASTDoWhileNode)node).replaceBody(newBody);
87         G.v().ASTTransformations_modified = true;
88         //System.out.println("REMOVED ELSE BODY");
89
}
90         else {
91         //there is no other case something is wrong if we get here
92
return;
93         }
94     }
95     else{//its an ASTIfElseNode
96
//if its an ASIfElseNode then check which Subbody has the labeledBlock
97
if(subBodyNumber!=0 && subBodyNumber!=1){
98         //something bad is happening dont do nothin
99
//System.out.println("Error-------not modifying AST");
100
return;
101         }
102         List subBodies = node.get_SubBodies();
103         if(subBodies.size()!=2){
104         //there is something wrong
105
throw new RuntimeException JavaDoc("Please report this benchmark to the programmer");
106         }
107
108         List toModifySubBody = (List)subBodies.get(subBodyNumber);
109
110         /*
111           The toModifySubBody contains the ASTIfElseNode to be removed
112           at location given by the nodeNumber variable
113         */

114         List newBody = createNewNodeBody(toModifySubBody,nodeNumber,ifElseNode);
115         if(newBody==null){
116         //something went wrong
117
return;
118         }
119         if(subBodyNumber==0){
120         //the if body was modified
121
//System.out.println("REMOVED ELSE BODY");
122
G.v().ASTTransformations_modified = true;
123         ((ASTIfElseNode)node).replaceBody(newBody,(List)subBodies.get(1));
124         }
125         else if(subBodyNumber==1){
126         //else body was modified
127
//System.out.println("REMOVED ELSE BODY");
128
G.v().ASTTransformations_modified = true;
129         ((ASTIfElseNode)node).replaceBody((List)subBodies.get(0),newBody);
130         }
131         else{//realllly shouldnt come here
132
//something bad is happening dont do nothin
133
//System.out.println("Error-------not modifying AST");
134
return;
135         }
136
137     }//end of ASTIfElseNode
138
}
139
140
141
142
143
144
145     public static List createNewNodeBody(List oldSubBody,int nodeNumber,ASTIfElseNode ifElseNode){
146     //create a new SubBody
147
List newSubBody = new ArrayList();
148     
149     //this is an iterator of ASTNodes
150
Iterator it = oldSubBody.iterator();
151     
152     //copy to newSubBody all nodes until you get to nodeNumber
153
int index=0;
154     while(index!=nodeNumber ){
155         if(!it.hasNext()){
156         return null;
157         }
158         newSubBody.add(it.next());
159         index++;
160     }
161
162     //at this point the iterator is pointing to the ASTIfElseNode to be removed
163
//just to make sure check this
164
ASTNode toRemove = (ASTNode)it.next();
165     if(!(toRemove instanceof ASTIfElseNode)){
166         //something is wrong
167
return null;
168     }
169     else{
170         ASTIfElseNode toRemoveNode = (ASTIfElseNode)toRemove;
171
172         //just double checking that this is a empty else node
173
List elseBody = toRemoveNode.getElseBody();
174         if(elseBody.size()!=0){
175         //something is wrong we cant remove a non empty elsebody
176
return null;
177         }
178         
179         //so this is the ElseBody to remove
180

181         //need to create an ASTIfNode from the ASTIfElseNode
182
ASTIfNode newNode = new ASTIfNode(toRemoveNode.get_Label(),toRemoveNode.get_Condition(),toRemoveNode.getIfBody());
183
184         //add this node to the newSubBody
185
newSubBody.add(newNode);
186     }
187
188     //add any remaining nodes in the oldSubBody to the new one
189
while(it.hasNext()){
190         newSubBody.add(it.next());
191     }
192
193     //newSubBody is ready return it
194
return newSubBody;
195     }
196 }
Popular Tags