1 package com.sun.org.apache.bcel.internal.util; 2 3 56 57 import java.io.*; 58 59 import java.util.Map ; 60 import java.util.HashMap ; 61 62 import com.sun.org.apache.bcel.internal.classfile.*; 63 64 77 public class ClassLoaderRepository 78 implements Repository 79 { 80 private java.lang.ClassLoader loader; 81 private HashMap loadedClasses = 82 new HashMap (); 84 public ClassLoaderRepository( java.lang.ClassLoader loader ) { 85 this.loader = loader; 86 } 87 88 91 public void storeClass( JavaClass clazz ) { 92 loadedClasses.put( clazz.getClassName(), 93 clazz ); 94 clazz.setRepository( this ); 95 } 96 97 100 public void removeClass(JavaClass clazz) { 101 loadedClasses.remove(clazz.getClassName()); 102 } 103 104 107 public JavaClass findClass( String className ) { 108 if ( loadedClasses.containsKey( className )) { 109 return (JavaClass) loadedClasses.get( className ); 110 } else { 111 return null; 112 } 113 } 114 115 118 public JavaClass loadClass( String className ) 119 throws ClassNotFoundException 120 { 121 String classFile = className.replace('.', '/'); 122 123 JavaClass RC = findClass( className ); 124 if (RC != null) { return RC; } 125 126 try { 127 InputStream is = 128 loader.getResourceAsStream( classFile + ".class" ); 129 130 if(is == null) { 131 throw new ClassNotFoundException (className + " not found."); 132 } 133 134 ClassParser parser = new ClassParser( is, className ); 135 RC = parser.parse(); 136 137 storeClass( RC ); 138 139 return RC; 140 } catch (IOException e) { 141 throw new ClassNotFoundException ( e.toString() ); 142 } 143 } 144 145 public JavaClass loadClass(Class clazz) throws ClassNotFoundException { 146 return loadClass(clazz.getName()); 147 } 148 149 151 public void clear() { 152 loadedClasses.clear(); 153 } 154 } 155 156 | Popular Tags |