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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 (https://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. * <https://www.apache.org/>.
  54. */
  55. import java.io.IOException;
  56. import org.aspectj.apache.bcel.classfile.JavaClass;
  57. import org.aspectj.apache.bcel.util.ClassPath;
  58. import org.aspectj.apache.bcel.util.SyntheticRepository;
  59. /**
  60. * The repository maintains informations about class interdependencies, e.g., whether a class is a sub-class of another. Delegates
  61. * actual class loading 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.6 2009/09/09 22:18:20 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. /**
  72. * @return currently used repository instance
  73. */
  74. public static org.aspectj.apache.bcel.util.Repository getRepository() {
  75. if (_repository == null) {
  76. _repository = SyntheticRepository.getInstance();
  77. }
  78. return _repository;
  79. }
  80. /**
  81. * Set repository instance to be used for class loading
  82. */
  83. public static void setRepository(org.aspectj.apache.bcel.util.Repository rep) {
  84. _repository = rep;
  85. }
  86. /**
  87. * Lookup class somewhere found on your CLASSPATH, or whereever the repository instance looks for it.
  88. *
  89. * @return class object for given fully qualified class name, or null if the class could not be found or parsed correctly
  90. */
  91. public static JavaClass lookupClass(String class_name) {
  92. try {
  93. JavaClass clazz = getRepository().findClass(class_name);
  94. if (clazz != null) {
  95. return clazz;
  96. }
  97. return getRepository().loadClass(class_name);
  98. } catch (ClassNotFoundException ex) {
  99. return null;
  100. }
  101. }
  102. // /**
  103. // * Try to find class source via getResourceAsStream().
  104. // *
  105. // * @see Class
  106. // * @return JavaClass object for given runtime class
  107. // */
  108. // public static JavaClass lookupClass(Class clazz) {
  109. // try {
  110. // return getRepository().loadClass(clazz);
  111. // } catch (ClassNotFoundException ex) {
  112. // return null;
  113. // }
  114. // }
  115. /**
  116. * @return class file object for given Java class.
  117. */
  118. public static ClassPath.ClassFile lookupClassFile(String class_name) {
  119. try {
  120. return ClassPath.getSystemClassPath().getClassFile(class_name);
  121. } catch (IOException e) {
  122. return null;
  123. }
  124. }
  125. /**
  126. * Clear the repository.
  127. */
  128. public static void clearCache() {
  129. getRepository().clear();
  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 = getRepository().findClass(clazz.getClassName());
  138. getRepository().storeClass(clazz);
  139. return old;
  140. }
  141. /**
  142. * Remove class with given (fully qualified) name from repository.
  143. */
  144. public static void removeClass(String clazz) {
  145. getRepository().removeClass(getRepository().findClass(clazz));
  146. }
  147. // /**
  148. // * Remove given class from repository.
  149. // */
  150. // public static void removeClass(JavaClass clazz) {
  151. // getRepository().removeClass(clazz);
  152. // }
  153. // /**
  154. // * @return list of super classes of clazz in ascending order, i.e., Object is always the last element
  155. // */
  156. // public static JavaClass[] getSuperClasses(JavaClass clazz) {
  157. // return clazz.getSuperClasses();
  158. // }
  159. //
  160. // /**
  161. // * @return list of super classes of clazz in ascending order, i.e., Object is always the last element. return "null", if class
  162. // * cannot be found.
  163. // */
  164. // public static JavaClass[] getSuperClasses(String class_name) {
  165. // JavaClass jc = lookupClass(class_name);
  166. // return jc == null ? null : getSuperClasses(jc);
  167. // }
  168. //
  169. // /**
  170. // * @return all interfaces implemented by class and its super classes and the interfaces that those interfaces extend, and so
  171. // on.
  172. // * (Some people call this a transitive hull).
  173. // */
  174. // public static JavaClass[] getInterfaces(JavaClass clazz) {
  175. // return clazz.getAllInterfaces();
  176. // }
  177. // /**
  178. // * @return all interfaces implemented by class and its super classes and the interfaces that extend those interfaces, and so
  179. // on
  180. // */
  181. // public static JavaClass[] getInterfaces(String class_name) {
  182. // return getInterfaces(lookupClass(class_name));
  183. // }
  184. /**
  185. * Equivalent to runtime "instanceof" operator.
  186. *
  187. * @return true, if clazz is an instance of super_class
  188. */
  189. public static boolean instanceOf(JavaClass clazz, JavaClass super_class) {
  190. return clazz.instanceOf(super_class);
  191. }
  192. /**
  193. * @return true, if clazz is an instance of super_class
  194. */
  195. public static boolean instanceOf(String clazz, String super_class) {
  196. return instanceOf(lookupClass(clazz), lookupClass(super_class));
  197. }
  198. // /**
  199. // * @return true, if clazz is an instance of super_class
  200. // */
  201. // public static boolean instanceOf(JavaClass clazz, String super_class) {
  202. // return instanceOf(clazz, lookupClass(super_class));
  203. // }
  204. // /**
  205. // * @return true, if clazz is an instance of super_class
  206. // */
  207. // public static boolean instanceOf(String clazz, JavaClass super_class) {
  208. // return instanceOf(lookupClass(clazz), super_class);
  209. // }
  210. /**
  211. * @return true, if clazz is an implementation of interface inter
  212. */
  213. public static boolean implementationOf(JavaClass clazz, JavaClass inter) {
  214. return clazz.implementationOf(inter);
  215. }
  216. /**
  217. * @return true, if clazz is an implementation of interface inter
  218. */
  219. public static boolean implementationOf(String clazz, String inter) {
  220. return implementationOf(lookupClass(clazz), lookupClass(inter));
  221. }
  222. //
  223. // /**
  224. // * @return true, if clazz is an implementation of interface inter
  225. // */
  226. // public static boolean implementationOf(JavaClass clazz, String inter) {
  227. // return implementationOf(clazz, lookupClass(inter));
  228. // }
  229. // /**
  230. // * @return true, if clazz is an implementation of interface inter
  231. // */
  232. // public static boolean implementationOf(String clazz, JavaClass inter) {
  233. // return implementationOf(lookupClass(clazz), inter);
  234. // }
  235. }