KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > exceptions > AbstractThrowAnalysis


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 John Jorgensen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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.toolkits.exceptions;
21
22 import soot.AnySubType;
23 import soot.G;
24 import soot.RefType;
25 import soot.Type;
26 import soot.Unit;
27 import soot.UnknownType;
28 import soot.NullType;
29 import soot.Value;
30 import soot.Singletons;
31 import soot.toolkits.exceptions.*;
32 import soot.baf.ThrowInst;
33 import soot.grimp.NewInvokeExpr;
34 import soot.jimple.ThrowStmt;
35
36 /**
37  * Abstract class implementing parts of the {@link ThrowAnalysis}
38  * interface which may be common to multiple concrete
39  * <code>ThrowAnalysis</code> classes.
40  * <code>AbstractThrowAnalysis</code> provides straightforward
41  * implementations of {@link mightThrowExplicitly(ThrowInst)} and
42  * {@link mightThrowExplicitly(ThrowStmt)}, since concrete
43  * implementations of <code>ThrowAnalysis</code> seem likely to differ
44  * mainly in their treatment of implicit exceptions.
45  */

46 public abstract class AbstractThrowAnalysis implements ThrowAnalysis {
47
48     abstract public ThrowableSet mightThrow(Unit u);
49
50
51     public ThrowableSet mightThrowExplicitly(ThrowInst t) {
52     // Deducing the type at the top of the Baf stack is beyond me, so...
53
return ThrowableSet.Manager.v().ALL_THROWABLES;
54     }
55
56
57     public ThrowableSet mightThrowExplicitly(ThrowStmt t) {
58     Value thrownExpression = t.getOp();
59     Type thrownType = thrownExpression.getType();
60     if (thrownType == null || thrownType instanceof UnknownType) {
61         // We can't identify the type of thrownExpression, so...
62
return ThrowableSet.Manager.v().ALL_THROWABLES;
63         } else if (thrownType instanceof NullType) {
64             ThrowableSet result = ThrowableSet.Manager.v().EMPTY;
65             result = result.add(ThrowableSet.Manager.v().NULL_POINTER_EXCEPTION);
66             return result;
67     } else if (! (thrownType instanceof RefType)) {
68         throw new IllegalStateException JavaDoc("UnitThrowAnalysis StmtSwitch: type of throw argument is not a RefType!");
69     } else {
70         ThrowableSet result = ThrowableSet.Manager.v().EMPTY;
71         if (thrownExpression instanceof NewInvokeExpr) {
72         // In this case, we know the exact type of the
73
// argument exception.
74
result = result.add((RefType) thrownType);
75         } else {
76         result = result.add(AnySubType.v((RefType) thrownType));
77         }
78         return result;
79     }
80     }
81
82
83     abstract public ThrowableSet mightThrowImplicitly(ThrowInst t);
84     
85     
86     abstract public ThrowableSet mightThrowImplicitly(ThrowStmt t);
87 }
88
89
Popular Tags