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
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 public abstract class Repository {
00021 private static ClassPath class_path = new ClassPath();
00022 private static Hashtable classes;
00023 private static JavaClass OBJECT;
00024
00025 static { clearCache(); }
00026
00027
00028
00029
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
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
00053
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
00080
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
00093
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
00105
00106
00107 public static JavaClass[] getSuperClasses(String class_name) {
00108 return getSuperClasses(lookupClass(class_name));
00109 }
00110
00111
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
00127
00128 public static boolean implementationOf(JavaClass clazz, String inter) {
00129 return implementationOf(clazz, lookupClass(inter));
00130 }
00131
00132
00133
00134 public static boolean implementationOf(String clazz, JavaClass inter) {
00135 return implementationOf(lookupClass(clazz), inter);
00136 }
00137
00138
00139
00140 public static boolean implementationOf(String clazz, String inter) {
00141 return implementationOf(lookupClass(clazz), lookupClass(inter));
00142 }
00143
00144
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
00163
00164 public static boolean instanceOf(JavaClass clazz, String super_class) {
00165 return instanceOf(clazz, lookupClass(super_class));
00166 }
00167
00168
00169
00170 public static boolean instanceOf(String clazz, JavaClass super_class) {
00171 return instanceOf(lookupClass(clazz), super_class);
00172 }
00173
00174
00175
00176 public static boolean instanceOf(String clazz, String super_class) {
00177 return instanceOf(lookupClass(clazz), lookupClass(super_class));
00178 }
00179
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
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 }