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 public class InvokeResolver {
00048 static InvokeResolver staticResolver;
00049 static Hashtable compiledClasses;
00050 private IITree iiTree;
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;
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
00067 return new Vector();
00068
00069 } else if (sc.hasSuperClass() || ((sc.getName()).compareTo("java.lang.Object")==0))
00070 {
00071
00072
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
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
00100
00101
00102
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() {
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
00123 CompilationManager.reset();
00124 CompilationManager.setFilename(args[0]);
00125 CompilationManager.setClasspath(".");
00126 CompilationManager.setIncludedPackagesOrTypes(new String[0]);
00127
00128
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 }