00001 package edu.ksu.cis.bandera.bofa; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * Bandera, a Java(TM) analysis and transformation toolkit * 00005 * Copyright (C) 1998, 1999 * 00006 * John Hatcliff (hatcliff@cis.ksu.edu) 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 00037 import java.io.*; 00038 import ca.mcgill.sable.soot.*; 00039 import ca.mcgill.sable.util.*; 00040 import org.apache.log4j.Category; 00041 00042 /* 00043 * MethodVariantManager.java 00044 * $Id: MethodVariantManager.java,v 1.1.1.1 2002/01/24 03:42:08 pserver Exp $ 00045 */ 00046 00047 /** 00048 * This class manages the method variants corresponding to various methods on a 00049 * per class basis. 00050 * @author <A HREF="http://www.cis.ksu.edu/~hatcliff">John Hatcliff</A> 00051 * @author 00052 * <A HREF="http://www.cis.ksu.edu/~rvprasad">Venkatesh Prasad Ranganath</A> 00053 * @version $Name: $($Revision: 1.1.1.1 $) 00054 */ 00055 public class MethodVariantManager 00056 { 00057 /** 00058 * The Manager that provides indices to partition the variants. 00059 */ 00060 private static MethodIndexManager methodIndexManager; 00061 00062 /** 00063 * A Map from method to all possible variants. 00064 */ 00065 private static Map methodMap; 00066 00067 /** 00068 * Provides logging capability through log4j. 00069 * 00070 */ 00071 private static Category cat; 00072 00073 static { 00074 cat = Category.getInstance(MethodVariantManager.class.getName()); 00075 } 00076 00077 /** 00078 * Provides the defining SootMethod for the method. The SootMethod 00079 * in which the method is actually implemented and not just inherited. 00080 * 00081 * @param sootClass the class in which the sootMethod is available. 00082 * @param sootMethod the method for which we need the object corresponding 00083 * to it's implementation. 00084 * @return the object representing an implementation of the requested method. 00085 */ 00086 public static SootMethod findDeclaringMethod(SootClass sootClass, 00087 SootMethod sootMethod) 00088 { 00089 String methodName = sootMethod.getName(); 00090 List parameterTypes = sootMethod.getParameterTypes(); 00091 Type returnType = sootMethod.getReturnType(); 00092 return findDeclaringMethod(sootClass, 00093 methodName, 00094 parameterTypes, 00095 returnType); 00096 } 00097 /** 00098 * Provides the defining SootMethod for the method. The SootMethod 00099 * in which the method is actually implemented and not just inherited. 00100 * 00101 * @param sootClass the class in which the sootMethod is available. 00102 * @param methodName the name of the method 00103 * @param parameterTypes the type of the parameters to the method 00104 * @param returnType the return type of the method. 00105 * @return the object representing an implementation of the requested method. 00106 */ 00107 public static SootMethod findDeclaringMethod(SootClass sootClass, 00108 String methodName, 00109 List parameterTypes, 00110 Type returnType) 00111 { 00112 if (sootClass.declaresMethod(methodName,parameterTypes,returnType)) { 00113 // should we avoid the extra test, call getMethod directly, 00114 // and catch the exception that it throws if method is not found? 00115 return sootClass.getMethod(methodName,parameterTypes,returnType); 00116 } else { 00117 if (sootClass.hasSuperClass()) { 00118 SootClass superClass = sootClass.getSuperClass(); 00119 return findDeclaringMethod(superClass, 00120 methodName, 00121 parameterTypes, 00122 returnType); 00123 } else { 00124 throw new RuntimeException("Method not found in hierarchy:" + 00125 sootClass + "." + methodName + ":" 00126 + returnType); 00127 } 00128 } 00129 00130 } 00131 /** 00132 * Provides the set of managed methods. 00133 * 00134 * @return set of managed methods. 00135 */ 00136 public static Set getMethods() 00137 { 00138 return methodMap.keySet(); 00139 } 00140 /** 00141 * Provides the set of variants associated with the given method. 00142 * 00143 * @param sootMethod the method corresponding to which the variants are 00144 * required. 00145 * @return the collection of variants associated with the method. 00146 */ 00147 public static Collection getVariants(SootMethod sootMethod) 00148 { 00149 return ((Map) methodMap.get(sootMethod)).values(); 00150 } 00151 /** 00152 * Initialize the Manager. 00153 * 00154 * @param methodIndexManager the manager that provides the indices for 00155 * managing the variants. 00156 */ 00157 public static void init(MethodIndexManager methodIndexManager) { 00158 MethodVariantManager.methodIndexManager = methodIndexManager; 00159 MethodVariantManager.methodMap = new HashMap(); 00160 } 00161 /** 00162 * Tells whether the given method is managed or not. 00163 * 00164 * @param sootMethod the method which may or may not be managed. 00165 * @return true indicates the method is managed, false otherwise. 00166 */ 00167 public static boolean isManaged(SootMethod sootMethod) 00168 { 00169 return (methodMap.containsKey(sootMethod)); 00170 } 00171 /** 00172 * Describe <code>reset</code> method here. 00173 * 00174 */ 00175 public static void reset() { 00176 if (methodMap != null) { 00177 methodMap.clear(); 00178 } 00179 if (methodIndexManager != null) { 00180 methodIndexManager.reset(); 00181 } 00182 } 00183 /** 00184 * Provides the Method variant associated with the method. if none exist 00185 * then a new variant is created. 00186 * 00187 * @param sootMethod the method corresponding to which the Variant is 00188 * required. 00189 * @return the associated method variant. 00190 */ 00191 public static MethodVariant select(SootMethod sootMethod) 00192 { 00193 Map indexMap; 00194 Index methodIndex; 00195 MethodVariant methodVariant; 00196 00197 methodIndex = methodIndexManager.select(); 00198 00199 if (methodMap.containsKey(sootMethod)) { 00200 // if method is already registered, compute variant index, and 00201 // see if it is registered. 00202 indexMap = (Map) methodMap.get(sootMethod); 00203 00204 if (indexMap.containsKey(methodIndex)) { 00205 // if variant index is registered, then return method. 00206 methodVariant = (MethodVariant)indexMap.get(methodIndex); 00207 } else { 00208 // index was not registered, so make new method and register. 00209 methodVariant = new MethodVariant(sootMethod, methodIndex); 00210 indexMap.put(methodIndex, methodVariant); 00211 } 00212 } else { 00213 // if method is not registered, then register it with an 00214 // index map with the current variant 00215 indexMap = new HashMap(); 00216 methodVariant = new MethodVariant(sootMethod, methodIndex); 00217 indexMap.put(methodIndex, methodVariant); 00218 methodMap.put(sootMethod, indexMap); 00219 } 00220 return methodVariant; 00221 } 00222 }