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