001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2003 Jerome Miecznikowski
003: * Copyright (C) 2005 Nomair A. Naeem
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
018: * Boston, MA 02111-1307, USA.
019: */
020:
021: package soot.dava.internal.AST;
022:
023: import soot.*;
024: import java.util.*;
025: import soot.dava.internal.SET.*;
026: import soot.dava.toolkits.base.AST.*;
027: import soot.dava.toolkits.base.AST.analysis.*;
028:
029: public class ASTTryNode extends ASTLabeledNode {
030: private List<Object> tryBody, catchList;
031: private Map<Object, Object> exceptionMap, paramMap;
032: private container tryBodyContainer;
033:
034: public class container {
035: public Object o;
036:
037: public container(Object o) {
038: this .o = o;
039: }
040:
041: public void replaceBody(Object newBody) {
042: this .o = newBody;
043: }
044: }
045:
046: public ASTTryNode(SETNodeLabel label, List<Object> tryBody,
047: List<Object> catchList, Map<Object, Object> exceptionMap,
048: Map<Object, Object> paramMap) {
049: super (label);
050:
051: this .tryBody = tryBody;
052: tryBodyContainer = new container(tryBody);
053:
054: this .catchList = new ArrayList<Object>();
055: Iterator<Object> cit = catchList.iterator();
056: while (cit.hasNext())
057: this .catchList.add(new container(cit.next()));
058:
059: this .exceptionMap = new HashMap<Object, Object>();
060: cit = this .catchList.iterator();
061: while (cit.hasNext()) {
062: container c = (container) cit.next();
063: this .exceptionMap.put(c, exceptionMap.get(c.o));
064: }
065:
066: this .paramMap = new HashMap<Object, Object>();
067: cit = this .catchList.iterator();
068: while (cit.hasNext()) {
069: container c = (container) cit.next();
070: this .paramMap.put(c, paramMap.get(c.o));
071: }
072:
073: subBodies.add(tryBodyContainer);
074: cit = this .catchList.iterator();
075: while (cit.hasNext())
076: subBodies.add(cit.next());
077: }
078:
079: /*
080: Nomair A Naeem 21-FEB-2005
081: used to support UselessLabeledBlockRemover
082: */
083: public void replaceTryBody(List<Object> tryBody) {
084: this .tryBody = tryBody;
085: tryBodyContainer = new container(tryBody);
086:
087: List<Object> oldSubBodies = subBodies;
088: subBodies = new ArrayList<Object>();
089:
090: subBodies.add(tryBodyContainer);
091:
092: Iterator<Object> oldIt = oldSubBodies.iterator();
093: //discard the first since that was the old tryBodyContainer
094: oldIt.next();
095:
096: while (oldIt.hasNext())
097: subBodies.add(oldIt.next());
098:
099: }
100:
101: protected void perform_AnalysisOnSubBodies(ASTAnalysis a) {
102: if (a instanceof TryContentsFinder) {
103: Iterator<Object> sbit = subBodies.iterator();
104: while (sbit.hasNext()) {
105: container subBody = (container) sbit.next();
106:
107: Iterator it = ((List) subBody.o).iterator();
108: while (it.hasNext()) {
109: ASTNode n = (ASTNode) it.next();
110:
111: n.perform_Analysis(a);
112: TryContentsFinder.v().add_ExceptionSet(subBody,
113: TryContentsFinder.v().get_ExceptionSet(n));
114: }
115: }
116:
117: a.analyseASTNode(this );
118: } else
119: super .perform_AnalysisOnSubBodies(a);
120: }
121:
122: public boolean isEmpty() {
123: return tryBody.isEmpty();
124: }
125:
126: public List<Object> get_TryBody() {
127: return tryBody;
128: }
129:
130: public container get_TryBodyContainer() {
131: return tryBodyContainer;
132: }
133:
134: public List<Object> get_CatchList() {
135: return catchList;
136: }
137:
138: public Map<Object, Object> get_ExceptionMap() {
139: return exceptionMap;
140: }
141:
142: /*
143: Nomair A. Naeem 08-FEB-2005
144: Needed for call from DepthFirstAdapter
145: */
146: public Map<Object, Object> get_ParamMap() {
147: return paramMap;
148: }
149:
150: public Set<Object> get_ExceptionSet() {
151: HashSet<Object> s = new HashSet<Object>();
152:
153: Iterator<Object> it = catchList.iterator();
154: while (it.hasNext())
155: s.add(exceptionMap.get(it.next()));
156:
157: return s;
158: }
159:
160: public Object clone() {
161: ArrayList<Object> newCatchList = new ArrayList<Object>();
162: Iterator<Object> it = catchList.iterator();
163: while (it.hasNext())
164: newCatchList.add(((container) it.next()).o);
165:
166: return new ASTTryNode(get_Label(), tryBody, newCatchList,
167: exceptionMap, paramMap);
168: }
169:
170: public void toString(UnitPrinter up) {
171: label_toString(up);
172:
173: up.literal("try");
174: up.newline();
175:
176: up.literal("{");
177: up.newline();
178:
179: up.incIndent();
180: body_toString(up, tryBody);
181: up.decIndent();
182:
183: up.literal("}");
184: up.newline();
185:
186: Iterator<Object> cit = catchList.iterator();
187: while (cit.hasNext()) {
188: container catchBody = (container) cit.next();
189:
190: up.literal("catch");
191: up.literal(" ");
192: up.literal("(");
193: up
194: .type(((SootClass) exceptionMap.get(catchBody))
195: .getType());
196: up.literal(" ");
197: up.local((Local) paramMap.get(catchBody));
198: up.literal(")");
199: up.newline();
200:
201: up.literal("{");
202: up.newline();
203:
204: up.incIndent();
205: body_toString(up, (List<Object>) catchBody.o);
206: up.decIndent();
207:
208: up.literal("}");
209: up.newline();
210: }
211: }
212:
213: public String toString() {
214: StringBuffer b = new StringBuffer();
215:
216: b.append(label_toString());
217:
218: b.append("try");
219: b.append(NEWLINE);
220:
221: b.append("{");
222: b.append(NEWLINE);
223:
224: b.append(body_toString(tryBody));
225:
226: b.append("}");
227: b.append(NEWLINE);
228:
229: Iterator<Object> cit = catchList.iterator();
230: while (cit.hasNext()) {
231: container catchBody = (container) cit.next();
232:
233: b.append("catch (");
234: b.append(((SootClass) exceptionMap.get(catchBody))
235: .getName());
236: b.append(" ");
237: b.append(((Local) paramMap.get(catchBody)).getName());
238: b.append(")");
239: b.append(NEWLINE);
240:
241: b.append("{");
242: b.append(NEWLINE);
243:
244: b.append(body_toString((List<Object>) catchBody.o));
245:
246: b.append("}");
247: b.append(NEWLINE);
248: }
249:
250: return b.toString();
251: }
252:
253: /*
254: Nomair A. Naeem, 7-FEB-05
255: Part of Visitor Design Implementation for AST
256: See: soot.dava.toolkits.base.AST.analysis For details
257: */
258: public void apply(Analysis a) {
259: a.caseASTTryNode(this);
260: }
261: }
|