1 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 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 73 public void normalRetrieving(ASTNode node){ 74 if(node instanceof ASTSwitchNode){ 75 dealWithSwitchNode((ASTSwitchNode)node); 76 return; 77 } 78 79 Iterator sbit = node.get_SubBodies().iterator(); 81 82 int subBodyNumber=0; 84 while (sbit.hasNext()) { 85 Object subBody = sbit.next(); 86 Iterator it = ((List) subBody).iterator(); 87 88 int nodeNumber=0; 89 while (it.hasNext()){ 91 ASTNode temp = (ASTNode) it.next(); 92 if(temp instanceof ASTLabeledBlockNode){ 93 ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp; 95 SETNodeLabel label = labelBlock.get_Label(); 96 if(label.toString()==null){ 97 UselessLabeledBlockRemover.removeLabeledBlock(node,labelBlock,subBodyNumber,nodeNumber); 99 if(G.v().ASTTransformations_modified){ 100 return; 101 } 102 } 103 } 104 else if(temp instanceof ASTIfElseNode){ 105 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 if(it.hasNext()){ ASTNode nextNode = (ASTNode)((List)subBody).get(nodeNumber+1); 115 if(nextNode instanceof ASTIfNode){ 116 OrAggregatorThree.checkAndTransform(node,(ASTIfNode)temp,(ASTIfNode)nextNode,nodeNumber,subBodyNumber); 118 if(G.v().ASTTransformations_modified){ 119 return; 122 } 123 124 } 125 } 126 } 127 temp.apply(this); 128 nodeNumber++; 129 } 130 subBodyNumber++; 131 } } 133 134 public void caseASTTryNode(ASTTryNode node){ 135 inASTTryNode(node); 136 137 List tryBody = node.get_TryBody(); 139 Iterator it = tryBody.iterator(); 140 141 int nodeNumber=0; 142 while (it.hasNext()){ 144 ASTNode temp = (ASTNode) it.next(); 145 if(temp instanceof ASTLabeledBlockNode){ 146 ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp; 148 SETNodeLabel label = labelBlock.get_Label(); 149 if(label.toString()==null){ 150 152 List newBody=UselessLabeledBlockRemover.createNewSubBody(tryBody,nodeNumber,labelBlock); 153 if(newBody!=null){ 154 node.replaceTryBody(newBody); 156 G.v().ASTTransformations_modified = true; 157 } 159 } 160 } 161 else if(temp instanceof ASTIfElseNode){ 162 List elseBody = ((ASTIfElseNode)temp).getElseBody(); 164 if(elseBody.size()==0){ 165 List newBody=EmptyElseRemover.createNewNodeBody(tryBody,nodeNumber,(ASTIfElseNode)temp); 167 if(newBody!=null){ 168 node.replaceTryBody(newBody); 170 G.v().ASTTransformations_modified = true; 171 return; 173 } 174 } 175 } 176 else if(temp instanceof ASTIfNode){ 177 if(it.hasNext()){ ASTNode nextNode = (ASTNode)tryBody.get(nodeNumber+1); 180 if(nextNode instanceof ASTIfNode){ 181 List newBody=OrAggregatorThree.createNewNodeBody(tryBody,nodeNumber,(ASTIfNode)temp,(ASTIfNode)nextNode); 183 if(newBody!=null){ 184 node.replaceTryBody(newBody); 186 G.v().ASTTransformations_modified = true; 187 return; 190 } 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 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 caseType(type); 219 220 Local local = (Local)paramMap.get(catchBody); 222 decideCaseExprOrRef(local); 223 224 List body = (List)catchBody.o; 226 itBody = body.iterator(); 227 228 nodeNumber=0; 229 while (itBody.hasNext()){ 231 ASTNode temp = (ASTNode) itBody.next(); 232 if(temp instanceof ASTLabeledBlockNode){ 233 ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp; 235 SETNodeLabel label = labelBlock.get_Label(); 236 if(label.toString()==null){ 237 239 List newBody=UselessLabeledBlockRemover.createNewSubBody(body,nodeNumber,labelBlock); 240 if(newBody!=null){ 241 catchBody.replaceBody(newBody); 243 G.v().ASTTransformations_modified = true; 244 } 246 247 } 248 } 249 else if(temp instanceof ASTIfElseNode){ 250 List elseBody = ((ASTIfElseNode)temp).getElseBody(); 252 if(elseBody.size()==0){ 253 List newBody=EmptyElseRemover.createNewNodeBody(body,nodeNumber,(ASTIfElseNode)temp); 255 if(newBody!=null){ 256 catchBody.replaceBody(newBody); 258 G.v().ASTTransformations_modified = true; 259 return; 261 } 262 } 263 } 264 else if(temp instanceof ASTIfNode){ 265 if(itBody.hasNext()){ ASTNode nextNode = (ASTNode)body.get(nodeNumber+1); 268 if(nextNode instanceof ASTIfNode){ 269 List newBody=OrAggregatorThree.createNewNodeBody(body,nodeNumber,(ASTIfNode)temp,(ASTIfNode)nextNode); 271 if(newBody!=null){ 272 catchBody.replaceBody(newBody); 274 G.v().ASTTransformations_modified = true; 275 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 293 List indexList = node.getIndexList(); 294 Map index2BodyList = node.getIndex2BodyList(); 295 296 Iterator it = indexList.iterator(); 297 while (it.hasNext()) { Object currentIndex = it.next(); 299 List body = (List) index2BodyList.get( currentIndex); 300 301 if (body != null){ 302 304 Iterator itBody = body.iterator(); 305 int nodeNumber=0; 306 while (itBody.hasNext()){ 308 ASTNode temp = (ASTNode) itBody.next(); 309 if(temp instanceof ASTLabeledBlockNode){ 310 ASTLabeledBlockNode labelBlock = (ASTLabeledBlockNode)temp; 312 SETNodeLabel label = labelBlock.get_Label(); 313 if(label.toString()==null){ 314 316 List newBody=UselessLabeledBlockRemover.createNewSubBody(body,nodeNumber,labelBlock); 317 if(newBody!=null){ 318 320 index2BodyList.put(currentIndex,newBody); 322 node.replaceIndex2BodyList(index2BodyList); 324 G.v().ASTTransformations_modified = true; 325 } 327 } 328 } 329 else if(temp instanceof ASTIfElseNode){ 330 List elseBody = ((ASTIfElseNode)temp).getElseBody(); 332 if(elseBody.size()==0){ 333 List newBody=EmptyElseRemover.createNewNodeBody(body,nodeNumber,(ASTIfElseNode)temp); 335 if(newBody!=null){ 336 338 index2BodyList.put(currentIndex,newBody); 340 node.replaceIndex2BodyList(index2BodyList); 342 G.v().ASTTransformations_modified = true; 343 return; 345 } 346 } 347 } 348 else if(temp instanceof ASTIfNode){ 349 if(itBody.hasNext()){ ASTNode nextNode = (ASTNode)body.get(nodeNumber+1); 352 if(nextNode instanceof ASTIfNode){ 353 List newBody=OrAggregatorThree.createNewNodeBody(body,nodeNumber,(ASTIfNode)temp,(ASTIfNode)nextNode); 355 if(newBody!=null){ 356 358 index2BodyList.put(currentIndex,newBody); 360 node.replaceIndex2BodyList(index2BodyList); 362 363 G.v().ASTTransformations_modified = true; 364 return; 366 } 367 } 368 } 369 } 370 temp.apply(this); 371 nodeNumber++; 372 } 373 } 374 } 375 } 376 } | Popular Tags |