1 package Analysis.SSJava;
3 import java.util.HashMap;
4 import java.util.HashSet;
9 import IR.MethodDescriptor;
11 public class FlowGraph {
15 Set<FlowNode> nodeSet;
18 // maps the composite representation of field/var descriptors to infer nodes
19 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
21 // maps an infer node to the set of neighbors which is pointed by the node
22 Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
26 public FlowGraph(MethodDescriptor md) {
28 nodeSet = new HashSet<FlowNode>();
29 mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
30 mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
32 // create a node for 'this' varialbe
33 FlowNode thisNode = new FlowNode(null, md.getThis());
34 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
35 thisVarTuple.add(md.getThis());
36 mapDescTupleToInferNode.put(thisVarTuple, thisNode);
37 thisVarNode = thisNode;
41 public void addNeighbor(FlowNode node, FlowNode neighbor) {
42 Set<FlowNode> set = mapNodeToNeighborSet.get(node);
44 set = new HashSet<FlowNode>();
48 System.out.println("add a new neighbor " + neighbor + " to " + node);
51 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
53 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
54 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
56 System.out.println("create an edge from " + fromNode + " to " + toNode);
58 int fromTupleSize = fromDescTuple.size();
59 NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
60 for (int i = 0; i < fromTupleSize; i++) {
61 Descriptor desc = fromDescTuple.get(i);
63 addNeighbor(getInferNode(curTuple), toNode);
66 int toTupleSize = toDescTuple.size();
67 curTuple = new NTuple<Descriptor>();
68 for (int i = 0; i < toTupleSize; i++) {
69 Descriptor desc = toDescTuple.get(i);
71 addNeighbor(fromNode, getInferNode(curTuple));
76 public FlowNode getInferNode(NTuple<Descriptor> descTuple) {
77 if (mapDescTupleToInferNode.containsKey(descTuple)) {
78 return mapDescTupleToInferNode.get(descTuple);
83 public FlowNode getThisVarNode() {
87 public void createNewFlowNode(NTuple<Descriptor> base) {
89 FlowNode node = new FlowNode(base);
90 mapDescTupleToInferNode.put(base, node);
92 System.out.println("Creating new node=" + node);