00001 package edu.ksu.cis.bandera.birc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import edu.ksu.cis.bandera.jext.*;
00036
00037 import ca.mcgill.sable.util.*;
00038 import ca.mcgill.sable.soot.*;
00039 import ca.mcgill.sable.soot.jimple.*;
00040
00041 import ca.mcgill.sable.soot.ArrayType;
00042
00043 import java.io.*;
00044 import java.util.Vector;
00045 import java.util.Hashtable;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 public class DynamicMap {
00067
00068 SootClassManager classManager;
00069
00070
00071
00072
00073
00074
00075
00076 Vector classVector = new Vector();
00077 Hashtable classTable = new Hashtable();
00078
00079
00080
00081
00082
00083
00084
00085 Vector arrayVector = new Vector();
00086 Hashtable arrayTable = new Hashtable();
00087
00088
00089
00090
00091
00092
00093
00094 Hashtable collectTable = new Hashtable();
00095 Vector collectVector = new Vector();
00096
00097 DynamicMap(SootClassManager classManager) {
00098 this.classManager = classManager;
00099 }
00100
00101
00102
00103
00104
00105
00106 void addEntry(NewArrayExpr alloc, Object key, int size, int arrayLen) {
00107 ArrayType arrayType = (ArrayType) alloc.getType();
00108 if (arrayType != null) {
00109 DynamicMapEntry entry =
00110 new DynamicMapEntry(alloc,null,arrayType,key,size,arrayLen);
00111 storeByArray(entry,arrayType);
00112 storeByCollection(entry, key);
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123 void addEntry(NewExpr alloc, Object key, int size) {
00124 SootClass sootClass =
00125 classManager.getClass(alloc.getBaseType().className);
00126 if (! sootClass.getName().startsWith("java.")) {
00127 DynamicMapEntry entry =
00128 new DynamicMapEntry(alloc,sootClass,null,key,size,0);
00129 storeByClass(entry, sootClass);
00130 storeByCollection(entry, key);
00131 }
00132 }
00133 Vector getAllocsOfArray(Type arrayType) {
00134 return (Vector) arrayTable.get(arrayType);
00135 }
00136
00137
00138
00139
00140
00141 Vector getAllocsOfClass(SootClass sootClass) {
00142 return (Vector) classTable.get(sootClass);
00143 }
00144 Vector getAllocsOfCollection(Object key) {
00145 return (Vector) collectTable.get(key);
00146 }
00147 Vector getArrays() { return arrayVector; }
00148 Vector getClasses() { return classVector; }
00149
00150
00151
00152
00153
00154
00155
00156 ArrayType getCollectionArray(Object key) {
00157 Vector allocs = (Vector) collectTable.get(key);
00158 DynamicMapEntry entry = (DynamicMapEntry) allocs.elementAt(0);
00159 return entry.arrayType;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 SootClass getCollectionClass(Object key) {
00169 Vector allocs = (Vector) collectTable.get(key);
00170 DynamicMapEntry entry = (DynamicMapEntry) allocs.elementAt(0);
00171 return entry.sootClass;
00172 }
00173 Vector getCollections() { return collectVector; }
00174
00175
00176
00177
00178
00179 int getCollectionSize(Object key) {
00180 int size = 0;
00181 Vector allocs = (Vector) collectTable.get(key);
00182 for (int i = 0; i < allocs.size(); i++)
00183 size += ((DynamicMapEntry)allocs.elementAt(i)).size;
00184 return size;
00185 }
00186
00187
00188
00189
00190
00191 Vector getInterfaces() {
00192 Vector result = new Vector();
00193 for (int i = 0; i < classVector.size(); i++) {
00194 SootClass sootClass = (SootClass) classVector.elementAt(i);
00195 Iterator interfaceIt = sootClass.getInterfaces().iterator();
00196 while (interfaceIt.hasNext()) {
00197 SootClass interfaceClass = (SootClass) interfaceIt.next();
00198 if (! result.contains(interfaceClass))
00199
00200 result.addElement(interfaceClass);
00201 }
00202 }
00203 return result;
00204 }
00205 void print() {
00206 System.out.println("Dynamic Map:");
00207 for (int i = 0; i < classVector.size(); i++) {
00208 SootClass sootClass = (SootClass) classVector.elementAt(i);
00209 System.out.println(" class " + sootClass.getName() + ":");
00210 Vector classAllocs = (Vector) classTable.get(sootClass);
00211 for (int j = 0; j < classAllocs.size(); j++) {
00212 System.out.println(" " + classAllocs.elementAt(j));
00213 }
00214 }
00215 for (int i = 0; i < arrayVector.size(); i++) {
00216 Type arrayType = (Type) arrayVector.elementAt(i);
00217 System.out.println(" array " + arrayType + ":");
00218 Vector arrayAllocs = (Vector) arrayTable.get(arrayType);
00219 for (int j = 0; j < arrayAllocs.size(); j++) {
00220 System.out.println(" " + arrayAllocs.elementAt(j));
00221 }
00222 }
00223 for (int i = 0; i < collectVector.size(); i++) {
00224 Object key = collectVector.elementAt(i);
00225 System.out.println(" collect " + key + ":");
00226 Vector collectAllocs = (Vector) collectTable.get(key);
00227 for (int j = 0; j < collectAllocs.size(); j++) {
00228 System.out.println(" " + collectAllocs.elementAt(j));
00229 }
00230 }
00231 Vector interfaces = getInterfaces();
00232 for (int i = 0; i < interfaces.size(); i++) {
00233 SootClass sootClass = (SootClass) interfaces.elementAt(i);
00234 System.out.println(" interface " + sootClass.getName());
00235 }
00236 }
00237 void storeByArray(DynamicMapEntry entry, Type arrayType) {
00238
00239 Vector arrayAllocs = (Vector) arrayTable.get(arrayType);
00240 if (arrayAllocs == null) {
00241 arrayAllocs = new Vector();
00242 arrayTable.put(arrayType, arrayAllocs);
00243 arrayVector.addElement(arrayType);
00244 }
00245 arrayAllocs.addElement(entry);
00246 }
00247
00248
00249
00250
00251
00252 void storeByClass(DynamicMapEntry entry, SootClass sootClass) {
00253
00254 Vector classAllocs = (Vector) classTable.get(sootClass);
00255 if (classAllocs == null) {
00256 classAllocs = new Vector();
00257 classTable.put(sootClass, classAllocs);
00258 classVector.addElement(sootClass);
00259 }
00260 classAllocs.addElement(entry);
00261 }
00262 void storeByCollection(DynamicMapEntry entry, Object key) {
00263
00264 Vector collectAllocs = (Vector) collectTable.get(key);
00265 if (collectAllocs == null) {
00266 collectAllocs = new Vector();
00267 collectTable.put(key, collectAllocs);
00268 collectVector.addElement(key);
00269 }
00270 collectAllocs.addElement(entry);
00271 }
00272 }