KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > CFGPrinter


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.util.Iterator JavaDoc;
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 /**
36  * Print out a representation of a control-flow graph.
37  * For debugging.
38  *
39  * @see CFG
40  * @see CFGBuilder
41  */

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     /**
56      * @return Returns the isForwards.
57      */

58     public boolean isForwards() {
59         return isForwards;
60     }
61     
62     public void print(PrintStream JavaDoc out) {
63         Iterator JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc edgeAnnotate(Edge edge) {
93         return "";
94     }
95
96     public String JavaDoc blockStartAnnotate(BasicBlock block) {
97         return "";
98     }
99
100     public String JavaDoc blockAnnotate(BasicBlock block) {
101         return "";
102     }
103
104     public String JavaDoc instructionAnnotate(InstructionHandle handle, BasicBlock bb) {
105         return "";
106     }
107
108     protected Iterator JavaDoc<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 JavaDoc[] 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 JavaDoc 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 JavaDoc methodName = SystemProperties.getProperty("cfg.method");
131             PrintStream JavaDoc out = System.err;
132             if (argv.length == 2)
133                 out = new PrintStream JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc e) {
153             e.printStackTrace();
154         }
155     }
156 }
157
158 // vim:ts=4
159
Popular Tags