1 17 package org.apache.bcel.util; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import org.apache.bcel.classfile.ClassParser; 24 import org.apache.bcel.classfile.JavaClass; 25 26 39 public class ClassLoaderRepository implements Repository { 40 41 private java.lang.ClassLoader loader; 42 private Map loadedClasses = new HashMap (); 44 45 public ClassLoaderRepository(java.lang.ClassLoader loader) { 46 this.loader = loader; 47 } 48 49 50 53 public void storeClass( JavaClass clazz ) { 54 loadedClasses.put(clazz.getClassName(), clazz); 55 clazz.setRepository(this); 56 } 57 58 59 62 public void removeClass( JavaClass clazz ) { 63 loadedClasses.remove(clazz.getClassName()); 64 } 65 66 67 70 public JavaClass findClass( String className ) { 71 if (loadedClasses.containsKey(className)) { 72 return (JavaClass) loadedClasses.get(className); 73 } else { 74 return null; 75 } 76 } 77 78 79 82 public JavaClass loadClass( String className ) throws ClassNotFoundException { 83 String classFile = className.replace('.', '/'); 84 JavaClass RC = findClass(className); 85 if (RC != null) { 86 return RC; 87 } 88 try { 89 InputStream is = loader.getResourceAsStream(classFile + ".class"); 90 if (is == null) { 91 throw new ClassNotFoundException (className + " not found."); 92 } 93 ClassParser parser = new ClassParser(is, className); 94 RC = parser.parse(); 95 storeClass(RC); 96 return RC; 97 } catch (IOException e) { 98 throw new ClassNotFoundException (e.toString()); 99 } 100 } 101 102 103 public JavaClass loadClass( Class clazz ) throws ClassNotFoundException { 104 return loadClass(clazz.getName()); 105 } 106 107 108 110 public void clear() { 111 loadedClasses.clear(); 112 } 113 114 115 118 public ClassPath getClassPath() { 119 return null; 120 } 121 } 122 | Popular Tags |