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.

ClassLoaderRepository.java 12KB

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