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 85 public class SyntheticRepository implements Repository { 86 private static final String DEFAULT_PATH = ClassPath.getClassPath(); 87 88 private static HashMap _instances = new HashMap (); 90 private ClassPath _path = null; 91 private HashMap _loadedClasses = new HashMap (); 93 private SyntheticRepository(ClassPath path) { 94 _path = path; 95 } 96 97 public static SyntheticRepository getInstance() { 98 return getInstance(ClassPath.SYSTEM_CLASS_PATH); 99 } 100 101 public static SyntheticRepository getInstance(ClassPath classPath) { 102 SyntheticRepository rep = (SyntheticRepository)_instances.get(classPath); 103 104 if(rep == null) { 105 rep = new SyntheticRepository(classPath); 106 _instances.put(classPath, rep); 107 } 108 109 return rep; 110 } 111 112 115 public void storeClass(JavaClass clazz) { 116 _loadedClasses.put(clazz.getClassName(), clazz); 117 clazz.setRepository(this); 118 } 119 120 123 public void removeClass(JavaClass clazz) { 124 _loadedClasses.remove(clazz.getClassName()); 125 } 126 127 130 public JavaClass findClass(String className) { 131 return (JavaClass)_loadedClasses.get(className); 132 } 133 134 138 public JavaClass loadClass(String className) 139 throws ClassNotFoundException 140 { 141 if(className == null || className.equals("")) { 142 throw new IllegalArgumentException ("Invalid class name " + className); 143 } 144 145 className = className.replace('/', '.'); 147 try { 148 return loadClass(_path.getInputStream(className), className); 149 } catch(IOException e) { 150 throw new ClassNotFoundException ("Exception while looking for class " + 151 className + ": " + e.toString()); 152 } 153 } 154 155 160 public JavaClass loadClass(Class clazz) throws ClassNotFoundException { 161 String className = clazz.getName(); 162 String name = className; 163 int i = name.lastIndexOf('.'); 164 165 if(i > 0) { 166 name = name.substring(i + 1); 167 } 168 169 return loadClass(clazz.getResourceAsStream(name + ".class"), className); 170 } 171 172 private JavaClass loadClass(InputStream is, String className) 173 throws ClassNotFoundException 174 { 175 JavaClass clazz = findClass(className); 176 177 if(clazz != null) { 178 return clazz; 179 } 180 181 try { 182 if(is != null) { 183 ClassParser parser = new ClassParser(is, className); 184 clazz = parser.parse(); 185 186 storeClass(clazz); 187 188 return clazz; 189 } 190 } catch(IOException e) { 191 throw new ClassNotFoundException ("Exception while looking for class " + 192 className + ": " + e.toString()); 193 } 194 195 throw new ClassNotFoundException ("SyntheticRepository could not load " + 196 className); 197 } 198 199 201 public void clear() { 202 _loadedClasses.clear(); 203 } 204 } 205 | Popular Tags |