1 19 20 23 package edu.umd.cs.findbugs.ba; 24 25 import java.io.IOException ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.apache.bcel.classfile.JavaClass; 30 import org.apache.bcel.util.ClassPath; 31 import org.apache.bcel.util.Repository; 32 33 import edu.umd.cs.findbugs.SystemProperties; 34 import edu.umd.cs.findbugs.annotations.NonNull; 35 36 37 51 public class URLClassPathRepository implements Repository { 52 public static final boolean DEBUG = SystemProperties.getBoolean("findbugs.classpath.debug"); 53 54 private static final long serialVersionUID = 1L; 55 56 private Map <String , JavaClass> nameToClassMap; 57 private URLClassPath urlClassPath; 58 59 public URLClassPathRepository() { 60 this.nameToClassMap = new HashMap <String , JavaClass>(); 61 this.urlClassPath = new URLClassPath(); 62 } 63 64 67 public void destroy() { 68 nameToClassMap.clear(); 69 urlClassPath.close(); 70 if (DEBUG) { 71 System.out.println("Destroying Repository"); 72 } 73 } 74 75 80 public void addURL(String fileName) throws IOException { 81 urlClassPath.addURL(fileName); 82 } 83 84 87 public void storeClass(JavaClass javaClass) { 88 if (DEBUG) System.out.println("Storing class " + javaClass.getClassName() + " in repository"); 89 JavaClass previous = nameToClassMap.put(javaClass.getClassName(), javaClass); 90 if (DEBUG && previous != null) { 91 System.out.println("\t==> A previous class was evicted!"); 92 dumpStack(); 93 } 94 Repository tmp = org.apache.bcel.Repository.getRepository(); 95 if (tmp != null && tmp != this) 96 throw new IllegalStateException ("Wrong/multiple BCEL repository"); 97 if (tmp == null) 98 org.apache.bcel.Repository.setRepository(this); 99 } 100 101 104 public void removeClass(JavaClass javaClass) { 105 nameToClassMap.remove(javaClass.getClassName()); 106 if (DEBUG) { 107 System.out.println("Removing class " + javaClass.getClassName() + " from Repository"); 108 dumpStack(); 109 } 110 } 111 112 private void dumpStack() { 113 new Throwable ().printStackTrace(System.out); 114 } 115 116 119 public JavaClass findClass(@NonNull String className) { 120 String dottedClassName = className.replace('/', '.'); 126 127 return nameToClassMap.get(dottedClassName); 128 } 129 130 133 public JavaClass loadClass(@NonNull String className) throws ClassNotFoundException { 134 if (className == null) throw new IllegalArgumentException ("className is null"); 135 JavaClass javaClass = findClass(className); 137 if (javaClass == null) { 138 if (DEBUG) { 139 System.out.println("Looking up " + className + " on classpath"); 140 dumpStack(); 141 } 142 javaClass = urlClassPath.lookupClass(className); 143 storeClass(javaClass); 144 } 145 return javaClass; 146 } 147 148 151 public JavaClass loadClass(Class clazz) throws ClassNotFoundException { 152 return loadClass(clazz.getName()); 153 } 154 155 158 public void clear() { 159 if (DEBUG) { 160 System.out.println("Clearing Repository!"); 161 dumpStack(); 162 } 163 nameToClassMap.clear(); 164 } 165 166 169 public ClassPath getClassPath() { 170 return new ClassPath(urlClassPath.getClassPath()); 171 } 172 } 173 174 | Popular Tags |