1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.*; 23 import java.util.*; 24 import soot.dava.*; 25 import soot.dava.internal.SET.*; 26 import soot.dava.internal.AST.*; 27 import soot.dava.toolkits.base.AST.analysis.*; 28 29 30 43 44 public class ForLoopCreator extends DepthFirstAdapter{ 45 46 public ForLoopCreator(){ 47 } 48 49 public ForLoopCreator(boolean verbose){ 50 super(verbose); 51 } 52 53 54 public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){ 55 } 56 57 64 public void normalRetrieving(ASTNode node){ 65 if(node instanceof ASTSwitchNode){ 66 dealWithSwitchNode((ASTSwitchNode)node); 67 return; 68 } 69 70 Iterator sbit = node.get_SubBodies().iterator(); 72 73 int subBodyNumber=0; 75 while (sbit.hasNext()) { 76 List subBody = (List)sbit.next(); 77 Iterator it = subBody.iterator(); 78 79 int nodeNumber=0; 80 while (it.hasNext()){ 82 ASTNode temp = (ASTNode) it.next(); 83 if(temp instanceof ASTStatementSequenceNode){ 84 if(it.hasNext()){ 86 ASTNode temp1 = (ASTNode)subBody.get(nodeNumber+1); 88 if(temp1 instanceof ASTWhileNode){ 89 91 ForLoopCreationHelper helper = 92 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1); 93 94 if(helper.checkPattern()){ 95 97 List newBody = helper.createNewBody(subBody,nodeNumber); 98 if(newBody!= null){ 99 if(node instanceof ASTIfElseNode){ 100 if(subBodyNumber==0){ 101 List subBodies = node.get_SubBodies(); 103 List ifElseBody = (List)subBodies.get(1); 104 ((ASTIfElseNode)node).replaceBody(newBody,ifElseBody); 105 G.v().ASTTransformations_modified = true; 106 return; 108 } 109 else if(subBodyNumber==1){ 110 List subBodies = node.get_SubBodies(); 112 List ifBody = (List)subBodies.get(0); 113 ((ASTIfElseNode)node).replaceBody(ifBody,newBody); 114 G.v().ASTTransformations_modified = true; 115 return; 117 } 118 else{ 119 throw new RuntimeException ("Please report benchmark to programmer."); 120 } 121 } 122 else{ 123 if(node instanceof ASTMethodNode){ 124 ((ASTMethodNode)node).replaceBody(newBody); 125 G.v().ASTTransformations_modified = true; 126 return; 128 129 } 130 else if(node instanceof ASTSynchronizedBlockNode){ 131 ((ASTSynchronizedBlockNode)node).replaceBody(newBody); 132 G.v().ASTTransformations_modified = true; 133 return; 135 } 136 else if(node instanceof ASTLabeledBlockNode){ 137 ((ASTLabeledBlockNode)node).replaceBody(newBody); 138 G.v().ASTTransformations_modified = true; 139 return; 141 } 142 else if(node instanceof ASTUnconditionalLoopNode){ 143 ((ASTUnconditionalLoopNode)node).replaceBody(newBody); 144 G.v().ASTTransformations_modified = true; 145 return; 147 } 148 else if(node instanceof ASTIfNode){ 149 ((ASTIfNode)node).replaceBody(newBody); 150 G.v().ASTTransformations_modified = true; 151 return; 153 } 154 else if(node instanceof ASTWhileNode){ 155 ((ASTWhileNode)node).replaceBody(newBody); 156 G.v().ASTTransformations_modified = true; 157 return; 159 } 160 else if(node instanceof ASTDoWhileNode){ 161 ((ASTDoWhileNode)node).replaceBody(newBody); 162 G.v().ASTTransformations_modified = true; 163 return; 165 } 166 else if(node instanceof ASTForLoopNode){ 167 ((ASTForLoopNode)node).replaceBody(newBody); 168 G.v().ASTTransformations_modified = true; 169 return; 171 } 172 else { 173 throw new RuntimeException ("Please report benchmark to programmer."); 174 } 175 } 176 } } } } } temp.apply(this); 182 nodeNumber++; 183 } subBodyNumber++; 185 } } 187 188 public void caseASTTryNode(ASTTryNode node){ 189 inASTTryNode(node); 190 191 List tryBody = node.get_TryBody(); 193 Iterator it = tryBody.iterator(); 194 195 int nodeNumber=0; 196 while (it.hasNext()){ 198 ASTNode temp = (ASTNode) it.next(); 199 if(temp instanceof ASTStatementSequenceNode){ 200 if(it.hasNext()){ 202 ASTNode temp1 = (ASTNode)tryBody.get(nodeNumber+1); 204 if(temp1 instanceof ASTWhileNode){ 205 207 ForLoopCreationHelper helper = 208 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1); 209 210 if(helper.checkPattern()){ 211 213 List newBody = helper.createNewBody(tryBody,nodeNumber); 214 if(newBody!= null){ 215 node.replaceTryBody(newBody); 217 G.v().ASTTransformations_modified = true; 218 return; 220 } } } } } temp.apply(this); 226 nodeNumber++; 227 } 229 230 231 232 Map exceptionMap = node.get_ExceptionMap(); 233 Map paramMap = node.get_ParamMap(); 234 List catchList = node.get_CatchList(); 239 Iterator itBody=null; 240 it = catchList.iterator(); 241 while (it.hasNext()) { 242 ASTTryNode.container catchBody = (ASTTryNode.container)it.next(); 243 244 SootClass sootClass = ((SootClass)exceptionMap.get(catchBody)); 245 Type type = sootClass.getType(); 246 247 caseType(type); 249 250 Local local = (Local)paramMap.get(catchBody); 252 decideCaseExprOrRef(local); 253 254 List body = (List)catchBody.o; 256 itBody = body.iterator(); 257 258 nodeNumber=0; 259 while (itBody.hasNext()){ 261 ASTNode temp = (ASTNode) itBody.next(); 262 263 if(temp instanceof ASTStatementSequenceNode){ 264 if(itBody.hasNext()){ 266 ASTNode temp1 = (ASTNode)body.get(nodeNumber+1); 268 if(temp1 instanceof ASTWhileNode){ 269 271 ForLoopCreationHelper helper = 272 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1); 273 274 if(helper.checkPattern()){ 275 277 List newBody = helper.createNewBody(body,nodeNumber); 278 if(newBody!= null){ 279 catchBody.replaceBody(newBody); 281 G.v().ASTTransformations_modified = true; 282 return; 284 } } } } } 290 temp.apply(this); 291 nodeNumber++; 292 } 293 } 294 295 outASTTryNode(node); 296 } 297 298 299 private void dealWithSwitchNode(ASTSwitchNode node){ 300 302 List indexList = node.getIndexList(); 303 Map index2BodyList = node.getIndex2BodyList(); 304 305 Iterator it = indexList.iterator(); 306 while (it.hasNext()) { Object currentIndex = it.next(); 308 List body = (List) index2BodyList.get( currentIndex); 309 310 if (body != null){ 311 313 Iterator itBody = body.iterator(); 314 int nodeNumber=0; 315 while (itBody.hasNext()){ 317 ASTNode temp = (ASTNode) itBody.next(); 318 319 if(temp instanceof ASTStatementSequenceNode){ 320 if(itBody.hasNext()){ 322 ASTNode temp1 = (ASTNode)body.get(nodeNumber+1); 324 if(temp1 instanceof ASTWhileNode){ 325 327 ForLoopCreationHelper helper = 328 new ForLoopCreationHelper((ASTStatementSequenceNode)temp,(ASTWhileNode)temp1); 329 330 if(helper.checkPattern()){ 331 333 List newBody = helper.createNewBody(body,nodeNumber); 334 if(newBody!= null){ 335 337 index2BodyList.put(currentIndex,newBody); 339 node.replaceIndex2BodyList(index2BodyList); 341 342 G.v().ASTTransformations_modified = true; 343 return; 345 } } } } } temp.apply(this); 351 nodeNumber++; 352 } 353 } 354 } 355 } 356 } | Popular Tags |