1 20 21 package soot.dava; 22 23 import java.io.PrintWriter ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import soot.Body; 28 import soot.BooleanType; 29 import soot.ByteType; 30 import soot.CharType; 31 import soot.DoubleType; 32 import soot.FloatType; 33 import soot.G; 34 import soot.IntType; 35 import soot.LongType; 36 import soot.Modifier; 37 import soot.PhaseOptions; 38 import soot.RefType; 39 import soot.Scene; 40 import soot.ShortType; 41 import soot.Singletons; 42 import soot.SootClass; 43 import soot.SootField; 44 import soot.SootMethod; 45 import soot.Type; 46 import soot.UnitPrinter; 47 import soot.dava.internal.AST.ASTNode; 48 import soot.options.Options; 49 import soot.tagkit.DoubleConstantValueTag; 50 import soot.tagkit.FloatConstantValueTag; 51 import soot.tagkit.IntegerConstantValueTag; 52 import soot.tagkit.LongConstantValueTag; 53 import soot.tagkit.StringConstantValueTag; 54 import soot.tagkit.Tag; 55 import soot.util.Chain; 56 import soot.util.IterableSet; 57 58 public class DavaPrinter { 59 public DavaPrinter(Singletons.Global g) { 60 } 61 public static DavaPrinter v() { 62 return G.v().soot_dava_DavaPrinter(); 63 } 64 65 72 169 private void printStatementsInBody(Body body, java.io.PrintWriter out) { 170 Chain units = ((DavaBody) body).getUnits(); 171 172 if (units.size() != 1) { 173 throw new RuntimeException ("DavaBody AST doesn't have single root."); 174 } 175 176 UnitPrinter up = new DavaUnitPrinter(); 177 ((ASTNode) units.getFirst()).toString(up); 178 out.print( up.toString() ); 179 } 180 181 public void printTo(SootClass cl, PrintWriter out) { 182 183 184 IterableSet packagesUsed = new IterableSet(); 185 186 { 187 188 String curPackage = cl.getJavaPackageName(); 189 190 if (curPackage.equals("") == false) { 191 out.println("package " + curPackage + ";"); 192 out.println(); 193 } 194 195 if (cl.hasSuperclass()) { 196 SootClass superClass = cl.getSuperclass(); 197 packagesUsed.add(superClass.getJavaPackageName()); 198 } 199 200 Iterator interfaceIt = cl.getInterfaces().iterator(); 201 while (interfaceIt.hasNext()) { 202 String interfacePackage = 203 ((SootClass) interfaceIt.next()).getJavaPackageName(); 204 if (packagesUsed.contains(interfacePackage) == false) 205 packagesUsed.add(interfacePackage); 206 } 207 208 Iterator methodIt = cl.methodIterator(); 209 while (methodIt.hasNext()) { 210 SootMethod dm = (SootMethod) methodIt.next(); 211 212 if (dm.hasActiveBody()) 213 packagesUsed = 214 packagesUsed.union( 215 ((DavaBody) dm.getActiveBody()).get_PackagesUsed()); 216 217 Iterator eit = dm.getExceptions().iterator(); 218 while (eit.hasNext()) { 219 String thrownPackage = 220 ((SootClass) eit.next()).getJavaPackageName(); 221 if (packagesUsed.contains(thrownPackage) == false) 222 packagesUsed.add(thrownPackage); 223 } 224 225 Iterator pit = dm.getParameterTypes().iterator(); 226 while (pit.hasNext()) { 227 Type t = (Type) pit.next(); 228 229 if (t instanceof RefType) { 230 String paramPackage = 231 ((RefType) t).getSootClass().getJavaPackageName(); 232 if (packagesUsed.contains(paramPackage) == false) 233 packagesUsed.add(paramPackage); 234 } 235 } 236 237 Type t = dm.getReturnType(); 238 if (t instanceof RefType) { 239 String returnPackage = 240 ((RefType) t).getSootClass().getJavaPackageName(); 241 if (packagesUsed.contains(returnPackage) == false) 242 packagesUsed.add(returnPackage); 243 } 244 } 245 246 Iterator fieldIt = cl.getFields().iterator(); 247 while (fieldIt.hasNext()) { 248 SootField f = (SootField) fieldIt.next(); 249 250 if (f.isPhantom()) 251 continue; 252 253 Type t = f.getType(); 254 255 if (t instanceof RefType) { 256 String fieldPackage = 257 ((RefType) t).getSootClass().getJavaPackageName(); 258 if (packagesUsed.contains(fieldPackage) == false) 259 packagesUsed.add(fieldPackage); 260 } 261 } 262 263 if (packagesUsed.contains(curPackage)) 264 packagesUsed.remove(curPackage); 265 266 if (packagesUsed.contains("java.lang")) 267 packagesUsed.remove("java.lang"); 268 269 Iterator pit = packagesUsed.iterator(); 270 while (pit.hasNext()) 271 out.println("import " + (String ) pit.next() + ".*;"); 272 273 if (packagesUsed.isEmpty() == false) 274 out.println(); 275 276 packagesUsed.add("java.lang"); 277 packagesUsed.add(curPackage); 278 279 Dava.v().set_CurrentPackageContext(packagesUsed); 280 Dava.v().set_CurrentPackage(curPackage); 281 } 282 283 { 285 String classPrefix = ""; 286 287 classPrefix = 288 classPrefix + " " + Modifier.toString(cl.getModifiers()); 289 classPrefix = classPrefix.trim(); 290 291 if (!cl.isInterface()) { 292 classPrefix = classPrefix + " class"; 293 classPrefix = classPrefix.trim(); 294 } 295 296 out.print(classPrefix + " " + cl.getShortJavaStyleName()); 297 } 298 299 if (cl.hasSuperclass() 301 && !(cl.getSuperclass().getName().equals("java.lang.Object"))){ 302 303 String superClassName = cl.getSuperclass().getName(); 304 305 310 Map options = PhaseOptions.v().getPhaseOptions("db.renamer"); 311 boolean force = PhaseOptions.getBoolean(options, "remove-fully-qualified"); 312 314 315 if (force) { 316 superClassName = getShortName(superClassName,packagesUsed); 317 } 318 out.print(" extends " + superClassName + ""); 319 } 320 321 322 { 324 Iterator interfaceIt = cl.getInterfaces().iterator(); 325 326 if (interfaceIt.hasNext()) { 327 if( cl.isInterface() ) out.print(" extends "); 328 else out.print(" implements "); 329 330 out.print("" + ((SootClass) interfaceIt.next()).getName() + ""); 331 332 while (interfaceIt.hasNext()) 333 out.print( 334 ", " + ((SootClass) interfaceIt.next()).getName() + ""); 335 } 336 } 337 338 out.println(); 339 out.println("{"); 340 341 { 343 Iterator fieldIt = cl.getFields().iterator(); 344 if (fieldIt.hasNext()) { 345 while (fieldIt.hasNext()) { 346 SootField f = (SootField) fieldIt.next(); 347 348 if (f.isPhantom()) 349 continue; 350 351 352 String declaration = null; 353 354 Type fieldType = f.getType(); 355 356 357 String qualifiers = Modifier.toString(f.getModifiers()) + " "; 358 359 360 Map options = PhaseOptions.v().getPhaseOptions("db.renamer"); 362 boolean force = PhaseOptions.getBoolean(options, "remove-fully-qualified"); 363 365 if (force) { 366 qualifiers += getShortName(fieldType.toString(),packagesUsed); 367 } 368 else 369 qualifiers += fieldType.toString(); 370 371 372 373 qualifiers = qualifiers.trim(); 374 375 if(qualifiers.equals("")) 376 declaration = Scene.v().quotedNameOf(f.getName()); 377 else 378 declaration = qualifiers + " " + Scene.v().quotedNameOf(f.getName()) + ""; 379 380 381 if (f.isFinal() && f.isStatic()) { 382 383 if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) { 384 385 double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue(); 386 out.println(" " + declaration + " = "+ val + ";"); 387 388 } else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) { 389 390 float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue(); 391 out.println(" " + declaration + " = "+ val + "f;"); 392 393 } else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) { 394 395 long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue(); 396 out.println(" " + declaration + " = "+ val + "l;"); 397 398 } else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) { 399 400 int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue(); 401 out.println(" " + declaration + " = '" + ((char) val) + "';"); 402 403 } else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) { 404 405 int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue(); 406 407 if (val == 0) 408 out.println(" " + declaration+ " = false;"); 409 else 410 out.println(" " + declaration+ " = true;"); 411 412 } else if ((fieldType instanceof IntType 413 || fieldType instanceof ByteType || 414 fieldType instanceof ShortType) 415 && f.hasTag("IntegerConstantValueTag")) { 416 417 int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue(); 418 out.println(" " + declaration + " = "+ val + ";"); 419 420 } else if (f.hasTag("StringConstantValueTag")) { 421 422 String val = ((StringConstantValueTag) f.getTag("StringConstantValueTag")).getStringValue(); 423 out.println(" " + declaration + " = \""+ val + "\";"); 424 425 } else { 426 out.println(" " + declaration + ";"); 429 } 430 } else { 432 out.println(" " + declaration + ";"); 433 } 434 } 435 } 436 } 437 438 { 440 Iterator methodIt = cl.methodIterator(); 441 442 if (methodIt.hasNext()) { 443 if (cl.getMethodCount() != 0) 444 out.println(); 445 446 while (methodIt.hasNext()) { 447 SootMethod method = (SootMethod) methodIt.next(); 448 449 if (method.isPhantom()) 450 continue; 451 452 if (!Modifier.isAbstract(method.getModifiers()) 453 && !Modifier.isNative(method.getModifiers())) { 454 if (!method.hasActiveBody()) 455 throw new RuntimeException ( 456 "method " 457 + method.getName() 458 + " has no active body!"); 459 else 460 printTo(method.getActiveBody(), out); 461 462 if (methodIt.hasNext()) 463 out.println(); 464 } else { 465 out.print(" "); 467 out.print(method.getDavaDeclaration()); 468 out.println(";"); 469 470 if (methodIt.hasNext()) 471 out.println(); 472 } 473 } 474 } 475 } 476 477 478 479 480 489 490 if(G.v().SootClassNeedsDavaSuperHandlerClass.contains(cl)){ 491 out.println("\n private static class DavaSuperHandler{"); 492 out.println(" java.util.Vector myVector = new java.util.Vector();"); 493 494 out.println("\n public Object get(int pos){"); 495 out.println(" return myVector.elementAt(pos);"); 496 out.println(" }"); 497 498 out.println("\n public void store(Object obj){"); 499 out.println(" myVector.add(obj);"); 500 out.println(" }"); 501 out.println(" }"); 502 } 503 504 505 out.println("}"); 506 } 507 508 509 510 511 512 513 514 public String getShortName(String name, IterableSet packagesUsed) { 515 String packageName = null; 517 if (name.lastIndexOf('.') > 0) { packageName = name.substring(0, name.lastIndexOf('.')); 519 } 520 521 if (packageName != null && packagesUsed.contains(packageName)) { 522 name = name.substring(name.lastIndexOf('.') + 1); 524 } 525 526 return name; 527 } 528 529 535 private void printTo(Body b, PrintWriter out) { 536 b.validate(); 537 538 String decl = b.getMethod().getDavaDeclaration(); 539 540 { 541 out.println(" " + decl); 542 for( Iterator tIt = b.getMethod().getTags().iterator(); tIt.hasNext(); ) { 543 final Tag t = (Tag) tIt.next(); 544 if (Options.v().print_tags_in_output()){ 545 out.println(t); 546 } 547 } 548 out.println(" {"); 549 550 551 555 } 557 558 printStatementsInBody(b, out); 559 560 out.println(" }"); 561 562 } 563 564 } 565 | Popular Tags |