選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ClassLoaderRepository.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.Reference;
  58. import java.lang.ref.SoftReference;
  59. import java.net.URL;
  60. import java.util.HashMap;
  61. import java.util.Map;
  62. import java.util.WeakHashMap;
  63. import org.aspectj.apache.bcel.classfile.ClassParser;
  64. import org.aspectj.apache.bcel.classfile.JavaClass;
  65. /**
  66. * The repository maintains information about which classes have
  67. * been loaded.
  68. *
  69. * It loads its data from the ClassLoader implementation
  70. * passed into its constructor.
  71. *
  72. * @see org.aspectj.apache.bcel.Repository
  73. *
  74. * @version $Id: ClassLoaderRepository.java,v 1.6 2006/08/08 11:26:28 aclement Exp $
  75. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  76. * @author David Dixon-Peugh
  77. */
  78. public class ClassLoaderRepository implements Repository {
  79. private java.lang.ClassLoader loader;
  80. private WeakHashMap /*<String classname,JavaClass>*/loadedClassesLocalCache = new WeakHashMap();
  81. private static Map /*<URL,JavaClass>*/loadedUrlsSharedCache = new HashMap();
  82. public static boolean useSharedCache = true;
  83. private static long timeManipulatingURLs = 0L;
  84. private static long timeSpentLoading = 0L;
  85. private static int classesLoadedCount = 0;
  86. private static int cacheHitsShared = 0;
  87. private static int missSharedEvicted = 0; // Misses in shared cache access due to reference GC
  88. private static int misses = 0;
  89. private int cacheHitsLocal = 0;
  90. private int missLocalEvicted = 0; // Misses in local cache access due to reference GC
  91. static {
  92. useSharedCache = System.getProperty("org.aspectj.apache.bcel.useSharedCache","true").equalsIgnoreCase("true");
  93. }
  94. public ClassLoaderRepository( java.lang.ClassLoader loader ) {
  95. this.loader = loader;
  96. }
  97. /**
  98. * Store a new JavaClass into this repository as a soft reference and return the reference
  99. */
  100. private Reference storeClassAsReference( JavaClass clazz ) {
  101. Reference ref = new SoftReference(clazz);
  102. loadedClassesLocalCache.put( clazz.getClassName(), ref);
  103. clazz.setRepository( this );
  104. return ref;
  105. }
  106. /**
  107. * Store a reference in the shared cache
  108. */
  109. private void storeReferenceShared(URL url, Reference ref) {
  110. if (useSharedCache) loadedUrlsSharedCache.put(url, ref);
  111. }
  112. /**
  113. * Store a new JavaClass into this Repository.
  114. */
  115. public void storeClass( JavaClass clazz ) {
  116. storeClassAsReference(clazz);
  117. }
  118. /**
  119. * Remove class from repository
  120. */
  121. public void removeClass(JavaClass clazz) {
  122. loadedClassesLocalCache.remove(clazz.getClassName());
  123. }
  124. /**
  125. * Find an already defined JavaClass in the local cache.
  126. */
  127. public JavaClass findClass( String className ) {
  128. Object o = loadedClassesLocalCache.get( className );
  129. if (o != null) {
  130. o = ((Reference)o).get();
  131. if (o != null) {
  132. return (JavaClass)o;
  133. } else {
  134. missLocalEvicted++;
  135. }
  136. }
  137. return null;
  138. }
  139. /**
  140. * Find an already defined JavaClass in the shared cache.
  141. */
  142. private JavaClass findClassShared(URL url) {
  143. if (!useSharedCache) return null;
  144. Object o = (Reference)loadedUrlsSharedCache.get(url);
  145. if (o != null) {
  146. o = ((Reference)o).get();
  147. if (o != null) {
  148. return (JavaClass)o;
  149. } else {
  150. missSharedEvicted++;
  151. }
  152. }
  153. return null;
  154. }
  155. /**
  156. * Lookup a JavaClass object from the Class Name provided.
  157. */
  158. public JavaClass loadClass( String className ) throws ClassNotFoundException {
  159. String classFile = className.replace('.', '/');
  160. // Check the local cache
  161. JavaClass clazz = findClass(className);
  162. if (clazz != null) { cacheHitsLocal++; return clazz; }
  163. try {
  164. // Work out the URL
  165. long time = System.currentTimeMillis();
  166. java.net.URL url = (useSharedCache?loader.getResource( classFile + ".class" ):null);
  167. if (useSharedCache && url==null) throw new ClassNotFoundException(className + " not found.");
  168. InputStream is = (useSharedCache?url.openStream():loader.getResourceAsStream( classFile + ".class" ));
  169. timeManipulatingURLs += (System.currentTimeMillis() - time);
  170. // Check the shared cache
  171. clazz = findClassShared(url);
  172. if (clazz != null) { cacheHitsShared++; timeSpentLoading+=(System.currentTimeMillis() - time); return clazz; }
  173. // Didn't find it in either cache
  174. misses++;
  175. if (is == null) { // TODO move this up?
  176. throw new ClassNotFoundException(className + " not found.");
  177. }
  178. ClassParser parser = new ClassParser( is, className );
  179. clazz = parser.parse();
  180. // Store it in both caches
  181. Reference ref = storeClassAsReference( clazz );
  182. storeReferenceShared(url,ref);
  183. timeSpentLoading += (System.currentTimeMillis() - time);
  184. classesLoadedCount++;
  185. return clazz;
  186. } catch (IOException e) {
  187. throw new ClassNotFoundException( e.toString() );
  188. }
  189. }
  190. /**
  191. * Produce a report on cache usage.
  192. */
  193. public String reportAllStatistics() {
  194. StringBuffer sb = new StringBuffer();
  195. sb.append("BCEL repository report.");
  196. if (!useSharedCache) sb.append(" (Shared cache deactivated)");
  197. sb.append(" Total time spent loading: "+timeSpentLoading+"ms.");
  198. sb.append(" Time manipulating URLs: "+timeManipulatingURLs+"ms.");
  199. sb.append(" Classes loaded: "+classesLoadedCount+".");
  200. if (useSharedCache) sb.append(" URL cache (hits/missDueToEviction): ("+cacheHitsShared+"/"+missSharedEvicted+").");
  201. sb.append(" Local cache (hits/missDueToEviction): ("+cacheHitsLocal+"/"+missLocalEvicted+").");
  202. return sb.toString();
  203. }
  204. public int reportLocalCacheHits() {
  205. return cacheHitsLocal;
  206. }
  207. public static int reportSharedCacheHits() {
  208. return cacheHitsShared;
  209. }
  210. /**
  211. * Reset statistics and clear all caches
  212. */
  213. public void reset() {
  214. timeManipulatingURLs = 0L;
  215. timeSpentLoading = 0L;
  216. classesLoadedCount = 0;
  217. cacheHitsLocal = 0;
  218. cacheHitsShared = 0;
  219. missSharedEvicted = 0;
  220. missLocalEvicted = 0;
  221. misses = 0;
  222. clear();
  223. clearShared();
  224. }
  225. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  226. return loadClass(clazz.getName());
  227. }
  228. /** Clear all entries from the local cache */
  229. public void clear() {
  230. loadedClassesLocalCache.clear();
  231. }
  232. /** Clear all entries from the shared cache */
  233. public static void clearShared() {
  234. loadedUrlsSharedCache.clear();
  235. }
  236. }