1 19 20 package soot; 21 import soot.jimple.*; 22 import soot.util.*; 23 import java.util.*; 24 25 29 public abstract class LabeledUnitPrinter extends AbstractUnitPrinter { 30 31 protected Map labels; 32 33 protected Map references; 34 35 public LabeledUnitPrinter( Body b ) { 36 createLabelMaps(b); 37 } 38 39 public Map labels() { return labels; } 40 public Map references() { return references; } 41 42 public abstract void literal( String s ); 43 public abstract void methodRef( SootMethodRef m ); 44 public abstract void fieldRef( SootFieldRef f ); 45 public abstract void identityRef( IdentityRef r ); 46 public abstract void type( Type t ); 47 48 public void unitRef( Unit u, boolean branchTarget ) { 49 String oldIndent = getIndent(); 50 51 if(branchTarget){ 53 setIndent(labelIndent); 54 handleIndent(); 55 setIndent(oldIndent); 56 String label = (String ) labels.get( u ); 57 if( label == null || label.equals( "<unnamed>" ) ) 58 label = "[?= "+u+"]"; 59 output.append(label); 60 } 61 else{ 63 String ref = (String ) references.get( u ); 64 65 if(startOfLine){ 66 String newIndent = "(" + ref + ")" + 67 indent.substring(ref.length() + 2); 68 setIndent(newIndent); 69 handleIndent(); 70 setIndent(oldIndent); 71 } 72 else 73 output.append(ref); 74 } 75 } 76 77 private void createLabelMaps(Body body) { 78 Chain units = body.getUnits(); 79 80 labels = new HashMap(units.size() * 2 + 1, 0.7f); 81 references = new HashMap(units.size() * 2 + 1, 0.7f); 82 83 { 85 Iterator boxIt = body.getAllUnitBoxes().iterator(); 86 87 Set labelStmts = new HashSet(); 88 Set refStmts = new HashSet(); 89 90 { 92 while (boxIt.hasNext()) { 93 UnitBox box = (UnitBox) boxIt.next(); 94 Unit stmt = (Unit) box.getUnit(); 95 96 if(box.isBranchTarget()) 97 labelStmts.add(stmt); 98 else 99 refStmts.add(stmt); 100 } 101 102 } 103 104 { 106 int labelCount = 0; 107 int refCount = 0; 108 109 Iterator stmtIt = units.iterator(); 110 111 while (stmtIt.hasNext()) { 112 Unit s = (Unit) stmtIt.next(); 113 114 if (labelStmts.contains(s)) 115 labels.put(s, "label" + (labelCount++)); 116 117 if (refStmts.contains(s)) 118 references.put(s, Integer.toString(refCount++)); 119 } 120 } 121 } 122 } 123 124 protected String labelIndent = " "; 125 } 126 127
| Popular Tags
|