1 package com.sun.org.apache.bcel.internal; 2 3 56 57 import com.sun.org.apache.bcel.internal.classfile.*; 58 import com.sun.org.apache.bcel.internal.util.*; 59 import java.util.HashMap ; 60 import java.io.*; 61 62 74 public abstract class Repository { 75 private static ClassPath class_path = new ClassPath(); 76 private static HashMap classes; 77 private static JavaClass OBJECT; 79 static { clearCache(); } 80 81 83 public static JavaClass lookupClass(String class_name) { 84 if(class_name == null || class_name.equals("")) 85 throw new RuntimeException ("Invalid class name"); 86 87 class_name = class_name.replace('/', '.'); 88 89 JavaClass clazz = (JavaClass)classes.get(class_name); 90 91 if(clazz == null) { 92 try { 93 InputStream is = class_path.getInputStream(class_name); 94 clazz = new ClassParser(is, class_name).parse(); 95 class_name = clazz.getClassName(); 96 } catch(IOException e) { return null; } 97 98 classes.put(class_name, clazz); 99 } 100 101 return clazz; 102 } 103 104 106 public static ClassPath.ClassFile lookupClassFile(String class_name) { 107 try { 108 return class_path.getClassFile(class_name); 109 } catch(IOException e) { return null; } 110 } 111 112 114 public static void clearCache() { 115 classes = new HashMap (); 116 OBJECT = lookupClass("java.lang.Object"); 117 118 if(OBJECT == null) 119 System.err.println("Warning: java.lang.Object not found on CLASSPATH!"); 120 else 121 classes.put("java.lang.Object", OBJECT); 122 } 123 124 129 public static JavaClass addClass(JavaClass clazz) { 130 String name = clazz.getClassName(); 131 JavaClass cl = (JavaClass)classes.get(name); 132 133 if(cl == null) 134 classes.put(name, cl = clazz); 135 136 return cl; 137 } 138 139 142 public static void removeClass(String clazz) { 143 classes.remove(clazz); 144 } 145 146 149 public static void removeClass(JavaClass clazz) { 150 removeClass(clazz.getClassName()); 151 } 152 153 154 private static final JavaClass getSuperClass(JavaClass clazz) { 155 if(clazz == OBJECT) 156 return null; 157 158 return lookupClass(clazz.getSuperclassName()); 159 } 160 161 165 public static JavaClass[] getSuperClasses(JavaClass clazz) { 166 ClassVector vec = new ClassVector(); 167 168 for(clazz = getSuperClass(clazz); clazz != null; clazz = getSuperClass(clazz)) 169 vec.addElement(clazz); 170 171 return vec.toArray(); 172 } 173 174 178 public static JavaClass[] getSuperClasses(String class_name) { 179 JavaClass jc = lookupClass(class_name); 180 return (jc == null? null : getSuperClasses(jc)); 181 } 182 183 187 public static JavaClass[] getInterfaces(JavaClass clazz) { 188 ClassVector vec = new ClassVector(); 189 ClassQueue queue = new ClassQueue(); 190 191 queue.enqueue(clazz); 192 193 while(!queue.empty()) { 194 clazz = queue.dequeue(); 195 196 String s = clazz.getSuperclassName(); 197 String [] interfaces = clazz.getInterfaceNames(); 198 199 if(clazz.isInterface()) 200 vec.addElement(clazz); 201 else if(!s.equals("java.lang.Object")) 202 queue.enqueue(lookupClass(s)); 203 204 for(int i=0; i < interfaces.length; i++) 205 queue.enqueue(lookupClass(interfaces[i])); 206 } 207 208 return vec.toArray(); 209 } 210 211 215 public static JavaClass[] getInterfaces(String class_name) { 216 return getInterfaces(lookupClass(class_name)); 217 } 218 219 222 public static boolean instanceOf(JavaClass clazz, JavaClass super_class) { 223 if(clazz == super_class) 224 return true; 225 226 JavaClass[] super_classes = getSuperClasses(clazz); 227 228 for(int i=0; i < super_classes.length; i++) 229 if(super_classes[i] == super_class) 230 return true; 231 232 if(super_class.isInterface()) 233 return implementationOf(clazz, super_class); 234 235 return false; 236 } 237 238 241 public static boolean instanceOf(String clazz, String super_class) { 242 return instanceOf(lookupClass(clazz), lookupClass(super_class)); 243 } 244 245 248 public static boolean instanceOf(JavaClass clazz, String super_class) { 249 return instanceOf(clazz, lookupClass(super_class)); 250 } 251 252 255 public static boolean instanceOf(String clazz, JavaClass super_class) { 256 return instanceOf(lookupClass(clazz), super_class); 257 } 258 259 262 public static boolean implementationOf(JavaClass clazz, JavaClass inter) { 263 if(clazz == inter) 264 return true; 265 266 JavaClass[] super_interfaces = getInterfaces(clazz); 267 268 for(int i=0; i < super_interfaces.length; i++) 269 if(super_interfaces[i] == inter) 270 return true; 271 272 return false; 273 } 274 275 278 public static boolean implementationOf(String clazz, String inter) { 279 return implementationOf(lookupClass(clazz), lookupClass(inter)); 280 } 281 282 285 public static boolean implementationOf(JavaClass clazz, String inter) { 286 return implementationOf(clazz, lookupClass(inter)); 287 } 288 289 292 public static boolean implementationOf(String clazz, JavaClass inter) { 293 return implementationOf(lookupClass(clazz), inter); 294 } 295 } 296 | Popular Tags |