1 17 package org.apache.bcel.util; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.lang.ref.SoftReference ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import org.apache.bcel.classfile.ClassParser; 25 import org.apache.bcel.classfile.JavaClass; 26 27 48 public class SyntheticRepository implements Repository { 49 50 private static final String DEFAULT_PATH = ClassPath.getClassPath(); 51 private static Map _instances = new HashMap (); private ClassPath _path = null; 53 private Map _loadedClasses = new HashMap (); 55 56 private SyntheticRepository(ClassPath path) { 57 _path = path; 58 } 59 60 61 public static SyntheticRepository getInstance() { 62 return getInstance(ClassPath.SYSTEM_CLASS_PATH); 63 } 64 65 66 public static SyntheticRepository getInstance( ClassPath classPath ) { 67 SyntheticRepository rep = (SyntheticRepository) _instances.get(classPath); 68 if (rep == null) { 69 rep = new SyntheticRepository(classPath); 70 _instances.put(classPath, rep); 71 } 72 return rep; 73 } 74 75 76 79 public void storeClass( JavaClass clazz ) { 80 _loadedClasses.put(clazz.getClassName(), new SoftReference (clazz)); 81 clazz.setRepository(this); 82 } 83 84 85 88 public void removeClass( JavaClass clazz ) { 89 _loadedClasses.remove(clazz.getClassName()); 90 } 91 92 93 96 public JavaClass findClass( String className ) { 97 SoftReference ref = (SoftReference ) _loadedClasses.get(className); 98 if (ref == null) { 99 return null; 100 } 101 return (JavaClass) ref.get(); 102 } 103 104 105 116 public JavaClass loadClass( String className ) throws ClassNotFoundException { 117 if (className == null || className.equals("")) { 118 throw new IllegalArgumentException ("Invalid class name " + className); 119 } 120 className = className.replace('/', '.'); JavaClass clazz = findClass(className); 122 if (clazz != null) { 123 return clazz; 124 } 125 try { 126 return loadClass(_path.getInputStream(className), className); 127 } catch (IOException e) { 128 throw new ClassNotFoundException ("Exception while looking for class " + className 129 + ": " + e.toString()); 130 } 131 } 132 133 134 147 public JavaClass loadClass( Class clazz ) throws ClassNotFoundException { 148 String className = clazz.getName(); 149 JavaClass repositoryClass = findClass(className); 150 if (repositoryClass != null) { 151 return repositoryClass; 152 } 153 String name = className; 154 int i = name.lastIndexOf('.'); 155 if (i > 0) { 156 name = name.substring(i + 1); 157 } 158 return loadClass(clazz.getResourceAsStream(name + ".class"), className); 159 } 160 161 162 private JavaClass loadClass( InputStream is, String className ) throws ClassNotFoundException { 163 try { 164 if (is != null) { 165 ClassParser parser = new ClassParser(is, className); 166 JavaClass clazz = parser.parse(); 167 storeClass(clazz); 168 return clazz; 169 } 170 } catch (IOException e) { 171 throw new ClassNotFoundException ("Exception while looking for class " + className 172 + ": " + e.toString()); 173 } 174 throw new ClassNotFoundException ("SyntheticRepository could not load " + className); 175 } 176 177 178 180 public ClassPath getClassPath() { 181 return _path; 182 } 183 184 185 187 public void clear() { 188 _loadedClasses.clear(); 189 } 190 } 191 | Popular Tags |