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

Repository.java

00001 package de.fub.bytecode;
00002 
00003 import de.fub.bytecode.classfile.*;
00004 import de.fub.bytecode.util.*;
00005 import java.util.*;
00006 import java.io.*;
00007 
00008 /** 
00009  * Repository maintains informations about class interdependencies, e.g.
00010  * whether a class is a sub-class of another. JavaClass objects are put
00011  * into a cache which can be purged with clearCache().
00012  *
00013  * All JavaClass objects used as arguments must have been obtained via
00014  * the repository or been added with addClass() manually. This is
00015  * because we check for object equality (==).
00016  *
00017  * @version $Id: Repository.java,v 1.1.1.1 2002/01/24 03:43:59 pserver Exp $
00018  * @author <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A
00019  */
00020 public abstract class Repository {
00021   private static ClassPath class_path = new ClassPath();
00022   private static Hashtable classes;
00023   private static JavaClass OBJECT; // should be final ...
00024 
00025   static { clearCache(); }
00026   /**
00027    * Add clazz to repository if there isn't an equally named class already in there.
00028    *
00029    * @return old entry in repository
00030    */
00031   public static JavaClass addClass(JavaClass clazz) {
00032     String    name = clazz.getClassName();
00033     JavaClass cl   = (JavaClass)classes.get(name);
00034 
00035     if(cl == null)
00036       classes.put(name, cl = clazz);
00037 
00038     return cl;
00039   }  
00040   /** Clear the repository.
00041    */
00042   public static void clearCache() {
00043     classes = new Hashtable();
00044     OBJECT  = lookupClass("java.lang.Object");
00045 
00046     if(OBJECT == null)
00047       System.err.println("Warning: java.lang.Object not found on CLASSPATH!");
00048     else
00049       classes.put("java.lang.Object", OBJECT);
00050   }  
00051   /**
00052    * @return all interfaces implemented by class and its super
00053    * classes and the interfaces that those interfaces extend, and so on
00054    */
00055   public static JavaClass[] getInterfaces(JavaClass clazz) {
00056     ClassVector vec   = new ClassVector();
00057     ClassQueue  queue = new ClassQueue();
00058 
00059     queue.enqueue(clazz);
00060 
00061     while(!queue.empty()) {
00062       clazz = queue.dequeue();
00063 
00064       String   s          = clazz.getSuperclassName();
00065       String[] interfaces = clazz.getInterfaceNames();
00066 
00067       if(clazz.isInterface())
00068     vec.addElement(clazz);
00069       else if(!s.equals("java.lang.Object"))
00070     queue.enqueue(lookupClass(s));
00071       
00072       for(int i=0; i < interfaces.length; i++)
00073     queue.enqueue(lookupClass(interfaces[i]));
00074     }
00075 
00076     return vec.toArray();
00077   }  
00078   /**
00079    * @return all interfaces implemented by class and its super
00080    * classes and the interfaces that extend those interfaces, and so on
00081    */
00082   public static JavaClass[] getInterfaces(String class_name) {
00083     return getInterfaces(lookupClass(class_name));
00084   }  
00085   private static final JavaClass getSuperClass(JavaClass clazz) {
00086     if(clazz == OBJECT)
00087       return null;
00088 
00089     return lookupClass(clazz.getSuperclassName());
00090   }  
00091   /**
00092    * @return list of super classes of clazz in ascending order, i.e.
00093    * Object is always the last element
00094    */
00095   public static JavaClass[] getSuperClasses(JavaClass clazz) {
00096     ClassVector vec = new ClassVector();
00097 
00098     for(clazz = getSuperClass(clazz); clazz != null; clazz = getSuperClass(clazz))
00099       vec.addElement(clazz);
00100 
00101     return vec.toArray();
00102   }  
00103   /**
00104    * @return list of super classes of clazz in ascending order, i.e.
00105    * Object is always the last element
00106    */
00107   public static JavaClass[] getSuperClasses(String class_name) {
00108     return getSuperClasses(lookupClass(class_name));
00109   }  
00110   /**
00111    * @return true, if clazz is an implementation of interface inter
00112    */
00113   public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
00114     if(clazz == inter)
00115       return true;
00116 
00117     JavaClass[] super_interfaces = getInterfaces(clazz);
00118 
00119     for(int i=0; i < super_interfaces.length; i++)
00120       if(super_interfaces[i] == inter)
00121     return true;
00122 
00123     return false;
00124   }  
00125   /**
00126    * @return true, if clazz is an implementation of interface inter
00127    */
00128   public static boolean implementationOf(JavaClass clazz, String inter) {
00129     return implementationOf(clazz, lookupClass(inter));
00130   }  
00131   /**
00132    * @return true, if clazz is an implementation of interface inter
00133    */
00134   public static boolean implementationOf(String clazz, JavaClass inter) {
00135     return implementationOf(lookupClass(clazz), inter);
00136   }  
00137   /**
00138    * @return true, if clazz is an implementation of interface inter
00139    */
00140   public static boolean implementationOf(String clazz, String inter) {
00141     return implementationOf(lookupClass(clazz), lookupClass(inter));
00142   }  
00143   /**
00144    * @return true, if clazz is an instance of super_class
00145    */
00146   public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
00147     if(clazz == super_class)
00148       return true;
00149 
00150     JavaClass[] super_classes = getSuperClasses(clazz);
00151 
00152     for(int i=0; i < super_classes.length; i++)
00153       if(super_classes[i] == super_class)
00154     return true;
00155 
00156     if(super_class.isInterface())
00157       return implementationOf(clazz, super_class);
00158 
00159     return false;
00160   }  
00161   /**
00162    * @return true, if clazz is an instance of super_class
00163    */
00164   public static boolean instanceOf(JavaClass clazz, String super_class) {
00165     return instanceOf(clazz, lookupClass(super_class));
00166   }  
00167   /**
00168    * @return true, if clazz is an instance of super_class
00169    */
00170   public static boolean instanceOf(String clazz, JavaClass super_class) {
00171     return instanceOf(lookupClass(clazz), super_class);
00172   }  
00173   /**
00174    * @return true, if clazz is an instance of super_class
00175    */
00176   public static boolean instanceOf(String clazz, String super_class) {
00177     return instanceOf(lookupClass(clazz), lookupClass(super_class));
00178   }  
00179   /** @return class object for given class name.
00180    */
00181   public static JavaClass lookupClass(String class_name) {
00182     JavaClass clazz = (JavaClass)classes.get(class_name);
00183 
00184     if(clazz == null) {
00185       try {
00186     InputStream is = class_path.getInputStream(class_name);
00187     clazz = new ClassParser(is, class_name).parse();
00188       } catch(IOException e) { return null; }
00189 
00190       classes.put(class_name, clazz);
00191     }
00192 
00193     return clazz;
00194   }  
00195   /** @return class file object for given Java class.
00196    */
00197   public static ClassPath.ClassFile lookupClassFile(String class_name) {
00198     try {
00199       return class_path.getClassFile(class_name);
00200     } catch(IOException e) { return null; }
00201   }  
00202     public static void resetClassPath() { class_path = new ClassPath(); }
00203 }

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