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

HierarchyQuery.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.HashMap;
00038 import java.util.Enumeration;
00039 import java.util.Iterator;
00040 import java.io.*;
00041 
00042 import ca.mcgill.sable.soot.*;
00043 import ca.mcgill.sable.soot.jimple.*;
00044 import ca.mcgill.sable.util.*;
00045 
00046 import edu.ksu.cis.bandera.jjjc.*;
00047 
00048 public class HierarchyQuery {
00049         private static HashMap classHierarchies;
00050 
00051 public static void buildAncestors(SootClass theClass) {
00052     if (classHierarchies == null) classHierarchies = new HashMap();
00053     HashMap theMap = new HashMap();
00054     buildMap(theMap, theClass); 
00055     classHierarchies.put(theClass, theMap);
00056 }
00057 private static int buildMap(HashMap map, SootClass current) {
00058         if (current.hasSuperClass()) {
00059           int depth = buildMap(map, current.getSuperClass());
00060       map.put(new Integer(depth+1), current);
00061       return depth+1;
00062         } else {
00063       map.put(new Integer(0), current.getManager().getClass("java.lang.Object"));
00064       return 0;
00065         }
00066 }
00067 public static SootClass leastAncestor(SootClass aClass, SootClass bClass) {
00068     try {
00069     HashMap aMap = (HashMap)classHierarchies.get(aClass);
00070     HashMap bMap = (HashMap)classHierarchies.get(bClass);
00071         int maxDepth;
00072 
00073     if (aMap.size() > bMap.size()) {
00074       maxDepth = bMap.size();
00075     } else {
00076       maxDepth = aMap.size();
00077     } 
00078 
00079     // At this point maxDepth is the size of the shorter of the two Maps
00080     //  
00081     // The maxDepth is one greater than the maximal "depth from Object" 
00082     // since there is an explicit mapping for "Object" at depth 0.
00083 
00084         for (int i=maxDepth-1; i>0; i--) {
00085       if ( aMap.get(new Integer(i)) == bMap.get(new Integer(i)) )
00086         return (SootClass)aMap.get(new Integer(i));
00087         }
00088     return (SootClass)aMap.get(new Integer(0));
00089     } catch(java.lang.NullPointerException e) {
00090        System.out.println("Called with " + aClass.getName() + " and " +
00091                           bClass.getName());
00092        System.out.println("Hierarchy has maps for:");
00093        Iterator chIt = classHierarchies.values().iterator();
00094        while (chIt.hasNext()) {
00095          HashMap thisMap = (HashMap)chIt.next();
00096          System.out.println("   " + thisMap.get(new Integer(thisMap.size()-1)));
00097        }
00098        throw e;   
00099     }
00100 }
00101 public static void main(String args[]) {
00102     SootClassManager classManager = new SootClassManager();
00103     CompilationManager.reset();
00104     CompilationManager.setFilename(args[0]);
00105     CompilationManager.setClasspath(".");
00106     CompilationManager.setIncludedPackagesOrTypes(new String[0]);
00107 
00108     //Compile 
00109     try { CompilationManager.compile(); } 
00110         catch (Exception e) { 
00111       System.out.println(e);
00112       System.out.println("Compilation failed");
00113     }
00114 
00115     Hashtable storedClasses = CompilationManager.getCompiledClasses();
00116 
00117     Enumeration scIter = storedClasses.elements();
00118     while (scIter.hasMoreElements()) {
00119       ((SootClass) scIter.nextElement()).resolveIfNecessary();
00120     }
00121 
00122     scIter = storedClasses.elements();
00123     while (scIter.hasMoreElements()) {
00124       buildAncestors((SootClass)scIter.nextElement());
00125     }
00126 
00127     SootClass a = null;
00128         SootClass b = null;
00129 
00130     scIter = storedClasses.elements();
00131     while (scIter.hasMoreElements()) {
00132       SootClass tmp = (SootClass) scIter.nextElement();
00133       if (tmp.getName().equals(args[1])) {
00134         a = tmp;
00135       }
00136           if (tmp.getName().equals(args[2])) {
00137         b = tmp; 
00138       }
00139     }
00140 
00141         System.out.println("Least ancestor of " + a.getName() + " and " +
00142                            b.getName() + " is " + leastAncestor(a,b).getName());
00143 }
00144 }

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