2 * Copyright (C) 2001 Eric D. Friedman (eric@hfriedman.rdsl.lmi.net)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.List;
29 * An abstraction of ClassPath entries, which can concretely include
30 * directories, zipfiles, and jarfiles, some of which are immutable
31 * inasmuch as they are part of the JDK and are shared across projects
32 * by virtue of being part of the boot classpath.
34 * <p>ClassPathEntries are Singleton/Flyweight implementations: they
35 * can appear in multiple projects, but there is only ever one
38 * Created: Tue Aug 14 19:28:04 2001
40 * @author Eric D. Friedman
41 * @version $Id: ClassPathEntry.java,v 1.4 2002/09/06 12:57:55 jslopez Exp $
44 abstract class ClassPathEntry {
45 /** a map of unqualified class names to qualified names */
46 protected MultiValueMap nameToClassMap = new MultiValueMap();
47 /** a flag indicating whether this instance has been loaded or not */
48 protected boolean loaded = false;
49 private static Map entryMap = new HashMap();
55 * Returns the singleton/flyweight instance for
56 * <code>resource</code>. The specific instance type returned is
57 * based on the extension of the file or on it being a directory.
59 * @param resource a <code>File</code> value
60 * @return a <code>ClassPathEntry</code> value
61 * @exception IOException if an error occurs
62 * @exception IllegalArgumentException if resource is not a
63 * zip/jar or a directory.
65 static ClassPathEntry instanceForEntry(File resource)
67 ClassPathEntry entry = null;
69 if (entryMap.containsKey(resource)) {
70 entry = (ClassPathEntry)entryMap.get(resource);
72 if (resource.getName().toLowerCase().endsWith(".jar")) {
73 entry = new ClassPathZip(resource);
74 } else if (resource.getName().toLowerCase().endsWith(".zip")) {
75 entry = new ClassPathZip(resource);
76 } else if (resource.isDirectory()) {
77 entry = new ClassPathDir(resource);
79 entry = null; // shouldn't be in classpath
83 entryMap.put(resource,entry);
90 * defines class loading behavior.
92 * @exception IOException if an error occurs
94 abstract void load() throws IOException;
97 * clears the class list in the entry.
101 nameToClassMap.clear();
106 * clears and reloads the class list in the entry.
108 * @exception IOException if an error occurs
110 void reload() throws IOException {
116 * adds an unqualified => qualified mapping.
118 * @param qualifiedName a <code>String</code> value
120 protected void addClass(String qualifiedName) {
123 int lastDot = qualifiedName.lastIndexOf('.');
124 int innerClass = qualifiedName.indexOf('$');
125 if (innerClass < 0) {
126 unqualified = qualifiedName.substring(lastDot + 1);
128 unqualified = qualifiedName.substring(innerClass + 1);
130 nameToClassMap.put(unqualified, qualifiedName);
134 * Returns the list of qualified names that map to the specified
135 * unqualified name. Lazily loads the classes.
137 * @param unqualifiedName a <code>String</code> value
138 * @return a <code>List</code> value
139 * @exception IOException if an error occurs
141 List getClassNames(String unqualifiedName)
145 } // end of if (! isLoaded())
147 return nameToClassMap.getAsList(unqualifiedName);
152 * Get the value of loaded.
153 * @return value of loaded.
155 public boolean isLoaded() {
160 * Set the value of loaded.
162 * @param loaded a <code>boolean</code> value
164 public void setLoaded(boolean loaded) {
165 this.loaded = loaded;
170 * $Log: ClassPathEntry.java,v $
171 * Revision 1.4 2002/09/06 12:57:55 jslopez
172 * Fixes addClass method to handle inner classes in a friendlier manner.
173 * The key for an inner class such as jde.util.A$B was A$B.
174 * Now the key for such a class is only B.
176 * Revision 1.3 2001/10/17 04:09:33 paulk
177 * Cosmetic changes to fit JDE coding style.
182 // End of ClassPathEntry.java