KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > internal > AST > ASTSwitchNode


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
3  * Copyright (C) 2005 Nomair A. Naeem
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 package soot.dava.internal.AST;
22
23 import soot.*;
24 import java.util.*;
25 import soot.jimple.*;
26 import soot.dava.internal.SET.*;
27 import soot.dava.toolkits.base.AST.*;
28 import soot.dava.toolkits.base.AST.analysis.*;
29
30 public class ASTSwitchNode extends ASTLabeledNode
31 {
32     private ValueBox keyBox;
33     private List indexList;
34     private Map index2BodyList;
35
36     public ASTSwitchNode( SETNodeLabel label, Value key, List indexList, Map index2BodyList)
37     {
38     super( label);
39
40     this.keyBox = Jimple.v().newRValueBox( key );
41     this.indexList = indexList;
42     this.index2BodyList = index2BodyList;
43
44     Iterator it = indexList.iterator();
45     while (it.hasNext()) {
46         List body = (List) index2BodyList.get( it.next());
47         
48         if (body != null)
49         subBodies.add( body);
50     }
51     }
52
53     /*
54       Nomair A. Naeem 22-FEB-2005
55       Added for ASTCleaner
56     */

57     public List getIndexList(){
58     return indexList;
59     }
60
61     public Map getIndex2BodyList(){
62     return index2BodyList;
63     }
64
65     public void replaceIndex2BodyList(Map index2BodyList){
66     this.index2BodyList=index2BodyList;
67
68     subBodies = new ArrayList();
69     Iterator it = indexList.iterator();
70     while (it.hasNext()) {
71         List body = (List) index2BodyList.get( it.next());
72         
73         if (body != null)
74         subBodies.add( body);
75     }
76     }
77
78
79
80
81     public ValueBox getKeyBox(){
82     return keyBox;
83     }
84     
85
86
87     public Value get_Key()
88     {
89     return keyBox.getValue();
90     }
91
92     public void set_Key(Value key){
93     this.keyBox = Jimple.v().newRValueBox( key );
94     }
95
96
97
98     public Object JavaDoc clone()
99     {
100     return new ASTSwitchNode( get_Label(), get_Key(), indexList, index2BodyList);
101     }
102
103     public void perform_Analysis( ASTAnalysis a)
104     {
105     ASTWalker.v().walk_value( a, get_Key());
106
107     if (a instanceof TryContentsFinder) {
108         TryContentsFinder tcf = (TryContentsFinder) a;
109         tcf.v().add_ExceptionSet( this, tcf.v().remove_CurExceptionSet());
110     }
111   
112     perform_AnalysisOnSubBodies( a);
113     }
114
115
116     public void toString( UnitPrinter up )
117     {
118         label_toString( up );
119
120         up.literal( "switch" );
121         up.literal( " " );
122         up.literal( "(" );
123         keyBox.toString( up );
124         up.literal( ")" );
125         up.newline();
126
127         up.literal( "{" );
128         up.newline();
129
130     Iterator it = indexList.iterator();
131     while (it.hasNext()) {
132         
133         Object JavaDoc index = it.next();
134
135             up.incIndent();
136         
137         if (index instanceof String JavaDoc)
138                 up.literal( "default" );
139
140         else {
141                 up.literal( "case" );
142                 up.literal( " " );
143                 up.literal( index.toString() );
144         }
145         
146             up.literal( ":" );
147             up.newline();
148
149         List subBody = (List) index2BodyList.get( index);
150
151         if (subBody != null) {
152                 up.incIndent();
153                 body_toString( up, subBody );
154         
155         if (it.hasNext())
156             up.newline();
157                 up.decIndent();
158         }
159             up.decIndent();
160     }
161
162     up.literal( "}");
163         up.newline();
164     }
165
166     public String JavaDoc toString()
167     {
168     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
169     
170     b.append( label_toString( ));
171     
172     b.append( "switch (");
173     b.append( get_Key() );
174     b.append( ")");
175     b.append( NEWLINE);
176
177     b.append( "{");
178     b.append( NEWLINE);
179
180     Iterator it = indexList.iterator();
181     while (it.hasNext()) {
182         
183         Object JavaDoc index = it.next();
184
185         b.append( TAB);
186         
187         if (index instanceof String JavaDoc)
188         b.append( "default");
189
190         else {
191         b.append( "case ");
192         b.append( ((Integer JavaDoc) index).toString());
193         }
194         
195         b.append( ":");
196         b.append( NEWLINE);
197
198         List subBody = (List) index2BodyList.get( index);
199
200         if (subBody != null) {
201         b.append( body_toString(subBody));
202         
203         if (it.hasNext())
204             b.append( NEWLINE);
205         }
206     }
207
208     b.append( "}");
209     b.append( NEWLINE);
210
211     return b.toString();
212     }
213
214
215
216     /*
217       Nomair A. Naeem, 7-FEB-05
218       Part of Visitor Design Implementation for AST
219       See: soot.dava.toolkits.base.AST.analysis For details
220     */

221     public void apply(Analysis a){
222     a.caseASTSwitchNode(this);
223     }
224 }
225
Popular Tags