1 19 20 package soot.dava.toolkits.base.renamer; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import soot.Local; 28 import soot.RefLikeType; 29 import soot.SootClass; 30 import soot.SootField; 31 import soot.Type; 32 import soot.dava.internal.AST.ASTMethodNode; 33 import soot.util.Chain; 34 35 36 public class Renamer { 37 public final boolean DEBUG = false; 38 heuristicSet heuristics; 39 40 List locals; 42 Chain fields; 44 ASTMethodNode methodNode; 45 List forLoopNames; 46 47 HashMap changedOrNot; 49 public Renamer(heuristicSet info, ASTMethodNode node) { 50 heuristics = info; 51 locals = null; 52 methodNode = node; 53 54 changedOrNot = new HashMap (); 55 Iterator localIt = info.getLocalsIterator(); 56 while(localIt.hasNext()) 57 changedOrNot.put(localIt.next(),new Boolean (false)); 58 59 60 forLoopNames = new ArrayList (); 61 forLoopNames.add("i"); 62 forLoopNames.add("j"); 63 forLoopNames.add("k"); 64 forLoopNames.add("l"); 65 } 66 67 73 public void rename() { 74 debug("rename","Renaming started"); 75 76 mainMethodArgument(); 78 79 forLoopIndexing(); 81 82 exceptionNaming(); 84 85 assignedFromAField(); 87 88 newClassName(); 90 91 castedObject(); 93 94 objectsGetClassName(); 96 97 removeDollarSigns(); 99 } 100 101 105 private void objectsGetClassName(){ 106 Iterator it = heuristics.getLocalsIterator(); 107 while (it.hasNext()) { 108 Local tempLocal = (Local) it.next(); 109 if(alreadyChanged(tempLocal)){ 110 continue; 111 } 112 113 debug("objectsGetClassName","checking "+tempLocal); 114 115 Type type = tempLocal.getType(); 116 if(type instanceof RefLikeType){ 117 debug("objectsGetClassName","Local:"+tempLocal+" Type: "+type.toString()); 118 119 String tempClassName = type.toString(); 120 if(tempClassName.indexOf('.')!= -1){ 121 tempClassName=tempClassName.substring(tempClassName.lastIndexOf('.')+1); 123 } 124 125 126 String newName = tempClassName.toLowerCase(); 127 int count=0; 128 newName += count; 129 count++; 130 131 while(!isUniqueName(newName)){ 132 newName = newName.substring(0,newName.length()-1)+count; 133 count++; 134 } 135 setName(tempLocal,newName); 136 137 } 138 } 139 140 } 141 142 143 144 148 private void castedObject(){ 149 debug("castedObject",""); 150 151 Iterator it = heuristics.getLocalsIterator(); 152 while (it.hasNext()) { 153 Local tempLocal = (Local) it.next(); 154 if(!alreadyChanged(tempLocal)){ 155 debug("castedObject","checking "+tempLocal); 156 List classes = heuristics.getCastStrings(tempLocal); 157 158 Iterator itClass = classes.iterator(); 159 String classNameToUse = null; 160 while(itClass.hasNext()){ 161 String tempClassName = (String )itClass.next(); 162 if(tempClassName.indexOf('.')!= -1){ 163 tempClassName=tempClassName.substring(tempClassName.lastIndexOf('.')+1); 165 } 166 if(classNameToUse == null) 167 classNameToUse = tempClassName; 168 else if(!classNameToUse.equals(tempClassName)){ 169 classNameToUse=null; 172 break; 173 } 174 } if(classNameToUse!=null){ 176 debug("castedObject","found a classNametoUse through cast expr"); 177 181 String newName = classNameToUse.toLowerCase(); 182 int count=0; 183 newName += count; 184 count++; 185 186 while(!isUniqueName(newName)){ 187 newName = newName.substring(0,newName.length()-1)+count; 188 count++; 189 } 190 setName(tempLocal,newName); 191 } 192 } } } 195 196 200 private void newClassName(){ 201 202 debug("newClassName",""); 203 Iterator it = heuristics.getLocalsIterator(); 206 while (it.hasNext()) { 207 Local tempLocal = (Local) it.next(); 208 if(!alreadyChanged(tempLocal)){ 209 debug("newClassName","checking "+tempLocal); 210 List classes = heuristics.getObjectClassName(tempLocal); 211 Iterator itClass = classes.iterator(); 212 String classNameToUse = null; 213 while(itClass.hasNext()){ 214 String tempClassName = (String )itClass.next(); 215 if(tempClassName.indexOf('.')!= -1){ 216 tempClassName=tempClassName.substring(tempClassName.lastIndexOf('.')+1); 218 } 219 if(classNameToUse == null) 220 classNameToUse = tempClassName; 221 else if(!classNameToUse.equals(tempClassName)){ 222 classNameToUse=null; 225 break; 226 } 227 } if(classNameToUse!=null){ 229 debug("newClassName","found a classNametoUse"); 230 234 String newName = classNameToUse.toLowerCase(); 235 int count=0; 236 newName += count; 237 count++; 238 239 while(!isUniqueName(newName)){ 240 newName = newName.substring(0,newName.length()-1)+count; 241 count++; 242 } 243 setName(tempLocal,newName); 244 } 245 } } 248 } 249 250 258 private void assignedFromAField(){ 259 Iterator it = heuristics.getLocalsIterator(); 260 while (it.hasNext()) { 261 Local tempLocal = (Local) it.next(); 262 if(!alreadyChanged(tempLocal)){ 263 debug("assignedFromField","checking "+tempLocal); 264 List fieldNames = heuristics.getFieldName(tempLocal); 265 if(fieldNames.size()>1){ 266 continue; 268 } 269 else if(fieldNames.size()==1){ 270 String fieldName = (String )fieldNames.get(0); 272 273 int count=0; 276 while(!isUniqueName(fieldName)){ 277 if(count==0) 278 fieldName = fieldName+count; 279 else 280 fieldName = fieldName.substring(0,fieldName.length()-1)+count; 281 count++; 282 } 283 284 setName(tempLocal,fieldName); 285 } } } } 289 290 291 292 293 294 295 296 299 private void removeDollarSigns(){ 300 Iterator it = heuristics.getLocalsIterator(); 301 while (it.hasNext()) { 302 Local tempLocal = (Local) it.next(); 303 String currentName = tempLocal.getName(); 304 int dollarIndex = currentName.indexOf('$'); 305 if(dollarIndex == 0){ 306 String newName = currentName.substring(1,currentName.length()); 308 309 310 if(isUniqueName(newName)){ 311 setName(tempLocal,newName); 312 } 315 } 316 } 317 } 318 319 320 321 324 private void exceptionNaming(){ 325 Iterator it = heuristics.getLocalsIterator(); 326 while (it.hasNext()) { 327 Local tempLocal = (Local) it.next(); 328 Type localType = tempLocal.getType(); 329 String typeString = localType.toString(); 330 if(typeString.indexOf("Exception")>=0){ 331 debug("exceptionNaming","Type is an exception"+ tempLocal); 333 334 String newName = ""; 336 for(int i=0;i<typeString.length();i++){ 337 char character = typeString.charAt(i); 338 if(Character.isUpperCase(character)){ 339 newName += Character.toLowerCase(character); 340 } 341 } 342 int count =0; 343 if(!isUniqueName(newName)){ 344 count++; 345 while(!isUniqueName(newName+count)){ 346 count++; 347 } 348 } 349 if(count !=0) 350 newName = newName + count; 351 352 setName(tempLocal,newName); 353 } 354 } 355 } 356 357 358 359 360 361 362 366 private void forLoopIndexing(){ 367 Iterator it = heuristics.getLocalsIterator(); 368 while (it.hasNext()) { 369 Local tempLocal = (Local) it.next(); 370 debug("foeLoopIndexing","Checking local"+tempLocal.getName()); 371 if (heuristics.getHeuristic(tempLocal, 372 infoGatheringAnalysis.FORLOOPUPDATE)) { 373 int count = -1; 376 377 String newName; 378 379 do{ 380 count++; 381 if(count>=forLoopNames.size()){ 382 newName=null; 383 break; 384 } 385 newName = (String )forLoopNames.get(count); 386 }while (!isUniqueName(newName)); 387 388 if(newName!=null){ 389 setName(tempLocal,newName); 390 } 391 } 392 } 393 } 394 395 396 397 400 private void mainMethodArgument() { 401 Iterator it = heuristics.getLocalsIterator(); 402 while (it.hasNext()) { 403 Local tempLocal = (Local) it.next(); 404 if (heuristics.getHeuristic(tempLocal, 405 infoGatheringAnalysis.MAINARG)) { 406 407 String newName = "args"; 410 int count = 0; 411 while (!isUniqueName(newName)) { 412 if(count==0) 413 newName = newName+count; 414 else 415 newName = newName.substring(0,newName.length()-1)+count; 416 417 count++; 418 } 419 setName(tempLocal,newName); 420 return; 422 } 423 } 424 425 } 426 427 428 429 430 431 436 private void setName(Local var, String newName){ 437 438 Object truthValue = changedOrNot.get(var); 439 440 if(truthValue == null) 442 changedOrNot.put(var,new Boolean (false)); 443 else{ 444 if(((Boolean )truthValue).booleanValue()){ 445 debug("setName","Var: "+var + " had already been renamed"); 447 return; 448 } 449 } 450 452 debug("setName","Changed "+var.getName()+" to "+newName); 453 var.setName(newName); 454 changedOrNot.put(var,new Boolean (true)); 455 } 456 457 458 459 460 461 462 463 464 465 466 467 472 private boolean alreadyChanged(Local var){ 473 Object truthValue = changedOrNot.get(var); 474 475 if(truthValue == null){ 477 changedOrNot.put(var,new Boolean (false)); 478 return false; 479 } 480 else{ 481 if(((Boolean )truthValue).booleanValue()){ 482 debug("alreadyChanged","Var: "+var + " had already been renamed"); 484 return true; 485 } 486 else 487 return false; 488 } 489 } 490 491 492 493 496 private boolean isUniqueName(String name) { 497 Iterator it = getScopedLocals(); 498 while (it.hasNext()) { 500 Local tempLocal = (Local) it.next(); 501 if (tempLocal.getName().equals(name)){ 502 debug("isUniqueName","New Name "+ name+ " is not unique (matches some local)..changing"); 503 return false; 504 } 505 else 506 debug("isUniqueName","New Name "+ name+ " is different from local "+ tempLocal.getName()); 507 } 508 509 it = getScopedFields(); 510 while (it.hasNext()) { 512 SootField tempField = (SootField) it.next(); 513 if (tempField.getName().equals(name)){ 514 debug("isUniqueName","New Name "+ name+ " is not unique (matches field)..changing"); 515 return false; 516 } 517 else 518 debug("isUniqurName","New Name "+ name+ " is different from field "+ tempField.getName()); 519 } 520 return true; 521 } 522 523 528 private Iterator getScopedFields() { 529 SootClass sootClass = methodNode.getDavaBody().getMethod() 531 .getDeclaringClass(); 532 fields = sootClass.getFields(); 533 534 return fields.iterator(); 535 } 536 537 542 private Iterator getScopedLocals() { 543 Iterator it = heuristics.getLocalsIterator(); 544 545 locals = new ArrayList (); 546 while(it.hasNext()) 547 locals.add((Local) it.next()); 548 549 return locals.iterator(); 550 551 } 552 553 public void debug(String methodName, String debug){ 554 555 if(DEBUG) 556 System.out.println(methodName+ " DEBUG: "+debug); 557 } 558 559 } | Popular Tags |