FileDocCategorySizeDatePackage
Repository.javaAPI DocJava SE 5 API9503Fri Aug 26 14:55:16 BST 2005com.sun.org.apache.bcel.internal

Repository

public abstract class Repository extends Object
Repository maintains informations about class interdependencies, e.g. whether a class is a sub-class of another. JavaClass objects are put into a cache which can be purged with clearCache(). All JavaClass objects used as arguments must have been obtained via the repository or been added with addClass() manually. This is because we have to check for object identity (==).
version
$Id: Repository.java,v 1.1.1.1 2001/10/29 19:59:57 jvanzyl Exp $
author
M. Dahm

Fields Summary
private static ClassPath
class_path
private static HashMap
classes
private static JavaClass
OBJECT
Constructors Summary
Methods Summary
public static com.sun.org.apache.bcel.internal.classfile.JavaClassaddClass(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz)
Add clazz to repository if there isn't an equally named class already in there.

return
old entry in repository

    String    name = clazz.getClassName();
    JavaClass cl   = (JavaClass)classes.get(name);

    if(cl == null)
      classes.put(name, cl = clazz);

    return cl;
  
public static voidclearCache()
Clear the repository.

    classes = new HashMap();
    OBJECT  = lookupClass("java.lang.Object");

    if(OBJECT == null)
      System.err.println("Warning: java.lang.Object not found on CLASSPATH!");
    else
      classes.put("java.lang.Object", OBJECT);
  
public static com.sun.org.apache.bcel.internal.classfile.JavaClass[]getInterfaces(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz)

return
all interfaces implemented by class and its super classes and the interfaces that those interfaces extend, and so on

    ClassVector vec   = new ClassVector();
    ClassQueue  queue = new ClassQueue();

    queue.enqueue(clazz);

    while(!queue.empty()) {
      clazz = queue.dequeue();

      String   s          = clazz.getSuperclassName();
      String[] interfaces = clazz.getInterfaceNames();

      if(clazz.isInterface())
	vec.addElement(clazz);
      else if(!s.equals("java.lang.Object"))
	queue.enqueue(lookupClass(s));
      
      for(int i=0; i < interfaces.length; i++)
	queue.enqueue(lookupClass(interfaces[i]));
    }

    return vec.toArray();
  
public static com.sun.org.apache.bcel.internal.classfile.JavaClass[]getInterfaces(java.lang.String class_name)

return
all interfaces implemented by class and its super classes and the interfaces that extend those interfaces, and so on

    return getInterfaces(lookupClass(class_name));
  
private static final com.sun.org.apache.bcel.internal.classfile.JavaClassgetSuperClass(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz)

    if(clazz == OBJECT)
      return null;

    return lookupClass(clazz.getSuperclassName());
  
public static com.sun.org.apache.bcel.internal.classfile.JavaClass[]getSuperClasses(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz)

return
list of super classes of clazz in ascending order, i.e., Object is always the last element

    ClassVector vec = new ClassVector();

    for(clazz = getSuperClass(clazz); clazz != null; clazz = getSuperClass(clazz))
      vec.addElement(clazz);

    return vec.toArray();
  
public static com.sun.org.apache.bcel.internal.classfile.JavaClass[]getSuperClasses(java.lang.String class_name)

return
list of super classes of clazz in ascending order, i.e., Object is always the last element. "null", if clazz cannot be found.

    JavaClass jc = lookupClass(class_name);
    return (jc == null? null : getSuperClasses(jc));
  
public static booleanimplementationOf(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz, com.sun.org.apache.bcel.internal.classfile.JavaClass inter)

return
true, if clazz is an implementation of interface inter

    if(clazz == inter)
      return true;

    JavaClass[] super_interfaces = getInterfaces(clazz);

    for(int i=0; i < super_interfaces.length; i++)
      if(super_interfaces[i] == inter)
	return true;

    return false;
  
public static booleanimplementationOf(java.lang.String clazz, java.lang.String inter)

return
true, if clazz is an implementation of interface inter

    return implementationOf(lookupClass(clazz), lookupClass(inter));
  
public static booleanimplementationOf(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz, java.lang.String inter)

return
true, if clazz is an implementation of interface inter

    return implementationOf(clazz, lookupClass(inter));
  
public static booleanimplementationOf(java.lang.String clazz, com.sun.org.apache.bcel.internal.classfile.JavaClass inter)

return
true, if clazz is an implementation of interface inter

    return implementationOf(lookupClass(clazz), inter);
  
public static booleaninstanceOf(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz, com.sun.org.apache.bcel.internal.classfile.JavaClass super_class)

return
true, if clazz is an instance of super_class

    if(clazz == super_class)
      return true;

    JavaClass[] super_classes = getSuperClasses(clazz);

    for(int i=0; i < super_classes.length; i++)
      if(super_classes[i] == super_class)
	return true;

    if(super_class.isInterface())
      return implementationOf(clazz, super_class);

    return false;
  
public static booleaninstanceOf(java.lang.String clazz, java.lang.String super_class)

return
true, if clazz is an instance of super_class

    return instanceOf(lookupClass(clazz), lookupClass(super_class));
  
public static booleaninstanceOf(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz, java.lang.String super_class)

return
true, if clazz is an instance of super_class

    return instanceOf(clazz, lookupClass(super_class));
  
public static booleaninstanceOf(java.lang.String clazz, com.sun.org.apache.bcel.internal.classfile.JavaClass super_class)

return
true, if clazz is an instance of super_class

    return instanceOf(lookupClass(clazz), super_class);
  
public static com.sun.org.apache.bcel.internal.classfile.JavaClasslookupClass(java.lang.String class_name)

return
class object for given fully qualified class name.

 // should be final ...

    clearCache(); 
    if(class_name == null || class_name.equals(""))
      throw new RuntimeException("Invalid class name");

    class_name = class_name.replace('/", '.");

    JavaClass clazz = (JavaClass)classes.get(class_name);

    if(clazz == null) {
      try {
	InputStream is = class_path.getInputStream(class_name);
	clazz = new ClassParser(is, class_name).parse();
	class_name = clazz.getClassName();
      } catch(IOException e) { return null; }

      classes.put(class_name, clazz);
    }

    return clazz;
  
public static ClassPath.ClassFilelookupClassFile(java.lang.String class_name)

return
class file object for given Java class.

    try {
      return class_path.getClassFile(class_name);
    } catch(IOException e) { return null; }
  
public static voidremoveClass(java.lang.String clazz)
Remove class with given (fully qualifid) name from repository.

    classes.remove(clazz);
  
public static voidremoveClass(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz)
Remove given class from repository.

    removeClass(clazz.getClassName());