1 15 16 package javassist; 17 18 import java.io.*; 19 import java.util.Hashtable ; 20 import java.util.Vector ; 21 22 130 public class Loader extends ClassLoader { 131 private Hashtable notDefinedHere; private Vector notDefinedPackages; private ClassPool source; 134 private Translator translator; 135 136 147 public boolean doDelegation = true; 148 149 152 public Loader() { 153 this(null); 154 } 155 156 161 public Loader(ClassPool cp) { 162 init(cp); 163 } 164 165 172 public Loader(ClassLoader parent, ClassPool cp) { 173 super(parent); 174 init(cp); 175 } 176 177 private void init(ClassPool cp) { 178 notDefinedHere = new Hashtable (); 179 notDefinedPackages = new Vector (); 180 source = cp; 181 translator = null; 182 delegateLoadingOf("javassist.Loader"); 183 } 184 185 193 public void delegateLoadingOf(String classname) { 194 if (classname.endsWith(".")) 195 notDefinedPackages.addElement(classname); 196 else 197 notDefinedHere.put(classname, this); 198 } 199 200 203 public void setClassPool(ClassPool cp) { 204 source = cp; 205 } 206 207 216 public void addTranslator(ClassPool cp, Translator t) 217 throws NotFoundException, CannotCompileException { 218 source = cp; 219 translator = t; 220 t.start(cp); 221 } 222 223 238 public static void main(String [] args) throws Throwable { 239 Loader cl = new Loader(); 240 cl.run(args); 241 } 242 243 253 public void run(String [] args) throws Throwable { 254 int n = args.length - 1; 255 if (n >= 0) { 256 String [] args2 = new String [n]; 257 for (int i = 0; i < n; ++i) 258 args2[i] = args[i + 1]; 259 260 run(args[0], args2); 261 } 262 } 263 264 270 public void run(String classname, String [] args) throws Throwable { 271 Class c = loadClass(classname); 272 try { 273 c.getDeclaredMethod("main", new Class [] { String [].class }).invoke( 274 null, 275 new Object [] { args }); 276 } 277 catch (java.lang.reflect.InvocationTargetException e) { 278 throw e.getTargetException(); 279 } 280 } 281 282 285 protected Class loadClass(String name, boolean resolve) 286 throws ClassFormatError , ClassNotFoundException { 287 Class c = findLoadedClass(name); 288 if (c == null) 289 c = loadClassByDelegation(name); 290 291 if (c == null) 292 c = findClass(name); 293 294 if (c == null) 295 c = delegateToParent(name); 296 297 if (resolve) 298 resolveClass(c); 299 300 return c; 301 } 302 303 315 protected Class findClass(String name) throws ClassNotFoundException { 316 byte[] classfile; 317 try { 318 if (source != null) { 319 if (translator != null) 320 translator.onLoad(source, name); 321 322 try { 323 classfile = source.get(name).toBytecode(); 324 } 325 catch (NotFoundException e) { 326 return null; 327 } 328 } 329 else { 330 String jarname = "/" + name.replace('.', '/') + ".class"; 331 InputStream in = this.getClass().getResourceAsStream(jarname); 332 if (in == null) 333 return null; 334 335 classfile = ClassPoolTail.readStream(in); 336 } 337 } 338 catch (Exception e) { 339 throw new ClassNotFoundException ( 340 "caught an exception while obtaining a class file for " 341 + name, e); 342 } 343 344 int i = name.lastIndexOf('.'); 345 if (i != -1) { 346 String pname = name.substring(0, i); 347 if (getPackage(pname) == null) 348 try { 349 definePackage( 350 pname, null, null, null, null, null, null, null); 351 } 352 catch (IllegalArgumentException e) { 353 } 356 } 357 358 return defineClass(name, classfile, 0, classfile.length); 359 } 360 361 protected Class loadClassByDelegation(String name) 362 throws ClassNotFoundException 363 { 364 373 374 Class c = null; 375 if (doDelegation) 376 if (name.startsWith("java.") 377 || name.startsWith("javax.") 378 || name.startsWith("sun.") 379 || name.startsWith("com.sun.") 380 || name.startsWith("org.w3c.") 381 || name.startsWith("org.xml.") 382 || notDelegated(name)) 383 c = delegateToParent(name); 384 385 return c; 386 } 387 388 private boolean notDelegated(String name) { 389 if (notDefinedHere.get(name) != null) 390 return true; 391 392 int n = notDefinedPackages.size(); 393 for (int i = 0; i < n; ++i) 394 if (name.startsWith((String )notDefinedPackages.elementAt(i))) 395 return true; 396 397 return false; 398 } 399 400 protected Class delegateToParent(String classname) 401 throws ClassNotFoundException 402 { 403 ClassLoader cl = getParent(); 404 if (cl != null) 405 return cl.loadClass(classname); 406 else 407 return findSystemClass(classname); 408 } 409 410 protected Package getPackage(String name) { 411 return super.getPackage(name); 412 } 413 423 } 424 | Popular Tags |