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

InvokeResolver.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 public class InvokeResolver {
00048     static InvokeResolver staticResolver;  // this is a hack
00049     static Hashtable compiledClasses;
00050     private IITree iiTree;  // Inheritence/Interface Tree 
00051 
00052     public InvokeResolver(Hashtable storedClasses)
00053     {
00054     iiTree = new IITree(storedClasses.size());
00055         iiTree.insert(storedClasses);
00056         iiTree.number();
00057         compiledClasses = storedClasses;
00058         staticResolver = this;  // This is a hack
00059     }
00060     public Vector getPairs(SootClass sc, String methodName, List parameterTypes, Type returnType)
00061     {
00062     Set relevant = new ArraySet();
00063 
00064         if (methodName.equals("<init>")) 
00065         {
00066           // Constructors are special invokes so inheritence doesn't matter
00067           return new Vector();
00068 
00069     } else if (sc.hasSuperClass() || ((sc.getName()).compareTo("java.lang.Object")==0))
00070     {
00071         //this is a regular class 
00072         //need to output all children along with the methods
00073         IITreeNode startNode = iiTree.get(sc, iiTree.getRoot());
00074         if(startNode != null)
00075             iiTree.findPairs(relevant, startNode, methodName, parameterTypes, returnType);
00076         else
00077         System.out.println("Declaring SootClass < "+sc+ " > is a library class that wasn't supplied to the program!!!!!!!!!!");
00078     }
00079     else
00080     {
00081         //sc here is an interface 
00082         iiTree.findPairs(relevant, sc, methodName, parameterTypes, returnType); 
00083     }
00084 
00085         Vector orderedPairs = new Vector();
00086         HashMap classMethodMap = new HashMap();
00087         Iterator initMap = relevant.iterator();
00088         while (initMap.hasNext()) {
00089           ClassMethodPair currentPair = (ClassMethodPair)initMap.next();
00090           classMethodMap.put(currentPair.getInvClass(), currentPair.getInvMethod());
00091         }
00092       
00093 
00094         for (int i=iiTree.getMaxDepth(); i>=0; i--) {
00095           Iterator pairs = relevant.iterator();
00096           while (pairs.hasNext()) {
00097             ClassMethodPair currentPair = (ClassMethodPair)pairs.next();
00098             if (currentPair.getDepth() == i) 
00099               // If the current pair corresponds to a class that does not
00100               // override the invoked method, then we skip over adding the 
00101               // pair and lets its parent's "pair" represent the method to 
00102               // to be invoked for this (sub)class
00103               if (!currentPair.getInvClass().hasSuperClass() ||
00104                   !classMethodMap.containsKey(currentPair.getInvClass().getSuperClass()) ||
00105                   currentPair.getInvMethod() != classMethodMap.get(currentPair.getInvClass().getSuperClass())) {
00106                 orderedPairs.addElement(currentPair);
00107               }
00108           }
00109         }
00110         return orderedPairs;
00111     }
00112     static public InvokeResolver getResolver() {  // this is a hack
00113     if (compiledClasses != CompilationManager.getCompiledClasses()) {
00114       staticResolver = new InvokeResolver( 
00115                          CompilationManager.getCompiledClasses());
00116     }
00117       return staticResolver;
00118     }
00119     public static void  main(String[] args)
00120     {
00121         SootClassManager classManager = new SootClassManager();
00122     //Prepare the Compilation Manager
00123     CompilationManager.reset();
00124     CompilationManager.setFilename(args[0]);
00125     CompilationManager.setClasspath(".");
00126     CompilationManager.setIncludedPackagesOrTypes(new String[0]);
00127 
00128         //Compile 
00129         try
00130         {
00131                 CompilationManager.compile();
00132         } catch (Exception e)
00133         {
00134         System.out.println(e);
00135                 System.out.println("Compilation failed");
00136     }
00137         Hashtable storedClasses = CompilationManager.getCompiledClasses();
00138     InvokeResolver resolver = new InvokeResolver(storedClasses);
00139 
00140         for(Enumeration e = storedClasses.elements(); e.hasMoreElements();)
00141         {
00142             SootClass sc = (SootClass)e.nextElement();
00143             String className = sc.getName();
00144             List methodsList = sc.getMethods();
00145             Iterator mi = methodsList.iterator();
00146 
00147             while( mi.hasNext())
00148             {
00149                 SootMethod sm = (SootMethod)mi.next();
00150                 System.out.println("\nSoot Method: "+sm.toString());
00151 
00152                 if(sm.isBodyStored(Jimple.v()))
00153                 {
00154                     JimpleBody jb = (JimpleBody)sm.getBody(Jimple.v());
00155                     for(Iterator si = jb.getStmtList().iterator(); si.hasNext();
00156 )
00157                     {
00158                         Stmt s = (Stmt)si.next();
00159                         System.out.println(s);
00160 
00161                         if (s instanceof JInvokeStmt)
00162                         {
00163 
00164                             SootClass methodDecl = (((InvokeExpr)(((JInvokeStmt)s).getInvokeExpr())).getMethod()).getDeclaringClass();
00165                             String methodName = (((InvokeExpr)(((JInvokeStmt)s).getInvokeExpr())).getMethod()).getName();
00166                             List parameterTypes = (((InvokeExpr)(((JInvokeStmt)s).getInvokeExpr())).getMethod()).getParameterTypes();
00167                             Type returnType = (((InvokeExpr)(((JInvokeStmt)s).getInvokeExpr())).getMethod()).getReturnType();
00168                             Vector orderedPairs = resolver.getPairs(methodDecl, methodName, parameterTypes, returnType);
00169                             System.out.println("Class/Method pairs: ");
00170                             for (int i=0; i<orderedPairs.size(); i++) 
00171                               System.out.println("  "+
00172                                  (ClassMethodPair)orderedPairs.elementAt(i));
00173                         }
00174                     }
00175                 }
00176             }
00177         }
00178     }
00179 }

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