KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > Repository


1 package com.sun.org.apache.bcel.internal;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.classfile.*;
58 import com.sun.org.apache.bcel.internal.util.*;
59 import java.util.HashMap JavaDoc;
60 import java.io.*;
61
62 /**
63  * Repository maintains informations about class interdependencies, e.g.
64  * whether a class is a sub-class of another. JavaClass objects are put
65  * into a cache which can be purged with clearCache().
66  *
67  * All JavaClass objects used as arguments must have been obtained via
68  * the repository or been added with addClass() manually. This is
69  * because we have to check for object identity (==).
70  *
71  * @version $Id: Repository.java,v 1.1.1.1 2001/10/29 19:59:57 jvanzyl Exp $
72  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A
73  */

74 public abstract class Repository {
75   private static ClassPath class_path = new ClassPath();
76   private static HashMap JavaDoc classes;
77   private static JavaClass OBJECT; // should be final ...
78

79   static { clearCache(); }
80
81   /** @return class object for given fully qualified class name.
82    */

83   public static JavaClass lookupClass(String JavaDoc class_name) {
84     if(class_name == null || class_name.equals(""))
85       throw new RuntimeException JavaDoc("Invalid class name");
86
87     class_name = class_name.replace('/', '.');
88
89     JavaClass clazz = (JavaClass)classes.get(class_name);
90
91     if(clazz == null) {
92       try {
93     InputStream is = class_path.getInputStream(class_name);
94     clazz = new ClassParser(is, class_name).parse();
95     class_name = clazz.getClassName();
96       } catch(IOException e) { return null; }
97
98       classes.put(class_name, clazz);
99     }
100
101     return clazz;
102   }
103
104   /** @return class file object for given Java class.
105    */

106   public static ClassPath.ClassFile lookupClassFile(String JavaDoc class_name) {
107     try {
108       return class_path.getClassFile(class_name);
109     } catch(IOException e) { return null; }
110   }
111
112   /** Clear the repository.
113    */

114   public static void clearCache() {
115     classes = new HashMap JavaDoc();
116     OBJECT = lookupClass("java.lang.Object");
117
118     if(OBJECT == null)
119       System.err.println("Warning: java.lang.Object not found on CLASSPATH!");
120     else
121       classes.put("java.lang.Object", OBJECT);
122   }
123
124   /**
125    * Add clazz to repository if there isn't an equally named class already in there.
126    *
127    * @return old entry in repository
128    */

129   public static JavaClass addClass(JavaClass clazz) {
130     String JavaDoc name = clazz.getClassName();
131     JavaClass cl = (JavaClass)classes.get(name);
132
133     if(cl == null)
134       classes.put(name, cl = clazz);
135
136     return cl;
137   }
138
139   /**
140    * Remove class with given (fully qualifid) name from repository.
141    */

142   public static void removeClass(String JavaDoc clazz) {
143     classes.remove(clazz);
144   }
145
146   /**
147    * Remove given class from repository.
148    */

149   public static void removeClass(JavaClass clazz) {
150     removeClass(clazz.getClassName());
151   }
152
153
154   private static final JavaClass getSuperClass(JavaClass clazz) {
155     if(clazz == OBJECT)
156       return null;
157
158     return lookupClass(clazz.getSuperclassName());
159   }
160
161   /**
162    * @return list of super classes of clazz in ascending order, i.e.,
163    * Object is always the last element
164    */

165   public static JavaClass[] getSuperClasses(JavaClass clazz) {
166     ClassVector vec = new ClassVector();
167
168     for(clazz = getSuperClass(clazz); clazz != null; clazz = getSuperClass(clazz))
169       vec.addElement(clazz);
170
171     return vec.toArray();
172   }
173
174   /**
175    * @return list of super classes of clazz in ascending order, i.e.,
176    * Object is always the last element. "null", if clazz cannot be found.
177    */

178   public static JavaClass[] getSuperClasses(String JavaDoc class_name) {
179     JavaClass jc = lookupClass(class_name);
180     return (jc == null? null : getSuperClasses(jc));
181   }
182
183   /**
184    * @return all interfaces implemented by class and its super
185    * classes and the interfaces that those interfaces extend, and so on
186    */

187   public static JavaClass[] getInterfaces(JavaClass clazz) {
188     ClassVector vec = new ClassVector();
189     ClassQueue queue = new ClassQueue();
190
191     queue.enqueue(clazz);
192
193     while(!queue.empty()) {
194       clazz = queue.dequeue();
195
196       String JavaDoc s = clazz.getSuperclassName();
197       String JavaDoc[] interfaces = clazz.getInterfaceNames();
198
199       if(clazz.isInterface())
200     vec.addElement(clazz);
201       else if(!s.equals("java.lang.Object"))
202     queue.enqueue(lookupClass(s));
203       
204       for(int i=0; i < interfaces.length; i++)
205     queue.enqueue(lookupClass(interfaces[i]));
206     }
207
208     return vec.toArray();
209   }
210
211   /**
212    * @return all interfaces implemented by class and its super
213    * classes and the interfaces that extend those interfaces, and so on
214    */

215   public static JavaClass[] getInterfaces(String JavaDoc class_name) {
216     return getInterfaces(lookupClass(class_name));
217   }
218
219   /**
220    * @return true, if clazz is an instance of super_class
221    */

222   public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
223     if(clazz == super_class)
224       return true;
225
226     JavaClass[] super_classes = getSuperClasses(clazz);
227
228     for(int i=0; i < super_classes.length; i++)
229       if(super_classes[i] == super_class)
230     return true;
231
232     if(super_class.isInterface())
233       return implementationOf(clazz, super_class);
234
235     return false;
236   }
237
238   /**
239    * @return true, if clazz is an instance of super_class
240    */

241   public static boolean instanceOf(String JavaDoc clazz, String JavaDoc super_class) {
242     return instanceOf(lookupClass(clazz), lookupClass(super_class));
243   }
244     
245   /**
246    * @return true, if clazz is an instance of super_class
247    */

248   public static boolean instanceOf(JavaClass clazz, String JavaDoc super_class) {
249     return instanceOf(clazz, lookupClass(super_class));
250   }
251
252   /**
253    * @return true, if clazz is an instance of super_class
254    */

255   public static boolean instanceOf(String JavaDoc clazz, JavaClass super_class) {
256     return instanceOf(lookupClass(clazz), super_class);
257   }
258
259   /**
260    * @return true, if clazz is an implementation of interface inter
261    */

262   public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
263     if(clazz == inter)
264       return true;
265
266     JavaClass[] super_interfaces = getInterfaces(clazz);
267
268     for(int i=0; i < super_interfaces.length; i++)
269       if(super_interfaces[i] == inter)
270     return true;
271
272     return false;
273   }
274
275   /**
276    * @return true, if clazz is an implementation of interface inter
277    */

278   public static boolean implementationOf(String JavaDoc clazz, String JavaDoc inter) {
279     return implementationOf(lookupClass(clazz), lookupClass(inter));
280   }
281
282   /**
283    * @return true, if clazz is an implementation of interface inter
284    */

285   public static boolean implementationOf(JavaClass clazz, String JavaDoc inter) {
286     return implementationOf(clazz, lookupClass(inter));
287   }
288
289   /**
290    * @return true, if clazz is an implementation of interface inter
291    */

292   public static boolean implementationOf(String JavaDoc clazz, JavaClass inter) {
293     return implementationOf(lookupClass(clazz), inter);
294   }
295 }
296
Popular Tags