1 19 20 package soot.util.cfgcmd; 21 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.InvocationTargetException ; 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 48 public abstract class CFGGraphType extends CFGOptionMatcher.CFGOption { 49 50 private static final boolean DEBUG = true; 51 52 59 public abstract DirectedGraph buildGraph(Body b); 60 61 76 public abstract DotGraph drawGraph(CFGToDotGraph drawer, DirectedGraph g, 77 Body b); 78 79 private CFGGraphType(String name) { 80 super(name); 81 } 82 83 94 public static CFGGraphType getGraphType(String option) { 95 return (CFGGraphType) graphTypeOptions.match(option); 96 } 97 98 99 117 public static String 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 className, Body b) { 236 try { 237 Class graphClass = AltClassLoader.v().loadClass(className); 238 Class [] paramTypes = new Class [] { Body.class }; 239 Constructor constructor = graphClass.getConstructor(paramTypes); 240 DirectedGraph result = (DirectedGraph) 241 constructor.newInstance(new Object [] { b }); 242 return result; 243 } 244 catch (ClassNotFoundException e) { 247 if (DEBUG) { 248 e.printStackTrace(G.v().out); 249 } 250 throw new IllegalArgumentException ("Unable to find " + className + 251 " in alternate classpath: " + 252 e.getMessage()); 253 } 254 catch (NoSuchMethodException e) { 255 if (DEBUG) { 256 e.printStackTrace(G.v().out); 257 } 258 throw new IllegalArgumentException ("There is no " + className + 259 "(Body) constructor: " + 260 e.getMessage()); 261 } 262 catch (InstantiationException e) { 263 if (DEBUG) { 264 e.printStackTrace(G.v().out); 265 } 266 throw new IllegalArgumentException ("Unable to instantiate " + className + 267 " in alternate classpath: " + 268 e.getMessage()); 269 } 270 catch (IllegalAccessException e) { 271 if (DEBUG) { 272 e.printStackTrace(G.v().out); 273 } 274 throw new IllegalArgumentException ("Unable to access " + className + 275 "(Body) in alternate classpath: " + 276 e.getMessage()); 277 } 278 catch (InvocationTargetException e) { 279 if (DEBUG) { 280 e.printStackTrace(G.v().out); 281 } 282 throw new IllegalArgumentException ("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 |