KickJava   Java API By Example, From Geeks To Geeks.

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


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