01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 2004 John Jorgensen
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Library General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * 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.RefType;
24: import soot.Type;
25: import soot.Unit;
26: import soot.UnknownType;
27: import soot.NullType;
28: import soot.Value;
29: import soot.baf.ThrowInst;
30: import soot.grimp.NewInvokeExpr;
31: import soot.jimple.ThrowStmt;
32:
33: /**
34: * Abstract class implementing parts of the {@link ThrowAnalysis}
35: * interface which may be common to multiple concrete
36: * <code>ThrowAnalysis</code> classes.
37: * <code>AbstractThrowAnalysis</code> provides straightforward
38: * implementations of {@link mightThrowExplicitly(ThrowInst)} and
39: * {@link mightThrowExplicitly(ThrowStmt)}, since concrete
40: * implementations of <code>ThrowAnalysis</code> seem likely to differ
41: * mainly in their treatment of implicit exceptions.
42: */
43: public abstract class AbstractThrowAnalysis implements ThrowAnalysis {
44:
45: abstract public ThrowableSet mightThrow(Unit u);
46:
47: public ThrowableSet mightThrowExplicitly(ThrowInst t) {
48: // Deducing the type at the top of the Baf stack is beyond me, so...
49: return ThrowableSet.Manager.v().ALL_THROWABLES;
50: }
51:
52: public ThrowableSet mightThrowExplicitly(ThrowStmt t) {
53: Value thrownExpression = t.getOp();
54: Type thrownType = thrownExpression.getType();
55: if (thrownType == null || thrownType instanceof UnknownType) {
56: // We can't identify the type of thrownExpression, so...
57: return ThrowableSet.Manager.v().ALL_THROWABLES;
58: } else if (thrownType instanceof NullType) {
59: ThrowableSet result = ThrowableSet.Manager.v().EMPTY;
60: result = result
61: .add(ThrowableSet.Manager.v().NULL_POINTER_EXCEPTION);
62: return result;
63: } else if (!(thrownType instanceof RefType)) {
64: throw new IllegalStateException(
65: "UnitThrowAnalysis StmtSwitch: type of throw argument is not a RefType!");
66: } else {
67: ThrowableSet result = ThrowableSet.Manager.v().EMPTY;
68: if (thrownExpression instanceof NewInvokeExpr) {
69: // In this case, we know the exact type of the
70: // argument exception.
71: result = result.add((RefType) thrownType);
72: } else {
73: result = result.add(AnySubType.v((RefType) thrownType));
74: }
75: return result;
76: }
77: }
78:
79: abstract public ThrowableSet mightThrowImplicitly(ThrowInst t);
80:
81: abstract public ThrowableSet mightThrowImplicitly(ThrowStmt t);
82: }
|