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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.HashSet;
  66. import java.util.Map;
  67. import java.util.Set;
  68. import java.util.WeakHashMap;
  69. import org.aspectj.apache.bcel.classfile.ClassParser;
  70. import org.aspectj.apache.bcel.classfile.JavaClass;
  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<>();
  87. private static SoftHashMap /* <URL,JavaClass> */sharedCache = new SoftHashMap(Collections.synchronizedMap(new HashMap<>()));
  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 =
  91. System.getProperty("org.aspectj.apache.bcel.useSharedCache", "true").equalsIgnoreCase("true");
  92. // Cache not found classes as well to prevent unnecessary file I/O operations
  93. public static boolean useUnavailableClassesCache =
  94. System.getProperty("org.aspectj.apache.bcel.useUnavailableClassesCache", "false").equalsIgnoreCase("true");
  95. // Ignore cache clear requests to not build up the cache over and over again
  96. public static boolean ignoreCacheClearRequests =
  97. System.getProperty("org.aspectj.apache.bcel.ignoreCacheClearRequests", "false").equalsIgnoreCase("true");
  98. // Second cache for the unavailable classes
  99. private static Set<String> unavailableClasses = new HashSet<String>();
  100. private static int cacheHitsShared = 0;
  101. private static int missSharedEvicted = 0; // Misses in shared cache access due to reference GC
  102. private long timeManipulatingURLs = 0L;
  103. private long timeSpentLoading = 0L;
  104. private int classesLoadedCount = 0;
  105. private int misses = 0;
  106. private int cacheHitsLocal = 0;
  107. private int unavailableClassesCacheHits = 0;
  108. private int missLocalEvicted = 0; // Misses in local cache access due to reference GC
  109. public ClassLoaderRepository(java.lang.ClassLoader loader) {
  110. this.loaderRef = new DefaultClassLoaderReference((loader != null) ? loader : getBootClassLoader());
  111. }
  112. public ClassLoaderRepository(ClassLoaderReference loaderRef) {
  113. this.loaderRef = loaderRef;
  114. }
  115. private static synchronized java.lang.ClassLoader getBootClassLoader() {
  116. if (bootClassLoader == null) {
  117. bootClassLoader = new URLClassLoader(new URL[0]);
  118. }
  119. return bootClassLoader;
  120. }
  121. // Can track back to its key
  122. public static class SoftHashMap extends AbstractMap {
  123. private Map<Object, SpecialValue> map;
  124. boolean recordMiss = true; // only interested in recording miss stats sometimes
  125. private ReferenceQueue rq = new ReferenceQueue();
  126. public SoftHashMap(Map<Object, SpecialValue> map) {
  127. this.map = map;
  128. }
  129. public SoftHashMap() {
  130. this(new HashMap());
  131. }
  132. public SoftHashMap(Map map, boolean b) {
  133. this(map);
  134. this.recordMiss = b;
  135. }
  136. class SpecialValue extends SoftReference {
  137. private final Object key;
  138. SpecialValue(Object k, Object v) {
  139. super(v, rq);
  140. this.key = k;
  141. }
  142. }
  143. private void processQueue() {
  144. SpecialValue sv = null;
  145. while ((sv = (SpecialValue) rq.poll()) != null) {
  146. map.remove(sv.key);
  147. }
  148. }
  149. @Override
  150. public Object get(Object key) {
  151. SpecialValue value = map.get(key);
  152. if (value == null)
  153. return null;
  154. if (value.get() == null) {
  155. // it got GC'd
  156. map.remove(value.key);
  157. if (recordMiss)
  158. missSharedEvicted++;
  159. return null;
  160. } else {
  161. return value.get();
  162. }
  163. }
  164. @Override
  165. public Object put(Object k, Object v) {
  166. processQueue();
  167. return map.put(k, new SpecialValue(k, v));
  168. }
  169. @Override
  170. public Set entrySet() {
  171. return map.entrySet();
  172. }
  173. @Override
  174. public void clear() {
  175. if (!ignoreCacheClearRequests) {
  176. processQueue();
  177. map.clear();
  178. }
  179. }
  180. @Override
  181. public int size() {
  182. processQueue();
  183. return map.size();
  184. }
  185. @Override
  186. public Object remove(Object k) {
  187. processQueue();
  188. SpecialValue value = map.remove(k);
  189. if (value == null)
  190. return null;
  191. if (value.get() != null) {
  192. return value.get();
  193. }
  194. return null;
  195. }
  196. }
  197. /**
  198. * Store a new JavaClass into this repository as a soft reference and return the reference
  199. */
  200. private void storeClassAsReference(URL url, JavaClass clazz) {
  201. if (useSharedCache) {
  202. clazz.setRepository(null); // can't risk setting repository, we'll get in a pickle!
  203. sharedCache.put(url, clazz);
  204. } else {
  205. clazz.setRepository(this);
  206. localCache.put(url, new SoftReference<>(clazz));
  207. }
  208. }
  209. /**
  210. * Store a new JavaClass into this Repository.
  211. */
  212. public void storeClass(JavaClass clazz) {
  213. storeClassAsReference(toURL(clazz.getClassName()), clazz);
  214. }
  215. /**
  216. * Remove class from repository
  217. */
  218. public void removeClass(JavaClass clazz) {
  219. if (useSharedCache)
  220. sharedCache.remove(toURL(clazz.getClassName()));
  221. else
  222. localCache.remove(toURL(clazz.getClassName()));
  223. }
  224. /**
  225. * Find an already defined JavaClass in the local cache.
  226. */
  227. public JavaClass findClass(String className) {
  228. if (useSharedCache)
  229. return findClassShared(toURL(className));
  230. else
  231. return findClassLocal(toURL(className));
  232. }
  233. private JavaClass findClassLocal(URL url) {
  234. Object o = localCache.get(url);
  235. if (o != null) {
  236. o = ((Reference) o).get();
  237. if (o != null) {
  238. return (JavaClass) o;
  239. } else {
  240. missLocalEvicted++;
  241. }
  242. }
  243. return null;
  244. }
  245. /**
  246. * Find an already defined JavaClass in the shared cache.
  247. */
  248. private JavaClass findClassShared(URL url) {
  249. return (JavaClass) sharedCache.get(url);
  250. }
  251. private URL toURL(String className) {
  252. URL url = (URL) nameMap.get(className);
  253. if (url == null) {
  254. String classFile = className.replace('.', '/');
  255. url = loaderRef.getClassLoader().getResource(classFile + ".class");
  256. nameMap.put(className, url);
  257. }
  258. return url;
  259. }
  260. /**
  261. * Lookup a JavaClass object from the classname provided.
  262. */
  263. public JavaClass loadClass(String className) throws ClassNotFoundException {
  264. // Quick evaluation of unavailable classes to prevent unnecessary file I/O
  265. if (useUnavailableClassesCache && unavailableClasses.contains(className)) {
  266. unavailableClassesCacheHits++;
  267. throw new ClassNotFoundException(className + " not found.");
  268. }
  269. long time = System.currentTimeMillis();
  270. java.net.URL url = toURL(className);
  271. timeManipulatingURLs += (System.currentTimeMillis() - time);
  272. if (url == null) {
  273. if (useUnavailableClassesCache) {
  274. unavailableClasses.add(className);
  275. }
  276. throw new ClassNotFoundException(className + " not found - unable to determine URL");
  277. }
  278. JavaClass clazz = null;
  279. // Look in the appropriate cache
  280. if (useSharedCache) {
  281. clazz = findClassShared(url);
  282. if (clazz != null) {
  283. cacheHitsShared++;
  284. return clazz;
  285. }
  286. } else {
  287. clazz = findClassLocal(url);
  288. if (clazz != null) {
  289. cacheHitsLocal++;
  290. return clazz;
  291. }
  292. }
  293. // Didn't find it in either cache
  294. misses++;
  295. try {
  296. // Load it
  297. String classFile = className.replace('.', '/');
  298. InputStream is = (useSharedCache ? url.openStream() : loaderRef.getClassLoader().getResourceAsStream(
  299. classFile + ".class"));
  300. if (is == null) {
  301. if (useUnavailableClassesCache) {
  302. unavailableClasses.add(className);
  303. }
  304. throw new ClassNotFoundException(className + " not found using url " + url);
  305. }
  306. ClassParser parser = new ClassParser(is, className);
  307. clazz = parser.parse();
  308. // Cache it
  309. storeClassAsReference(url, clazz);
  310. timeSpentLoading += (System.currentTimeMillis() - time);
  311. classesLoadedCount++;
  312. return clazz;
  313. } catch (IOException e) {
  314. if (useUnavailableClassesCache) {
  315. unavailableClasses.add(className);
  316. }
  317. throw new ClassNotFoundException(e.toString());
  318. }
  319. }
  320. /**
  321. * Produce a report on cache usage.
  322. */
  323. public String report() {
  324. StringBuilder sb = new StringBuilder();
  325. sb.append("BCEL repository report.");
  326. if (useSharedCache)
  327. sb.append(" (shared cache)");
  328. else
  329. sb.append(" (local cache)");
  330. sb.append(" Total time spent loading: " + timeSpentLoading + "ms.");
  331. sb.append(" Time spent manipulating URLs: " + timeManipulatingURLs + "ms.");
  332. sb.append(" Classes loaded: " + classesLoadedCount + ".");
  333. if (useSharedCache) {
  334. sb.append(" Shared cache size: " + sharedCache.size());
  335. sb.append(" Shared cache (hits/missDueToEviction): (" + cacheHitsShared + "/" + missSharedEvicted + ").");
  336. } else {
  337. sb.append(" Local cache size: " + localCache.size());
  338. sb.append(" Local cache (hits/missDueToEviction): (" + cacheHitsLocal + "/" + missLocalEvicted + ").");
  339. }
  340. return sb.toString();
  341. }
  342. /**
  343. * Returns an array of the stats, for testing, the order is fixed: 0=time spent loading (static) 1=time spent manipulating URLs
  344. * (static) 2=classes loaded (static) 3=cache hits shared (static) 4=misses in shared due to eviction (static) 5=cache hits
  345. * local 6=misses in local due to eviction 7=shared cache size
  346. */
  347. public long[] reportStats() {
  348. return new long[] { timeSpentLoading, timeManipulatingURLs, classesLoadedCount, cacheHitsShared, missSharedEvicted,
  349. cacheHitsLocal, missLocalEvicted, sharedCache.size(), unavailableClassesCacheHits };
  350. }
  351. /**
  352. * Reset statistics and clear all caches
  353. */
  354. public void reset() {
  355. timeManipulatingURLs = 0L;
  356. timeSpentLoading = 0L;
  357. classesLoadedCount = 0;
  358. cacheHitsLocal = 0;
  359. cacheHitsShared = 0;
  360. missSharedEvicted = 0;
  361. missLocalEvicted = 0;
  362. unavailableClassesCacheHits = 0;
  363. misses = 0;
  364. clear();
  365. }
  366. public JavaClass loadClass(Class clazz) throws ClassNotFoundException {
  367. return loadClass(clazz.getName());
  368. }
  369. /** Clear all entries from the local cache */
  370. public void clear() {
  371. if (!ignoreCacheClearRequests) {
  372. if (useSharedCache) {
  373. sharedCache.clear();
  374. } else {
  375. localCache.clear();
  376. }
  377. unavailableClasses.clear();
  378. }
  379. }
  380. }