KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > annotation > callgraph > CallGraphTagger


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer 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.annotation.callgraph;
21
22 import soot.*;
23 import soot.jimple.toolkits.callgraph.*;
24 import soot.tagkit.*;
25 import java.util.*;
26 import soot.jimple.*;
27
28 public class CallGraphTagger extends BodyTransformer {
29
30     public CallGraphTagger( Singletons.Global g ) {}
31     public static CallGraphTagger v() { return G.v().soot_jimple_toolkits_annotation_callgraph_CallGraphTagger(); }
32     
33     private MethodToContexts methodToContexts;
34     protected void internalTransform(
35             Body b, String JavaDoc phaseName, Map options)
36     {
37         
38         CallGraph cg = Scene.v().getCallGraph();
39         if( methodToContexts == null ) {
40             methodToContexts = new MethodToContexts( Scene.v().getReachableMethods().listener() );
41         }
42     
43         Iterator stmtIt = b.getUnits().iterator();
44
45         while (stmtIt.hasNext()){
46         
47             Stmt s = (Stmt) stmtIt.next();
48
49             Iterator edges = cg.edgesOutOf(s);
50             
51             while (edges.hasNext()){
52                 Edge e = (Edge)edges.next();
53                 SootMethod m = e.tgt();
54                 s.addTag(new LinkTag("CallGraph: Type: "+e.kind()+" Target Method/Context: "+e.getTgt().toString(), m, m.getDeclaringClass().getName(), "Call Graph"));
55                 
56             }
57         }
58
59         SootMethod m = b.getMethod();
60         for( Iterator momcIt = methodToContexts.get(m).iterator(); momcIt.hasNext(); ) {
61             final MethodOrMethodContext momc = (MethodOrMethodContext) momcIt.next();
62         Iterator callerEdges = cg.edgesInto(momc);
63             while (callerEdges.hasNext()){
64                 Edge callEdge = (Edge)callerEdges.next();
65                 SootMethod methodCaller = callEdge.src();
66                 Host src = methodCaller;
67                 if( callEdge.srcUnit() != null ) {
68                     src = callEdge.srcUnit();
69                 }
70                 m.addTag(
71                         new LinkTag(
72                             "CallGraph: Source Type: "+callEdge.kind()+" Source Method/Context: "+callEdge.getSrc().toString(),
73                             src,
74                             methodCaller.getDeclaringClass().getName(),
75                             "Call Graph"));
76             }
77         }
78     }
79
80 }
81
82
Popular Tags