KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 public class UselessLabeledBlockRemover {
28
29     public static void removeLabeledBlock(ASTNode node, ASTLabeledBlockNode labelBlock, int subBodyNumber, int nodeNumber){
30     if(!(node instanceof ASTIfElseNode)){
31         //these are the nodes which always have one subBody
32
List subBodies = node.get_SubBodies();
33         if(subBodies.size()!=1){
34         //there is something wrong
35
throw new RuntimeException JavaDoc("Please report this benchmark to the programmer");
36         }
37         List onlySubBody = (List)subBodies.get(0);
38
39         /*
40           The onlySubBody contains the labeledBlockNode to be removed
41           at location given by the nodeNumber variable
42         */

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

107         List newBody = createNewSubBody(toModifySubBody,nodeNumber,labelBlock);
108         if(newBody==null){
109         //something went wrong
110
return;
111         }
112         if(subBodyNumber==0){
113         //the if body was modified
114
//System.out.println("REMOVED LABEL");
115
G.v().ASTTransformations_modified = true;
116         ((ASTIfElseNode)node).replaceBody(newBody,(List)subBodies.get(1));
117         }
118         else if(subBodyNumber==1){
119         //else body was modified
120
//System.out.println("REMOVED LABEL");
121
G.v().ASTTransformations_modified = true;
122         ((ASTIfElseNode)node).replaceBody((List)subBodies.get(0),newBody);
123         }
124         else{//realllly shouldnt come here
125
//something bad is happening dont do nothin
126
//System.out.println("Error-------not modifying AST");
127
return;
128         }
129
130     }//end of ASTIfElseNode
131
}
132
133
134
135
136
137     public static List createNewSubBody(List oldSubBody,int nodeNumber,ASTLabeledBlockNode labelBlock){
138     //create a new SubBody
139
List newSubBody = new ArrayList();
140     
141     //this is an iterator of ASTNodes
142
Iterator it = oldSubBody.iterator();
143     
144     //copy to newSubBody all nodes until you get to nodeNumber
145
int index=0;
146     while(index!=nodeNumber ){
147         if(!it.hasNext()){
148         return null;
149         }
150         newSubBody.add(it.next());
151         index++;
152     }
153
154     //at this point the iterator is pointing to the ASTLabeledBlock to be removed
155
//just to make sure check this
156
ASTNode toRemove = (ASTNode)it.next();
157     if(!(toRemove instanceof ASTLabeledBlockNode)){
158         //something is wrong
159
return null;
160     }
161     else{
162         ASTLabeledBlockNode toRemoveNode = (ASTLabeledBlockNode)toRemove;
163
164         //just double checking that this is a null label
165
SETNodeLabel label = toRemoveNode.get_Label();
166         if(label.toString()!=null){
167         //something is wrong we cant remove a non null label
168
return null;
169         }
170         
171         //so this is the label to remove
172
//removing a label means bringing all its bodies one step up the hierarchy
173
List blocksSubBodies = toRemoveNode.get_SubBodies();
174         //we know this is a labeledBlock so it has only one subBody
175
List onlySubBodyOfLabeledBlock = (List)blocksSubBodies.get(0);
176         
177         //all these subBodies should be added to the newSubbody
178
newSubBody.addAll(onlySubBodyOfLabeledBlock);
179     }
180
181     //add any remaining nodes in the oldSubBody to the new one
182
while(it.hasNext()){
183         newSubBody.add(it.next());
184     }
185
186     //newSubBody is ready return it
187
return newSubBody;
188     }
189 }
Popular Tags