com.android.tools.lint.checks
Class ControlFlowGraph

java.lang.Object
  extended by com.android.tools.lint.checks.ControlFlowGraph

public class ControlFlowGraph
extends java.lang.Object

A ControlFlowGraph is a graph containing a node for each instruction in a method, and an edge for each possible control flow; usually just "next" for the instruction following the current instruction, but in the case of a branch such as an "if", multiple edges to each successive location, or with a "goto", a single edge to the jumped-to instruction.

It also adds edges for abnormal control flow, such as the possibility of a method call throwing a runtime exception.


Nested Class Summary
static class ControlFlowGraph.Node
          A ControlFlowGraph.Node is a node in the control flow graph for a method, pointing to the instruction and its possible successors
 
Constructor Summary
ControlFlowGraph()
           
 
Method Summary
protected  void add(org.objectweb.asm.tree.AbstractInsnNode from, org.objectweb.asm.tree.AbstractInsnNode to)
          Adds an exception flow to this graph
static ControlFlowGraph create(ControlFlowGraph initial, org.objectweb.asm.tree.ClassNode classNode, org.objectweb.asm.tree.MethodNode method)
          Creates a new ControlFlowGraph and populates it with the flow control for the given method.
protected  void exception(org.objectweb.asm.tree.AbstractInsnNode from, org.objectweb.asm.tree.AbstractInsnNode to)
          Adds an exception flow to this graph
protected  void exception(org.objectweb.asm.tree.AbstractInsnNode from, org.objectweb.asm.tree.TryCatchBlockNode tcb)
          Adds an exception try block node to this graph
 ControlFlowGraph.Node getNode(org.objectweb.asm.tree.AbstractInsnNode instruction)
          Looks up (and if necessary) creates a graph node for the given instruction
 boolean isConnected(org.objectweb.asm.tree.AbstractInsnNode from, org.objectweb.asm.tree.AbstractInsnNode to)
          Checks whether there is a path from the given instruction to the given instruction node
 boolean isConnected(ControlFlowGraph.Node from, ControlFlowGraph.Node to)
          Checks whether there is a path from the given source node to the given destination node
 java.lang.String toDot(java.util.Set<ControlFlowGraph.Node> highlight)
          Generates dot output of the graph.
 java.lang.String toString()
           
 java.lang.String toString(ControlFlowGraph.Node start)
          Creates a human readable version of the graph
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ControlFlowGraph

public ControlFlowGraph()
Method Detail

create

@NonNull
public static ControlFlowGraph create(@Nullable
                                              ControlFlowGraph initial,
                                              @NonNull
                                              org.objectweb.asm.tree.ClassNode classNode,
                                              @NonNull
                                              org.objectweb.asm.tree.MethodNode method)
                               throws org.objectweb.asm.tree.analysis.AnalyzerException
Creates a new ControlFlowGraph and populates it with the flow control for the given method. If the optional initial parameter is provided with an existing graph, then the graph is simply populated, not created. This allows subclassing of the graph instance, if necessary.

Parameters:
initial - usually null, but can point to an existing instance of a ControlFlowGraph in which that graph is reused (but populated with new edges)
classNode - the class containing the method to be analyzed
method - the method to be analyzed
Returns:
a ControlFlowGraph with nodes for the control flow in the given method
Throws:
org.objectweb.asm.tree.analysis.AnalyzerException - if the underlying bytecode library is unable to analyze the method bytecode

isConnected

public boolean isConnected(@NonNull
                           ControlFlowGraph.Node from,
                           @NonNull
                           ControlFlowGraph.Node to)
Checks whether there is a path from the given source node to the given destination node


isConnected

public boolean isConnected(@NonNull
                           org.objectweb.asm.tree.AbstractInsnNode from,
                           @NonNull
                           org.objectweb.asm.tree.AbstractInsnNode to)
Checks whether there is a path from the given instruction to the given instruction node


add

protected void add(@NonNull
                   org.objectweb.asm.tree.AbstractInsnNode from,
                   @NonNull
                   org.objectweb.asm.tree.AbstractInsnNode to)
Adds an exception flow to this graph


exception

protected void exception(@NonNull
                         org.objectweb.asm.tree.AbstractInsnNode from,
                         @NonNull
                         org.objectweb.asm.tree.AbstractInsnNode to)
Adds an exception flow to this graph


exception

protected void exception(@NonNull
                         org.objectweb.asm.tree.AbstractInsnNode from,
                         @NonNull
                         org.objectweb.asm.tree.TryCatchBlockNode tcb)
Adds an exception try block node to this graph


getNode

@NonNull
public ControlFlowGraph.Node getNode(@NonNull
                                             org.objectweb.asm.tree.AbstractInsnNode instruction)
Looks up (and if necessary) creates a graph node for the given instruction

Parameters:
instruction - the instruction
Returns:
the control flow graph node corresponding to the given instruction

toString

@NonNull
public java.lang.String toString(@Nullable
                                         ControlFlowGraph.Node start)
Creates a human readable version of the graph

Parameters:
start - the starting instruction, or null if not known or to use the first instruction
Returns:
a string version of the graph

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toDot

public java.lang.String toDot(@Nullable
                              java.util.Set<ControlFlowGraph.Node> highlight)
Generates dot output of the graph. This can be used with graphwiz to visualize the graph. For example, if you save the output as graph1.gv you can run
 $ dot -Tps graph1.gv -o graph1.ps
 
to generate a postscript file, which you can then view with "gv graph1.ps". (There are also some online web sites where you can paste in dot graphs and see the visualization right there in the browser.)

Returns:
a dot description of this control flow graph, useful for debugging