KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > callgraph > SlowCallGraph


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Ondrej 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 soot.jimple.toolkits.callgraph;
21 import soot.*;
22 import soot.util.*;
23 import soot.util.queue.*;
24 import java.util.*;
25
26 /** Represents the edges in a call graph. This class is meant to act as
27  * only a container of edges; code for various call graph builders should
28  * be kept out of it, as well as most code for accessing the edges.
29  * @author Ondrej Lhotak
30  */

31 public class SlowCallGraph extends CallGraph
32 {
33     private Set edges = new HashSet();
34     private MultiMap srcMap = new HashMultiMap();
35     private MultiMap unitMap = new HashMultiMap();
36     private MultiMap tgtMap = new HashMultiMap();
37     private ChunkedQueue stream = new ChunkedQueue();
38     private QueueReader reader = stream.reader();
39     private Edge dummy = new Edge( null, null, null, Kind.INVALID );
40
41     /** Used to add an edge to the call graph. Returns true iff the edge was
42      * not already present. */

43     public boolean addEdge( Edge e ) {
44         if( !edges.add( e ) ) return false;
45         stream.add( e );
46
47         srcMap.put( e.getSrc(), e );
48         tgtMap.put( e.getTgt(), e );
49         unitMap.put( e.srcUnit(), e );
50
51         return true;
52     }
53     /** Removes the edge e from the call graph. Returns true iff the edge
54      * was originally present in the call graph. */

55     public boolean removeEdge( Edge e ) {
56         if( !edges.remove( e ) ) return false;
57
58         srcMap.remove(e.getSrc(), e);
59         tgtMap.remove(e.getTgt(), e);
60         unitMap.remove(e.srcUnit(), e);
61
62         return true;
63     }
64
65     /** Returns an iterator over all methods that are the sources of at least
66      * one edge. */

67     public Iterator sourceMethods() {
68         return new ArrayList(srcMap.keySet()).iterator();
69     }
70     /** Returns an iterator over all edges that have u as their source unit. */
71     public Iterator edgesOutOf( Unit u ) {
72         return new ArrayList(unitMap.get(u)).iterator();
73     }
74     /** Returns an iterator over all edges that have m as their source method. */
75     public Iterator edgesOutOf( MethodOrMethodContext m ) {
76         return new ArrayList(srcMap.get(m)).iterator();
77     }
78     /** Returns an iterator over all edges that have m as their target method. */
79     public Iterator edgesInto( MethodOrMethodContext m ) {
80         return new ArrayList(tgtMap.get(m)).iterator();
81     }
82     /** Returns a QueueReader object containing all edges added so far, and
83      * which will be informed of any new edges that are later added to
84      * the graph. */

85     public QueueReader listener() {
86         return (QueueReader) reader.clone();
87     }
88     /** Returns a QueueReader object which will contain ONLY NEW edges
89      * which will be added to the graph.
90      */

91     public QueueReader newListener() {
92         return stream.reader();
93     }
94     public String JavaDoc toString() {
95         QueueReader reader = listener();
96         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
97         while(reader.hasNext()) {
98             Edge e = (Edge) reader.next();
99             out.append( e.toString() + "\n" );
100         }
101         return out.toString();
102     }
103     /** Returns the number of edges in the call graph. */
104     public int size() {
105         return edges.size();
106     }
107 }
108
109
Popular Tags