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 33 34 public class EmptyElseRemover{ 35 36 public static void removeElseBody(ASTNode node,ASTIfElseNode ifElseNode ,int subBodyNumber, int nodeNumber){ 37 if(!(node instanceof ASTIfElseNode)){ 38 List subBodies = node.get_SubBodies(); 40 if(subBodies.size()!=1){ 41 throw new RuntimeException ("Please report this benchmark to the programmer"); 43 } 44 List onlySubBody = (List)subBodies.get(0); 45 46 50 List newBody = createNewNodeBody(onlySubBody,nodeNumber,ifElseNode); 51 if(newBody==null){ 52 return; 54 } 55 if(node instanceof ASTMethodNode){ 56 ((ASTMethodNode)node).replaceBody(newBody); 57 G.v().ASTTransformations_modified = true; 58 } 60 else if(node instanceof ASTSynchronizedBlockNode){ 61 ((ASTSynchronizedBlockNode)node).replaceBody(newBody); 62 G.v().ASTTransformations_modified = true; 63 } 65 else if(node instanceof ASTLabeledBlockNode){ 66 ((ASTLabeledBlockNode)node).replaceBody(newBody); 67 G.v().ASTTransformations_modified = true; 68 } 70 else if(node instanceof ASTUnconditionalLoopNode){ 71 ((ASTUnconditionalLoopNode)node).replaceBody(newBody); 72 G.v().ASTTransformations_modified = true; 73 } 75 else if(node instanceof ASTIfNode){ 76 ((ASTIfNode)node).replaceBody(newBody); 77 G.v().ASTTransformations_modified = true; 78 } 80 else if(node instanceof ASTWhileNode){ 81 ((ASTWhileNode)node).replaceBody(newBody); 82 G.v().ASTTransformations_modified = true; 83 } 85 else if(node instanceof ASTDoWhileNode){ 86 ((ASTDoWhileNode)node).replaceBody(newBody); 87 G.v().ASTTransformations_modified = true; 88 } 90 else { 91 return; 93 } 94 } 95 else{ if(subBodyNumber!=0 && subBodyNumber!=1){ 98 return; 101 } 102 List subBodies = node.get_SubBodies(); 103 if(subBodies.size()!=2){ 104 throw new RuntimeException ("Please report this benchmark to the programmer"); 106 } 107 108 List toModifySubBody = (List)subBodies.get(subBodyNumber); 109 110 114 List newBody = createNewNodeBody(toModifySubBody,nodeNumber,ifElseNode); 115 if(newBody==null){ 116 return; 118 } 119 if(subBodyNumber==0){ 120 G.v().ASTTransformations_modified = true; 123 ((ASTIfElseNode)node).replaceBody(newBody,(List)subBodies.get(1)); 124 } 125 else if(subBodyNumber==1){ 126 G.v().ASTTransformations_modified = true; 129 ((ASTIfElseNode)node).replaceBody((List)subBodies.get(0),newBody); 130 } 131 else{ return; 135 } 136 137 } } 139 140 141 142 143 144 145 public static List createNewNodeBody(List oldSubBody,int nodeNumber,ASTIfElseNode ifElseNode){ 146 List newSubBody = new ArrayList(); 148 149 Iterator it = oldSubBody.iterator(); 151 152 int index=0; 154 while(index!=nodeNumber ){ 155 if(!it.hasNext()){ 156 return null; 157 } 158 newSubBody.add(it.next()); 159 index++; 160 } 161 162 ASTNode toRemove = (ASTNode)it.next(); 165 if(!(toRemove instanceof ASTIfElseNode)){ 166 return null; 168 } 169 else{ 170 ASTIfElseNode toRemoveNode = (ASTIfElseNode)toRemove; 171 172 List elseBody = toRemoveNode.getElseBody(); 174 if(elseBody.size()!=0){ 175 return null; 177 } 178 179 181 ASTIfNode newNode = new ASTIfNode(toRemoveNode.get_Label(),toRemoveNode.get_Condition(),toRemoveNode.getIfBody()); 183 184 newSubBody.add(newNode); 186 } 187 188 while(it.hasNext()){ 190 newSubBody.add(it.next()); 191 } 192 193 return newSubBody; 195 } 196 } | Popular Tags |