| 1 2 20 21 24 25 31 32 33 34 35 54 55 56 57 package soot.dava.toolkits.base.AST.traversals; 58 59 import soot.*; 60 import java.util.*; 61 import soot.util.*; 62 import soot.jimple.*; 63 import soot.dava.*; 64 import soot.dava.internal.asg.*; 65 import soot.dava.internal.AST.*; 66 import soot.dava.toolkits.base.AST.analysis.*; 68 70 71 72 73 public class AllVariableUses extends DepthFirstAdapter{ 74 ASTMethodNode methodNode; 75 76 79 HashMap localsToUses; 80 HashMap fieldsToUses; 81 82 public AllVariableUses(ASTMethodNode node){ 83 super(); 84 this.methodNode=node; 85 init(); 86 } 87 88 public AllVariableUses(boolean verbose, ASTMethodNode node){ 89 super(verbose); 90 this.methodNode=node; 91 init(); 92 } 93 94 95 96 public void init(){ 97 98 localsToUses = new HashMap(); 99 fieldsToUses = new HashMap(); 100 101 124 125 126 } 127 128 129 130 133 public void inASTSynchronizedBlockNode(ASTSynchronizedBlockNode node){ 134 Local local = node.getLocal(); 135 136 addLocalUse(local,node); 137 } 138 139 140 141 142 148 public void inASTSwitchNode(ASTSwitchNode node){ 149 Value val = (Value)node.get_Key(); 150 List localUses = new ArrayList(); 151 List fieldUses = new ArrayList(); 152 153 if(val instanceof Local){ 154 localUses.add((Local)val); 155 System.out.println("Added "+val+" to local uses for switch"); 156 } 157 else if(val instanceof FieldRef){ 158 fieldUses.add((FieldRef)val); 159 System.out.println("Added "+val+" to field uses for switch"); 160 } 161 else{ 162 List useBoxes = val.getUseBoxes(); 163 164 List localsOrFieldRefs= getUsesFromBoxes(useBoxes); 165 Iterator it = localsOrFieldRefs.iterator(); 166 167 while(it.hasNext()){ 168 Value temp = (Value)it.next(); 169 if(temp instanceof Local){ 170 localUses.add((Local)temp); 171 System.out.println("Added "+temp+" to local uses for switch"); 172 } 173 else if(temp instanceof FieldRef){ 174 fieldUses.add((FieldRef)temp); 175 System.out.println("Added "+temp+" to field uses for switch"); 176 } 177 } 178 } 179 180 Iterator it = localUses.iterator(); 182 while(it.hasNext()){ 183 Local local = (Local)it.next(); 184 185 addLocalUse(local,node); 186 } 188 189 190 it = fieldUses.iterator(); 192 while(it.hasNext()){ 193 FieldRef field = (FieldRef)it.next(); 194 SootField sootField = field.getField(); 195 196 addFieldUse(sootField,node); 197 } } 199 200 201 202 203 public void inASTStatementSequenceNode(ASTStatementSequenceNode node){ 204 List statements = node.getStatements(); 205 Iterator it = statements.iterator(); 206 207 while(it.hasNext()){ 208 AugmentedStmt as = (AugmentedStmt)it.next(); 209 Stmt s = as.get_Stmt(); 210 checkStatementUses(s,s); 214 } 215 } 216 217 218 219 220 221 222 228 public void inASTForLoopNode(ASTForLoopNode node){ 229 230 List init = node.getInit(); 232 Iterator it = init.iterator(); 233 while(it.hasNext()){ 234 AugmentedStmt as = (AugmentedStmt)it.next(); 235 Stmt s = as.get_Stmt(); 236 checkStatementUses(s,node); 237 } 238 239 ASTCondition cond = node.get_Condition(); 241 checkConditionalUses(cond,node); 242 243 244 List update = node.getUpdate(); 246 it = update.iterator(); 247 while(it.hasNext()){ 248 AugmentedStmt as = (AugmentedStmt)it.next(); 249 Stmt s = as.get_Stmt(); 250 checkStatementUses(s,node); 251 } 252 } 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 public void checkStatementUses(Stmt s,Object useNodeOrStatement){ 268 List useBoxes = s.getUseBoxes(); 269 270 List uses= getUsesFromBoxes(useBoxes); 272 273 274 Iterator it = uses.iterator(); 275 while(it.hasNext()){ 276 Value temp = (Value)it.next(); 277 if(temp instanceof Local){ 278 addLocalUse((Local)temp,useNodeOrStatement); 279 } 280 else if(temp instanceof FieldRef){ 281 FieldRef field = (FieldRef)temp; 282 SootField sootField = field.getField(); 283 addFieldUse(sootField,useNodeOrStatement); 284 } 285 } 286 } 287 288 289 290 291 292 296 public void checkConditionalUses(ASTCondition cond,ASTNode node){ 297 List useList = getUseList(cond); 298 299 301 Iterator it = useList.iterator(); 303 while(it.hasNext()){ 304 Value temp = (Value)it.next(); 305 if(temp instanceof Local){ 306 addLocalUse((Local)temp,node); 307 } 308 else if(temp instanceof FieldRef){ 309 FieldRef field = (FieldRef)temp; 310 SootField sootField = field.getField(); 311 addFieldUse(sootField,node); 312 } 313 } } 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 336 public void inASTIfNode(ASTIfNode node){ 337 ASTCondition cond = node.get_Condition(); 338 checkConditionalUses(cond,node); 339 } 340 341 342 346 public void inASTIfElseNode(ASTIfElseNode node){ 347 ASTCondition cond = node.get_Condition(); 348 checkConditionalUses(cond,node); 349 } 350 351 352 353 354 358 public void inASTWhileNode(ASTWhileNode node){ 359 ASTCondition cond = node.get_Condition(); 360 checkConditionalUses(cond,node); 361 } 362 363 364 365 369 public void inASTDoWhileNode(ASTDoWhileNode node){ 370 ASTCondition cond = node.get_Condition(); 371 checkConditionalUses(cond,node); 372 } 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 407 public List getUseList(ASTCondition cond){ 408 ArrayList useList = new ArrayList(); 409 if(cond instanceof ASTAggregatedCondition){ 410 useList.addAll(getUseList(((ASTAggregatedCondition)cond).getLeftOp())); 411 useList.addAll(getUseList(((ASTAggregatedCondition)cond).getRightOp())); 412 return useList; 413 } 414 else if(cond instanceof ASTUnaryCondition){ 415 List uses = new ArrayList(); 417 418 Value val = ((ASTUnaryCondition)cond).getValue(); 419 if(val instanceof Local || val instanceof FieldRef){ 420 uses.add(val); 421 } 422 else{ 423 List useBoxes = val.getUseBoxes(); 424 uses= getUsesFromBoxes(useBoxes); 425 } 426 return uses; 427 } 428 else if(cond instanceof ASTBinaryCondition){ 429 Value val = ((ASTBinaryCondition)cond).getConditionExpr(); 431 List useBoxes = val.getUseBoxes(); 432 return getUsesFromBoxes(useBoxes); 433 } 434 else{ 435 throw new RuntimeException ("Method getUseList in ASTUsesAndDefs encountered unknown condition type"); 436 } 437 } 438 439 440 441 442 443 444 445 446 447 448 449 450 private void addLocalUse(Local local, Object obj){ 451 452 Object temp = localsToUses.get(local); 453 List uses; 454 if(temp == null) 455 uses = new ArrayList(); 456 else 457 uses = (ArrayList)temp; 458 459 uses.add(obj); 461 462 localsToUses.put(local,uses); 464 } 465 466 467 468 469 470 471 private void addFieldUse(SootField field, Object obj){ 472 473 Object temp = fieldsToUses.get(field); 474 List uses; 475 if(temp == null) 476 uses = new ArrayList(); 477 else 478 uses = (ArrayList)temp; 479 480 uses.add(obj); 482 483 fieldsToUses.put(field,uses); 485 } 486 487 488 489 490 491 492 496 private List getUsesFromBoxes(List useBoxes){ 497 ArrayList toReturn = new ArrayList(); 498 Iterator it = useBoxes.iterator(); 499 while(it.hasNext()){ 500 Value val =((ValueBox)it.next()).getValue(); 501 if(val instanceof Local || val instanceof FieldRef) 502 toReturn.add(val); 503 } 504 return toReturn; 506 } 507 508 509 510 511 512 public List getUsesForField(SootField field){ 513 Object temp = fieldsToUses.get(field); 514 if(temp == null) 515 return null; 516 else 517 return (List)temp; 518 } 519 520 521 522 523 524 public List getUsesForLocal(Local local){ 525 Object temp = localsToUses.get(local); 526 if(temp == null) 527 return null; 528 else 529 return (List)temp; 530 } 531 532 } | Popular Tags |