1 19 20 package soot; 21 import soot.jimple.*; 22 import java.util.*; 23 import soot.tagkit.*; 24 25 28 public class AttributesUnitPrinter { 29 30 private int startOffset; 31 private Stack startOffsets; 32 private int endOffset; 33 private int startStmtOffset; 34 private int startLn; 35 private int currentLn; 36 private int lastNewline; 37 private UnitPrinter printer; 38 39 public AttributesUnitPrinter( int currentLnNum ) { 40 this.currentLn = currentLnNum; 41 } 42 public void startUnit( Unit u ) { 43 startLn = currentLn; 44 startStmtOffset = output().length() - lastNewline; 45 } 46 public void endUnit( Unit u ) { 47 int endStmtOffset = output().length() - lastNewline; 48 if (hasTag(u)){ 50 u.addTag( new JimpleLineNumberTag( startLn, currentLn )); 52 } 53 if (hasColorTag(u)) { 54 u.addTag( new PositionTag(startStmtOffset, endStmtOffset) ); 55 } 56 } 57 public void startValueBox( ValueBox u ) { 58 if (startOffsets == null) { 59 startOffsets = new Stack(); 60 } 61 startOffsets.push(new Integer (output().length() - lastNewline)); 62 } 63 public void endValueBox( ValueBox u ) { 64 endOffset = output().length() - lastNewline; 65 if (hasColorTag(u)) { 66 u.addTag(new PositionTag(((Integer )startOffsets.pop()).intValue(), endOffset)); 67 } 68 } 69 70 private boolean hasTag(Host h) { 71 if (h instanceof Unit) { 72 Iterator usesAndDefsIt = ((Unit)h).getUseAndDefBoxes().iterator(); 73 while (usesAndDefsIt.hasNext()){ 74 if (hasTag((ValueBox)usesAndDefsIt.next())) return true; 75 } 76 } 77 if (h.getTags().isEmpty()) return false; 78 return true; 79 } 80 81 private boolean hasColorTag(Host h) { 82 Iterator it = h.getTags().iterator(); 83 while (it.hasNext()){ 84 if (it.next() instanceof ColorTag) return true; 85 } 86 return false; 87 } 88 89 public void setEndLn(int ln){ 90 currentLn = ln; 91 } 92 public int getEndLn() { 93 return currentLn; 94 } 95 public void newline() { 96 currentLn++; 97 lastNewline = output().length(); 98 } 99 private StringBuffer output() { 100 return printer.output(); 101 } 102 public void setUnitPrinter( UnitPrinter up ) { 103 printer = up; 104 } 105 } 106 107 108 | Popular Tags |