You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Repository.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package org.aspectj.apache.bcel;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import org.aspectj.apache.bcel.classfile.JavaClass;
  56. import org.aspectj.apache.bcel.util.*;
  57. import java.io.*;
  58. /**
  59. * The repository maintains informations about class interdependencies, e.g.,
  60. * whether a class is a sub-class of another. Delegates actual class loading
  61. * to SyntheticRepository with current class path by default.
  62. *
  63. * @see org.aspectj.apache.bcel.util.Repository
  64. * @see org.aspectj.apache.bcel.util.SyntheticRepository
  65. *
  66. * @version $Id: Repository.java,v 1.3.10.3 2008/05/08 19:26:47 aclement Exp $
  67. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  68. */
  69. public abstract class Repository {
  70. private static org.aspectj.apache.bcel.util.Repository _repository = null;
  71. /** @return currently used repository instance
  72. */
  73. public static org.aspectj.apache.bcel.util.Repository getRepository() {
  74. if (_repository == null) _repository = SyntheticRepository.getInstance();
  75. return _repository;
  76. }
  77. /** Set repository instance to be used for class loading
  78. */
  79. public static void setRepository(org.aspectj.apache.bcel.util.Repository rep) {
  80. _repository = rep;
  81. }
  82. /** Lookup class somewhere found on your CLASSPATH, or whereever the
  83. * repository instance looks for it.
  84. *
  85. * @return class object for given fully qualified class name, or null
  86. * if the class could not be found or parsed correctly
  87. */
  88. public static JavaClass lookupClass(String class_name) {
  89. try {
  90. JavaClass clazz = getRepository().findClass(class_name);
  91. if(clazz != null) return clazz;
  92. return getRepository().loadClass(class_name);
  93. } catch(ClassNotFoundException ex) { return null; }
  94. }
  95. /**
  96. * Try to find class source via getResourceAsStream().
  97. * @see Class
  98. * @return JavaClass object for given runtime class
  99. */
  100. public static JavaClass lookupClass(Class clazz) {
  101. try {
  102. return getRepository().loadClass(clazz);
  103. } catch(ClassNotFoundException ex) { return null; }
  104. }
  105. /** @return class file object for given Java class.
  106. */
  107. public static ClassPath.ClassFile lookupClassFile(String class_name) {
  108. try {
  109. return ClassPath.getSystemClassPath().getClassFile(class_name);
  110. } catch(IOException e) { return null; }
  111. }
  112. /** Clear the repository.
  113. */
  114. public static void clearCache() {
  115. getRepository().clear();
  116. }
  117. /**
  118. * Add clazz to repository if there isn't an equally named class already in there.
  119. *
  120. * @return old entry in repository
  121. */
  122. public static JavaClass addClass(JavaClass clazz) {
  123. JavaClass old = getRepository().findClass(clazz.getClassName());
  124. getRepository().storeClass(clazz);
  125. return old;
  126. }
  127. /**
  128. * Remove class with given (fully qualified) name from repository.
  129. */
  130. public static void removeClass(String clazz) {
  131. getRepository().removeClass(getRepository().findClass(clazz));
  132. }
  133. /**
  134. * Remove given class from repository.
  135. */
  136. public static void removeClass(JavaClass clazz) {
  137. getRepository().removeClass(clazz);
  138. }
  139. /**
  140. * @return list of super classes of clazz in ascending order, i.e.,
  141. * Object is always the last element
  142. */
  143. public static JavaClass[] getSuperClasses(JavaClass clazz) {
  144. return clazz.getSuperClasses();
  145. }
  146. /**
  147. * @return list of super classes of clazz in ascending order, i.e.,
  148. * Object is always the last element. return "null", if class
  149. * cannot be found.
  150. */
  151. public static JavaClass[] getSuperClasses(String class_name) {
  152. JavaClass jc = lookupClass(class_name);
  153. return (jc == null? null : getSuperClasses(jc));
  154. }
  155. /**
  156. * @return all interfaces implemented by class and its super
  157. * classes and the interfaces that those interfaces extend, and so on.
  158. * (Some people call this a transitive hull).
  159. */
  160. public static JavaClass[] getInterfaces(JavaClass clazz) {
  161. return clazz.getAllInterfaces();
  162. }
  163. /**
  164. * @return all interfaces implemented by class and its super
  165. * classes and the interfaces that extend those interfaces, and so on
  166. */
  167. public static JavaClass[] getInterfaces(String class_name) {
  168. return getInterfaces(lookupClass(class_name));
  169. }
  170. /**
  171. * Equivalent to runtime "instanceof" operator.
  172. * @return true, if clazz is an instance of super_class
  173. */
  174. public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
  175. return clazz.instanceOf(super_class);
  176. }
  177. /**
  178. * @return true, if clazz is an instance of super_class
  179. */
  180. public static boolean instanceOf(String clazz, String super_class) {
  181. return instanceOf(lookupClass(clazz), lookupClass(super_class));
  182. }
  183. /**
  184. * @return true, if clazz is an instance of super_class
  185. */
  186. public static boolean instanceOf(JavaClass clazz, String super_class) {
  187. return instanceOf(clazz, lookupClass(super_class));
  188. }
  189. /**
  190. * @return true, if clazz is an instance of super_class
  191. */
  192. public static boolean instanceOf(String clazz, JavaClass super_class) {
  193. return instanceOf(lookupClass(clazz), super_class);
  194. }
  195. /**
  196. * @return true, if clazz is an implementation of interface inter
  197. */
  198. public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
  199. return clazz.implementationOf(inter);
  200. }
  201. /**
  202. * @return true, if clazz is an implementation of interface inter
  203. */
  204. public static boolean implementationOf(String clazz, String inter) {
  205. return implementationOf(lookupClass(clazz), lookupClass(inter));
  206. }
  207. /**
  208. * @return true, if clazz is an implementation of interface inter
  209. */
  210. public static boolean implementationOf(JavaClass clazz, String inter) {
  211. return implementationOf(clazz, lookupClass(inter));
  212. }
  213. /**
  214. * @return true, if clazz is an implementation of interface inter
  215. */
  216. public static boolean implementationOf(String clazz, JavaClass inter) {
  217. return implementationOf(lookupClass(clazz), inter);
  218. }
  219. }