Bug 458871 - Make weaving agent attachable during runtime via VirtualMachine.loadAgent
Summary: Make weaving agent attachable during runtime via VirtualMachine.loadAgent
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: LTWeaving (show other bugs)
Version: 1.8.4   Edit
Hardware: PC Windows 8
: P3 normal with 1 vote (vote)
Target Milestone: 1.8.7   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-31 07:20 EST by Alexander Kriegisch CLA
Modified: 2015-09-08 16:33 EDT (History)
2 users (show)

See Also:


Attachments
Patch for AspectJ weaver to enable dynamic weaver attachment (1.50 KB, application/octet-stream)
2015-01-31 07:20 EST, Alexander Kriegisch CLA
no flags Details
Sample Eclipse project showing how to dynamically attach the weaving agent (1.67 MB, application/x-zip-compressed)
2015-01-31 07:21 EST, Alexander Kriegisch CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Kriegisch CLA 2015-01-31 07:20:00 EST
Created attachment 250406 [details]
Patch for AspectJ weaver to enable dynamic weaver attachment

Currently the weaving agent in aspectjweaver.jar does not contain an 'agentmain' method and a corresponding manifest entry. I added those to the code and patched my JAR with the updated Agent.class and MANIFEST.MF. Now dynamically attaching the agent to the current classloader works.

I am attaching
  - a patch against current Git master,
  - an Eclipse sample project showing the effect. You need to adjust the path
    of tools.jar (needed for compilation). A patched version of
    aspectjweaver.jar and a sample aspect.jar with a
    'before() : execution(* *(..))' pointcut (so we have something to weave)
    are contained in the 'lib' subdirectory. If you just run the 'Application'
    class as a Java, it will determine that the aspect weaver has not been
    loaded and attach it dynamically. Subsequently defined/loaded classes will
    then be subject to weaving as can be seen in the console output:

Hello world!
java.lang.UnsupportedOperationException: AspectJ weaving agent was neither started via '-javaagent' (preMain) nor attached via 'VirtualMachine.loadAgent' (agentMain)
[AppClassLoader@2d63c5bb] info AspectJ Weaver Version 1.8.4 built on Thursday Nov 6, 2014 at 20:19:21 GMT
[AppClassLoader@2d63c5bb] info register classloader sun.misc.Launcher$AppClassLoader@2d63c5bb
[AppClassLoader@2d63c5bb] info using configuration file:/C:/Users/Alexander/Documents/java-src/SO_AJ_LTW_CheckIfWeaverIsLoaded/lib/aspect.jar!/META-INF/aop.xml
[AppClassLoader@2d63c5bb] info register aspect com.aspect.generic.GenericAspect
[AppClassLoader@2d63c5bb] weaveinfo Join point 'method-execution(void de.scrum_master.app.Dummy.doSomething())' in Type 'de.scrum_master.app.Dummy' (Dummy.java:4) advised by before advice from 'com.aspect.generic.GenericAspect' (GenericAspect.aj)
[AppClassLoader@2d63c5bb] weaveinfo Join point 'method-execution(int de.scrum_master.app.Dummy.square(int))' in Type 'de.scrum_master.app.Dummy' (Dummy.java:9) advised by before advice from 'com.aspect.generic.GenericAspect' (GenericAspect.aj)
execution(void de.scrum_master.app.Dummy.doSomething())
Do something
execution(int de.scrum_master.app.Dummy.square(int))
Square of 7 = 49
[AppClassLoader@2d63c5bb] weaveinfo Join point 'method-execution(void de.scrum_master.app.Application$1.run())' in Type 'de.scrum_master.app.Application$1' (Application.java:50) advised by before advice from 'com.aspect.generic.GenericAspect' (GenericAspect.aj)
execution(void de.scrum_master.app.Application.1.run())
Anonymous runnable
Hello world!
Comment 1 Alexander Kriegisch CLA 2015-01-31 07:21:09 EST
Created attachment 250407 [details]
Sample Eclipse project showing how to dynamically attach the weaving agent
Comment 2 Andrew Clement CLA 2015-01-31 13:45:06 EST
Cool stuff. Have you signed the eclipse CLA?
Comment 3 Alexander Kriegisch CLA 2015-01-31 14:37:57 EST
LOL. Maybe I will as soon as AspectJ has a clean Maven build. ;-P
Comment 4 Alexander Kriegisch CLA 2015-01-31 17:18:50 EST
BTW, just so as not to spark false hopes in anyone reading this ticket, I want to mention that reloading or retransforming already loaded classes without aspects and replacing them by woven classes will not work because reloading/retransforming only works if no members or methods are added to the class in question. But AspectJ does change the class structure that way, so theoretically it would only be possible to reload/retransform classes which already contain aspect code (e.g. via CTW) under the condition that the newly LT-woven class has the same structure as the previously CT-woven one.
Comment 5 Andrew Clement CLA 2015-02-04 16:20:11 EST
Well I did a local build and tested with the changes and it works (aspectjweaver produced by the build satisfies your test program).  So I could take the change without any regression tests (which isn't totally ideal but I guess they are tricky to write) but I would still need to have the CLA signed to accept it.
Comment 6 Alexander Kriegisch CLA 2015-02-04 17:18:47 EST
Seriously? I thought that was a joke. OMG, next time maybe I just create a bug ticket without a patch attached. I am nkt a committer, I just shggested a minor improvement.

Just out of curiosity: Where do I find this CLA and how the heck do I sign it?
Comment 7 Andrew Clement CLA 2015-02-04 17:27:52 EST
Unfortunately not a joke. It shouldn't be too painful but it is part of the process. It is now embedded in bugzilla so you can see my comments have a CLA icon next to them with a green tick. Whilst yours do not, if you click that icon, I think it takes you to where to sign it.

From the CLA page:
"The Eclipse Foundation has implemented Contributor License Agreements (CLA) to improve its intellectual property (IP) management and workflow. In part, CLAs are a portion of the work required to implement signed-off-by for all contributions coming to Eclipse via Git or Gerrit. All contributors are required to sign the CLA before contributing to any Eclipse project they are not a committer on."

I'd like to take the change if you are able to sign that. Should be once and not for every contribution.
Comment 8 Alexander Kriegisch CLA 2015-02-14 06:14:24 EST
Sorry, I was busy. I have just signed the CLA. As AspectJ 1.8.5 is already out, my changed will only make it into 1.8.6, I figure. My bad. :-)
Comment 9 Carlo De CLA 2015-03-03 08:40:44 EST
Ha Ha. Just wanted to request the same fix. 

+1 for merging this into one of the upcoming releases.
Comment 10 Alexander Kriegisch CLA 2015-06-28 05:42:23 EDT
Andy, is anything blocking this one? It could have been in 1.8.6, but does not seem to be.
Comment 11 Andrew Clement CLA 2015-09-08 16:33:53 EDT
It is in for 1.8.7