KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 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 java.util.*;
24 import soot.util.queue.*;
25
26
27 /** Keeps track of the methods transitively reachable from the specified
28  * entry points through the given call graph edges.
29  * @author Ondrej Lhotak
30  */

31 public class ReachableMethods
32 {
33     private CallGraph cg;
34     private List entryPoints = new ArrayList();
35     private Iterator edgeSource;
36     private ChunkedQueue reachables = new ChunkedQueue();
37     private Set set = new HashSet();
38     private QueueReader unprocessedMethods;
39     private QueueReader allReachables = reachables.reader();
40     private Filter filter;
41     public ReachableMethods( CallGraph graph, Iterator entryPoints ) {
42         this( graph, entryPoints, null );
43     }
44     public ReachableMethods( CallGraph graph, Iterator entryPoints, Filter filter ) {
45         this.filter = filter;
46         this.cg = graph;
47         addMethods( entryPoints );
48         unprocessedMethods = reachables.reader();
49         this.edgeSource = graph.listener();
50         if( filter != null ) this.edgeSource = filter.wrap( this.edgeSource );
51     }
52     public ReachableMethods( CallGraph graph, Collection entryPoints ) {
53         this(graph, entryPoints.iterator());
54     }
55     private void addMethods( Iterator methods ) {
56         while( methods.hasNext() )
57             addMethod( (MethodOrMethodContext) methods.next() );
58     }
59     private void addMethod( MethodOrMethodContext m ) {
60             if( set.add( m ) ) {
61                 reachables.add( m );
62             }
63     }
64     /** Causes the QueueReader objects to be filled up with any methods
65      * that have become reachable since the last call. */

66     public void update() {
67         while(edgeSource.hasNext()) {
68             Edge e = (Edge) edgeSource.next();
69             if( set.contains( e.getSrc() ) ) addMethod( e.getTgt() );
70         }
71         while(unprocessedMethods.hasNext()) {
72             MethodOrMethodContext m = (MethodOrMethodContext) unprocessedMethods.next();
73             Iterator targets = cg.edgesOutOf( m );
74             if( filter != null ) targets = filter.wrap( targets );
75             addMethods( new Targets( targets ) );
76         }
77     }
78     /** Returns a QueueReader object containing all methods found reachable
79      * so far, and which will be informed of any new methods that are later
80      * found to be reachable. */

81     public QueueReader listener() {
82         return (QueueReader) allReachables.clone();
83     }
84     /** Returns a QueueReader object which will contain ONLY NEW methods
85      * which will be found to be reachable, but not those that have already
86      * been found to be reachable.
87      */

88     public QueueReader newListener() {
89         return reachables.reader();
90     }
91     /** Returns true iff method is reachable. */
92     public boolean contains( MethodOrMethodContext m ) {
93         return set.contains( m );
94     }
95     /** Returns the number of methods that are reachable. */
96     public int size() {
97         return set.size();
98     }
99 }
100
101
102
Popular Tags