KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > TrapManager


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 package soot;
27
28 import soot.*;
29 import soot.jimple.*;
30 import soot.util.*;
31 import java.util.*;
32
33 /** Utility methods for dealing with traps. */
34 public class TrapManager
35 {
36     /** If exception e is caught at unit u in body b, return true;
37      * otherwise, return false. */

38     public static boolean isExceptionCaughtAt(SootClass e, Unit u, Body b)
39     {
40         /* Look through the traps t of b, checking to see if:
41          * - caught exception is e;
42          * - and, unit lies between t.beginUnit and t.endUnit */

43
44         Hierarchy h = Scene.v().getActiveHierarchy();
45         Chain units = b.getUnits();
46         Iterator trapsIt = b.getTraps().iterator();
47
48         while (trapsIt.hasNext())
49         {
50             Trap t = (Trap)trapsIt.next();
51
52             /* Ah ha, we might win. */
53             if (h.isClassSubclassOfIncluding(e, t.getException()))
54             {
55                 Iterator it = units.iterator(t.getBeginUnit(),
56                                              units.getPredOf(t.getEndUnit()));
57                 while (it.hasNext())
58                     if (u.equals(it.next()))
59                         return true;
60             }
61         }
62
63         return false;
64     }
65
66     /** Returns the list of traps caught at Unit u in Body b. */
67     public static List getTrapsAt(Unit unit, Body b)
68     {
69         List trapsList = new ArrayList();
70         Chain units = b.getUnits();
71
72         Iterator trapsIt = b.getTraps().iterator();
73
74         while (trapsIt.hasNext())
75         {
76             Trap t = (Trap)trapsIt.next();
77
78             Iterator it = units.iterator(t.getBeginUnit(),
79                                          units.getPredOf(t.getEndUnit()));
80             while (it.hasNext())
81                 if (unit.equals(it.next()))
82                     trapsList.add(t);
83         }
84
85         return trapsList;
86     }
87
88     /** Returns a set of units which lie inside the range of any trap. */
89     public static Set getTrappedUnitsOf(Body b)
90     {
91         Set trapsSet = new HashSet();
92         Chain units = b.getUnits();
93
94         Iterator trapsIt = b.getTraps().iterator();
95
96         while (trapsIt.hasNext())
97         {
98                 Trap t = (Trap)trapsIt.next();
99
100             Iterator it = units.iterator(t.getBeginUnit(),
101                                          units.getPredOf(t.getEndUnit()));
102             while (it.hasNext())
103                 trapsSet.add(it.next());
104         }
105         return trapsSet;
106     }
107
108     /** Splits all traps so that they do not cross the range rangeStart - rangeEnd.
109      * Note that rangeStart is inclusive, rangeEnd is exclusive. */

110     public static void splitTrapsAgainst(Body b, Unit rangeStart, Unit rangeEnd)
111     {
112         Chain traps = b.getTraps(), units = b.getUnits();
113         Iterator trapsIt = traps.snapshotIterator();
114         
115         while (trapsIt.hasNext())
116         {
117             Trap t = (Trap)trapsIt.next();
118             
119             Iterator unitIt = units.iterator(t.getBeginUnit(),
120                                              t.getEndUnit());
121
122             boolean insideRange = false;
123
124             while (unitIt.hasNext())
125             {
126                 Unit u = (Unit)unitIt.next();
127                 if (u.equals(rangeStart))
128                     insideRange = true;
129                 if (!unitIt.hasNext()) // i.e. u.equals(t.getEndUnit())
130
{
131                     if (insideRange)
132                     {
133                         Trap newTrap = (Trap)t.clone();
134                         t.setBeginUnit(rangeStart);
135                         newTrap.setEndUnit(rangeStart);
136                         traps.insertAfter(newTrap, t);
137                     }
138                     else
139                         break;
140                 }
141                 if (u.equals(rangeEnd))
142                 {
143                     // insideRange had better be true now.
144
if (!insideRange)
145                         throw new RuntimeException JavaDoc("inversed range?");
146                     Trap firstTrap = (Trap)t.clone();
147                     Trap secondTrap = (Trap)t.clone();
148                     firstTrap.setEndUnit(rangeStart);
149                     secondTrap.setBeginUnit(rangeStart);
150                     secondTrap.setEndUnit(rangeEnd);
151                     t.setBeginUnit(rangeEnd);
152
153                     traps.insertAfter(firstTrap, t);
154                     traps.insertAfter(secondTrap, t);
155                 }
156             }
157         }
158     }
159
160     /** Given a body and a unit handling an exception,
161      * returns the list of exception types possibly caught
162      * by the handler. */

163     public static List getExceptionTypesOf(Unit u, Body body)
164     {
165          List possibleTypes = new ArrayList();
166         
167          Iterator trapIt = body.getTraps().iterator();
168         
169          while(trapIt.hasNext())
170          {
171             Trap trap = (Trap) trapIt.next();
172             
173             Unit handler = trap.getHandlerUnit();
174              
175             if(handler == u)
176             {
177                 possibleTypes.add(RefType.v(trap.getException().
178                     getName()));
179             }
180         }
181         
182         return possibleTypes;
183     }
184 }
185
Popular Tags