KickJava   Java API By Example, From Geeks To Geeks.

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


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 ASTTryNode extends ASTLabeledNode
31 {
32     private List tryBody, catchList;
33     private Map exceptionMap, paramMap;
34     private container tryBodyContainer;
35
36     public class container
37     {
38     public Object JavaDoc o;
39
40     public container( Object JavaDoc o)
41     {
42         this.o = o;
43     }
44
45     public void replaceBody(Object JavaDoc newBody){
46         this.o=newBody;
47     }
48     }
49
50     public ASTTryNode( SETNodeLabel label, List tryBody, List catchList, Map exceptionMap, Map paramMap)
51     {
52     super( label);
53
54     this.tryBody = tryBody;
55     tryBodyContainer = new container( tryBody);
56
57     this.catchList = new ArrayList();
58     Iterator cit = catchList.iterator();
59     while (cit.hasNext())
60         this.catchList.add( new container( cit.next()));
61     
62     this.exceptionMap = new HashMap();
63     cit = this.catchList.iterator();
64     while (cit.hasNext()) {
65         container c = (container) cit.next();
66         this.exceptionMap.put( c, exceptionMap.get( c.o));
67     }
68
69     this.paramMap = new HashMap();
70     cit = this.catchList.iterator();
71     while (cit.hasNext()) {
72         container c = (container) cit.next();
73         this.paramMap.put( c, paramMap.get( c.o));
74     }
75     
76     subBodies.add( tryBodyContainer);
77     cit = this.catchList.iterator();
78     while (cit.hasNext())
79         subBodies.add( cit.next());
80     }
81
82
83     /*
84       Nomair A Naeem 21-FEB-2005
85       used to support UselessLabeledBlockRemover
86     */

87     public void replaceTryBody(List tryBody){
88     this.tryBody = tryBody;
89     tryBodyContainer = new container( tryBody);
90     
91     List oldSubBodies=subBodies;
92     subBodies=new ArrayList();
93
94     subBodies.add( tryBodyContainer);
95
96     Iterator oldIt = oldSubBodies.iterator();
97     //discard the first since that was the old tryBodyContainer
98
oldIt.next();
99
100     while (oldIt.hasNext())
101         subBodies.add( oldIt.next());
102     
103     }
104
105     protected void perform_AnalysisOnSubBodies( ASTAnalysis a)
106     {
107     if (a instanceof TryContentsFinder) {
108         TryContentsFinder tcf = (TryContentsFinder) a;
109
110         Iterator sbit = subBodies.iterator();
111         while (sbit.hasNext()) {
112         container subBody = (container) sbit.next();
113
114         Iterator it = ((List) subBody.o).iterator();
115         while (it.hasNext()) {
116             ASTNode n = (ASTNode) it.next();
117             
118             n.perform_Analysis( a);
119             tcf.v().add_ExceptionSet( subBody, tcf.v().get_ExceptionSet( n));
120         }
121         }
122
123         a.analyseASTNode( this);
124     }
125     else
126         super.perform_AnalysisOnSubBodies( a);
127     }
128
129     public boolean isEmpty()
130     {
131     return tryBody.isEmpty();
132     }
133
134     public List get_TryBody()
135     {
136     return tryBody;
137     }
138
139     public container get_TryBodyContainer()
140     {
141     return tryBodyContainer;
142     }
143
144     public List get_CatchList()
145     {
146     return catchList;
147     }
148
149     public Map get_ExceptionMap()
150     {
151     return exceptionMap;
152     }
153
154
155     /*
156       Nomair A. Naeem 08-FEB-2005
157       Needed for call from DepthFirstAdapter
158     */

159     public Map get_ParamMap(){
160     return paramMap;
161     }
162
163     public Set get_ExceptionSet()
164     {
165     HashSet s = new HashSet();
166
167     Iterator it = catchList.iterator();
168     while (it.hasNext())
169         s.add( exceptionMap.get( it.next()));
170
171     return s;
172     }
173
174     public Object JavaDoc clone()
175     {
176     ArrayList newCatchList = new ArrayList();
177     Iterator it = catchList.iterator();
178     while (it.hasNext())
179         newCatchList.add( ((container) it.next()).o);
180
181     return new ASTTryNode( get_Label(), tryBody, newCatchList, exceptionMap, paramMap);
182     }
183
184     public void toString( UnitPrinter up )
185     {
186         label_toString( up );
187
188     up.literal( "try");
189         up.newline();
190     
191         up.literal( "{" );
192         up.newline();
193
194         up.incIndent();
195     body_toString( up, tryBody );
196         up.decIndent();
197
198         up.literal( "}" );
199         up.newline();
200
201
202
203     Iterator cit = catchList.iterator();
204     while (cit.hasNext()) {
205         container catchBody = (container) cit.next();
206
207             up.literal( "catch" );
208             up.literal( " " );
209             up.literal( "(" );
210             up.type( ((SootClass) exceptionMap.get(catchBody)).getType() );
211         up.literal( " ");
212         up.local( (Local) paramMap.get(catchBody) );
213         up.literal( ")");
214             up.newline();
215
216             up.literal("{");
217             up.newline();
218
219             up.incIndent();
220             body_toString( up, (List) catchBody.o );
221             up.decIndent();
222
223             up.literal( "}" );
224             up.newline();
225     }
226     }
227
228     public String JavaDoc toString()
229     {
230     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
231     
232     b.append( label_toString( ));
233
234     b.append( "try");
235     b.append( NEWLINE);
236     
237     b.append( "{");
238     b.append( NEWLINE);
239
240     b.append( body_toString( tryBody));
241
242     b.append( "}");
243     b.append( NEWLINE);
244
245     Iterator cit = catchList.iterator();
246     while (cit.hasNext()) {
247         container catchBody = (container) cit.next();
248
249         b.append( "catch (");
250         b.append( ((SootClass) exceptionMap.get( catchBody)).getName());
251         b.append( " ");
252         b.append( ((Local) paramMap.get( catchBody)).getName());
253         b.append( ")");
254         b.append( NEWLINE);
255
256         b.append( "{");
257         b.append( NEWLINE);
258
259         b.append( body_toString( (List) catchBody.o));
260
261         b.append( "}");
262         b.append( NEWLINE);
263     }
264
265     return b.toString();
266     }
267
268
269     /*
270       Nomair A. Naeem, 7-FEB-05
271       Part of Visitor Design Implementation for AST
272       See: soot.dava.toolkits.base.AST.analysis For details
273     */

274     public void apply(Analysis a){
275     a.caseASTTryNode(this);
276     }
277 }
278
Popular Tags