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.

Aj.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Alexandre Vasseur initial implementation
  11. *******************************************************************************/
  12. package org.aspectj.weaver.loadtime;
  13. import java.lang.ref.ReferenceQueue;
  14. import java.lang.ref.WeakReference;
  15. import java.security.ProtectionDomain;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.StringTokenizer;
  24. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  25. import org.aspectj.weaver.Dump;
  26. import org.aspectj.weaver.tools.Trace;
  27. import org.aspectj.weaver.tools.TraceFactory;
  28. import org.aspectj.weaver.tools.WeavingAdaptor;
  29. import org.aspectj.weaver.tools.cache.SimpleCache;
  30. import org.aspectj.weaver.tools.cache.SimpleCacheFactory;
  31. /**
  32. * Adapter between the generic class pre processor interface and the AspectJ weaver Load time weaving consistency relies on
  33. * Bcel.setRepository
  34. *
  35. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  36. */
  37. public class Aj implements ClassPreProcessor {
  38. private IWeavingContext weavingContext;
  39. public static SimpleCache laCache=SimpleCacheFactory.createSimpleCache();
  40. /**
  41. * References are added to this queue when their associated classloader is removed, and once on here that indicates that we
  42. * should tidy up the adaptor map and remove the adaptor (weaver) from the map we are maintaining from adaptorkey > adaptor
  43. * (weaver)
  44. */
  45. private static ReferenceQueue adaptorQueue = new ReferenceQueue();
  46. private static Trace trace = TraceFactory.getTraceFactory().getTrace(Aj.class);
  47. public Aj() {
  48. this(null);
  49. }
  50. public Aj(IWeavingContext context) {
  51. if (trace.isTraceEnabled())
  52. trace.enter("<init>", this, new Object[] { context, getClass().getClassLoader() });
  53. this.weavingContext = context;
  54. if (trace.isTraceEnabled())
  55. trace.exit("<init>");
  56. }
  57. /**
  58. * Initialization
  59. */
  60. @Override
  61. public void initialize() {
  62. }
  63. private final static String deleLoader = "sun.reflect.DelegatingClassLoader";
  64. private final static String deleLoader2 = "jdk.internal.reflect.DelegatingClassLoader"; // On JDK11+
  65. @Override
  66. public byte[] preProcess(String className, byte[] bytes, ClassLoader loader, ProtectionDomain protectionDomain) {
  67. if (loader == null || className == null ||
  68. loader.getClass().getName().equals(deleLoader) || loader.getClass().getName().equals(deleLoader2)) {
  69. // skip boot loader, null classes (hibernate), or those from a reflection loader
  70. return bytes;
  71. }
  72. if (loadersToSkip != null) {
  73. // Check whether to reject it
  74. if (loadersToSkip.contains(loader.getClass().getName())) {
  75. // System.out.println("debug: no weaver created for loader '"+loader.getClass().getName()+"'");
  76. return bytes;
  77. }
  78. }
  79. if (trace.isTraceEnabled())
  80. trace.enter("preProcess", this, new Object[] { className, bytes, loader });
  81. if (trace.isTraceEnabled())
  82. trace.event("preProcess", this, new Object[] { loader.getParent(), Thread.currentThread().getContextClassLoader() });
  83. try {
  84. synchronized (loader) {
  85. if (SimpleCacheFactory.isEnabled()) {
  86. byte[] cacheBytes= laCache.getAndInitialize(className, bytes,loader,protectionDomain);
  87. if (cacheBytes!=null){
  88. return cacheBytes;
  89. }
  90. }
  91. WeavingAdaptor weavingAdaptor = WeaverContainer.getWeaver(loader, weavingContext);
  92. if (weavingAdaptor == null) {
  93. if (trace.isTraceEnabled())
  94. trace.exit("preProcess");
  95. return bytes;
  96. }
  97. try {
  98. weavingAdaptor.setActiveProtectionDomain(protectionDomain);
  99. byte[] newBytes = weavingAdaptor.weaveClass(className, bytes, false);
  100. Dump.dumpOnExit(weavingAdaptor.getMessageHolder(), true);
  101. if (trace.isTraceEnabled())
  102. trace.exit("preProcess", newBytes);
  103. if (SimpleCacheFactory.isEnabled()) {
  104. laCache.put(className, bytes, newBytes);
  105. }
  106. return newBytes;
  107. } finally {
  108. weavingAdaptor.setActiveProtectionDomain(null);
  109. }
  110. }
  111. /* Don't like to do this but JVMTI swallows all exceptions */
  112. } catch (Throwable th) {
  113. trace.error(className, th);
  114. Dump.dumpWithException(th);
  115. // FIXME AV wondering if we should have the option to fail (throw runtime exception) here
  116. // would make sense at least in test f.e. see TestHelper.handleMessage()
  117. if (trace.isTraceEnabled())
  118. trace.exit("preProcess", th);
  119. return bytes;
  120. } finally {
  121. CompilationAndWeavingContext.resetForThread();
  122. }
  123. }
  124. /**
  125. * An AdaptorKey is a WeakReference wrapping a classloader reference that will enqueue to a specified queue when the classloader
  126. * is GC'd. Since the AdaptorKey is used as a key into a hashmap we need to give it a non-varying hashcode/equals
  127. * implementation, and we need that hashcode not to vary even when the internal referent has been GC'd. The hashcode is
  128. * calculated on creation of the AdaptorKey based on the loader instance that it is wrapping. This means even when the referent
  129. * is gone we can still use the AdaptorKey and it will 'point' to the same place as it always did.
  130. */
  131. private static class AdaptorKey extends WeakReference {
  132. private final int loaderHashCode, sysHashCode, hashValue;
  133. private final String loaderClass;
  134. public AdaptorKey(ClassLoader loader) {
  135. super(loader, adaptorQueue);
  136. loaderHashCode = loader.hashCode();
  137. sysHashCode = System.identityHashCode(loader);
  138. loaderClass = loader.getClass().getName();
  139. hashValue = loaderHashCode + sysHashCode + loaderClass.hashCode();
  140. }
  141. public ClassLoader getClassLoader() {
  142. ClassLoader instance = (ClassLoader) get();
  143. // Assert instance!=null - shouldn't be asked for after a GC of the referent has occurred !
  144. return instance;
  145. }
  146. @Override
  147. public boolean equals(Object obj) {
  148. if (!(obj instanceof AdaptorKey)) {
  149. return false;
  150. }
  151. AdaptorKey other = (AdaptorKey) obj;
  152. return (other.loaderHashCode == loaderHashCode)
  153. && (other.sysHashCode == sysHashCode)
  154. && loaderClass.equals(other.loaderClass);
  155. }
  156. @Override
  157. public int hashCode() {
  158. return hashValue;
  159. }
  160. }
  161. /**
  162. * The reference queue is only processed when a request is made for a weaver adaptor. This means there can be one or two stale
  163. * weavers left around. If the user knows they have finished all their weaving, they might wish to call removeStaleAdaptors
  164. * which will process anything left on the reference queue containing adaptorKeys for garbage collected classloaders.
  165. *
  166. * @param displayProgress produce System.err info on the tidying up process
  167. * @return number of stale weavers removed
  168. */
  169. public static int removeStaleAdaptors(boolean displayProgress) {
  170. int removed = 0;
  171. synchronized (WeaverContainer.weavingAdaptors) {
  172. if (displayProgress) {
  173. System.err.println("Weaver adaptors before queue processing:");
  174. Map<AdaptorKey,ExplicitlyInitializedClassLoaderWeavingAdaptor> m = WeaverContainer.weavingAdaptors;
  175. Set<AdaptorKey> keys = m.keySet();
  176. for (Iterator<AdaptorKey> iterator = keys.iterator(); iterator.hasNext();) {
  177. Object object = iterator.next();
  178. System.err.println(object + " = " + WeaverContainer.weavingAdaptors.get(object));
  179. }
  180. }
  181. Object o = adaptorQueue.poll();
  182. while (o != null) {
  183. if (displayProgress)
  184. System.err.println("Processing referencequeue entry " + o);
  185. AdaptorKey wo = (AdaptorKey) o;
  186. boolean didit = WeaverContainer.weavingAdaptors.remove(wo) != null;
  187. if (didit) {
  188. removed++;
  189. } else {
  190. throw new RuntimeException("Eh?? key=" + wo);
  191. }
  192. if (displayProgress)
  193. System.err.println("Removed? " + didit);
  194. o = adaptorQueue.poll();
  195. }
  196. if (displayProgress) {
  197. System.err.println("Weaver adaptors after queue processing:");
  198. Map<AdaptorKey,ExplicitlyInitializedClassLoaderWeavingAdaptor> m = WeaverContainer.weavingAdaptors;
  199. Set<AdaptorKey> keys = m.keySet();
  200. for (Iterator<AdaptorKey> iterator = keys.iterator(); iterator.hasNext();) {
  201. Object object = iterator.next();
  202. System.err.println(object + " = " + WeaverContainer.weavingAdaptors.get(object));
  203. }
  204. }
  205. }
  206. return removed;
  207. }
  208. /**
  209. * @return the number of entries still in the weavingAdaptors map
  210. */
  211. public static int getActiveAdaptorCount() {
  212. return WeaverContainer.weavingAdaptors.size();
  213. }
  214. /**
  215. * Process the reference queue that contains stale AdaptorKeys - the keys are put on the queue when their classloader referent
  216. * is garbage collected and so the associated adaptor (weaver) should be removed from the map
  217. */
  218. public static void checkQ() {
  219. synchronized (adaptorQueue) {
  220. Object o = adaptorQueue.poll();
  221. while (o != null) {
  222. AdaptorKey wo = (AdaptorKey) o;
  223. // boolean removed =
  224. WeaverContainer.weavingAdaptors.remove(wo);
  225. // DBG System.err.println("Evicting key " + wo + " = " + didit);
  226. o = adaptorQueue.poll();
  227. }
  228. }
  229. }
  230. public static List<String> loadersToSkip = null;
  231. static {
  232. // pr271840 - touch the types early and outside the locks
  233. new ExplicitlyInitializedClassLoaderWeavingAdaptor(new ClassLoaderWeavingAdaptor());
  234. try {
  235. String loadersToSkipProperty = System.getProperty("aj.weaving.loadersToSkip","");
  236. StringTokenizer st = new StringTokenizer(loadersToSkipProperty, ",");
  237. if (loadersToSkipProperty != null && loadersToSkip == null) {
  238. if (st.hasMoreTokens()) {
  239. // System.out.println("aj.weaving.loadersToSkip is set. Skipping loaders: '"+loadersToSkipProperty+"'");
  240. loadersToSkip = new ArrayList<String>();
  241. }
  242. while (st.hasMoreTokens()) {
  243. String nextLoader = st.nextToken();
  244. loadersToSkip.add(nextLoader);
  245. }
  246. }
  247. } catch (Exception e) {
  248. // Likely security issue related to property access...
  249. }
  250. }
  251. /**
  252. * Cache of weaver There is one weaver per classloader
  253. */
  254. static class WeaverContainer {
  255. final static Map<AdaptorKey,ExplicitlyInitializedClassLoaderWeavingAdaptor> weavingAdaptors =
  256. Collections.synchronizedMap(new HashMap<AdaptorKey,ExplicitlyInitializedClassLoaderWeavingAdaptor>());
  257. static WeavingAdaptor getWeaver(ClassLoader loader, IWeavingContext weavingContext) {
  258. ExplicitlyInitializedClassLoaderWeavingAdaptor adaptor = null;
  259. AdaptorKey adaptorKey = new AdaptorKey(loader);
  260. String loaderClassName = loader.getClass().getName();
  261. synchronized (weavingAdaptors) {
  262. checkQ();
  263. if (loader.equals(myClassLoader)){
  264. adaptor = myClassLoaderAdaptor;
  265. } else {
  266. adaptor = weavingAdaptors.get(adaptorKey);
  267. }
  268. if (adaptor == null) {
  269. // create it and put it back in the weavingAdaptors map but avoid any kind of instantiation
  270. // within the synchronized block
  271. ClassLoaderWeavingAdaptor weavingAdaptor = new ClassLoaderWeavingAdaptor();
  272. adaptor = new ExplicitlyInitializedClassLoaderWeavingAdaptor(weavingAdaptor);
  273. if(myClassLoaderAdaptor == null && loader.equals(myClassLoader)){
  274. myClassLoaderAdaptor = adaptor;
  275. } else {
  276. weavingAdaptors.put(adaptorKey, adaptor);
  277. }
  278. }
  279. }
  280. // perform the initialization
  281. return adaptor.getWeavingAdaptor(loader, weavingContext);
  282. }
  283. private static final ClassLoader myClassLoader = WeavingAdaptor.class.getClassLoader();
  284. private static ExplicitlyInitializedClassLoaderWeavingAdaptor myClassLoaderAdaptor;
  285. }
  286. static class ExplicitlyInitializedClassLoaderWeavingAdaptor {
  287. private final ClassLoaderWeavingAdaptor weavingAdaptor;
  288. private boolean isInitialized;
  289. public ExplicitlyInitializedClassLoaderWeavingAdaptor(ClassLoaderWeavingAdaptor weavingAdaptor) {
  290. this.weavingAdaptor = weavingAdaptor;
  291. this.isInitialized = false;
  292. }
  293. private void initialize(ClassLoader loader, IWeavingContext weavingContext) {
  294. if (!isInitialized) {
  295. isInitialized = true;
  296. weavingAdaptor.initialize(loader, weavingContext);
  297. }
  298. }
  299. public ClassLoaderWeavingAdaptor getWeavingAdaptor(ClassLoader loader, IWeavingContext weavingContext) {
  300. initialize(loader, weavingContext);
  301. return weavingAdaptor;
  302. }
  303. }
  304. /**
  305. * Returns a namespace based on the contest of the aspects available
  306. */
  307. public String getNamespace(ClassLoader loader) {
  308. ClassLoaderWeavingAdaptor weavingAdaptor = (ClassLoaderWeavingAdaptor) WeaverContainer.getWeaver(loader, weavingContext);
  309. return weavingAdaptor.getNamespace();
  310. }
  311. /**
  312. * Check to see if any classes have been generated for a particular classes loader. Calls
  313. * ClassLoaderWeavingAdaptor.generatedClassesExist()
  314. *
  315. * @param loader the class cloder
  316. * @return true if classes have been generated.
  317. */
  318. public boolean generatedClassesExist(ClassLoader loader) {
  319. return ((ClassLoaderWeavingAdaptor) WeaverContainer.getWeaver(loader, weavingContext)).generatedClassesExistFor(null);
  320. }
  321. public void flushGeneratedClasses(ClassLoader loader) {
  322. ((ClassLoaderWeavingAdaptor) WeaverContainer.getWeaver(loader, weavingContext)).flushGeneratedClasses();
  323. }
  324. @Override
  325. public void prepareForRedefinition(ClassLoader loader, String className) {
  326. ((ClassLoaderWeavingAdaptor) WeaverContainer.getWeaver(loader, weavingContext)).flushGeneratedClassesFor(className);
  327. }
  328. }