KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam
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  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31 package soot.jimple.internal;
32
33 import soot.tagkit.*;
34 import soot.*;
35 import soot.jimple.*;
36 import soot.baf.*;
37 import soot.util.*;
38 import java.util.*;
39
40 public class JLookupSwitchStmt extends AbstractStmt
41     implements LookupSwitchStmt
42 {
43     UnitBox defaultTargetBox;
44     ValueBox keyBox;
45     /** List of lookup values from the corresponding bytecode instruction,
46      * represented as IntConstants. */

47     List lookupValues;
48     protected UnitBox[] targetBoxes;
49
50     List stmtBoxes;
51
52     // This method is necessary to deal with constructor-must-be-first-ism.
53
private static UnitBox[] getTargetBoxesArray(List targets)
54     {
55         UnitBox[] targetBoxes = new UnitBox[targets.size()];
56
57         for(int i = 0; i < targetBoxes.length; i++)
58             targetBoxes[i] = Jimple.v().newStmtBox((Stmt) targets.get(i));
59
60         return targetBoxes;
61     }
62
63
64     private static UnitBox[] unitBoxListToArray(List targets) {
65         UnitBox[] targetBoxes = new UnitBox[targets.size()];
66         
67         for(int i = 0; i < targetBoxes.length; i++)
68             targetBoxes[i] = (UnitBox) targets.get(i);
69         return targetBoxes;
70     }
71
72     /** Constructs a new JLookupSwitchStmt. lookupValues should be a list of IntConst s. */
73     public JLookupSwitchStmt(Value key, List lookupValues, List targets, Unit defaultTarget)
74     {
75         this(Jimple.v().newImmediateBox(key),
76              lookupValues, getTargetBoxesArray(targets),
77              Jimple.v().newStmtBox(defaultTarget));
78     }
79
80     /** Constructs a new JLookupSwitchStmt. lookupValues should be a list of IntConst s. */
81     public JLookupSwitchStmt(Value key, List lookupValues, List targets, UnitBox defaultTarget)
82     {
83         this(Jimple.v().newImmediateBox(key),
84              lookupValues, unitBoxListToArray(targets),
85              defaultTarget);
86     }
87
88
89     public Object JavaDoc clone()
90     {
91         int lookupValueCount = lookupValues.size();
92         List clonedLookupValues = new ArrayList(lookupValueCount);
93
94         for( int i = 0; i < lookupValueCount ;i++) {
95             clonedLookupValues.add(i, IntConstant.v(getLookupValue(i)));
96         }
97         
98         return new JLookupSwitchStmt(getKey(), clonedLookupValues, getTargets(), getDefaultTarget());
99     }
100
101
102     protected JLookupSwitchStmt(ValueBox keyBox, List lookupValues,
103                                 UnitBox[] targetBoxes,
104                                 UnitBox defaultTargetBox)
105     {
106         this.keyBox = keyBox;
107         this.defaultTargetBox = defaultTargetBox;
108         this.targetBoxes = targetBoxes;
109
110         this.lookupValues = new ArrayList();
111         this.lookupValues.addAll(lookupValues);
112
113         // Build up stmtBoxes
114
{
115             stmtBoxes = new ArrayList();
116
117             for(int i = 0; i < targetBoxes.length; i++)
118                 stmtBoxes.add(targetBoxes[i]);
119
120             stmtBoxes.add(defaultTargetBox);
121             stmtBoxes = Collections.unmodifiableList(stmtBoxes);
122         }
123     }
124
125     public String JavaDoc toString()
126     {
127         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
128         String JavaDoc endOfLine = " ";
129         
130         buffer.append(Jimple.v().LOOKUPSWITCH + "(" +
131             keyBox.getValue().toString() + ")" + endOfLine);
132             
133         buffer.append("{" + endOfLine);
134         
135         for(int i = 0; i < lookupValues.size(); i++)
136         {
137             buffer.append(" " + Jimple.v().CASE + " " + lookupValues.get(i) + ": " + Jimple.v().GOTO + " " +
138                 getTarget(i) + ";" + endOfLine);
139         }
140
141         buffer.append(" " + Jimple.v().DEFAULT + ": " + Jimple.v().GOTO +
142                       " " + getDefaultTarget() + ";" + endOfLine);
143         buffer.append("}");
144
145         return buffer.toString();
146     }
147     
148     public void toString(UnitPrinter up)
149     {
150         up.literal(Jimple.v().LOOKUPSWITCH);
151         up.literal("(");
152         keyBox.toString(up);
153         up.literal(")");
154         up.newline();
155         up.literal("{");
156         up.newline();
157         for(int i = 0; i < lookupValues.size(); i++) {
158             up.literal(" ");
159             up.literal(Jimple.v().CASE);
160             up.literal(" ");
161             up.constant((Constant)lookupValues.get(i));
162             up.literal(": ");
163             up.literal(Jimple.v().GOTO);
164             up.literal(" ");
165             targetBoxes[i].toString(up);
166             up.literal(";");
167             up.newline();
168         }
169         
170         up.literal(" ");
171         up.literal(Jimple.v().DEFAULT);
172         up.literal(": ");
173         up.literal(Jimple.v().GOTO);
174         up.literal(" ");
175         defaultTargetBox.toString(up);
176         up.literal(";");
177         up.newline();
178         up.literal("}");
179     }
180
181     public Unit getDefaultTarget()
182     {
183         return defaultTargetBox.getUnit();
184     }
185
186     public void setDefaultTarget(Unit defaultTarget)
187     {
188         defaultTargetBox.setUnit(defaultTarget);
189     }
190
191     public UnitBox getDefaultTargetBox()
192     {
193         return defaultTargetBox;
194     }
195
196     public Value getKey()
197     {
198         return keyBox.getValue();
199     }
200
201     public void setKey(Value key)
202     {
203         keyBox.setValue(key);
204     }
205
206     public ValueBox getKeyBox()
207     {
208         return keyBox;
209     }
210
211     public void setLookupValues(List lookupValues)
212     {
213         this.lookupValues = new ArrayList();
214         this.lookupValues.addAll(lookupValues);
215     }
216
217     public void setLookupValue(int index, int value)
218     {
219         this.lookupValues.set(index, IntConstant.v(value));
220     }
221
222
223     public int getLookupValue(int index)
224     {
225         return ((IntConstant)lookupValues.get(index)).value;
226     }
227
228     public List getLookupValues()
229     {
230         return Collections.unmodifiableList(lookupValues);
231     }
232
233     public int getTargetCount()
234     {
235         return targetBoxes.length;
236     }
237
238     public Unit getTarget(int index)
239     {
240         return targetBoxes[index].getUnit();
241     }
242
243     public UnitBox getTargetBox(int index)
244     {
245         return targetBoxes[index];
246     }
247
248     public void setTarget(int index, Unit target)
249     {
250         targetBoxes[index].setUnit(target);
251     }
252
253     public List getTargets()
254     {
255         List targets = new ArrayList();
256
257         for(int i = 0; i < targetBoxes.length; i++)
258             targets.add(targetBoxes[i].getUnit());
259
260         return targets;
261     }
262
263     public void setTargets(Unit[] targets)
264     {
265         for(int i = 0; i < targets.length; i++)
266             targetBoxes[i].setUnit(targets[i]);
267     }
268
269     public List getUseBoxes()
270     {
271         List list = new ArrayList();
272
273         list.addAll(keyBox.getValue().getUseBoxes());
274         list.add(keyBox);
275
276         return list;
277     }
278
279     public List getUnitBoxes()
280     {
281         return stmtBoxes;
282     }
283
284     public void apply(Switch sw)
285     {
286       ((StmtSwitch) sw).caseLookupSwitchStmt(this);
287     }
288     
289     public void convertToBaf(JimpleToBafContext context, List out)
290     {
291         ArrayList targetPlaceholders = new ArrayList();
292
293         ((ConvertToBaf)(getKey())).convertToBaf(context, out);
294
295         for (int i = 0; i < targetBoxes.length; i++)
296         {
297             targetPlaceholders.add(Baf.v().newPlaceholderInst
298                                    (getTarget(i)));
299         }
300     
301     Unit u;
302         out.add(u = Baf.v().newLookupSwitchInst
303                 (Baf.v().newPlaceholderInst(getDefaultTarget()),
304                  getLookupValues(), targetPlaceholders));
305
306     Unit currentUnit = this;
307
308     Iterator it = currentUnit.getTags().iterator();
309     while(it.hasNext()) {
310         u.addTag((Tag) it.next());
311     }
312     
313
314     }
315
316
317
318
319     
320
321     public boolean fallsThrough(){return false;}
322     public boolean branches(){return true;}
323
324 }
325
326
Popular Tags