KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > finders > ExceptionNode


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
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 package soot.dava.toolkits.base.finders;
21
22 import soot.*;
23 import java.util.*;
24 import soot.dava.*;
25 import soot.util.*;
26 import soot.dava.internal.asg.*;
27
28 public class ExceptionNode
29 {
30     private IterableSet body, tryBody, catchBody;
31     private boolean dirty;
32     private LinkedList exitList, catchList;
33     private SootClass exception;
34     private HashMap catch2except;
35     private AugmentedStmt handlerAugmentedStmt;
36
37     public ExceptionNode( IterableSet tryBody, SootClass exception, AugmentedStmt handlerAugmentedStmt)
38     {
39     this.tryBody = tryBody;
40     this.catchBody = null;
41     this.exception = exception;
42     this.handlerAugmentedStmt = handlerAugmentedStmt;
43
44     body = new IterableSet();
45     body.addAll( tryBody);
46
47     dirty = true;
48     exitList = null;
49     catchList = null;
50
51     catch2except = null;
52     }
53
54     public boolean add_TryStmts( Collection c)
55     {
56     Iterator it = c.iterator();
57     while (it.hasNext())
58         if (add_TryStmt( (AugmentedStmt) it.next()) == false)
59         return false;
60
61     return true;
62     }
63
64     public boolean add_TryStmt( AugmentedStmt as)
65     {
66     if ((body.contains( as)) || (tryBody.contains( as)))
67         return false;
68     
69     body.add( as);
70     tryBody.add( as);
71
72     return true;
73     }
74
75     public void refresh_CatchBody( ExceptionFinder ef)
76     {
77     if (catchBody != null)
78         body.removeAll( catchBody);
79
80     catchBody = ef.get_CatchBody( handlerAugmentedStmt);
81     body.addAll( catchBody);
82     }
83
84
85     public IterableSet get_Body()
86     {
87     return body;
88     }
89     
90     public IterableSet get_TryBody()
91     {
92     return tryBody;
93     }
94
95     public IterableSet get_CatchBody()
96     {
97     return catchBody;
98     }
99
100     public boolean remove( AugmentedStmt as)
101     {
102     if (body.contains( as) == false)
103         return false;
104
105     if (tryBody.contains( as))
106         tryBody.remove( as);
107     else if ((catchBody != null) && (catchBody.contains( as))) {
108         catchBody.remove( as);
109         dirty = true;
110     }
111     else
112         return false;
113
114     body.remove( as);
115
116     return true;
117     }
118
119     public List get_CatchExits()
120     {
121     if (catchBody == null)
122         return null;
123
124     if (dirty) {
125         exitList = new LinkedList();
126         dirty = false;
127
128         Iterator it = catchBody.iterator();
129         while (it.hasNext()) {
130         AugmentedStmt as = (AugmentedStmt) it.next();
131
132         Iterator sit = as.bsuccs.iterator();
133         while (sit.hasNext())
134             if (catchBody.contains( sit.next()) == false) {
135             exitList.add( as);
136             break;
137             }
138         }
139     }
140     
141     return exitList;
142     }
143
144     public void splitOff_ExceptionNode( IterableSet newTryBody, AugmentedStmtGraph asg, IterableSet enlist)
145     {
146     IterableSet oldTryBody = new IterableSet();
147     oldTryBody.addAll( tryBody);
148
149     IterableSet oldBody = new IterableSet();
150     oldBody.addAll( body);
151
152     Iterator it = newTryBody.iterator();
153     while (it.hasNext()) {
154         AugmentedStmt as = (AugmentedStmt) it.next();
155         if (remove( as) == false) {
156
157         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
158         it = newTryBody.iterator();
159         while (it.hasNext())
160             b.append( "\n" + ((AugmentedStmt) it.next()).toString());
161         b.append( "\n-");
162
163         it = oldTryBody.iterator();
164         while (it.hasNext())
165             b.append( "\n" + ((AugmentedStmt) it.next()).toString());
166         b.append( "\n-");
167
168         it = oldBody.iterator();
169         while (it.hasNext())
170             b.append( "\n" + ((AugmentedStmt) it.next()).toString());
171         b.append( "\n-");
172         
173         throw new RuntimeException JavaDoc( "Tried to split off a new try body that isn't in the old one.\n"+ as +"\n - " + b.toString());
174         }
175     }
176
177     asg.clone_Body( catchBody);
178
179     AugmentedStmt
180         oldCatchTarget = handlerAugmentedStmt,
181         newCatchTarget = asg.get_CloneOf( handlerAugmentedStmt);
182
183     Iterator tbit = newTryBody.iterator();
184     while (tbit.hasNext()) {
185         AugmentedStmt as = (AugmentedStmt) tbit.next();
186
187         as.remove_CSucc( oldCatchTarget);
188         oldCatchTarget.remove_CPred( as);
189     }
190
191     tbit = tryBody.iterator();
192     while (tbit.hasNext()) {
193         AugmentedStmt as = (AugmentedStmt) tbit.next();
194
195         as.remove_CSucc( newCatchTarget);
196         newCatchTarget.remove_CPred( as);
197     }
198     
199     Iterator enlit = enlist.snapshotIterator();
200     while (enlit.hasNext()) {
201         ExceptionNode en = (ExceptionNode) enlit.next();
202         
203         if (this == en)
204         continue;
205         
206         if (catchBody.isSupersetOf( en.get_Body())) {
207         
208         IterableSet clonedTryBody = new IterableSet();
209         
210         Iterator trit = en.get_TryBody().iterator();
211         while (trit.hasNext())
212             clonedTryBody.add( asg.get_CloneOf( (AugmentedStmt) trit.next()));
213         
214         enlist.addLast( new ExceptionNode( clonedTryBody, en.exception, asg.get_CloneOf( en.handlerAugmentedStmt)));
215         }
216     }
217
218     enlist.addLast( new ExceptionNode( newTryBody, exception, asg.get_CloneOf( handlerAugmentedStmt)));
219
220     enlit = enlist.iterator();
221     while (enlit.hasNext())
222         ((ExceptionNode) enlit.next()).refresh_CatchBody( ExceptionFinder.v());
223     
224     asg.find_Dominators();
225     }
226
227     public void add_CatchBody( ExceptionNode other)
228     {
229     if (other.get_CatchList() == null) {
230         add_CatchBody( other.get_CatchBody(), other.get_Exception());
231         return;
232     }
233
234     Iterator it = other.get_CatchList().iterator();
235     while (it.hasNext()) {
236         IterableSet c = (IterableSet) it.next();
237
238         add_CatchBody( c, other.get_Exception( c));
239     }
240     }
241
242     public void add_CatchBody( IterableSet newCatchBody, SootClass except)
243     {
244     if (catchList == null) {
245         catchList = new LinkedList();
246         catchList.addLast( catchBody);
247
248         catch2except = new HashMap();
249         catch2except.put( catchBody, exception);
250     }
251
252     body.addAll( newCatchBody);
253     catchList.addLast( newCatchBody);
254     catch2except.put( newCatchBody, except);
255     }
256
257     public List get_CatchList()
258     {
259     List l = catchList;
260
261     if (l == null) {
262         l = new LinkedList();
263         l.add( catchBody);
264     }
265
266     return l;
267     }
268
269     public Map get_ExceptionMap()
270     {
271     Map m = catch2except;
272
273     if (m == null) {
274         m = new HashMap();
275         m.put( catchBody, exception);
276     }
277     
278     return m;
279     }
280
281     public SootClass get_Exception()
282     {
283     return exception;
284     }
285
286     public SootClass get_Exception( IterableSet catchBody)
287     {
288     if (catch2except == null)
289         return exception;
290
291     return (SootClass) catch2except.get( catchBody);
292     }
293
294     public void dump()
295     {
296     G.v().out.println("try {");
297     Iterator tit = get_TryBody().iterator();
298     while (tit.hasNext())
299         G.v().out.println( "\t" + tit.next());
300     G.v().out.println( "}");
301     
302     Iterator cit = get_CatchList().iterator();
303     while (cit.hasNext()) {
304         IterableSet catchBody = (IterableSet) cit.next();
305         
306         G.v().out.println( "catch " + get_ExceptionMap().get( catchBody) + " {");
307         Iterator cbit = catchBody.iterator();
308         while (cbit.hasNext())
309         G.v().out.println( "\t" + cbit.next());
310         G.v().out.println("}");
311         
312     }
313     }
314 }
315
Popular Tags