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:41:37 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   /**
00028    * Add clazz to repository if there isn't an equally named class already in there.
00029    *
00030    * @return old entry in repository
00031    */
00032   public static JavaClass addClass(JavaClass clazz) {
00033     String    name = clazz.getClassName();
00034     JavaClass cl   = (JavaClass)classes.get(name);
00035 
00036     if(cl == null)
00037       classes.put(name, cl = clazz);
00038 
00039     return cl;
00040   }  
00041   /** Clear the repository.
00042    */
00043   public static void clearCache() {
00044     classes = new Hashtable();
00045     OBJECT  = lookupClass("java.lang.Object");
00046 
00047     if(OBJECT == null)
00048       System.err.println("Warning: java.lang.Object not found on CLASSPATH!");
00049     else
00050       classes.put("java.lang.Object", OBJECT);
00051   }  
00052   /**
00053    * @return all interfaces implemented by class and its super
00054    * classes and the interfaces that those interfaces extend, and so on
00055    */
00056   public static JavaClass[] getInterfaces(JavaClass clazz) {
00057     ClassVector vec   = new ClassVector();
00058     ClassQueue  queue = new ClassQueue();
00059 
00060     queue.enqueue(clazz);
00061 
00062     while(!queue.empty()) {
00063       clazz = queue.dequeue();
00064 
00065       String   s          = clazz.getSuperclassName();
00066       String[] interfaces = clazz.getInterfaceNames();
00067 
00068       if(clazz.isInterface())
00069     vec.addElement(clazz);
00070       else if(!s.equals("java.lang.Object"))
00071     queue.enqueue(lookupClass(s));
00072       
00073       for(int i=0; i < interfaces.length; i++)
00074     queue.enqueue(lookupClass(interfaces[i]));
00075     }
00076 
00077     return vec.toArray();
00078   }  
00079   /**
00080    * @return all interfaces implemented by class and its super
00081    * classes and the interfaces that extend those interfaces, and so on
00082    */
00083   public static JavaClass[] getInterfaces(String class_name) {
00084     return getInterfaces(lookupClass(class_name));
00085   }  
00086   private static final JavaClass getSuperClass(JavaClass clazz) {
00087     if(clazz == OBJECT)
00088       return null;
00089 
00090     return lookupClass(clazz.getSuperclassName());
00091   }  
00092   /**
00093    * @return list of super classes of clazz in ascending order, i.e.,
00094    * Object is always the last element
00095    */
00096   public static JavaClass[] getSuperClasses(JavaClass clazz) {
00097     ClassVector vec = new ClassVector();
00098 
00099     for(clazz = getSuperClass(clazz); clazz != null; clazz = getSuperClass(clazz))
00100       vec.addElement(clazz);
00101 
00102     return vec.toArray();
00103   }  
00104   /**
00105    * @return list of super classes of clazz in ascending order, i.e.,
00106    * Object is always the last element
00107    */
00108   public static JavaClass[] getSuperClasses(String class_name) {
00109     return getSuperClasses(lookupClass(class_name));
00110   }  
00111   /**
00112    * @return true, if clazz is an implementation of interface inter
00113    */
00114   public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
00115     if(clazz == inter)
00116       return true;
00117 
00118     JavaClass[] super_interfaces = getInterfaces(clazz);
00119 
00120     for(int i=0; i < super_interfaces.length; i++)
00121       if(super_interfaces[i] == inter)
00122     return true;
00123 
00124     return false;
00125   }  
00126   /**
00127    * @return true, if clazz is an implementation of interface inter
00128    */
00129   public static boolean implementationOf(JavaClass clazz, String inter) {
00130     return implementationOf(clazz, lookupClass(inter));
00131   }  
00132   /**
00133    * @return true, if clazz is an implementation of interface inter
00134    */
00135   public static boolean implementationOf(String clazz, JavaClass inter) {
00136     return implementationOf(lookupClass(clazz), inter);
00137   }  
00138   /**
00139    * @return true, if clazz is an implementation of interface inter
00140    */
00141   public static boolean implementationOf(String clazz, String inter) {
00142     return implementationOf(lookupClass(clazz), lookupClass(inter));
00143   }  
00144   /**
00145    * @return true, if clazz is an instance of super_class
00146    */
00147   public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
00148     if(clazz == super_class)
00149       return true;
00150 
00151     JavaClass[] super_classes = getSuperClasses(clazz);
00152 
00153     for(int i=0; i < super_classes.length; i++)
00154       if(super_classes[i] == super_class)
00155     return true;
00156 
00157     if(super_class.isInterface())
00158       return implementationOf(clazz, super_class);
00159 
00160     return false;
00161   }  
00162   /**
00163    * @return true, if clazz is an instance of super_class
00164    */
00165   public static boolean instanceOf(JavaClass clazz, String super_class) {
00166     return instanceOf(clazz, lookupClass(super_class));
00167   }  
00168   /**
00169    * @return true, if clazz is an instance of super_class
00170    */
00171   public static boolean instanceOf(String clazz, JavaClass super_class) {
00172     return instanceOf(lookupClass(clazz), super_class);
00173   }  
00174   /**
00175    * @return true, if clazz is an instance of super_class
00176    */
00177   public static boolean instanceOf(String clazz, String super_class) {
00178     return instanceOf(lookupClass(clazz), lookupClass(super_class));
00179   }  
00180   /** @return class object for given fully qualified class name.
00181    */
00182   public static JavaClass lookupClass(String class_name) {
00183     if(class_name == null || class_name.equals(""))
00184       return null;
00185 
00186     class_name = class_name.replace('/', '.');
00187 
00188     JavaClass clazz = (JavaClass)classes.get(class_name);
00189 
00190     if(clazz == null) {
00191       try {
00192     InputStream is = class_path.getInputStream(class_name);
00193     clazz = new ClassParser(is, class_name).parse();
00194       } catch(IOException e) { return null; }
00195 
00196       classes.put(class_name, clazz);
00197     }
00198 
00199     return clazz;
00200   }  
00201   /** @return class file object for given Java class.
00202    */
00203   public static ClassPath.ClassFile lookupClassFile(String class_name) {
00204     try {
00205       return class_path.getClassFile(class_name);
00206     } catch(IOException e) { return null; }
00207   }  
00208   /**
00209    * Remove given class from repository.
00210    */
00211   public static void removeClass(JavaClass clazz) {
00212     removeClass(clazz.getClassName());
00213   }  
00214   /**
00215    * Remove class with given (fully qualifid) name from repository.
00216    */
00217   public static void removeClass(String clazz) {
00218     classes.remove(clazz);
00219   }  
00220 }

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