KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > util > ClassLoaderRepository


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.util;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import org.apache.bcel.classfile.ClassParser;
24 import org.apache.bcel.classfile.JavaClass;
25
26 /**
27  * The repository maintains information about which classes have
28  * been loaded.
29  *
30  * It loads its data from the ClassLoader implementation
31  * passed into its constructor.
32  *
33  * @see org.apache.bcel.Repository
34  *
35  * @version $Id: ClassLoaderRepository.java 386056 2006-03-15 11:31:56Z tcurdt $
36  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
37  * @author David Dixon-Peugh
38  */

39 public class ClassLoaderRepository implements Repository {
40
41     private java.lang.ClassLoader JavaDoc loader;
42     private Map JavaDoc loadedClasses = new HashMap JavaDoc(); // CLASSNAME X JAVACLASS
43

44
45     public ClassLoaderRepository(java.lang.ClassLoader JavaDoc loader) {
46         this.loader = loader;
47     }
48
49
50     /**
51      * Store a new JavaClass into this Repository.
52      */

53     public void storeClass( JavaClass clazz ) {
54         loadedClasses.put(clazz.getClassName(), clazz);
55         clazz.setRepository(this);
56     }
57
58
59     /**
60      * Remove class from repository
61      */

62     public void removeClass( JavaClass clazz ) {
63         loadedClasses.remove(clazz.getClassName());
64     }
65
66
67     /**
68      * Find an already defined JavaClass.
69      */

70     public JavaClass findClass( String JavaDoc className ) {
71         if (loadedClasses.containsKey(className)) {
72             return (JavaClass) loadedClasses.get(className);
73         } else {
74             return null;
75         }
76     }
77
78
79     /**
80      * Lookup a JavaClass object from the Class Name provided.
81      */

82     public JavaClass loadClass( String JavaDoc className ) throws ClassNotFoundException JavaDoc {
83         String JavaDoc classFile = className.replace('.', '/');
84         JavaClass RC = findClass(className);
85         if (RC != null) {
86             return RC;
87         }
88         try {
89             InputStream JavaDoc is = loader.getResourceAsStream(classFile + ".class");
90             if (is == null) {
91                 throw new ClassNotFoundException JavaDoc(className + " not found.");
92             }
93             ClassParser parser = new ClassParser(is, className);
94             RC = parser.parse();
95             storeClass(RC);
96             return RC;
97         } catch (IOException JavaDoc e) {
98             throw new ClassNotFoundException JavaDoc(e.toString());
99         }
100     }
101
102
103     public JavaClass loadClass( Class JavaDoc clazz ) throws ClassNotFoundException JavaDoc {
104         return loadClass(clazz.getName());
105     }
106
107
108     /** Clear all entries from cache.
109      */

110     public void clear() {
111         loadedClasses.clear();
112     }
113
114
115     /*
116      * @return null
117      */

118     public ClassPath getClassPath() {
119         return null;
120     }
121 }
122
Popular Tags