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
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
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
00054
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
00081
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
00094
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
00106
00107
00108 public static JavaClass[] getSuperClasses(String class_name) {
00109 return getSuperClasses(lookupClass(class_name));
00110 }
00111
00112
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
00128
00129 public static boolean implementationOf(JavaClass clazz, String inter) {
00130 return implementationOf(clazz, lookupClass(inter));
00131 }
00132
00133
00134
00135 public static boolean implementationOf(String clazz, JavaClass inter) {
00136 return implementationOf(lookupClass(clazz), inter);
00137 }
00138
00139
00140
00141 public static boolean implementationOf(String clazz, String inter) {
00142 return implementationOf(lookupClass(clazz), lookupClass(inter));
00143 }
00144
00145
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
00164
00165 public static boolean instanceOf(JavaClass clazz, String super_class) {
00166 return instanceOf(clazz, lookupClass(super_class));
00167 }
00168
00169
00170
00171 public static boolean instanceOf(String clazz, JavaClass super_class) {
00172 return instanceOf(lookupClass(clazz), super_class);
00173 }
00174
00175
00176
00177 public static boolean instanceOf(String clazz, String super_class) {
00178 return instanceOf(lookupClass(clazz), lookupClass(super_class));
00179 }
00180
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
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
00210
00211 public static void removeClass(JavaClass clazz) {
00212 removeClass(clazz.getClassName());
00213 }
00214
00215
00216
00217 public static void removeClass(String clazz) {
00218 classes.remove(clazz);
00219 }
00220 }