Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

DynamicMap.java

00001 package edu.ksu.cis.bandera.birc;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   James Corbett (corbett@hawaii.edu)     *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
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  * A dynamic map, defining the collection associated with each allocator site.
00049  * <p>
00050  * A dynamic map contains some number of DynamicMapEntry objects, each of
00051  * which binds an allocator site (NewExpr or NewArrayExpr) to a collection.
00052  * We don't create collections here, rather we assign each collection a
00053  * unique key---to store objects from different allocator sites in the
00054  * same collection, we would provide the same key when adding the
00055  * entries for those sites (e.g., to store all members of a given
00056  * class in the same collection, the SootClass object of the allocator
00057  * could be used as the collection key).   
00058  * <p>
00059  * The guts of this class maintains several data structures for accessing
00060  * the dynamic map entries in various ways (e.g., by collection, by class).
00061  * <p>
00062  * Classes are identified with their SootClass object, while arrays
00063  * are identified with their (Jimple) ArrayType object.
00064  */
00065 
00066 public class DynamicMap {
00067 
00068     SootClassManager classManager; 
00069 
00070     /**
00071      * A vector of classes (SootClass objects) in the map and
00072      * a hashtable mapping each class to a vector of the
00073      * map entries for that class.
00074      */
00075 
00076     Vector classVector = new Vector();
00077     Hashtable classTable = new Hashtable(); 
00078 
00079     /**
00080      * A vector of arrays (Jimple ArrayType objects) in the map and
00081      * a hashtable mapping each array type to a vector of the
00082      * map entries for that class.
00083      */
00084 
00085     Vector arrayVector = new Vector();
00086     Hashtable arrayTable = new Hashtable();
00087 
00088     /**
00089      * A vector of the collection keys in the map and 
00090      * a hashtable mapping each collection key to a vector of
00091      * the map entries for that collection key.
00092      */
00093 
00094     Hashtable collectTable = new Hashtable();
00095     Vector collectVector = new Vector();
00096 
00097     DynamicMap(SootClassManager classManager) {
00098     this.classManager = classManager;
00099     }
00100     /**
00101      * Add a map entry for an array allocator, given the allocator, the
00102      * collection key, the number of arrays that must be stored, and
00103      * the maximum length of the arrays.
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      * Add a map entry for a class allocator, given the allocator, the
00117      * collection key, and the number of instances that must be
00118      * supported.  
00119      * <p>
00120      * Note that we do not model classes in any subpackage of java.*
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      * These methods return vectors of map entries for a given
00138      * class/array/collection.
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      * Get the array type of objects stored in a given collection.
00151      * <p>
00152      * Note: all objects stored in a given collection should be
00153      * of the same array type.
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      * Get the class of objects stored in a given collection.
00163      * <p>
00164      * Note: all objects stored in a given collection should be
00165      * of the same class.
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      * Compute the size of a collection by summing the size of
00176      * the map entries associated with that collection.
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      * Get the interfaces implemented by the classes in the map.
00188      * @return a vector of SootClass objects representing interfaces.
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             // && ! interfaceClass.getName().startsWith("java.")
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     // Indexed by array
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      * These methods store an entry by class/array/collection in
00249      * the vectors and hashtables.
00250      */
00251 
00252     void storeByClass(DynamicMapEntry entry, SootClass sootClass)  {
00253     // Indexed by class
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     // Indexed by collection key
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 }

Generated at Thu Feb 7 06:44:59 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001