1 21 22 25 26 30 31 package soot.dava.internal.AST; 32 33 import soot.*; 34 import java.util.*; 35 import soot.dava.*; 36 import soot.util.*; 37 import soot.jimple.*; 38 import soot.jimple.internal.*; 39 40 import soot.grimp.*; 41 import soot.grimp.internal.*; 42 43 import soot.dava.internal.javaRep.*; 44 import soot.dava.internal.asg.*; 45 import soot.dava.toolkits.base.AST.*; 46 import soot.dava.toolkits.base.AST.analysis.*; 47 48 public class ASTMethodNode extends ASTNode { 49 private List body; 50 51 private DavaBody davaBody; 52 53 private ASTStatementSequenceNode declarations; 54 55 58 private Map typeToLocals; 60 public ASTStatementSequenceNode getDeclarations() { 61 return declarations; 62 } 63 64 public void setDeclarations(ASTStatementSequenceNode decl) { 65 declarations = decl; 66 } 67 68 public void setDavaBody(DavaBody bod) { 69 this.davaBody = bod; 70 } 71 72 public DavaBody getDavaBody() { 73 return davaBody; 74 } 75 76 public void storeLocals(Body OrigBody) { 77 if ((OrigBody instanceof DavaBody) == false) 78 throw new RuntimeException ( 79 "Only DavaBodies should invoke this method"); 80 81 davaBody = (DavaBody) OrigBody; 82 typeToLocals = new DeterministicHashMap( 83 OrigBody.getLocalCount() * 2 + 1, 0.7f); 84 85 HashSet params = new HashSet(); 86 params.addAll(davaBody.get_ParamMap().values()); 87 params.addAll(davaBody.get_CaughtRefs()); 88 HashSet thisLocals = davaBody.get_ThisLocals(); 89 90 Iterator localIt = OrigBody.getLocals().iterator(); 92 while (localIt.hasNext()) { 93 Local local = (Local) localIt.next(); 94 95 if (params.contains(local) || thisLocals.contains(local)) 96 continue; 97 98 List localList; 99 100 String typeName; 101 Type t = local.getType(); 102 103 typeName = t.toString(); 104 105 if (typeToLocals.containsKey(t)) 106 localList = (List) typeToLocals.get(t); 107 else { 108 localList = new ArrayList(); 109 typeToLocals.put(t, localList); 110 } 111 112 localList.add(local); 113 } 114 115 117 List statementSequence = new ArrayList(); 118 119 Iterator typeIt = typeToLocals.keySet().iterator(); 120 121 while (typeIt.hasNext()) { 122 Type typeObject = (Type) typeIt.next(); 123 String type = typeObject.toString(); 124 125 List localList = (List) typeToLocals.get(typeObject); 126 Object [] locals = localList.toArray(); 127 128 DVariableDeclarationStmt varStmt = null; 129 varStmt = new DVariableDeclarationStmt(typeObject,davaBody); 130 131 for (int k = 0; k < locals.length; k++) { 132 varStmt.addLocal((Local) locals[k]); 133 } 134 AugmentedStmt as = new AugmentedStmt(varStmt); 135 statementSequence.add(as); 136 } 137 138 declarations = new ASTStatementSequenceNode(statementSequence); 139 140 body.add(0, declarations); 141 subBodies = new ArrayList(); 142 subBodies.add(body); 143 } 144 145 public ASTMethodNode(List body) { 146 super(); 147 this.body = body; 148 149 subBodies.add(body); 150 } 151 152 157 public List getDeclaredLocals() { 158 List toReturn = new ArrayList(); 159 160 Iterator it = declarations.getStatements().iterator(); 161 162 while (it.hasNext()) { Stmt s = ((AugmentedStmt) it.next()).get_Stmt(); 164 165 if (!(s instanceof DVariableDeclarationStmt)) 166 continue; 168 DVariableDeclarationStmt varStmt = (DVariableDeclarationStmt) s; 169 170 List declarations = varStmt.getDeclarations(); 172 Iterator decIt = declarations.iterator(); 173 while (decIt.hasNext()) { 174 176 toReturn.add(decIt.next()); 177 } } return toReturn; 180 } 181 182 189 public void removeDeclaredLocal(Local local) { 190 Stmt s = null; 191 Iterator it = declarations.getStatements().iterator(); 192 while (it.hasNext()) { s = ((AugmentedStmt) it.next()).get_Stmt(); 194 195 if (!(s instanceof DVariableDeclarationStmt)) 196 continue; 198 DVariableDeclarationStmt varStmt = (DVariableDeclarationStmt) s; 199 200 List declarations = varStmt.getDeclarations(); 202 Iterator decIt = declarations.iterator(); 203 204 boolean foundIt = false; while (decIt.hasNext()) { 206 Local temp = (Local) decIt.next(); 208 if (temp.getName().compareTo(local.getName()) == 0) { 209 foundIt = true; 211 break; 212 } 213 } 214 215 if (foundIt) { 216 varStmt.removeLocal(local); 217 break; } 219 } 220 223 List newSequence = new ArrayList(); 224 it = declarations.getStatements().iterator(); 225 while (it.hasNext()) { 226 AugmentedStmt as = (AugmentedStmt) it.next(); 227 s = as.get_Stmt(); 228 229 if (!(s instanceof DVariableDeclarationStmt)) 230 continue; 231 232 DVariableDeclarationStmt varStmt = (DVariableDeclarationStmt) s; 233 234 if (varStmt.getDeclarations().size() != 0) 235 newSequence.add(as); 236 237 } 238 declarations.setStatements(newSequence); 239 240 } 241 242 246 public void replaceBody(List body) { 247 this.body = body; 248 subBodies = new ArrayList(); 249 subBodies.add(body); 250 } 251 252 public Object clone() { 253 ASTMethodNode toReturn = new ASTMethodNode(body); 254 toReturn.setDeclarations((ASTStatementSequenceNode) declarations 255 .clone()); 256 return toReturn; 257 } 258 259 public void perform_Analysis(ASTAnalysis a) { 260 perform_AnalysisOnSubBodies(a); 261 } 262 263 public void toString(UnitPrinter up) { 264 if (!(up instanceof DavaUnitPrinter)) 265 throw new RuntimeException ( 266 "Only DavaUnitPrinter should be used to print DavaBody"); 267 268 DavaUnitPrinter dup = (DavaUnitPrinter) up; 269 270 273 if (davaBody != null) { 274 InstanceInvokeExpr constructorExpr = davaBody.get_ConstructorExpr(); 275 276 if (constructorExpr != null) { 277 if (davaBody.getMethod().getDeclaringClass().getName().equals( 278 constructorExpr.getMethodRef().declaringClass() 279 .toString())) 280 dup.printString(" this("); 281 else 282 dup.printString(" super("); 283 284 Iterator ait = constructorExpr.getArgs().iterator(); 285 while (ait.hasNext()) { 286 293 Object arg = ait.next(); 294 if (arg instanceof Value) { 295 dup.noIndent(); 298 ((Value) arg).toString(dup); 299 } else { 300 303 dup.printString(arg.toString()); 304 } 305 306 if (ait.hasNext()) 307 dup.printString(", "); 308 } 309 310 dup.printString(");\n"); 311 } 312 313 up.newline(); 315 } 317 320 body_toString(up, body); 322 } 323 324 public String toString() { 325 StringBuffer b = new StringBuffer (); 326 329 if (davaBody != null) { 330 InstanceInvokeExpr constructorExpr = davaBody.get_ConstructorExpr(); 331 if (constructorExpr != null) { 332 333 if (davaBody.getMethod().getDeclaringClass().getName().equals( 334 constructorExpr.getMethodRef().declaringClass() 335 .toString())) 336 b.append(" this("); 337 else 338 b.append(" super("); 339 340 Iterator ait = constructorExpr.getArgs().iterator(); 341 while (ait.hasNext()) { 342 b.append(ait.toString()); 343 344 if (ait.hasNext()) 345 b.append(", "); 346 } 347 348 b.append(");\n\n"); 349 } 350 } 351 352 b.append(body_toString(body)); 354 return b.toString(); 355 } 356 357 362 public void apply(Analysis a) { 363 a.caseASTMethodNode(this); 364 } 365 } 366 | Popular Tags |