KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > invoke > ThrowManager


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam, Raja Vallee-Rai
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.jimple.toolkits.invoke;
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 ThrowManager
35 {
36     /** Iterate through the statements in b (starting at the end), returning
37      * the last instance of the following pattern:
38      *
39      * r928 = new java.lang.NullPointerException;
40      * specialinvoke r928."<init>"();
41      * throw r928;
42      *
43      * Creates if necessary.
44      */

45         
46     public static Stmt getNullPointerExceptionThrower(JimpleBody b)
47     {
48         Chain units = b.getUnits();
49         Set trappedUnits = TrapManager.getTrappedUnitsOf(b);
50         
51         for (Stmt s = (Stmt)units.getLast(); s != units.getFirst();
52              s = (Stmt)units.getPredOf(s))
53         {
54             if (trappedUnits.contains(s))
55                 continue;
56             if (s instanceof ThrowStmt)
57             {
58                 Value throwee = ((ThrowStmt)s).getOp();
59                 if (throwee instanceof Constant)
60                     continue;
61
62                 if (s == units.getFirst())
63                     break;
64                 Stmt prosInvoke = (Stmt)units.getPredOf(s);
65
66                 if (!(prosInvoke instanceof InvokeStmt))
67                     continue;
68
69                 if (prosInvoke == units.getFirst())
70                     break;
71                 Stmt prosNew = (Stmt)units.getPredOf(prosInvoke);
72
73                 if (!(prosNew instanceof AssignStmt))
74                     continue;
75
76                 InvokeExpr ie = (InvokeExpr)((InvokeStmt)prosInvoke).getInvokeExpr();
77                 if (!(ie instanceof SpecialInvokeExpr))
78                     continue;
79
80                 if (((SpecialInvokeExpr)ie).getBase() != throwee ||
81                     !ie.getMethodRef().name().equals("<init>"))
82                     continue;
83
84                 Value lo = ((AssignStmt)prosNew).getLeftOp();
85                 Value ro = ((AssignStmt)prosNew).getRightOp();
86                 if (lo != throwee || !(ro instanceof NewExpr))
87                     continue;
88
89                 Type newType = ((NewExpr)ro).getBaseType();
90                 if (!newType.equals(RefType.v("java.lang.NullPointerException")))
91                     continue;
92
93                 // Whew!
94
return prosNew;
95             }
96         }
97
98         // Create.
99
Stmt last = (Stmt)units.getLast();
100
101         return addThrowAfter(b, last);
102     }
103
104     static Stmt addThrowAfter(JimpleBody b, Stmt target)
105     {
106         Chain units = b.getUnits();
107         Chain locals = b.getLocals();
108         int i = 0;
109         
110         // Bah!
111
boolean canAddI = false;
112         do
113         {
114             canAddI = true;
115             Iterator localIt = locals.iterator();
116             while (localIt.hasNext())
117             {
118                 Local l = (Local)localIt.next();
119                 if (l.getName().equals("__throwee"+i))
120                     canAddI = false;
121             }
122             if (!canAddI)
123                 i++;
124         }
125         while (!canAddI);
126
127         Local l = Jimple.v().newLocal("__throwee"+i, RefType.v("java.lang.NullPointerException"));
128         b.getLocals().add(l);
129
130         Stmt newStmt = Jimple.v().newAssignStmt
131             (l, Jimple.v().newNewExpr(RefType.v("java.lang.NullPointerException")));
132
133         Stmt invStmt = Jimple.v().newInvokeStmt
134             (Jimple.v().newSpecialInvokeExpr(l, Scene.v().getMethod("<java.lang.NullPointerException: void <init>()>").makeRef()));
135         
136         Stmt throwStmt = Jimple.v().newThrowStmt(l);
137
138         units.insertAfter(newStmt, target);
139         units.insertAfter(invStmt, newStmt);
140         units.insertAfter(throwStmt, invStmt);
141         return newStmt;
142     }
143
144     /** If exception e is caught at stmt s in body b, return the handler;
145      * otherwise, return null. */

146     static boolean isExceptionCaughtAt(SootClass e, Stmt stmt, Body b)
147     {
148         /* Look through the traps t of b, checking to see if:
149          * - caught exception is e;
150          * - and, stmt lies between t.beginUnit and t.endUnit */

151
152         Hierarchy h = new Hierarchy();
153
154         Iterator trapsIt = b.getTraps().iterator();
155
156         while (trapsIt.hasNext())
157         {
158             Trap t = (Trap)trapsIt.next();
159
160             /* Ah ha, we might win. */
161             if (h.isClassSubclassOfIncluding(e, t.getException()))
162             {
163                 Iterator it = b.getUnits().iterator(t.getBeginUnit(),
164                                                     t.getEndUnit());
165                 while (it.hasNext())
166                     if (stmt.equals(it.next()))
167                         return true;
168             }
169         }
170
171         return false;
172     }
173 }
174
Popular Tags