View Javadoc

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