KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > cfgcmd > CFGGraphType


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 John Jorgensen
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 soot.util.cfgcmd;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import soot.G;
25 import soot.Body;
26 import soot.toolkits.graph.DirectedGraph;
27 import soot.toolkits.graph.UnitGraph;
28 import soot.toolkits.graph.BriefUnitGraph;
29 import soot.toolkits.graph.ExceptionalUnitGraph;
30 import soot.toolkits.graph.CompleteUnitGraph;
31 import soot.toolkits.graph.TrapUnitGraph;
32 import soot.toolkits.graph.ClassicCompleteBlockGraph;
33 import soot.toolkits.graph.ClassicCompleteUnitGraph;
34 import soot.toolkits.graph.BlockGraph;
35 import soot.toolkits.graph.BriefBlockGraph;
36 import soot.toolkits.graph.ExceptionalBlockGraph;
37 import soot.toolkits.graph.CompleteBlockGraph;
38 import soot.toolkits.graph.ArrayRefBlockGraph;
39 import soot.toolkits.graph.ZonedBlockGraph;
40 import soot.util.cfgcmd.CFGToDotGraph;
41 import soot.util.dot.DotGraph;
42
43 /**
44  * An enumeration type for representing the varieties of control
45  * flow graph available, for use in tools that compare or display
46  * CFGs.
47  */

48 public abstract class CFGGraphType extends CFGOptionMatcher.CFGOption {
49
50   private static final boolean DEBUG = true;
51
52   /**
53    * Method that will build a graph of this type.
54    *
55    * @param b The method <code>Body</code> from which to build the graph.
56    *
57    * @return The control flow graph corresponding to <code>b</code>
58    */

59   public abstract DirectedGraph buildGraph(Body b);
60
61   /**
62    * Method that will draw a {@link DotGraph} representation of the
63    * control flow in this type of graph. This method is intended for
64    * use within {@link soot.tools.CFGViewer CFGViewer}.
65    *
66    * @param drawer The {@link CFGToDotGraph} object that will draw the
67    * graph.
68    *
69    * @param g The graph to draw.
70    *
71    * @param b The body associated with the graph, <code>g</code>.
72    *
73    * @return a <code>DotGraph</code> visualizing the control flow in
74    * <code>g</code>.
75    */

76   public abstract DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g,
77                      Body b);
78
79   private CFGGraphType(String JavaDoc name) {
80     super(name);
81   }
82
83   /**
84    * Returns the <code>CFGGraphType</code> identified by the
85    * passed name.
86    *
87    * @param name A {@link String} identifying the graph type.
88    *
89    * @return A {@link CFGGraphType} object whose {@link #buildGraph()}
90    * method will create the desired sort of control flow graph and
91    * whose {@link #drawGraph} method will produce a {@link
92    * DotGraph} corresponding to the graph.
93    */

94   public static CFGGraphType getGraphType(String JavaDoc option) {
95     return (CFGGraphType) graphTypeOptions.match(option);
96   }
97
98
99   /**
100    * Returns a string containing the names of all the
101    * available {@link CFGGraphType}s, separated by
102    * '|' characters.
103    *
104    * @param initialIndent The number of blank spaces to insert at the
105    * beginning of the returned string. Ignored if
106    * negative.
107    *
108    * @param rightMargin If positive, newlines will be inserted to try
109    * to keep the length of each line in the
110    * returned string less than or equal to
111    * <code>rightMargin</code>.
112    *
113    * @param hangingIndent If positive, this number of spaces will be
114    * inserted immediately after each newline
115    * inserted to respect the <code>rightMargin</code>.
116    */

117   public static String JavaDoc help(int initialIndent, int rightMargin,
118                 int hangingIndent) {
119     return graphTypeOptions.help(initialIndent, rightMargin, hangingIndent);
120   }
121
122
123   public static final CFGGraphType BRIEF_UNIT_GRAPH =
124     new CFGGraphType("BriefUnitGraph") {
125       public DirectedGraph buildGraph(Body b) {
126     return new BriefUnitGraph(b);
127       }
128
129     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
130       return drawer.drawCFG((BriefUnitGraph) g, b);
131     }
132   };
133
134   public static final CFGGraphType EXCEPTIONAL_UNIT_GRAPH =
135     new CFGGraphType("ExceptionalUnitGraph") {
136     public DirectedGraph buildGraph(Body b) {
137       return new ExceptionalUnitGraph(b);
138     }
139     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
140       return drawer.drawCFG((ExceptionalUnitGraph) g);
141     }
142   };
143
144   public static final CFGGraphType COMPLETE_UNIT_GRAPH =
145     new CFGGraphType("CompleteUnitGraph") {
146     public DirectedGraph buildGraph(Body b) {
147       return new CompleteUnitGraph(b);
148     }
149     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
150       return drawer.drawCFG((CompleteUnitGraph) g);
151     }
152   };
153
154   public static final CFGGraphType TRAP_UNIT_GRAPH =
155     new CFGGraphType("TrapUnitGraph") {
156     public DirectedGraph buildGraph(Body b) {
157       return new TrapUnitGraph(b);
158     }
159     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
160       return drawer.drawCFG((TrapUnitGraph) g, b);
161     }
162   };
163
164   public static final CFGGraphType CLASSIC_COMPLETE_UNIT_GRAPH =
165     new CFGGraphType("ClassicCompleteUnitGraph") {
166     public DirectedGraph buildGraph(Body b) {
167       return new ClassicCompleteUnitGraph(b);
168     }
169     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
170       return drawer.drawCFG((ClassicCompleteUnitGraph) g, b);
171     }
172   };
173
174   public static final CFGGraphType BRIEF_BLOCK_GRAPH =
175     new CFGGraphType("BriefBlockGraph") {
176     public DirectedGraph buildGraph(Body b) {
177       return new BriefBlockGraph(b);
178     }
179     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
180       return drawer.drawCFG((BriefBlockGraph) g, b);
181     }
182   };
183
184   public static final CFGGraphType EXCEPTIONAL_BLOCK_GRAPH =
185     new CFGGraphType("ExceptionalBlockGraph") {
186     public DirectedGraph buildGraph(Body b) {
187       return new ExceptionalBlockGraph(b);
188     }
189     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
190       return drawer.drawCFG((ExceptionalBlockGraph) g);
191     }
192   };
193
194   public static final CFGGraphType COMPLETE_BLOCK_GRAPH =
195     new CFGGraphType("CompleteBlockGraph") {
196     public DirectedGraph buildGraph(Body b) {
197       return new CompleteBlockGraph(b);
198     }
199     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
200       return drawer.drawCFG((CompleteBlockGraph) g, b);
201     }
202   };
203
204   public static final CFGGraphType CLASSIC_COMPLETE_BLOCK_GRAPH =
205     new CFGGraphType("ClassicCompleteBlockGraph") {
206     public DirectedGraph buildGraph(Body b) {
207       return new ClassicCompleteBlockGraph(b);
208     }
209     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
210       return drawer.drawCFG((ClassicCompleteBlockGraph) g, b);
211     }
212   };
213
214   public static final CFGGraphType ARRAY_REF_BLOCK_GRAPH =
215     new CFGGraphType("ArrayRefBlockGraph") {
216     public DirectedGraph buildGraph(Body b) {
217       return new ArrayRefBlockGraph(b);
218     }
219     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
220       return drawer.drawCFG((ArrayRefBlockGraph) g, b);
221     }
222   };
223
224   public static final CFGGraphType ZONED_BLOCK_GRAPH =
225     new CFGGraphType("ZonedBlockGraph") {
226     public DirectedGraph buildGraph(Body b) {
227       return new ZonedBlockGraph(b);
228     }
229     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
230       return drawer.drawCFG((ZonedBlockGraph) g, b);
231     }
232   };
233
234
235   private static DirectedGraph loadAltGraph(String JavaDoc className, Body b) {
236     try {
237       Class JavaDoc graphClass = AltClassLoader.v().loadClass(className);
238       Class JavaDoc[] paramTypes = new Class JavaDoc[] { Body.class };
239       Constructor JavaDoc constructor = graphClass.getConstructor(paramTypes);
240       DirectedGraph result = (DirectedGraph)
241     constructor.newInstance(new Object JavaDoc[] { b });
242       return result;
243     }
244     // Turn class loading exceptions into RuntimeExceptions, so callers
245
// don't need to declare them: perhaps a shoddy tactic.
246
catch (ClassNotFoundException JavaDoc e) {
247       if (DEBUG) {
248     e.printStackTrace(G.v().out);
249       }
250       throw new IllegalArgumentException JavaDoc("Unable to find " + className +
251                  " in alternate classpath: " +
252                      e.getMessage());
253     }
254     catch (NoSuchMethodException JavaDoc e) {
255       if (DEBUG) {
256     e.printStackTrace(G.v().out);
257       }
258       throw new IllegalArgumentException JavaDoc("There is no " + className +
259                      "(Body) constructor: " +
260                      e.getMessage());
261     }
262     catch (InstantiationException JavaDoc e) {
263       if (DEBUG) {
264     e.printStackTrace(G.v().out);
265       }
266       throw new IllegalArgumentException JavaDoc("Unable to instantiate " + className +
267                      " in alternate classpath: " +
268                      e.getMessage());
269     }
270     catch (IllegalAccessException JavaDoc e) {
271       if (DEBUG) {
272     e.printStackTrace(G.v().out);
273       }
274       throw new IllegalArgumentException JavaDoc("Unable to access " + className +
275                      "(Body) in alternate classpath: " +
276                      e.getMessage());
277     }
278     catch (InvocationTargetException JavaDoc e) {
279       if (DEBUG) {
280     e.printStackTrace(G.v().out);
281       }
282       throw new IllegalArgumentException JavaDoc("Unable to invoke " + className +
283                      "(Body) in alternate classpath: " +
284                      e.getMessage());
285     }
286   }
287
288
289   public static final CFGGraphType ALT_BRIEF_UNIT_GRAPH =
290     new CFGGraphType("AltBriefUnitGraph") {
291       public DirectedGraph buildGraph(Body b) {
292     return loadAltGraph("soot.toolkits.graph.BriefUnitGraph", b);
293       }
294
295     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
296       return drawer.drawCFG(g, b);
297     }
298   };
299
300   public static final CFGGraphType ALT_COMPLETE_UNIT_GRAPH =
301     new CFGGraphType("AltCompleteUnitGraph") {
302       public DirectedGraph buildGraph(Body b) {
303     return loadAltGraph("soot.toolkits.graph.CompleteUnitGraph", b);
304       }
305
306     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
307       return drawer.drawCFG(g, b);
308     }
309   };
310
311   public static final CFGGraphType ALT_TRAP_UNIT_GRAPH =
312     new CFGGraphType("AltTrapUnitGraph") {
313       public DirectedGraph buildGraph(Body b) {
314     return loadAltGraph("soot.toolkits.graph.TrapUnitGraph", b);
315       }
316
317     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
318       return drawer.drawCFG(g, b);
319     }
320   };
321
322   public static final CFGGraphType ALT_ARRAY_REF_BLOCK_GRAPH =
323     new CFGGraphType("AltArrayRefBlockGraph") {
324       public DirectedGraph buildGraph(Body b) {
325     return loadAltGraph("soot.toolkits.graph.ArrayRefBlockGraph", b);
326       }
327
328     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
329       return drawer.drawCFG(g, b);
330     }
331   };
332
333   public static final CFGGraphType ALT_BRIEF_BLOCK_GRAPH =
334     new CFGGraphType("AltBriefBlockGraph") {
335       public DirectedGraph buildGraph(Body b) {
336     return loadAltGraph("soot.toolkits.graph.BriefBlockGraph", b);
337       }
338
339     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
340       return drawer.drawCFG(g, b);
341     }
342   };
343
344   public static final CFGGraphType ALT_COMPLETE_BLOCK_GRAPH =
345     new CFGGraphType("AltCompleteBlockGraph") {
346       public DirectedGraph buildGraph(Body b) {
347     return loadAltGraph("soot.toolkits.graph.CompleteBlockGraph", b);
348       }
349
350     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
351       return drawer.drawCFG(g, b);
352     }
353   };
354
355   public static final CFGGraphType ALT_ZONED_BLOCK_GRAPH =
356     new CFGGraphType("AltZonedBlockGraph") {
357       public DirectedGraph buildGraph(Body b) {
358     return loadAltGraph("soot.toolkits.graph.ZonedBlockGraph", b);
359       }
360
361     public DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, Body b) {
362       return drawer.drawCFG(g, b);
363     }
364   };
365
366   private final static CFGOptionMatcher graphTypeOptions =
367     new CFGOptionMatcher(new CFGGraphType[] {
368       BRIEF_UNIT_GRAPH,
369       EXCEPTIONAL_UNIT_GRAPH,
370       COMPLETE_UNIT_GRAPH,
371       TRAP_UNIT_GRAPH,
372       CLASSIC_COMPLETE_UNIT_GRAPH,
373       BRIEF_BLOCK_GRAPH,
374       EXCEPTIONAL_BLOCK_GRAPH,
375       COMPLETE_BLOCK_GRAPH,
376       CLASSIC_COMPLETE_BLOCK_GRAPH,
377       ARRAY_REF_BLOCK_GRAPH,
378       ZONED_BLOCK_GRAPH,
379       ALT_ARRAY_REF_BLOCK_GRAPH,
380       ALT_BRIEF_UNIT_GRAPH,
381       ALT_COMPLETE_UNIT_GRAPH,
382       ALT_TRAP_UNIT_GRAPH,
383       ALT_BRIEF_BLOCK_GRAPH,
384       ALT_COMPLETE_BLOCK_GRAPH,
385       ALT_ZONED_BLOCK_GRAPH,
386     });
387 }
388
389
Popular Tags