KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > cl > GraphTalker


1 /* GraphTalker.java */
2 package org.quilt.cl;
3
4 import org.apache.bcel.generic.*;
5 import org.quilt.graph.*;
6
7 /**
8  * Walks through a control flow graph, displaying information about
9  * each vertex and edge. Useful for debugging and as a model
10  * GraphXformer implementation.
11  *
12  * @author <a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
13  */

14 public class GraphTalker implements GraphXformer, org.quilt.graph.Visitor {
15
16     private static String JavaDoc name = null;
17
18     public GraphTalker () { }
19
20     // INTERFACE VISITOR ////////////////////////////////////////
21
public void discoverGraph (Directed graph) {
22         ControlFlowGraph g = (ControlFlowGraph) graph;
23         System.out.println("--------------------------------------" );
24         if (g.getParent() != null)
25             System.out.print("SUB");
26         System.out.println("GRAPH with " + g.size() + " vertices");
27     }
28     public void finishGraph (Directed graph) {
29         System.out.println("--------------------------------------" );
30     }
31     public void discoverVertex (Vertex v) {
32         Connector conn = v.getConnector();
33         // MAKE THIS A STRINGBUFFER
34
StringBuffer JavaDoc sb = new StringBuffer JavaDoc().append("VERTEX ").append(v);
35         if (v instanceof Entry) {
36             ControlFlowGraph parent
37                             = (ControlFlowGraph)v.getGraph().getParent();
38             sb.append(" graph " ).append(v.getGraph().getIndex())
39                 .append(" whose parent is graph ");
40             if (parent == null) {
41                 sb.append ("<null>\n");
42             } else {
43                 sb.append(parent.getIndex()).append("\n");
44             }
45             if (conn instanceof ComplexConnector) {
46                 int k = conn.size();
47                 sb.append(" HANDLERS\n");
48                 for (int i = 0; i < k; i++) {
49                     sb.append(" -> ")
50                       .append(((ComplexConnector)conn).getEdge(i).getTarget())
51                       .append("\n");
52                 }
53             }
54         } else if (v instanceof Exit) {
55             sb.append(" EXIT\n");
56         } else {
57             CodeVertex cv = (CodeVertex) v;
58             sb.append("\n instructions:\n");
59             InstructionList ilist = cv.getInstructionList();
60             Instruction[] inst = ilist.getInstructions();
61             if (inst.length == 0)
62                 sb.append(" none\n");
63             for (int i = 0; i < inst.length; i++) {
64                 sb.append(" ").append(inst[i]).append("\n");
65             }
66             Instruction connInst = cv.getConnInst();
67             if (connInst == null) {
68                 sb.append(
69                     " NO CONNECTING INSTRUCTION (flows through)\n");
70             } else {
71                 sb.append(" CONNECTING INST: ")
72                                         .append(connInst).append("\n");
73             }
74         }
75         System.out.print(sb.toString());
76     }
77     public void finishVertex (Vertex v) {
78     }
79     public void discoverEdge (Edge e) {
80         System.out.println(" EDGE " + e.getSource()
81                                             + " -> " + e.getTarget() );
82     }
83     public void finishEdge (Edge e) {
84     }
85     
86     // INTERFACE GRAPHXFORMER ///////////////////////////////////
87
public void xform (final ClassGen cg, final MethodGen method,
88                                                     ControlFlowGraph cfg) {
89         Walker walker = new Walker();
90         walker.visit (cfg, this);
91     }
92     public static String JavaDoc getName() {
93         return name;
94     }
95     public static void setName (String JavaDoc s) {
96         name = s;
97     }
98 }
99
Popular Tags