1 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 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 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 81 public QueueReader listener() { 82 return (QueueReader) allReachables.clone(); 83 } 84 88 public QueueReader newListener() { 89 return reachables.reader(); 90 } 91 92 public boolean contains( MethodOrMethodContext m ) { 93 return set.contains( m ); 94 } 95 96 public int size() { 97 return set.size(); 98 } 99 } 100 101 102 | Popular Tags |