1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.io.FileOutputStream ; 23 import java.io.PrintStream ; 24 import java.util.Iterator ; 25 26 import org.apache.bcel.classfile.ClassParser; 27 import org.apache.bcel.classfile.JavaClass; 28 import org.apache.bcel.classfile.Method; 29 import org.apache.bcel.generic.CodeExceptionGen; 30 import org.apache.bcel.generic.InstructionHandle; 31 import org.apache.bcel.generic.MethodGen; 32 33 import edu.umd.cs.findbugs.SystemProperties; 34 35 42 public class CFGPrinter { 43 private CFG cfg; 44 private boolean isForwards; 45 46 public CFGPrinter(CFG cfg) { 47 this.cfg = cfg; 48 this.isForwards = true; 49 } 50 51 public void setIsForwards(boolean isForwards) { 52 this.isForwards = isForwards; 53 } 54 55 58 public boolean isForwards() { 59 return isForwards; 60 } 61 62 public void print(PrintStream out) { 63 Iterator <BasicBlock> i = cfg.blockIterator(); 64 while (i.hasNext()) { 65 BasicBlock bb = i.next(); 66 out.println(); 67 out.println("BASIC BLOCK: " + bb.getId() + (bb.isExceptionThrower() ? " [EXCEPTION THROWER]" : "") + blockStartAnnotate(bb)); 68 if (bb.isExceptionThrower()) { 69 out.println(" Exception thrower: " + bb.getExceptionThrower()); 70 } 71 CodeExceptionGen exceptionGen = bb.getExceptionGen(); 72 if (exceptionGen != null) { 73 out.println(" CATCHES " + exceptionGen.getCatchType()); 74 } 75 Iterator <InstructionHandle> j = instructionIterator(bb); 76 while (j.hasNext()) { 77 InstructionHandle handle = j.next(); 78 out.println(handle + instructionAnnotate(handle, bb)); 79 } 80 out.println("END" + blockAnnotate(bb)); 81 Iterator <Edge> edgeIter = 82 isForwards 83 ? cfg.outgoingEdgeIterator(bb) 84 : cfg.incomingEdgeIterator(bb); 85 while (edgeIter.hasNext()) { 86 Edge edge = edgeIter.next(); 87 out.println(" " + edge.formatAsString(!isForwards) + " " + edgeAnnotate(edge)); 88 } 89 } 90 } 91 92 public String edgeAnnotate(Edge edge) { 93 return ""; 94 } 95 96 public String blockStartAnnotate(BasicBlock block) { 97 return ""; 98 } 99 100 public String blockAnnotate(BasicBlock block) { 101 return ""; 102 } 103 104 public String instructionAnnotate(InstructionHandle handle, BasicBlock bb) { 105 return ""; 106 } 107 108 protected Iterator <InstructionHandle> instructionIterator(BasicBlock bb) { 109 if (isForwards) 110 return bb.instructionIterator(); 111 else 112 return bb.instructionReverseIterator(); 113 } 114 115 public static void main(String [] argv) { 116 try { 117 if (argv.length == 0 || argv.length > 2) { 118 System.out.println("Usage: " + CFGPrinter.class.getName() + " <class file> [outputFile]"); 119 System.exit(1); 120 } 121 122 String className = argv[0]; 123 JavaClass cls = new ClassParser(className).parse(); 124 RepositoryLookupFailureCallback lookupFailureCallback = new DebugRepositoryLookupFailureCallback(); 125 126 AnalysisContext analysisContext = AnalysisContext.create(lookupFailureCallback); 127 ClassContext classContext = analysisContext.getClassContext(cls); 128 129 Method[] methods = cls.getMethods(); 130 String methodName = SystemProperties.getProperty("cfg.method"); 131 PrintStream out = System.err; 132 if (argv.length == 2) 133 out = new PrintStream (new FileOutputStream (argv[1])); 134 for (Method method : methods) { 135 MethodGen methodGen = classContext.getMethodGen(method); 136 if (methodGen == null) 137 continue; 138 139 if (methodName != null && !method.getName().equals(methodName)) 140 continue; 141 142 143 out.println(); 144 out.println("----------------------------------------------------------------------------"); 145 out.println("Method " + SignatureConverter.convertMethodSignature(methodGen)); 146 out.println("----------------------------------------------------------------------------"); 147 148 CFG cfg = classContext.getCFG(method); 149 CFGPrinter printer = new CFGPrinter(cfg); 150 printer.print(out); 151 } 152 } catch (Exception e) { 153 e.printStackTrace(); 154 } 155 } 156 } 157 158 | Popular Tags |