001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot.jbco.util;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import soot.Local;
027: import soot.PatchingChain;
028: import soot.RefType;
029: import soot.SootClass;
030: import soot.SootMethod;
031: import soot.SootField;
032: import soot.Trap;
033: import soot.Type;
034: import soot.Unit;
035: import soot.baf.IfCmpEqInst;
036: import soot.baf.IfCmpGeInst;
037: import soot.baf.IfCmpGtInst;
038: import soot.baf.IfCmpLeInst;
039: import soot.baf.IfCmpLtInst;
040: import soot.baf.IfCmpNeInst;
041: import soot.baf.IfEqInst;
042: import soot.baf.IfGeInst;
043: import soot.baf.IfGtInst;
044: import soot.baf.IfLeInst;
045: import soot.baf.IfLtInst;
046: import soot.baf.IfNeInst;
047: import soot.baf.IfNonNullInst;
048: import soot.baf.IfNullInst;
049: import soot.jimple.Jimple;
050: import soot.jimple.ThisRef;
051: import soot.util.Chain;
052:
053: /**
054: * @author Michael Batchelder
055: *
056: * Created on 7-Feb-2006
057: */
058: public class BodyBuilder {
059:
060: public static boolean bodiesHaveBeenBuilt = false;
061: public static boolean namesHaveBeenRetrieved = false;
062: public static ArrayList<String> nameList = new ArrayList<String>();
063:
064: public static void retrieveAllBodies() {
065: if (bodiesHaveBeenBuilt)
066: return;
067:
068: // iterate through application classes, rename fields with junk
069: Iterator it = soot.Scene.v().getApplicationClasses().iterator();
070: while (it.hasNext()) {
071: SootClass c = (SootClass) it.next();
072:
073: Iterator mIt = c.getMethods().iterator();
074: while (mIt.hasNext()) {
075: SootMethod m = (SootMethod) mIt.next();
076: if (!m.isConcrete())
077: continue;
078:
079: if (!m.hasActiveBody())
080: m.retrieveActiveBody();
081: }
082: }
083:
084: bodiesHaveBeenBuilt = true;
085: }
086:
087: public static void retrieveAllNames() {
088: if (namesHaveBeenRetrieved)
089: return;
090:
091: // iterate through application classes, rename fields with junk
092: Iterator it = soot.Scene.v().getApplicationClasses().iterator();
093: while (it.hasNext()) {
094: SootClass c = (SootClass) it.next();
095: nameList.add(c.getName());
096:
097: Iterator _it = c.getMethods().iterator();
098: while (_it.hasNext()) {
099: SootMethod m = (SootMethod) _it.next();
100: nameList.add(m.getName());
101: }
102: _it = c.getFields().iterator();
103: while (_it.hasNext()) {
104: SootField f = (SootField) _it.next();
105: nameList.add(f.getName());
106: }
107: }
108:
109: namesHaveBeenRetrieved = true;
110: }
111:
112: public static Local buildThisLocal(PatchingChain units, ThisRef tr,
113: Chain locals) {
114: Local ths = Jimple.v().newLocal("ths", tr.getType());
115: locals.add(ths);
116: units.add(Jimple.v().newIdentityStmt(ths,
117: Jimple.v().newThisRef((RefType) tr.getType())));
118: return ths;
119: }
120:
121: public static ArrayList buildParameterLocals(PatchingChain units,
122: Chain locals, List paramTypes) {
123: ArrayList args = new ArrayList();
124: for (int k = 0; k < paramTypes.size(); k++) {
125: Type type = (Type) paramTypes.get(k);
126: Local loc = Jimple.v().newLocal("l" + k, type);
127: locals.add(loc);
128:
129: units.add(Jimple.v().newIdentityStmt(loc,
130: Jimple.v().newParameterRef(type, k)));
131:
132: args.add(loc);
133: }
134: return args;
135: }
136:
137: public static void updateTraps(Unit oldu, Unit newu, Chain traps) {
138: int size = traps.size();
139: if (size == 0)
140: return;
141:
142: Trap t = (Trap) traps.getFirst();
143: do {
144: if (t.getBeginUnit() == oldu)
145: t.setBeginUnit(newu);
146: if (t.getEndUnit() == oldu)
147: t.setEndUnit(newu);
148: if (t.getHandlerUnit() == oldu)
149: t.setHandlerUnit(newu);
150: } while ((--size > 0)
151: && (t = (Trap) traps.getSuccOf(t)) != null);
152: }
153:
154: public static boolean isExceptionCaughtAt(Chain units, Unit u,
155: Iterator trapsIt) {
156: while (trapsIt.hasNext()) {
157: Trap t = (Trap) trapsIt.next();
158: Iterator it = units.iterator(t.getBeginUnit(), units
159: .getPredOf(t.getEndUnit()));
160: while (it.hasNext())
161: if (u.equals(it.next()))
162: return true;
163: }
164:
165: return false;
166: }
167:
168: public static int getIntegerNine() {
169: int r1 = Rand.getInt(8388606) * 256;
170:
171: int r2 = Rand.getInt(28) * 9;
172:
173: if (r2 > 126)
174: r2 += 4;
175:
176: return r1 + r2;
177: }
178:
179: public static boolean isBafIf(Unit u) {
180: if (u instanceof IfCmpEqInst || u instanceof IfCmpGeInst
181: || u instanceof IfCmpGtInst || u instanceof IfCmpLeInst
182: || u instanceof IfCmpLtInst || u instanceof IfCmpNeInst
183: || u instanceof IfEqInst || u instanceof IfGeInst
184: || u instanceof IfGtInst || u instanceof IfLeInst
185: || u instanceof IfLtInst || u instanceof IfNeInst
186: || u instanceof IfNonNullInst
187: || u instanceof IfNullInst)
188: return true;
189: return false;
190: }
191: }
|