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.

NonCachingClassLoaderRepository.java 7.3KB

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