Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

NonCachingClassLoaderRepository.java 7.4KB

14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
14 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package org.aspectj.apache.bcel.util;
  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 java.io.IOException;
  56. import java.io.InputStream;
  57. import java.lang.ref.ReferenceQueue;
  58. import java.lang.ref.SoftReference;
  59. import java.net.URL;
  60. import java.net.URLClassLoader;
  61. import java.util.AbstractMap;
  62. import java.util.HashMap;
  63. import java.util.Iterator;
  64. import java.util.Map;
  65. import java.util.Set;
  66. import org.aspectj.apache.bcel.classfile.ClassParser;
  67. import org.aspectj.apache.bcel.classfile.JavaClass;
  68. /**
  69. * The repository maintains information about which classes have been loaded.
  70. *
  71. * It loads its data from the ClassLoader implementation passed into its constructor.
  72. *
  73. * @see org.aspectj.apache.bcel.Repository
  74. *
  75. * @version $Id: NonCachingClassLoaderRepository.java,v 1.6 2009/09/09 19:56:20 aclement Exp $
  76. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  77. * @author David Dixon-Peugh
  78. *
  79. */
  80. public class NonCachingClassLoaderRepository implements Repository {
  81. private static java.lang.ClassLoader bootClassLoader = null;
  82. private final ClassLoaderReference loaderRef;
  83. private final Map<String, JavaClass> loadedClasses = new SoftHashMap();
  84. public static class SoftHashMap extends AbstractMap {
  85. private Map<Object, SpecialValue> map;
  86. private ReferenceQueue rq = new ReferenceQueue();
  87. public SoftHashMap(Map<Object, SpecialValue> map) {
  88. this.map = map;
  89. }
  90. public SoftHashMap() {
  91. this(new HashMap<Object,SpecialValue>());
  92. }
  93. public SoftHashMap(Map<Object,SpecialValue> map, boolean b) {
  94. this(map);
  95. }
  96. class SpecialValue extends SoftReference {
  97. private final Object key;
  98. SpecialValue(Object k, Object v) {
  99. super(v, rq);
  100. this.key = k;
  101. }
  102. }
  103. private void processQueue() {
  104. SpecialValue sv = null;
  105. while ((sv = (SpecialValue) rq.poll()) != null) {
  106. map.remove(sv.key);
  107. }
  108. }
  109. @Override
  110. public Object get(Object key) {
  111. SpecialValue value = map.get(key);
  112. if (value == null)
  113. return null;
  114. if (value.get() == null) {
  115. // it got GC'd
  116. map.remove(value.key);
  117. return null;
  118. } else {
  119. return value.get();
  120. }
  121. }
  122. @Override
  123. public Object put(Object k, Object v) {
  124. processQueue();
  125. return map.put(k, new SpecialValue(k, v));
  126. }
  127. @Override
  128. public Set entrySet() {
  129. return map.entrySet();
  130. }
  131. @Override
  132. public void clear() {
  133. processQueue();
  134. Set<Object> keys = map.keySet();
  135. for (Object name : keys) {
  136. map.remove(name);
  137. }
  138. }
  139. @Override
  140. public int size() {
  141. processQueue();
  142. return map.size();
  143. }
  144. @Override
  145. public Object remove(Object k) {
  146. processQueue();
  147. SpecialValue value = map.remove(k);
  148. if (value == null)
  149. return null;
  150. if (value.get() != null) {
  151. return value.get();
  152. }
  153. return null;
  154. }
  155. }
  156. public NonCachingClassLoaderRepository(java.lang.ClassLoader loader) {
  157. this.loaderRef = new DefaultClassLoaderReference((loader != null) ? loader : getBootClassLoader());
  158. }
  159. public NonCachingClassLoaderRepository(ClassLoaderReference loaderRef) {
  160. this.loaderRef = loaderRef;
  161. }
  162. private static synchronized java.lang.ClassLoader getBootClassLoader() {
  163. if (bootClassLoader == null) {
  164. bootClassLoader = new URLClassLoader(new URL[0]);
  165. }
  166. return bootClassLoader;
  167. }
  168. /**
  169. * Store a new JavaClass into this Repository.
  170. */
  171. public void storeClass(JavaClass clazz) {
  172. synchronized (loadedClasses) {
  173. loadedClasses.put(clazz.getClassName(), clazz);
  174. }
  175. clazz.setRepository(this);
  176. }
  177. /**
  178. * Remove class from repository
  179. */
  180. public void removeClass(JavaClass clazz) {
  181. synchronized (loadedClasses) {
  182. loadedClasses.remove(clazz.getClassName());
  183. }
  184. }
  185. /**
  186. * Find an already defined JavaClass.
  187. */
  188. public JavaClass findClass(String className) {
  189. synchronized (loadedClasses) {
  190. if (loadedClasses.containsKey(className)) {
  191. return loadedClasses.get(className);
  192. } else {
  193. return null;
  194. }
  195. }
  196. }
  197. /**
  198. * Clear all entries from cache.
  199. */
  200. public void clear() {
  201. synchronized (loadedClasses) {
  202. loadedClasses.clear();
  203. }
  204. }
  205. /**
  206. * Lookup a JavaClass object from the Class Name provided.
  207. */
  208. public JavaClass loadClass(String className) throws ClassNotFoundException {
  209. JavaClass javaClass = findClass(className);
  210. if (javaClass != null) {
  211. return javaClass;
  212. }
  213. javaClass = loadJavaClass(className);
  214. storeClass(javaClass);
  215. return javaClass;
  216. }
  217. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  218. return loadClass(clazz.getName());
  219. }
  220. private JavaClass loadJavaClass(String className) throws ClassNotFoundException {
  221. String classFile = className.replace('.', '/');
  222. try {
  223. InputStream is = loaderRef.getClassLoader().getResourceAsStream(classFile + ".class");
  224. if (is == null) {
  225. throw new ClassNotFoundException(className + " not found.");
  226. }
  227. ClassParser parser = new ClassParser(is, className);
  228. return parser.parse();
  229. } catch (IOException e) {
  230. throw new ClassNotFoundException(e.toString());
  231. }
  232. }
  233. }