Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 317874

Summary: race condition when LTW - classes loaded before initialization completes are not woven
Product: [Tools] AspectJ Reporter: Andrew Clement <aclement>
Component: LTWeavingAssignee: aspectj inbox <aspectj-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: peter.melnikov
Version: 1.6.9RC1   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Andrew Clement CLA 2010-06-24 14:57:06 EDT
Raised on the list by Peter Melnikov:

---
What I have found is that aspect is not applied to my servlet classes. While debugging the AspectJ I discovered that there are racing conditions in Aj.java when I run application under Tomcat. WeavingAdaptor initialization has not been complete yet (see line 254) when Tomcat loads servlet classes using TomcatInstrumentableClassLoader. Thought there are synchronized section but it only ensures that only one instance per classloader will be created. While WeavingAdaptor is not initialized yet and  ClassLoaderWeavingAdaptor's field enabled is false that prevents class from being weaved:
WeavingAdaptor.java (line 294):
if (!enabled) {
 	 if (trace.isTraceEnabled()) {
 	 trace.exit("weaveClass", false);
 	 }
 	 return bytes;
 	 }

23.06.2010 18:45:16 org.aspectj.weaver.loadtime.Aj preProcess
FINER: EVENT
23.06.2010 18:45:16 org.aspectj.weaver.tools.WeavingAdaptor weaveClass
FINER: ENTRY org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor@1d0740e
23.06.2010 18:45:16 org.aspectj.weaver.tools.WeavingAdaptor weaveClass
FINER: ENTRY net/mansion/redirect/impl/engine/web/RedirectProcessorServlet/class byte[12079]
23.06.2010 18:45:16 org.aspectj.weaver.tools.WeavingAdaptor weaveClass
FINER: RETURN false
In AspectJ output to stdout there is no messages like "weaving [class name]" or "not weaving [class name]" that could said that class is processed with weaver (no servlet classes mentioned at all).
When initialize method of ClassLoaderWeavingAdaptor is complete, I see that messages but for other classes of my application.
Has any one run into this issue before?
Comment 1 Peter Melnikov CLA 2010-06-25 10:30:01 EDT
Problem appears only when TomcatInstrumentableClassLoader is used.

context.xml.default:

<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
 useSystemClassLoaderAsParent="false" />

Spring's application-context.xml:

...
<context:load-time-weaver/>
...

If spring's context initialization is turned off (org.springframework.web.context.ContextLoaderListener commented out in web.xml) weaving does not occur at all, even if there are servlets in web.xml that are targets of aspect to be applied to. 

When -javaagent option is used in catalina.bat instead to run aspectjweaver problem goes away and servlets are weaved.