1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.*; 23 import java.util.*; 24 import soot.jimple.*; 25 import soot.dava.internal.SET.*; 26 import soot.dava.internal.AST.*; 27 import soot.dava.internal.asg.*; 28 import soot.dava.internal.javaRep.*; 29 import soot.dava.toolkits.base.AST.analysis.*; 30 31 32 54 55 public class OrAggregatorOne extends DepthFirstAdapter{ 56 57 public OrAggregatorOne(){ 58 } 59 public OrAggregatorOne(boolean verbose){ 60 super(verbose); 61 } 62 63 public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){ 64 } 65 66 public void outASTLabeledBlockNode(ASTLabeledBlockNode node){ 67 String outerLabel = node.get_Label().toString(); 68 if(outerLabel==null) 69 return; 70 71 String innerLabel=null; 72 73 ASTLabeledBlockNode secondLabeledBlockNode = isLabelWithinLabel(node); 74 if(secondLabeledBlockNode == null){ 75 return; 77 } 78 79 innerLabel=secondLabeledBlockNode.get_Label().toString(); 81 if(innerLabel==null){ 82 return; 84 } 85 86 List secondLabelsBodies= getSecondLabeledBlockBodies(secondLabeledBlockNode); 87 88 boolean allIfs = checkAllAreIfsWithProperBreaks(secondLabelsBodies.iterator(),outerLabel,innerLabel); 89 if(!allIfs){ 90 return; 92 } 93 94 96 List conditions = getConditions(secondLabelsBodies.iterator()); 99 100 Iterator condIt = conditions.iterator(); 102 ASTCondition newCond=null;; 103 while(condIt.hasNext()){ 104 ASTCondition next = (ASTCondition)condIt.next(); 105 if(newCond==null) 106 newCond=next; 107 else 108 newCond=new ASTOrCondition(newCond,next); 109 } 110 111 112 List newIfBody = new ArrayList(); 114 115 List subBodies = node.get_SubBodies(); 117 119 List labeledBlockBody = (List)subBodies.get(0); 120 Iterator subBodiesIt = labeledBlockBody.iterator(); 123 subBodiesIt.next(); while(subBodiesIt.hasNext()){ 125 ASTNode temp = (ASTNode)subBodiesIt.next(); 126 newIfBody.add(temp); 127 } 128 129 ASTIfNode newNode = new ASTIfNode(new SETNodeLabel(),newCond,newIfBody); 130 131 List newLabeledBlockBody = new ArrayList(); 132 newLabeledBlockBody.add(newNode); 133 134 G.v().ASTTransformations_modified = true; 135 node.replaceBody(newLabeledBlockBody); 137 138 139 142 143 UselessLabelFinder.v().findAndKill(node); 144 } 145 146 147 private ASTLabeledBlockNode isLabelWithinLabel(ASTLabeledBlockNode node){ 148 List subBodies = node.get_SubBodies(); 149 if(subBodies.size()==0){ node.set_Label(new SETNodeLabel()); 153 return null; 154 } 155 156 List bodies = (List)subBodies.get(0); 158 if(bodies.size()==0){ 160 node.set_Label(new SETNodeLabel()); 163 return null; 164 } 165 166 ASTNode firstBody = (ASTNode)bodies.get(0); 168 if(!(firstBody instanceof ASTLabeledBlockNode)){ 169 return null; 174 } 175 176 return (ASTLabeledBlockNode)firstBody; 178 } 179 180 181 private List getSecondLabeledBlockBodies(ASTLabeledBlockNode secondLabeledBlockNode){ 182 List secondLabelsSubBodies = secondLabeledBlockNode.get_SubBodies(); 184 if(secondLabelsSubBodies.size()==0){ 185 secondLabeledBlockNode.set_Label(new SETNodeLabel()); 188 return null; 189 } 190 194 List secondLabelsBodies = (List)secondLabelsSubBodies.get(0); 195 196 return secondLabelsBodies; 198 } 199 200 private boolean checkAllAreIfsWithProperBreaks(Iterator it,String outerLabel, String innerLabel){ 201 while(it.hasNext()){ 203 ASTNode secondLabelsBody = (ASTNode)it.next(); 204 205 Stmt stmt = isIfNodeWithOneStatement(secondLabelsBody); 207 if(stmt == null){ 208 return false; 210 } 211 String labelBroken = breaksLabel(stmt); 213 if(labelBroken == null){ 214 return false; 216 } 217 218 if(labelBroken.compareTo(innerLabel)==0 && it.hasNext()) 220 continue; 221 if(labelBroken.compareTo(outerLabel)==0 && !it.hasNext()) 223 continue; 224 225 return false; 227 } 229 return true; 231 } 232 233 234 235 236 241 private String breaksLabel(Stmt stmt){ 242 if(!(stmt instanceof DAbruptStmt)){ 243 return null; 245 } 246 DAbruptStmt abStmt = (DAbruptStmt)stmt; 247 if(!abStmt.is_Break()){ 248 return null; 250 } 251 SETNodeLabel label = abStmt.getLabel(); 252 return label.toString(); 253 } 254 255 256 257 private Stmt isIfNodeWithOneStatement(ASTNode secondLabelsBody){ 258 if(!(secondLabelsBody instanceof ASTIfNode)){ 259 return null; 261 } 262 264 ASTIfNode ifNode =(ASTIfNode)secondLabelsBody; 265 List ifSubBodies =ifNode.get_SubBodies(); 266 if(ifSubBodies.size()!=1){ 267 return null; 269 } 270 271 List ifBody = (List)ifSubBodies.get(0); 273 274 if(ifBody.size()!=1){ 276 return null; 278 } 279 280 ASTNode ifBodysBody = (ASTNode)ifBody.get(0); 282 if(!(ifBodysBody instanceof ASTStatementSequenceNode)){ 283 return null; 285 } 286 287 List statements = ((ASTStatementSequenceNode)ifBodysBody).getStatements(); 289 if(statements.size()!=1){ 290 return null; 292 } 293 294 AugmentedStmt as = (AugmentedStmt)statements.get(0); 296 Stmt s = as.get_Stmt(); 297 return s; 298 } 299 300 301 302 308 private List getConditions(Iterator it){ 309 List toReturn = new ArrayList(); 310 while(it.hasNext()){ 311 ASTIfNode node = (ASTIfNode)it.next(); 313 314 ASTCondition cond = node.get_Condition(); 315 if(it.hasNext()){ 317 toReturn.add(cond); 319 } 320 else{ 321 cond.flip(); 323 toReturn.add(cond); 324 } 325 } return toReturn; 327 } 328 } | Popular Tags |