KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > internal > AbstractInterfaceInvokeExpr


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam
3  * Copyright (C) 2004 Ondrej Lhotak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */

20
21 /*
22  * Modified by the Sable Research Group and others 1997-1999.
23  * See the 'credits' file distributed with Soot for the complete list of
24  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
25  */

26
27
28 package soot.jimple.internal;
29
30 import soot.*;
31 import soot.jimple.*;
32 import soot.baf.*;
33 import soot.jimple.*;
34 import soot.util.*;
35 import java.util.*;
36
37
38 import soot.tagkit.*;
39
40 public abstract class AbstractInterfaceInvokeExpr extends AbstractInstanceInvokeExpr
41                              implements InterfaceInvokeExpr, ConvertToBaf
42 {
43     protected AbstractInterfaceInvokeExpr(ValueBox baseBox, SootMethodRef methodRef,
44                                   ValueBox[] argBoxes)
45     {
46         if( methodRef.isStatic() ) throw new RuntimeException JavaDoc("wrong static-ness");
47         this.baseBox = baseBox; this.methodRef = methodRef;
48         this.argBoxes = argBoxes;
49     }
50
51     public boolean equivTo(Object JavaDoc o)
52     {
53         if (o instanceof AbstractInterfaceInvokeExpr)
54         {
55             AbstractInterfaceInvokeExpr ie = (AbstractInterfaceInvokeExpr)o;
56             if (!(baseBox.getValue().equivTo(ie.baseBox.getValue()) &&
57                     getMethod().equals(ie.getMethod()) &&
58                     argBoxes.length == ie.argBoxes.length))
59                 return false;
60             for (int i = 0; i < argBoxes.length; i++)
61                 if (!(argBoxes[i].getValue().equivTo(ie.argBoxes[i].getValue())))
62                     return false;
63             return true;
64         }
65         return false;
66     }
67
68     /** Returns a hash code for this object, consistent with structural equality. */
69     public int equivHashCode()
70     {
71         return baseBox.getValue().equivHashCode() * 101 + getMethod().equivHashCode() * 17;
72     }
73
74     public abstract Object JavaDoc clone();
75
76     public String JavaDoc toString()
77     {
78         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
79
80         buffer.append(Jimple.INTERFACEINVOKE + " " + baseBox.getValue().toString() +
81             "." + methodRef.getSignature() + "(");
82
83         for(int i = 0; i < argBoxes.length; i++)
84         {
85             if(i != 0)
86                 buffer.append(", ");
87
88             buffer.append(argBoxes[i].getValue().toString());
89         }
90
91         buffer.append(")");
92
93         return buffer.toString();
94     }
95
96     public void toString(UnitPrinter up)
97     {
98         up.literal(Jimple.INTERFACEINVOKE);
99         up.literal(" ");
100         baseBox.toString(up);
101         up.literal(".");
102         up.methodRef(methodRef);
103         up.literal("(");
104         
105         for(int i = 0; i < argBoxes.length; i++)
106         {
107             if(i != 0)
108                 up.literal(", ");
109                 
110             argBoxes[i].toString(up);
111         }
112
113         up.literal(")");
114     }
115
116     
117     public void apply(Switch sw)
118     {
119         ((ExprSwitch) sw).caseInterfaceInvokeExpr(this);
120     }
121
122     private static int sizeOfType(Type t)
123     {
124         if(t instanceof DoubleType || t instanceof LongType)
125             return 2;
126         else if(t instanceof VoidType)
127             return 0;
128         else
129             return 1;
130     }
131
132     private static int argCountOf(SootMethodRef m)
133     {
134         int argCount = 0;
135         Iterator typeIt = m.parameterTypes().iterator();
136
137         while(typeIt.hasNext())
138         {
139             Type t = (Type) typeIt.next();
140
141             argCount += sizeOfType(t);
142         }
143
144         return argCount;
145     }
146
147     public void convertToBaf(JimpleToBafContext context, List out)
148     {
149         ((ConvertToBaf)getBase()).convertToBaf(context, out);;
150
151        for(int i = 0; i < argBoxes.length; i++)
152         {
153             ((ConvertToBaf)(argBoxes[i].getValue())).convertToBaf(context, out);
154         }
155        
156        Unit u;
157        out.add(u = Baf.v().newInterfaceInvokeInst(methodRef, argCountOf(methodRef)));
158
159     Unit currentUnit = context.getCurrentUnit();
160
161     Iterator it = currentUnit.getTags().iterator();
162     while(it.hasNext()) {
163         u.addTag((Tag) it.next());
164     }
165     
166     }
167 }
168
169
Popular Tags