1 package org.apache.bcel;
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 org.apache.bcel.classfile.JavaClass;
58 import org.apache.bcel.util.*;
59 import java.io.*;
60
61 /***
62 * The repository maintains informations about class interdependencies, e.g.,
63 * whether a class is a sub-class of another. Delegates actual class loading
64 * to SyntheticRepository with current class path by default.
65 *
66 * @see org.apache.bcel.util.Repository
67 * @see org.apache.bcel.util.SyntheticRepository
68 *
69 * @version $Id: Repository.java,v 1.11 2002/10/11 20:34:47 mdahm Exp $
70 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
71 */
72 public abstract class Repository {
73 private static org.apache.bcel.util.Repository _repository =
74 SyntheticRepository.getInstance();
75
76 /*** @return currently used repository instance
77 */
78 public static org.apache.bcel.util.Repository getRepository() {
79 return _repository;
80 }
81
82 /*** Set repository instance to be used for class loading
83 */
84 public static void setRepository(org.apache.bcel.util.Repository rep) {
85 _repository = rep;
86 }
87
88 /*** Lookup class somewhere found on your CLASSPATH, or whereever the
89 * repository instance looks for it.
90 *
91 * @return class object for given fully qualified class name, or null
92 * if the class could not be found or parsed correctly
93 */
94 public static JavaClass lookupClass(String class_name) {
95 try {
96 JavaClass clazz = _repository.findClass(class_name);
97
98 if(clazz == null) {
99 return _repository.loadClass(class_name);
100 } else {
101 return clazz;
102 }
103 } catch(ClassNotFoundException ex) { return null; }
104 }
105
106 /***
107 * Try to find class source via getResourceAsStream().
108 * @see Class
109 * @return JavaClass object for given runtime class
110 */
111 public static JavaClass lookupClass(Class clazz) {
112 try {
113 return _repository.loadClass(clazz);
114 } catch(ClassNotFoundException ex) { return null; }
115 }
116
117 /*** @return class file object for given Java class.
118 */
119 public static ClassPath.ClassFile lookupClassFile(String class_name) {
120 try {
121 return ClassPath.SYSTEM_CLASS_PATH.getClassFile(class_name);
122 } catch(IOException e) { return null; }
123 }
124
125 /*** Clear the repository.
126 */
127 public static void clearCache() {
128 _repository.clear();
129 }
130
131 /***
132 * Add clazz to repository if there isn't an equally named class already in there.
133 *
134 * @return old entry in repository
135 */
136 public static JavaClass addClass(JavaClass clazz) {
137 JavaClass old = _repository.findClass(clazz.getClassName());
138 _repository.storeClass(clazz);
139 return old;
140 }
141
142 /***
143 * Remove class with given (fully qualified) name from repository.
144 */
145 public static void removeClass(String clazz) {
146 _repository.removeClass(_repository.findClass(clazz));
147 }
148
149 /***
150 * Remove given class from repository.
151 */
152 public static void removeClass(JavaClass clazz) {
153 _repository.removeClass(clazz);
154 }
155
156 /***
157 * @return list of super classes of clazz in ascending order, i.e.,
158 * Object is always the last element
159 */
160 public static JavaClass[] getSuperClasses(JavaClass clazz) {
161 return clazz.getSuperClasses();
162 }
163
164 /***
165 * @return list of super classes of clazz in ascending order, i.e.,
166 * Object is always the last element. return "null", if class
167 * cannot be found.
168 */
169 public static JavaClass[] getSuperClasses(String class_name) {
170 JavaClass jc = lookupClass(class_name);
171 return (jc == null? null : getSuperClasses(jc));
172 }
173
174 /***
175 * @return all interfaces implemented by class and its super
176 * classes and the interfaces that those interfaces extend, and so on.
177 * (Some people call this a transitive hull).
178 */
179 public static JavaClass[] getInterfaces(JavaClass clazz) {
180 return clazz.getAllInterfaces();
181 }
182
183 /***
184 * @return all interfaces implemented by class and its super
185 * classes and the interfaces that extend those interfaces, and so on
186 */
187 public static JavaClass[] getInterfaces(String class_name) {
188 return getInterfaces(lookupClass(class_name));
189 }
190
191 /***
192 * Equivalent to runtime "instanceof" operator.
193 * @return true, if clazz is an instance of super_class
194 */
195 public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
196 return clazz.instanceOf(super_class);
197 }
198
199 /***
200 * @return true, if clazz is an instance of super_class
201 */
202 public static boolean instanceOf(String clazz, String super_class) {
203 return instanceOf(lookupClass(clazz), lookupClass(super_class));
204 }
205
206 /***
207 * @return true, if clazz is an instance of super_class
208 */
209 public static boolean instanceOf(JavaClass clazz, String super_class) {
210 return instanceOf(clazz, lookupClass(super_class));
211 }
212
213 /***
214 * @return true, if clazz is an instance of super_class
215 */
216 public static boolean instanceOf(String clazz, JavaClass super_class) {
217 return instanceOf(lookupClass(clazz), super_class);
218 }
219
220 /***
221 * @return true, if clazz is an implementation of interface inter
222 */
223 public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
224 return clazz.implementationOf(inter);
225 }
226
227 /***
228 * @return true, if clazz is an implementation of interface inter
229 */
230 public static boolean implementationOf(String clazz, String inter) {
231 return implementationOf(lookupClass(clazz), lookupClass(inter));
232 }
233
234 /***
235 * @return true, if clazz is an implementation of interface inter
236 */
237 public static boolean implementationOf(JavaClass clazz, String inter) {
238 return implementationOf(clazz, lookupClass(inter));
239 }
240
241 /***
242 * @return true, if clazz is an implementation of interface inter
243 */
244 public static boolean implementationOf(String clazz, JavaClass inter) {
245 return implementationOf(lookupClass(clazz), inter);
246 }
247 }
248
This page was automatically generated by Maven