KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > internal > javaRep > DVariableDeclarationStmt


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Nomair A. Naeem
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 /*
21  *TODO: Nomair (November 23rd 2005) should look at all methods in AbstractUnit
22  * and figure out what to do with them!!
23  * With the changed semantics maybe another stmt which deals with int i=2; separatly should be
24  * created
25  */

26
27 /*
28  * CHANGE LOG: November 23rd 2005: Changing the semantics of DVariableDeclarationStmt.
29  * The fact that we can have definition stmts inside the list of locals
30  * declared is going to lead to way too many problems. So DVairableDeclarationStmt
31  * is going to be restricted to declaration which do not have definitions
32  * e.g. int i,j,k; is allowed
33  * int i=2,j; is not allowed
34  */

35 package soot.dava.internal.javaRep;
36
37 import soot.*;
38
39 import java.util.*;
40
41 import soot.jimple.*;
42 import soot.util.IterableSet;
43 import soot.grimp.*;
44 import soot.dava.*;
45
46 public class DVariableDeclarationStmt extends AbstractUnit implements Stmt {
47
48     Type declarationType = null;
49
50     List declarations = null;
51     
52     //added solely for the purpose of retrieving packages used when printing
53
DavaBody davaBody = null;
54     
55     public DVariableDeclarationStmt(Type decType, DavaBody davaBody) {
56         if (declarationType != null)
57             throw new RuntimeException JavaDoc(
58                     "creating a VariableDeclaration which has already been created");
59         else {
60             declarationType = decType;
61             declarations = new ArrayList();
62             this.davaBody=davaBody;
63         }
64     }
65
66     public List getDeclarations() {
67         return declarations;
68     }
69
70     public void addLocal(Local add) {
71         declarations.add(add);
72     }
73
74     public void removeLocal(Local remove) {
75         for (int i = 0; i < declarations.size(); i++) {
76             Local temp = (Local) declarations.get(i);
77             if (temp.getName().compareTo(remove.getName()) == 0) {
78                 //this is the local to be removed
79
//System.out.println("REMOVED"+temp);
80
declarations.remove(i);
81                 return;
82             }
83         }
84     }
85
86     public Type getType() {
87         return declarationType;
88     }
89
90     public boolean isOfType(Type type) {
91         if (type.toString().compareTo(declarationType.toString()) == 0)
92             return true;
93         else
94             return false;
95     }
96
97     public Object JavaDoc clone() {
98         DVariableDeclarationStmt temp = new DVariableDeclarationStmt(
99                 declarationType,davaBody);
100         Iterator it = declarations.iterator();
101         while (it.hasNext()) {
102             Local obj = (Local) it.next();
103
104             Value temp1 = Grimp.cloneIfNecessary(obj);
105             if (temp1 instanceof Local)
106                 temp.addLocal((Local) temp1);
107         }
108         return temp;
109     }
110
111     public String JavaDoc toString() {
112         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
113
114         if (declarations.size() == 0)
115             return b.toString();
116
117         String JavaDoc type = declarationType.toString();
118
119         if (type.equals("null_type"))
120             b.append("Object");
121         else
122             b.append(type);
123         b.append(" ");
124
125         Iterator decIt = declarations.iterator();
126         while (decIt.hasNext()) {
127             Local tempDec = (Local) decIt.next();
128             b.append(tempDec.getName());
129
130             if (decIt.hasNext())
131                 b.append(", ");
132         }
133         return b.toString();
134     }
135
136     public void toString(UnitPrinter up) {
137         if (declarations.size() == 0)
138             return;
139
140         if (!(up instanceof DavaUnitPrinter))
141             throw new RuntimeException JavaDoc(
142                     "DavaBody should always be printed using the DavaUnitPrinter");
143         else {
144             DavaUnitPrinter dup = (DavaUnitPrinter) up;
145
146             String JavaDoc type = declarationType.toString();
147
148             if (type.equals("null_type"))
149                 dup.printString("Object");
150             else{
151                 
152                 
153                 /*
154                  * Nomair A. Naeem 8th Feb 2006
155                  * It is nice to remove the fully qualified type names
156                  * of locals if the package they belong to have been imported
157                  * javax.swing.ImageIcon should be just ImageIcon if javax.swing is imported
158                  * If not imported WHY NOT..import it!!
159                  *
160                  * However this can cause recompilation problems and hence is added
161                  * as a flag
162                  */

163                 
164                 Map options = PhaseOptions.v().getPhaseOptions("db.renamer");
165                 boolean force = PhaseOptions.getBoolean(options, "remove-fully-qualified");
166                 //System.out.println("In DVariableDeclarationStmt Force is"+force);
167

168
169                 if (force) {
170                     IterableSet set = davaBody.get_PackagesUsed();
171
172                     // get the package name of the object if one exists
173
String JavaDoc packageName = null;
174                     if (type.lastIndexOf('.') > 0) {// 0 doesnt make sense
175
packageName = type.substring(0, type.lastIndexOf('.'));
176                     }
177                     if (packageName != null) {
178                         //System.out.println("Parameter belongs to
179
// package"+packageName);
180
// check if package is not contained in packages retrieved
181
if (!set.contains(packageName)) {
182                             // add package
183
davaBody.addPackage(packageName);
184                             // System.out.println(packageName+ "added to list");
185
} else {
186                             // System.out.println(packageName+ "already in list");
187
}
188
189                         // change tempString to just the object type name
190
type = type.substring(type.lastIndexOf('.') + 1);
191                     }
192                 }
193                 
194                 
195                 dup.printString(type);
196             }
197             dup.printString(" ");
198
199             Iterator decIt = declarations.iterator();
200             while (decIt.hasNext()) {
201                 Local tempDec = (Local) decIt.next();
202                 dup.printString(tempDec.getName());
203                 if (decIt.hasNext())
204                     dup.printString(", ");
205             }
206         }
207     }
208
209     /*
210      Methods needed to satisfy all obligations due to extension from AbstractUnit
211      and implementing Stmt
212
213      */

214
215     public boolean fallsThrough() {
216         return true;
217     }
218
219     public boolean branches() {
220         return false;
221     }
222
223     public boolean containsInvokeExpr() {
224         return false;
225     }
226
227     public InvokeExpr getInvokeExpr() {
228         throw new RuntimeException JavaDoc(
229                 "getInvokeExpr() called with no invokeExpr present!");
230     }
231
232     public ValueBox getInvokeExprBox() {
233         throw new RuntimeException JavaDoc(
234                 "getInvokeExprBox() called with no invokeExpr present!");
235     }
236
237     public boolean containsArrayRef() {
238         return false;
239     }
240
241     public ArrayRef getArrayRef() {
242         throw new RuntimeException JavaDoc(
243                 "getArrayRef() called with no ArrayRef present!");
244     }
245
246     public ValueBox getArrayRefBox() {
247         throw new RuntimeException JavaDoc(
248                 "getArrayRefBox() called with no ArrayRef present!");
249     }
250
251     public boolean containsFieldRef() {
252         return false;
253     }
254
255     public FieldRef getFieldRef() {
256         throw new RuntimeException JavaDoc(
257                 "getFieldRef() called with no FieldRef present!");
258     }
259
260     public ValueBox getFieldRefBox() {
261         throw new RuntimeException JavaDoc(
262                 "getFieldRefBox() called with no FieldRef present!");
263     }
264
265 }
266
Popular Tags