001: /*
002: * Copyright 2000-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package org.apache.bcel;
018:
019: import java.io.IOException;
020: import org.apache.bcel.classfile.JavaClass;
021: import org.apache.bcel.util.ClassPath;
022: import org.apache.bcel.util.SyntheticRepository;
023:
024: /**
025: * The repository maintains informations about class interdependencies, e.g.,
026: * whether a class is a sub-class of another. Delegates actual class loading
027: * to SyntheticRepository with current class path by default.
028: *
029: * @see org.apache.bcel.util.Repository
030: * @see org.apache.bcel.util.SyntheticRepository
031: *
032: * @version $Id: Repository.java 386056 2006-03-15 11:31:56Z tcurdt $
033: * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
034: */
035: public abstract class Repository {
036:
037: private static org.apache.bcel.util.Repository _repository = SyntheticRepository
038: .getInstance();
039:
040: /** @return currently used repository instance
041: */
042: public static org.apache.bcel.util.Repository getRepository() {
043: return _repository;
044: }
045:
046: /** Set repository instance to be used for class loading
047: */
048: public static void setRepository(org.apache.bcel.util.Repository rep) {
049: _repository = rep;
050: }
051:
052: /** Lookup class somewhere found on your CLASSPATH, or whereever the
053: * repository instance looks for it.
054: *
055: * @return class object for given fully qualified class name
056: * @throws ClassNotFoundException if the class could not be found or
057: * parsed correctly
058: */
059: public static JavaClass lookupClass(String class_name)
060: throws ClassNotFoundException {
061: return _repository.loadClass(class_name);
062: }
063:
064: /**
065: * Try to find class source using the internal repository instance.
066: * @see Class
067: * @return JavaClass object for given runtime class
068: * @throws ClassNotFoundException if the class could not be found or
069: * parsed correctly
070: */
071: public static JavaClass lookupClass(Class clazz)
072: throws ClassNotFoundException {
073: return _repository.loadClass(clazz);
074: }
075:
076: /**
077: * @return class file object for given Java class by looking on the
078: * system class path; returns null if the class file can't be
079: * found
080: */
081: public static ClassPath.ClassFile lookupClassFile(String class_name) {
082: try {
083: ClassPath path = _repository.getClassPath();
084: if (path == null) {
085: return null;
086: }
087: return path.getClassFile(class_name);
088: } catch (IOException e) {
089: return null;
090: }
091: }
092:
093: /** Clear the repository.
094: */
095: public static void clearCache() {
096: _repository.clear();
097: }
098:
099: /**
100: * Add clazz to repository if there isn't an equally named class already in there.
101: *
102: * @return old entry in repository
103: */
104: public static JavaClass addClass(JavaClass clazz) {
105: JavaClass old = _repository.findClass(clazz.getClassName());
106: _repository.storeClass(clazz);
107: return old;
108: }
109:
110: /**
111: * Remove class with given (fully qualified) name from repository.
112: */
113: public static void removeClass(String clazz) {
114: _repository.removeClass(_repository.findClass(clazz));
115: }
116:
117: /**
118: * Remove given class from repository.
119: */
120: public static void removeClass(JavaClass clazz) {
121: _repository.removeClass(clazz);
122: }
123:
124: /**
125: * @return list of super classes of clazz in ascending order, i.e.,
126: * Object is always the last element
127: * @throws ClassNotFoundException if any of the superclasses can't be found
128: */
129: public static JavaClass[] getSuperClasses(JavaClass clazz)
130: throws ClassNotFoundException {
131: return clazz.getSuperClasses();
132: }
133:
134: /**
135: * @return list of super classes of clazz in ascending order, i.e.,
136: * Object is always the last element.
137: * @throws ClassNotFoundException if the named class or any of its
138: * superclasses can't be found
139: */
140: public static JavaClass[] getSuperClasses(String class_name)
141: throws ClassNotFoundException {
142: JavaClass jc = lookupClass(class_name);
143: return getSuperClasses(jc);
144: }
145:
146: /**
147: * @return all interfaces implemented by class and its super
148: * classes and the interfaces that those interfaces extend, and so on.
149: * (Some people call this a transitive hull).
150: * @throws ClassNotFoundException if any of the class's
151: * superclasses or superinterfaces can't be found
152: */
153: public static JavaClass[] getInterfaces(JavaClass clazz)
154: throws ClassNotFoundException {
155: return clazz.getAllInterfaces();
156: }
157:
158: /**
159: * @return all interfaces implemented by class and its super
160: * classes and the interfaces that extend those interfaces, and so on
161: * @throws ClassNotFoundException if the named class can't be found,
162: * or if any of its superclasses or superinterfaces can't be found
163: */
164: public static JavaClass[] getInterfaces(String class_name)
165: throws ClassNotFoundException {
166: return getInterfaces(lookupClass(class_name));
167: }
168:
169: /**
170: * Equivalent to runtime "instanceof" operator.
171: * @return true, if clazz is an instance of super_class
172: * @throws ClassNotFoundException if any superclasses or superinterfaces
173: * of clazz can't be found
174: */
175: public static boolean instanceOf(JavaClass clazz,
176: JavaClass super _class) throws ClassNotFoundException {
177: return clazz.instanceOf(super _class);
178: }
179:
180: /**
181: * @return true, if clazz is an instance of super_class
182: * @throws ClassNotFoundException if either clazz or super_class
183: * can't be found
184: */
185: public static boolean instanceOf(String clazz, String super _class)
186: throws ClassNotFoundException {
187: return instanceOf(lookupClass(clazz), lookupClass(super _class));
188: }
189:
190: /**
191: * @return true, if clazz is an instance of super_class
192: * @throws ClassNotFoundException if super_class can't be found
193: */
194: public static boolean instanceOf(JavaClass clazz, String super _class)
195: throws ClassNotFoundException {
196: return instanceOf(clazz, lookupClass(super _class));
197: }
198:
199: /**
200: * @return true, if clazz is an instance of super_class
201: * @throws ClassNotFoundException if clazz can't be found
202: */
203: public static boolean instanceOf(String clazz, JavaClass super _class)
204: throws ClassNotFoundException {
205: return instanceOf(lookupClass(clazz), super _class);
206: }
207:
208: /**
209: * @return true, if clazz is an implementation of interface inter
210: * @throws ClassNotFoundException if any superclasses or superinterfaces
211: * of clazz can't be found
212: */
213: public static boolean implementationOf(JavaClass clazz,
214: JavaClass inter) throws ClassNotFoundException {
215: return clazz.implementationOf(inter);
216: }
217:
218: /**
219: * @return true, if clazz is an implementation of interface inter
220: * @throws ClassNotFoundException if clazz, inter, or any superclasses
221: * or superinterfaces of clazz can't be found
222: */
223: public static boolean implementationOf(String clazz, String inter)
224: throws ClassNotFoundException {
225: return implementationOf(lookupClass(clazz), lookupClass(inter));
226: }
227:
228: /**
229: * @return true, if clazz is an implementation of interface inter
230: * @throws ClassNotFoundException if inter or any superclasses
231: * or superinterfaces of clazz can't be found
232: */
233: public static boolean implementationOf(JavaClass clazz, String inter)
234: throws ClassNotFoundException {
235: return implementationOf(clazz, lookupClass(inter));
236: }
237:
238: /**
239: * @return true, if clazz is an implementation of interface inter
240: * @throws ClassNotFoundException if clazz or any superclasses or
241: * superinterfaces of clazz can't be found
242: */
243: public static boolean implementationOf(String clazz, JavaClass inter)
244: throws ClassNotFoundException {
245: return implementationOf(lookupClass(clazz), inter);
246: }
247: }
|