KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > cfg > CFGViewer


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

19
20 package ca.mcgill.sable.soot.cfg;
21
22 import org.eclipse.ui.*;
23 import org.eclipse.jface.action.*;
24 import org.eclipse.jface.viewers.*;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.draw2d.*;
28 import org.eclipse.draw2d.graph.*;
29 import org.eclipse.draw2d.geometry.*;
30 import java.util.*;
31 import ca.mcgill.sable.soot.launching.*;
32 import soot.toolkits.graph.*;
33 import org.eclipse.swt.SWT;
34
35 public class CFGViewer {
36     
37     public void run(Object JavaDoc sootGraph){
38         Shell shell = new Shell();
39         shell.open();
40         shell.setText("CFG Test");
41         
42         LightweightSystem lws = new LightweightSystem(shell);
43         
44         Panel p = new Panel();
45         HashMap nodeMap = new HashMap();
46         org.eclipse.draw2d.graph.DirectedGraph dg = makeSootGraph((soot.toolkits.graph.DirectedGraph)sootGraph);
47         Iterator nIt = dg.nodes.iterator();
48         while (nIt.hasNext()){
49             Node nextNode = (Node)nIt.next();
50             IFigure node = new RectangleFigure();
51             IFigure label = new Label((String JavaDoc)nextNode.data);
52             label.setSize(nextNode.width, 36);
53             node.add(label);
54             int len = ((String JavaDoc)nextNode.data).length() * 5;
55             node.setLocation(new Point(nextNode.x, nextNode.y));
56             node.setSize(nextNode.width, 36);
57             p.add(node);
58             nodeMap.put(nextNode, node);
59         }
60         Iterator eIt = dg.edges.iterator();
61         while (eIt.hasNext()){
62             Edge nextEdge = (Edge)eIt.next();
63             PolylineConnection edge = new PolylineConnection();
64             ChopboxAnchor ca1 = new ChopboxAnchor((IFigure)nodeMap.get(nextEdge.source));
65             ChopboxAnchor ca2 = new ChopboxAnchor((IFigure)nodeMap.get(nextEdge.target));
66             
67             edge.setSourceAnchor(ca1);
68             edge.setTargetAnchor(ca2);
69             edge.setTargetDecoration(new PolygonDecoration());
70             p.add(edge);
71         }
72         
73         lws.setContents(p);
74         Display display = Display.getDefault();
75         while (!shell.isDisposed ()) {
76             if (!display.readAndDispatch ())
77                 display.sleep ();
78         }
79     }
80     
81     public org.eclipse.draw2d.graph.DirectedGraph makeSootGraph(soot.toolkits.graph.DirectedGraph sootGraph){
82         org.eclipse.draw2d.graph.DirectedGraph dg = new org.eclipse.draw2d.graph.DirectedGraph();
83         NodeList nodes = new NodeList();
84         EdgeList edges = new EdgeList();
85         HashMap nodeMap = new HashMap();
86         
87         Iterator it = sootGraph.iterator();
88         while (it.hasNext()){
89             Object JavaDoc node = it.next();
90             Node n;
91             if (!nodeMap.containsKey(node)){
92                 n = new Node(node.toString());
93                 n.width = node.toString().length() * 7;
94                 nodes.add(n);
95                 nodeMap.put(node, n);
96             }
97             else {
98                 n = (Node)nodeMap.get(node);
99             }
100             Iterator succIt = sootGraph.getSuccsOf(node).iterator();
101             while (succIt.hasNext()){
102                 Object JavaDoc succ = succIt.next();
103                 Node s;
104                 if (!nodeMap.containsKey(succ)){
105                     s = new Node(succ.toString());
106                     s.width = s.toString().length() * 7;
107                     nodes.add(s);
108                     nodeMap.put(succ, s);
109                 }
110                 else {
111                     s = (Node)nodeMap.get(succ);
112                 }
113                 Edge e = new Edge(n, s);
114                 edges.add(e);
115             }
116         }
117         
118         dg.nodes = nodes;
119         dg.edges = edges;
120         DirectedGraphLayout dgl = new DirectedGraphLayout();
121         dgl.visit(dg);
122         return dg;
123     }
124     
125     public org.eclipse.draw2d.graph.DirectedGraph makeSimpleGraph(){
126         NodeList nl = new NodeList();
127         Node n1 = new Node();
128         String JavaDoc data = "y = 3";
129         n1.data = data;
130         n1.width = data.length() * 7;
131         nl.add(n1);
132         Node n2 = new Node();
133         data = "if i >= 10 goto L0";
134         n2.data = data;
135         n2.width = data.length() * 7;
136         nl.add(n2);
137         Node n3 = new Node();
138         data = "if i != 0 goto L1";
139         n3.data = data;
140         n3.width = data.length() * 7;
141         nl.add(n3);
142         Node n4 = new Node();
143         data = "x = 5";
144         n4.data = data;
145         n4.width = data.length() * 7;
146         nl.add(n4);
147         EdgeList el = new EdgeList();
148         Edge e1 = new Edge(n1, n2);
149         el.add(e1);
150         Edge e2 = new Edge(n2, n3);
151         el.add(e2);
152         Edge e3 = new Edge(n2, n4);
153         el.add(e3);
154         org.eclipse.draw2d.graph.DirectedGraph dg = new org.eclipse.draw2d.graph.DirectedGraph();
155         dg.edges = el;
156         dg.nodes = nl;
157         DirectedGraphLayout dgl = new DirectedGraphLayout();
158         dgl.visit(dg);
159         return dg;
160     }
161     
162     public void selectionChanged(IAction action, ISelection selection){
163         
164     }
165     
166     public void dispose(){
167     
168     }
169     
170     public void init(IWorkbenchWindow window){
171     
172     }
173     
174     
175     public CFGViewer() {
176         super();
177     }
178
179 }
180
Popular Tags