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.

ClassLoaderRepository.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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.ReferenceQueue;
  59. import java.lang.ref.SoftReference;
  60. import java.net.URL;
  61. import java.net.URLClassLoader;
  62. import java.util.AbstractMap;
  63. import java.util.Collections;
  64. import java.util.HashMap;
  65. import java.util.Map;
  66. import java.util.Set;
  67. import java.util.WeakHashMap;
  68. import org.aspectj.apache.bcel.classfile.ClassParser;
  69. import org.aspectj.apache.bcel.classfile.JavaClass;
  70. import org.aspectj.apache.bcel.util.ClassLoaderRepository.SoftHashMap.SpecialValue;
  71. /**
  72. * The repository maintains information about which classes have been loaded.
  73. *
  74. * It loads its data from the ClassLoader implementation passed into its constructor.
  75. *
  76. * @see org.aspectj.apache.bcel.Repository
  77. *
  78. * @version $Id: ClassLoaderRepository.java,v 1.13 2009/09/09 19:56:20 aclement Exp $
  79. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  80. * @author David Dixon-Peugh
  81. */
  82. public class ClassLoaderRepository implements Repository {
  83. private static java.lang.ClassLoader bootClassLoader = null;
  84. private ClassLoaderReference loaderRef;
  85. // Choice of cache...
  86. private WeakHashMap<URL, SoftReference<JavaClass>> localCache = new WeakHashMap<URL, SoftReference<JavaClass>>();
  87. private static SoftHashMap /* <URL,JavaClass> */sharedCache = new SoftHashMap(Collections.synchronizedMap(new HashMap<Object, SpecialValue>()));
  88. // For fast translation of the classname *intentionally not static*
  89. private SoftHashMap /* <String,URL> */nameMap = new SoftHashMap(new HashMap(), false);
  90. public static boolean useSharedCache = System.getProperty("org.aspectj.apache.bcel.useSharedCache", "true").equalsIgnoreCase("true");
  91. private static int cacheHitsShared = 0;
  92. private static int missSharedEvicted = 0; // Misses in shared cache access due to reference GC
  93. private long timeManipulatingURLs = 0L;
  94. private long timeSpentLoading = 0L;
  95. private int classesLoadedCount = 0;
  96. private int misses = 0;
  97. private int cacheHitsLocal = 0;
  98. private int missLocalEvicted = 0; // Misses in local cache access due to reference GC
  99. public ClassLoaderRepository(java.lang.ClassLoader loader) {
  100. this.loaderRef = new DefaultClassLoaderReference((loader != null) ? loader : getBootClassLoader());
  101. }
  102. public ClassLoaderRepository(ClassLoaderReference loaderRef) {
  103. this.loaderRef = loaderRef;
  104. }
  105. private static synchronized java.lang.ClassLoader getBootClassLoader() {
  106. if (bootClassLoader == null) {
  107. bootClassLoader = new URLClassLoader(new URL[0]);
  108. }
  109. return bootClassLoader;
  110. }
  111. // Can track back to its key
  112. public static class SoftHashMap extends AbstractMap {
  113. private Map<Object, SpecialValue> map;
  114. boolean recordMiss = true; // only interested in recording miss stats sometimes
  115. private ReferenceQueue rq = new ReferenceQueue();
  116. public SoftHashMap(Map<Object, SpecialValue> map) {
  117. this.map = map;
  118. }
  119. public SoftHashMap() {
  120. this(new HashMap());
  121. }
  122. public SoftHashMap(Map map, boolean b) {
  123. this(map);
  124. this.recordMiss = b;
  125. }
  126. class SpecialValue extends SoftReference {
  127. private final Object key;
  128. SpecialValue(Object k, Object v) {
  129. super(v, rq);
  130. this.key = k;
  131. }
  132. }
  133. private void processQueue() {
  134. SpecialValue sv = null;
  135. while ((sv = (SpecialValue) rq.poll()) != null) {
  136. map.remove(sv.key);
  137. }
  138. }
  139. @Override
  140. public Object get(Object key) {
  141. SpecialValue value = map.get(key);
  142. if (value == null)
  143. return null;
  144. if (value.get() == null) {
  145. // it got GC'd
  146. map.remove(value.key);
  147. if (recordMiss)
  148. missSharedEvicted++;
  149. return null;
  150. } else {
  151. return value.get();
  152. }
  153. }
  154. @Override
  155. public Object put(Object k, Object v) {
  156. processQueue();
  157. return map.put(k, new SpecialValue(k, v));
  158. }
  159. @Override
  160. public Set entrySet() {
  161. return map.entrySet();
  162. }
  163. @Override
  164. public void clear() {
  165. processQueue();
  166. map.clear();
  167. }
  168. @Override
  169. public int size() {
  170. processQueue();
  171. return map.size();
  172. }
  173. @Override
  174. public Object remove(Object k) {
  175. processQueue();
  176. SpecialValue value = map.remove(k);
  177. if (value == null)
  178. return null;
  179. if (value.get() != null) {
  180. return value.get();
  181. }
  182. return null;
  183. }
  184. }
  185. /**
  186. * Store a new JavaClass into this repository as a soft reference and return the reference
  187. */
  188. private void storeClassAsReference(URL url, JavaClass clazz) {
  189. if (useSharedCache) {
  190. clazz.setRepository(null); // can't risk setting repository, we'll get in a pickle!
  191. sharedCache.put(url, clazz);
  192. } else {
  193. clazz.setRepository(this);
  194. localCache.put(url, new SoftReference<JavaClass>(clazz));
  195. }
  196. }
  197. /**
  198. * Store a new JavaClass into this Repository.
  199. */
  200. public void storeClass(JavaClass clazz) {
  201. storeClassAsReference(toURL(clazz.getClassName()), clazz);
  202. }
  203. /**
  204. * Remove class from repository
  205. */
  206. public void removeClass(JavaClass clazz) {
  207. if (useSharedCache)
  208. sharedCache.remove(toURL(clazz.getClassName()));
  209. else
  210. localCache.remove(toURL(clazz.getClassName()));
  211. }
  212. /**
  213. * Find an already defined JavaClass in the local cache.
  214. */
  215. public JavaClass findClass(String className) {
  216. if (useSharedCache)
  217. return findClassShared(toURL(className));
  218. else
  219. return findClassLocal(toURL(className));
  220. }
  221. private JavaClass findClassLocal(URL url) {
  222. Object o = localCache.get(url);
  223. if (o != null) {
  224. o = ((Reference) o).get();
  225. if (o != null) {
  226. return (JavaClass) o;
  227. } else {
  228. missLocalEvicted++;
  229. }
  230. }
  231. return null;
  232. }
  233. /**
  234. * Find an already defined JavaClass in the shared cache.
  235. */
  236. private JavaClass findClassShared(URL url) {
  237. return (JavaClass) sharedCache.get(url);
  238. }
  239. private URL toURL(String className) {
  240. URL url = (URL) nameMap.get(className);
  241. if (url == null) {
  242. String classFile = className.replace('.', '/');
  243. url = loaderRef.getClassLoader().getResource(classFile + ".class");
  244. nameMap.put(className, url);
  245. }
  246. return url;
  247. }
  248. /**
  249. * Lookup a JavaClass object from the Class Name provided.
  250. */
  251. public JavaClass loadClass(String className) throws ClassNotFoundException {
  252. // translate to a URL
  253. long time = System.currentTimeMillis();
  254. java.net.URL url = toURL(className);
  255. timeManipulatingURLs += (System.currentTimeMillis() - time);
  256. if (url == null)
  257. throw new ClassNotFoundException(className + " not found - unable to determine URL");
  258. JavaClass clazz = null;
  259. // Look in the appropriate cache
  260. if (useSharedCache) {
  261. clazz = findClassShared(url);
  262. if (clazz != null) {
  263. cacheHitsShared++;
  264. return clazz;
  265. }
  266. } else {
  267. clazz = findClassLocal(url);
  268. if (clazz != null) {
  269. cacheHitsLocal++;
  270. return clazz;
  271. }
  272. }
  273. // Didn't find it in either cache
  274. misses++;
  275. try {
  276. // Load it
  277. String classFile = className.replace('.', '/');
  278. InputStream is = (useSharedCache ? url.openStream() : loaderRef.getClassLoader().getResourceAsStream(
  279. classFile + ".class"));
  280. if (is == null) {
  281. throw new ClassNotFoundException(className + " not found using url " + url);
  282. }
  283. ClassParser parser = new ClassParser(is, className);
  284. clazz = parser.parse();
  285. // Cache it
  286. storeClassAsReference(url, clazz);
  287. timeSpentLoading += (System.currentTimeMillis() - time);
  288. classesLoadedCount++;
  289. return clazz;
  290. } catch (IOException e) {
  291. throw new ClassNotFoundException(e.toString());
  292. }
  293. }
  294. /**
  295. * Produce a report on cache usage.
  296. */
  297. public String report() {
  298. StringBuffer sb = new StringBuffer();
  299. sb.append("BCEL repository report.");
  300. if (useSharedCache)
  301. sb.append(" (shared cache)");
  302. else
  303. sb.append(" (local cache)");
  304. sb.append(" Total time spent loading: " + timeSpentLoading + "ms.");
  305. sb.append(" Time spent manipulating URLs: " + timeManipulatingURLs + "ms.");
  306. sb.append(" Classes loaded: " + classesLoadedCount + ".");
  307. if (useSharedCache) {
  308. sb.append(" Shared cache size: " + sharedCache.size());
  309. sb.append(" Shared cache (hits/missDueToEviction): (" + cacheHitsShared + "/" + missSharedEvicted + ").");
  310. } else {
  311. sb.append(" Local cache size: " + localCache.size());
  312. sb.append(" Local cache (hits/missDueToEviction): (" + cacheHitsLocal + "/" + missLocalEvicted + ").");
  313. }
  314. return sb.toString();
  315. }
  316. /**
  317. * Returns an array of the stats, for testing, the order is fixed: 0=time spent loading (static) 1=time spent manipulating URLs
  318. * (static) 2=classes loaded (static) 3=cache hits shared (static) 4=misses in shared due to eviction (static) 5=cache hits
  319. * local 6=misses in local due to eviction 7=shared cache size
  320. */
  321. public long[] reportStats() {
  322. return new long[] { timeSpentLoading, timeManipulatingURLs, classesLoadedCount, cacheHitsShared, missSharedEvicted,
  323. cacheHitsLocal, missLocalEvicted, sharedCache.size() };
  324. }
  325. /**
  326. * Reset statistics and clear all caches
  327. */
  328. public void reset() {
  329. timeManipulatingURLs = 0L;
  330. timeSpentLoading = 0L;
  331. classesLoadedCount = 0;
  332. cacheHitsLocal = 0;
  333. cacheHitsShared = 0;
  334. missSharedEvicted = 0;
  335. missLocalEvicted = 0;
  336. misses = 0;
  337. clear();
  338. }
  339. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  340. return loadClass(clazz.getName());
  341. }
  342. /** Clear all entries from the local cache */
  343. public void clear() {
  344. if (useSharedCache)
  345. sharedCache.clear();
  346. else
  347. localCache.clear();
  348. }
  349. }