KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003, 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 /** Extends a TargetsOfMethod or TargetsOfUnit to include edges
27  * transitively reachable from any target methods.
28  * @author Ondrej Lhotak
29  */

30 public class TransitiveTargets
31 {
32     private CallGraph cg;
33     private Filter filter;
34     public TransitiveTargets( CallGraph cg ) {
35         this.cg = cg;
36     }
37     public TransitiveTargets( CallGraph cg, Filter filter ) {
38         this.cg = cg;
39         this.filter = filter;
40     }
41     public Iterator iterator( Unit u ) {
42         ArrayList methods = new ArrayList();
43         Iterator it = cg.edgesOutOf( u );
44         if( filter != null ) it = filter.wrap( it );
45         while( it.hasNext() ) {
46             Edge e = (Edge) it.next();
47             methods.add( e.getTgt() );
48         }
49         return iterator( methods.iterator() );
50     }
51     public Iterator iterator( MethodOrMethodContext momc ) {
52         ArrayList methods = new ArrayList();
53         Iterator it = cg.edgesOutOf( momc );
54         if( filter != null ) it = filter.wrap( it );
55         while( it.hasNext() ) {
56             Edge e = (Edge) it.next();
57             methods.add( e.getTgt() );
58         }
59         return iterator( methods.iterator() );
60     }
61     public Iterator iterator( Iterator methods ) {
62         Set s = new HashSet();
63         ArrayList worklist = new ArrayList();
64         while( methods.hasNext() ) {
65             MethodOrMethodContext method = (MethodOrMethodContext) methods.next();
66             if( s.add( method ) ) worklist.add( method );
67         }
68         return iterator( s, worklist );
69     }
70     private Iterator iterator( Set s, ArrayList worklist ) {
71         for( int i = 0; i < worklist.size(); i++ ) {
72             MethodOrMethodContext method = (MethodOrMethodContext) worklist.get(i);
73             Iterator it = cg.edgesOutOf( method );
74             if( filter != null ) it = filter.wrap( it );
75             while( it.hasNext() ) {
76                 Edge e = (Edge) it.next();
77                 if( s.add( e.getTgt() ) ) worklist.add( e.getTgt() );
78             }
79         }
80         return worklist.iterator();
81     }
82 }
83
84
85
Popular Tags