KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > URLClassPathRepository


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 /*
21  * Created on Sep 20, 2004
22  */

23 package edu.umd.cs.findbugs.ba;
24
25 import java.io.IOException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
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 /**
38  * BCEL Repository implementation that uses an URLClassPath
39  * to find classes. This class has two specific improvements
40  * over BCEL's SyntheticRepository class:
41  * <ol>
42  * <li> Classpath elements may be added at any time, not
43  * just when the object is created.
44  * <li> Classpath elements can be URLs. This allows repository
45  * lookups to find classes via http URLs, jar URLs, etc.
46  * </ol>
47  * FindBugs requires and uses both of these capabilities.
48  *
49  * @author David Hovemeyer
50  */

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 JavaDoc<String JavaDoc, JavaClass> nameToClassMap;
57     private URLClassPath urlClassPath;
58     
59     public URLClassPathRepository() {
60         this.nameToClassMap = new HashMap JavaDoc<String JavaDoc, JavaClass>();
61         this.urlClassPath = new URLClassPath();
62     }
63     
64     /**
65      * Clear the repository and close all underlying resources.
66      */

67     public void destroy() {
68         nameToClassMap.clear();
69         urlClassPath.close();
70         if (DEBUG) {
71             System.out.println("Destroying Repository");
72         }
73     }
74
75     /**
76      * Add a filename or URL to the classpath.
77      * @param fileName filename or URL of classpath entry to add
78      * @throws IOException
79      */

80     public void addURL(String JavaDoc fileName) throws IOException JavaDoc {
81         urlClassPath.addURL(fileName);
82     }
83     
84     /* (non-Javadoc)
85      * @see org.apache.bcel.util.Repository#storeClass(org.apache.bcel.classfile.JavaClass)
86      */

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 JavaDoc("Wrong/multiple BCEL repository");
97         if (tmp == null)
98             org.apache.bcel.Repository.setRepository(this);
99     }
100
101     /* (non-Javadoc)
102      * @see org.apache.bcel.util.Repository#removeClass(org.apache.bcel.classfile.JavaClass)
103      */

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 JavaDoc().printStackTrace(System.out);
114     }
115
116     /* (non-Javadoc)
117      * @see org.apache.bcel.util.Repository#findClass(java.lang.String)
118      */

119     public JavaClass findClass(@NonNull String JavaDoc className) {
120         // Make sure we handle class names with slashes.
121
// If we don't, we can get into serious trouble: a previously
122
// loaded class will appear to be missing (because we're using the
123
// wrong name to look it up) and be evicted by some other random
124
// version of the class loaded from the classpath.
125
String JavaDoc dottedClassName = className.replace('/', '.');
126         
127         return nameToClassMap.get(dottedClassName);
128     }
129
130     /* (non-Javadoc)
131      * @see org.apache.bcel.util.Repository#loadClass(java.lang.String)
132      */

133     public JavaClass loadClass(@NonNull String JavaDoc className) throws ClassNotFoundException JavaDoc {
134         if (className == null) throw new IllegalArgumentException JavaDoc("className is null");
135         //if (className.indexOf('/') >= 0) throw new IllegalStateException();
136
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     /* (non-Javadoc)
149      * @see org.apache.bcel.util.Repository#loadClass(java.lang.Class)
150      */

151     public JavaClass loadClass(Class JavaDoc clazz) throws ClassNotFoundException JavaDoc {
152         return loadClass(clazz.getName());
153     }
154
155     /* (non-Javadoc)
156      * @see org.apache.bcel.util.Repository#clear()
157      */

158     public void clear() {
159         if (DEBUG) {
160             System.out.println("Clearing Repository!");
161             dumpStack();
162         }
163         nameToClassMap.clear();
164     }
165
166     /* (non-Javadoc)
167      * @see org.apache.bcel.util.Repository#getClassPath()
168      */

169     public ClassPath getClassPath() {
170         return new ClassPath(urlClassPath.getClassPath());
171     }
172 }
173
174 // vim:ts=4
175
Popular Tags