00001 package edu.ksu.cis.bandera.prog;
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
00036 import java.util.Hashtable;
00037 import java.util.Vector;
00038 import java.util.Enumeration;
00039 import java.io.*;
00040
00041 import ca.mcgill.sable.soot.*;
00042 import ca.mcgill.sable.soot.jimple.*;
00043 import ca.mcgill.sable.util.*;
00044
00045 import edu.ksu.cis.bandera.jjjc.*;
00046
00047 class IITree
00048 {
00049 private IITreeNode classRoot;
00050 private int maxDepth;
00051 private int maxNodes;
00052 private HashMap implementationsMap;
00053
00054 public IITree(int num)
00055 {
00056 maxDepth = 0;
00057 maxNodes = num;
00058 classRoot = null;
00059 implementationsMap = new HashMap();
00060 }
00061 public SootMethod findDeclaringMethod(SootClass sootClass,
00062 String methodName,
00063 List parameterTypes,
00064 Type returnType)
00065 {
00066 if (sootClass.declaresMethod(methodName,parameterTypes,returnType))
00067 {
00068
00069
00070 return sootClass.getMethod(methodName, parameterTypes, returnType);
00071 }
00072 else
00073 {
00074 if (sootClass.hasSuperClass())
00075 {
00076 SootClass superClass = sootClass.getSuperClass();
00077 return findDeclaringMethod(superClass,
00078 methodName,
00079 parameterTypes,
00080 returnType);
00081 }
00082 else
00083 throw new RuntimeException("Method not found in hierarchy:" + methodName);
00084 }
00085
00086 }
00087 public void findPairs(Set relevant, SootClass sc,
00088 String methodName,
00089 List parameterTypes,
00090 Type returnType)
00091 {
00092 Set temp = getImplementations(sc);
00093
00094
00095 Iterator ti = temp.iterator();
00096 while(ti.hasNext())
00097 {
00098 Object tmpo = ti.next();
00099 SootClass inspected = (SootClass)tmpo;
00100 if(inspected.hasSuperClass())
00101 {
00102 IITreeNode startNode = get(inspected, classRoot);
00103 findPairs(relevant, startNode, methodName, parameterTypes, returnType);
00104 }
00105 else
00106
00107
00108 findPairs(relevant, inspected, methodName, parameterTypes, returnType);
00109 }
00110 }
00111 public void findPairs(Set relevant,
00112 IITreeNode node,
00113 String methodName,
00114 List parameterTypes,
00115 Type returnType)
00116 {
00117 SootClass sc = node.getElement();
00118 SootMethod sm;
00119 if (sc.declaresMethod(methodName,parameterTypes,returnType))
00120
00121
00122 sm = sc.getMethod(methodName, parameterTypes, returnType);
00123 else
00124 {
00125 if (sc.hasSuperClass())
00126 {
00127 SootClass superClass = sc.getSuperClass();
00128 sm = findDeclaringMethod(superClass,
00129 methodName,
00130 parameterTypes,
00131 returnType);
00132 }
00133 else
00134
00135 throw new RuntimeException("Method not found in hierarchy:" + methodName);
00136 }
00137 ClassMethodPair finalPair = new ClassMethodPair(sc, sm, node.getDepth());
00138 relevant.add(finalPair);
00139
00140
00141
00142
00143 for(int i=0; i<node.getChildrenCount(); i++)
00144 {
00145 IITreeNode temp = (node.getChildren())[i];
00146 recursiveFindPairs(relevant, sm, temp, methodName, parameterTypes, returnType);
00147 }
00148 }
00149
00150
00151
00152
00153
00154 public IITreeNode get(SootClass sc, IITreeNode node)
00155 {
00156 if(node == null)
00157 return null;
00158
00159 if(sc == null)
00160 throw new RuntimeException("bumps");
00161 if(((node.getElement()).getName()).compareTo(sc.getName()) == 0 )
00162 return node;
00163 else
00164 {
00165 for(int i=0; i<node.getChildrenCount(); i++)
00166 {
00167 IITreeNode temp = this.get(sc, (node.getChildren())[i]);
00168 if(temp != null)
00169 return temp;
00170 }
00171
00172 }
00173 return null;
00174 }
00175
00176
00177
00178
00179 public Set getImplementations(SootClass sc)
00180 {
00181 Set implementations = new ArraySet();
00182 Set temp = (Set)implementationsMap.get(sc);
00183 if(temp == null)
00184 {
00185 System.out.println("No mapping for :"+sc.getName()+" !!!!");
00186 throw new RuntimeException("No mapping");
00187 }
00188 Iterator ti = temp.iterator();
00189 while(ti.hasNext())
00190 {
00191 SootClass tempClass = (SootClass)ti.next();
00192 if(tempClass.hasSuperClass())
00193
00194
00195 implementations.add(tempClass);
00196 else
00197 {
00198
00199
00200 Iterator ii = this.getImplementations(tempClass).iterator();
00201 while (ii.hasNext())
00202 implementations.add((SootClass)ii.next());
00203 }
00204 }
00205
00206 return implementations;
00207 }
00208 public HashMap getImplementationsMap()
00209 {
00210 return implementationsMap;
00211 }
00212 public int getMaxDepth()
00213 {
00214 return maxDepth;
00215 }
00216 public IITreeNode getRoot()
00217 {
00218 return classRoot;
00219 }
00220 public void insert(Hashtable table)
00221 {
00222 LinkedList pendingList = new LinkedList();
00223 for(Enumeration e = table.elements(); e.hasMoreElements();)
00224 {
00225 SootClass sc = (SootClass)e.nextElement();
00226
00227
00228
00229
00230
00231 if(sc.hasSuperClass())
00232 recursiveInsert(sc);
00233 if(sc != null)
00234 {
00235 for (Iterator inIt = sc.getInterfaces().iterator(); inIt.hasNext();)
00236 {
00237 SootClass implementedClass = (SootClass) inIt.next();
00238 if (implementationsMap.containsKey(implementedClass))
00239 ((Set) implementationsMap.get(implementedClass)).add(sc);
00240 else
00241 {
00242 Set impSet = new ArraySet();
00243 impSet.add(sc);
00244 implementationsMap.put(implementedClass, impSet);
00245 }
00246 }
00247 }
00248 }
00249 }
00250 public void number()
00251 {
00252 maxDepth = classRoot.number(0);
00253 }
00254 public void print()
00255 {
00256 this.classRoot.print();
00257 }
00258 private void recursiveFindPairs(Set relevant,
00259 SootMethod sm,
00260 IITreeNode node,
00261 String methodName,
00262 List parameterTypes,
00263 Type returnType)
00264 {
00265 SootClass sc = node.getElement();
00266 if (sc.declaresMethod(methodName,parameterTypes,returnType))
00267 sm = sc.getMethod(methodName, parameterTypes, returnType);
00268 ClassMethodPair finalPair = new ClassMethodPair(sc, sm, node.getDepth());
00269 relevant.add(finalPair);
00270
00271 for(int i=0; i<node.getChildrenCount(); i++)
00272 {
00273 IITreeNode temp = (node.getChildren())[i];
00274 recursiveFindPairs(relevant, sm, temp, methodName, parameterTypes, returnType);
00275 }
00276
00277 }
00278 private IITreeNode recursiveInsert(SootClass sc)
00279 {
00280
00281 IITreeNode temp = get(sc, classRoot);
00282 if(temp!=null)
00283 return temp;
00284 if(((sc.getName()).compareTo("java.lang.Object"))==0)
00285 {
00286 classRoot = new IITreeNode(sc, maxNodes);
00287 return classRoot;
00288 }
00289
00290 IITreeNode parent = get(sc.getSuperClass(), classRoot);
00291 if(parent == null)
00292 parent = recursiveInsert(sc.getSuperClass());
00293 temp = new IITreeNode(sc, maxNodes);
00294 parent.addChild(temp);
00295 return temp;
00296 }
00297 }