KickJava   Java API By Example, From Geeks To Geeks.

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


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.jimple.*;
23
24 /** Represents a single edge in a call graph.
25  * @author Ondrej Lhotak
26  */

27 public final class Edge
28 {
29     /** The method in which the call occurs; may be null for calls not
30      * occurring in a specific method (eg. implicit calls by the VM)
31      */

32     private MethodOrMethodContext src;
33     public SootMethod src() {
34         if( src == null ) return null; else return src.method();
35     }
36     public Context srcCtxt() {
37         if( src == null ) return null; else return src.context();
38     }
39     public MethodOrMethodContext getSrc() { return src; }
40
41     /** The unit at which the call occurs; may be null for calls not
42      * occurring at a specific statement (eg. calls in native code)
43      */

44     private Unit srcUnit;
45     public Unit srcUnit() { return srcUnit; }
46     public Stmt srcStmt() { return (Stmt) srcUnit; }
47     
48     /** The target method of the call edge. */
49     private MethodOrMethodContext tgt;
50     public SootMethod tgt() { return tgt.method(); }
51     public Context tgtCtxt() { return tgt.context(); }
52     public MethodOrMethodContext getTgt() { return tgt; }
53
54     /** The kind of edge. Note: kind should not be tested by other classes;
55      * instead, accessors such as isExplicit() should be added.
56      **/

57     private Kind kind;
58     public Kind kind() { return kind; }
59
60     public Edge( MethodOrMethodContext src, Unit srcUnit, MethodOrMethodContext tgt, Kind kind ) {
61         this.src = src;
62         this.srcUnit = srcUnit;
63         this.tgt = tgt;
64         this.kind = kind;
65     }
66
67     public Edge( MethodOrMethodContext src, Stmt srcUnit, MethodOrMethodContext tgt ) {
68         this.kind = ieToKind( srcUnit.getInvokeExpr() );
69         this.src = src;
70         this.srcUnit = srcUnit;
71         this.tgt = tgt;
72     }
73
74     public static Kind ieToKind( InvokeExpr ie ) {
75         if( ie instanceof VirtualInvokeExpr ) return Kind.VIRTUAL;
76         else if( ie instanceof SpecialInvokeExpr ) return Kind.SPECIAL;
77         else if( ie instanceof InterfaceInvokeExpr ) return Kind.INTERFACE;
78         else if( ie instanceof StaticInvokeExpr ) return Kind.STATIC;
79         else throw new RuntimeException JavaDoc();
80     }
81
82     /** Returns true if the call is due to an explicit invoke statement. */
83     public boolean isExplicit() {
84         return kind.isExplicit();
85     }
86
87     
88     /** Returns true if the call is due to an explicit instance invoke
89      * statement. */

90     public boolean isInstance() {
91         return kind.isInstance();
92     }
93
94     /** Returns true if the call is to static initializer. */
95     public boolean isClinit() {
96         return kind.isClinit();
97     }
98     /** Returns true if the call is due to an explicit static invoke
99      * statement. */

100     public boolean isStatic() {
101         return kind.isStatic();
102     }
103
104     public boolean passesParameters() {
105         return kind.passesParameters();
106     }
107
108     public int hashCode() {
109         int ret = tgt.hashCode() + kind.getNumber();
110         if( src != null ) ret += src.hashCode();
111         if( srcUnit != null ) ret += srcUnit.hashCode();
112         return ret;
113     }
114     public boolean equals( Object JavaDoc other ) {
115         Edge o = (Edge) other;
116         if( o.src != src ) return false;
117         if( o.srcUnit != srcUnit ) return false;
118         if( o.tgt != tgt ) return false;
119         if( o.kind != kind ) return false;
120         return true;
121     }
122     
123     public String JavaDoc toString() {
124         return kind.toString()+" edge: "+srcUnit+" in "+src+" ==> "+tgt;
125     }
126
127     private Edge nextByUnit = this;
128     private Edge prevByUnit = this;
129     private Edge nextBySrc = this;
130     private Edge prevBySrc = this;
131     private Edge nextByTgt = this;
132     private Edge prevByTgt = this;
133     void insertAfterByUnit( Edge other ) {
134         nextByUnit = other.nextByUnit;
135         nextByUnit.prevByUnit = this;
136         other.nextByUnit = this;
137         prevByUnit = other;
138     }
139     void insertAfterBySrc( Edge other ) {
140         nextBySrc = other.nextBySrc;
141         nextBySrc.prevBySrc = this;
142         other.nextBySrc = this;
143         prevBySrc = other;
144     }
145     void insertAfterByTgt( Edge other ) {
146         nextByTgt = other.nextByTgt;
147         nextByTgt.prevByTgt = this;
148         other.nextByTgt = this;
149         prevByTgt = other;
150     }
151     void insertBeforeByUnit( Edge other ) {
152         prevByUnit = other.prevByUnit;
153         prevByUnit.nextByUnit = this;
154         other.prevByUnit = this;
155         nextByUnit = other;
156     }
157     void insertBeforeBySrc( Edge other ) {
158         prevBySrc = other.prevBySrc;
159         prevBySrc.nextBySrc = this;
160         other.prevBySrc = this;
161         nextBySrc = other;
162     }
163     void insertBeforeByTgt( Edge other ) {
164         prevByTgt = other.prevByTgt;
165         prevByTgt.nextByTgt = this;
166         other.prevByTgt = this;
167         nextByTgt = other;
168     }
169     void remove() {
170         nextByUnit.prevByUnit = prevByUnit;
171         prevByUnit.nextByUnit = nextByUnit;
172         nextBySrc.prevBySrc = prevBySrc;
173         prevBySrc.nextBySrc = nextBySrc;
174         nextByTgt.prevByTgt = prevByTgt;
175         prevByTgt.nextByTgt = nextByTgt;
176     }
177     Edge nextByUnit() {
178         return nextByUnit;
179     }
180     Edge nextBySrc() {
181         return nextBySrc;
182     }
183     Edge nextByTgt() {
184         return nextByTgt;
185     }
186     Edge prevByUnit() {
187         return prevByUnit;
188     }
189     Edge prevBySrc() {
190         return prevBySrc;
191     }
192     Edge prevByTgt() {
193         return prevByTgt;
194     }
195 }
196
197
198
Popular Tags