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 60 public class IfElseBreaker{ 61 ASTIfNode newIfNode; 62 List remainingBody; 63 64 public IfElseBreaker(){ 65 newIfNode=null; 66 remainingBody=null; 67 } 68 69 public boolean isIfElseBreakingPossiblePatternOne(ASTIfElseNode node){ 70 List ifBody = node.getIfBody(); 71 if(ifBody.size()!=1){ 72 return false; 74 } 75 76 ASTNode onlyNode=(ASTNode)ifBody.get(0); 77 boolean check = checkStmt(onlyNode,node); 78 if(!check){ 79 return false; 80 } 81 82 newIfNode = new ASTIfNode(((ASTLabeledNode)node).get_Label(),node.get_Condition(),ifBody); 85 remainingBody = node.getElseBody(); 86 87 return true; 88 } 89 90 91 public boolean isIfElseBreakingPossiblePatternTwo(ASTIfElseNode node){ 92 List elseBody = node.getElseBody(); 93 if(elseBody.size()!=1){ 94 return false; 96 } 97 98 ASTNode onlyNode=(ASTNode)elseBody.get(0); 99 boolean check = checkStmt(onlyNode,node); 100 if(!check){ 101 return false; 102 } 103 105 ASTCondition cond = node.get_Condition(); 106 cond.flip(); 108 109 newIfNode = new ASTIfNode(((ASTLabeledNode)node).get_Label(),cond,elseBody); 110 remainingBody = node.getIfBody(); 111 112 return true; 113 } 114 115 116 117 118 private boolean checkStmt(ASTNode onlyNode,ASTIfElseNode node){ 119 if(!(onlyNode instanceof ASTStatementSequenceNode)){ 120 return false; 122 } 123 124 ASTStatementSequenceNode stmtNode=(ASTStatementSequenceNode)onlyNode; 125 List statements = stmtNode.getStatements(); 126 if(statements.size()!=1){ 127 return false; 129 } 130 131 AugmentedStmt as = (AugmentedStmt)statements.get(0); 132 Stmt stmt = as.get_Stmt(); 133 134 if(!(stmt instanceof DAbruptStmt)){ 135 return false; 137 } 138 DAbruptStmt abStmt = (DAbruptStmt)stmt; 139 if(!(abStmt.is_Break() || abStmt.is_Continue())){ 140 return false; 142 } 143 144 SETNodeLabel ifLabel = ((ASTLabeledNode)node).get_Label(); 147 148 if (ifLabel!=null){ 149 if(ifLabel.toString()!=null){ 150 if(abStmt.is_Break()){ 151 String breakLabel = abStmt.getLabel().toString(); 152 if(breakLabel!=null){ 153 if(breakLabel.compareTo(ifLabel.toString())==0){ 154 return false; 156 } 157 } 158 } 159 } 160 } 161 return true; 162 } 163 164 165 166 167 168 176 public List createNewBody(List oldSubBody, int nodeNumber){ 177 if(newIfNode == null) 178 return null; 179 180 List newSubBody = new ArrayList(); 181 182 if(oldSubBody.size()<= nodeNumber){ 183 return null; 185 } 186 187 Iterator oldIt = oldSubBody.iterator(); 188 int index=0; 189 while(index!=nodeNumber){ 190 newSubBody.add(oldIt.next()); 191 index++; 192 } 193 194 ASTNode temp = (ASTNode)oldIt.next(); 196 if(!(temp instanceof ASTIfElseNode)) 197 return null; 198 199 newSubBody.add(newIfNode); 200 201 newSubBody.addAll(remainingBody); 202 203 204 while(oldIt.hasNext()){ 206 newSubBody.add(oldIt.next()); 207 } 208 209 return newSubBody; 210 } 211 } 212 | Popular Tags |