00001 package edu.ksu.cis.bandera.bofa; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * Bandera, a Java(TM) analysis and transformation toolkit * 00005 * Copyright (C) 1999, 2000, 2001 * 00006 * Venkatesh Prasad Ranganath (rvprasad@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.util.Collection; 00038 import java.util.Iterator; 00039 00040 import ca.mcgill.sable.soot.NoSuchMethodException; 00041 import ca.mcgill.sable.soot.SootClass; 00042 import ca.mcgill.sable.soot.SootMethod; 00043 import ca.mcgill.sable.soot.Type; 00044 import ca.mcgill.sable.soot.VoidType; 00045 import ca.mcgill.sable.util.List; 00046 import ca.mcgill.sable.util.VectorList; 00047 00048 import org.apache.log4j.Category; 00049 00050 /* 00051 * Util.java 00052 * $Id: Util.java,v 1.1.1.1 2002/01/24 03:42:08 pserver Exp $ 00053 */ 00054 00055 /** 00056 * This class provides utility methods which can be used through out the 00057 * package. 00058 * 00059 * @author <a href="mailto:rvprasad@cis.ksu.edu">Venkatesh Prasad Ranganath</a> 00060 * @version $Name: $($Revision: 1.1.1.1 $) 00061 */ 00062 00063 public class Util { 00064 00065 /** 00066 * An empty list to be used for queries on no parameter method. 00067 * 00068 */ 00069 final private static VectorList emptyList = new VectorList(); 00070 00071 /** 00072 * Provides logging through log4j. 00073 * 00074 */ 00075 private static Category cat = Category.getInstance(Util.class.getName()); 00076 00077 /** 00078 * A private constructor to prevent the instantiation of an object of this 00079 * class. 00080 */ 00081 private Util() { 00082 } 00083 /** 00084 * Creates a new object which implements ca.mcgill.sable.util.Collection and 00085 * copy the contents of the given collection into it. 00086 * 00087 * @param name <code>String</code> is the name of the class which implements 00088 * ca.mcgill.sable.util.collection interface and which will be the actual 00089 * type of the returned object. 00090 * @param c <code>Collection</code> is an object which implements 00091 * java.util.Collection interface and contains values that need to be copied 00092 * into another container object and returned. 00093 * @return <code>ca.mcgill.sable.util.Collection</code> implementing object 00094 * of type <code>String</code> containing all the values that were in 00095 * <code>Collection</code>. 00096 */ 00097 public static ca.mcgill.sable.util.Collection convert(String name, 00098 Collection c) { 00099 ca.mcgill.sable.util.Collection retval = null; 00100 try { 00101 Class collect = Class.forName(name); 00102 retval = (ca.mcgill.sable.util.Collection)collect.newInstance(); 00103 if (c != null) { 00104 Iterator i = c.iterator(); 00105 while (i.hasNext()) { 00106 retval.add(i.next()); 00107 } // end of while (i.hasNext()) 00108 } // end of if (c != null) 00109 00110 } catch (ClassNotFoundException e) { 00111 cat.info("The class named " + name + " is not available in the " + 00112 "class path."); 00113 e.printStackTrace(); 00114 } catch (Exception e) { 00115 cat.info("Error instantiating an object of class " + name + "."); 00116 e.printStackTrace(); 00117 retval = null; 00118 } // end of catch 00119 00120 return retval; 00121 } 00122 /** 00123 * Shorthand version of Util.getDefiningClass() where the parameter list is 00124 * empty and the returnType is void. 00125 * 00126 * @param sc class in or above which the method may be defined. 00127 * @param method name of the method (not the fully classified name). 00128 * @return If there is such a class then a SootClass object is returned else 00129 * null is returned. 00130 */ 00131 public static SootClass getDeclaringClass(SootClass sc, String method) { 00132 return getDeclaringClass(sc, method, emptyList, VoidType.v()); 00133 } 00134 /** 00135 * Provides the SootClass which injects the given method into the specific 00136 * branch of the inheritence hierarchy to which sc belongs to. 00137 * 00138 * @param sc class that defines the branch in which the injecting class 00139 * exists. 00140 * @param method name of the method (not the fully classified name). 00141 * @param parameterTypes list of type of the parameters of the method. 00142 * @param returnType return type of the method. 00143 * @return If there is such a class then a SootClass object is returned. 00144 * @throw <code>ca.mcgill.sable.soot.NoSuchMethodException</code> is thrown 00145 * when no such method is declared in the given hierarchy. 00146 */ 00147 public static SootClass getDeclaringClass(SootClass sc, String method, 00148 List parameterTypes, 00149 Type returnType) { 00150 SootClass contains = sc; 00151 while(!contains.declaresMethod(method, parameterTypes, returnType)) { 00152 if (contains.hasSuperClass()) { 00153 contains = contains.getSuperClass(); 00154 } else { 00155 throw new NoSuchMethodException(sc + "." + method); 00156 } // end of else 00157 } 00158 return contains; 00159 } 00160 /** 00161 * <code>isAncestorOf</code> checks if a class is an ancestor of another. 00162 * It is assumed that a class cannot be it's own ancestor. 00163 * 00164 * @param child <code>SootClass</code> representing the class whose ancestor 00165 * is of interest. 00166 * @param ancestor <code>String</code> representing the name of the 00167 * ancestor. 00168 * @return <code>boolean</code> true if ancestor is indeed is one of the 00169 * ancestor, false otherwise. 00170 */ 00171 public static boolean isAncestorOf(SootClass child, String ancestor) { 00172 00173 boolean retval = false; 00174 while ((retval == false) && child.hasSuperClass()) { 00175 if (child.getName().equals(ancestor)) { 00176 retval = true; 00177 } else { 00178 child = child.getSuperClass(); 00179 } 00180 } 00181 return retval; 00182 } 00183 }// Util