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

IITree.java

00001 package edu.ksu.cis.bandera.prog;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998-2001 SAnToS Laboratories (santos@cis.ksu.edu)  *
00006 
00007  * All rights reserved.                                              *
00008  *                                                                   *
00009  * This work was done as a project in the SAnToS Laboratory,         *
00010  * Department of Computing and Information Sciences, Kansas State    *
00011  * University, USA (http://www.cis.ksu.edu/santos).                  *
00012  * It is understood that any modification not identified as such is  *
00013  * not covered by the preceding statement.                           *
00014  *                                                                   *
00015  * This work is free software; you can redistribute it and/or        *
00016  * modify it under the terms of the GNU Library General Public       *
00017  * License as published by the Free Software Foundation; either      *
00018  * version 2 of the License, or (at your option) any later version.  *
00019  *                                                                   *
00020  * This work is distributed in the hope that it will be useful,      *
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00023  * Library General Public License for more details.                  *
00024  *                                                                   *
00025  * You should have received a copy of the GNU Library General Public *
00026  * License along with this toolkit; if not, write to the             *
00027  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00028  * Boston, MA  02111-1307, USA.                                      *
00029  *                                                                   *
00030  * Java is a trademark of Sun Microsystems, Inc.                     *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other SAnToS projects, please visit the web-site *
00034  *                http://www.cis.ksu.edu/santos                      *
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 //hierarchy of regular classes and interfaceMap
00048 {
00049     private IITreeNode classRoot;
00050     private int maxDepth;
00051     private int maxNodes; //used as a max size for arrays
00052     private HashMap implementationsMap; // maps an interface to a set of classes
00053                         // that implement it and direct subinterfaces
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         // should we avoid the extra test, call getMethod directly,
00069         // and catch the exception that it throws if method is not found?
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/*interface */,
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         //inspected is a subinterface and we need to do the same for classes
00107         //that implement it 
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             // should we avoid the extra test, call getMethod directly,
00121             // and catch the exception that it throws if method is not found?
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         // one should never get here !!!!!!!!!!!!!!!!!!!!!!!!
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     //need to do the same for all children of the node
00141     //that has sootClass as its element
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  * Given a SootClass sc finds a node that corresponds to it
00151  * and returns it
00152  * If the node doesn't exist then returns null
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  * Given an interface Class find all classes that implement it
00177  * including classes that implement its subinterfaces
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         //this is a regular class
00194         //that needs to be added to "implementations" set
00195         implementations.add(tempClass);
00196         else
00197         {
00198         //this is a subinterface
00199         //and we need to find all classes that implement it
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         // check whether this is a regular class
00228         // or an interface
00229         // put regular class into the tree
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 }

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