i am using Jade in eclipse IDE, i want to capture the main method from jade, because the main method is the starting point for every application, i want to test if it is also right with the running of the JADE middleware (ie: i want to check if the main method is executed when i start the jade middleware or not) i already did this :
public aspect MainAspect {
pointcut main() : execution(public static void main(..));
before() : main(){
System.out.println("main is executed");
}
}
but it is not capturing anything; is there any comment ? thanks
Okay, I had some time to find out what you have not told me by myself. My assumption was correct that JADE is a third-party library.
This means that you want to weave aspect code into a main
method contained within that very library. Normally with compile-time weaving only classes from your own project are woven during compilation, which explains why the external main
method is not intercepted.
So how do you go about getting your aspect code woven into JADE's main
method? There are two ways:
Compile-time binary weaving: You put the library on the compiler's inpath, causing it to spit out woven versions of all Java classes it finds in the library. Those can be re-packaged into an instrumented library JAR and then used from within your application. But this is not very flexible and quite intrusive, too.
Load-time weaving (LTW): You put -javaagent:/path/to/aspectjweaver.jar
on the JVM command line and also specify an aop.xml (or aop-ajc.xml) file telling the weaver which aspects to weave into which packages or classes, causing AspectJ to dynamically apply aspects during classloading. This is flexible and minimally invasive.
Example:
Assuming you use Eclipse the easiest way to achieve what you want is to create two projects,
It would look like this:
Dummy JADE agent:
package de.scrum_master.app;
import jade.core.Agent;
public class BookBuyerAgent extends Agent {
protected void setup() {
System.out.println("Hello! Buyer-agent " + getAID().getName() + " is ready.");
}
}
Aspect:
package de.scrum_master.aspect;
public aspect MainAspect {
before() : execution(public static void main(..)) {
System.out.println(thisJoinPoint);
}
}
LTW configuration META_INF/aop-ajc.xml:
This file will be auto-generated as soon as you create an LTW run configuration (see below).
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="de.scrum_master.aspect.MainAspect"/>
</aspects>
</aspectj>
AspectJ LTW run configuration:
Now select your Java project (not the AspectJ project) and create a run configuration for it: Select "Run" - "Run Configurations" from the Eclipse menu, then select category "AspectJ Load-Time Weaving Application" and click the "New launch configuration" button. Then specify the following information:
1.) Project name (should be preselected) and main class
2.) JADE program arguments
3.) Use "Add projects" button to add the AspectJ project to the so-called aspectpath
The console output should look similar to this:
execution(void jade.Boot.main(String[]))
Mrz 15, 2015 5:36:18 PM jade.core.Runtime beginContainer
Information: ----------------------------------
This is JADE 4.3.3 - revision 6726 of 2014/12/09 09:33:02
downloaded in Open Source, under LGPL restrictions,
at http://jade.tilab.com/
----------------------------------------
(...)
Hello! Buyer-agent buyer@192.168.178.33:1099/JADE is ready.
Mrz 15, 2015 5:36:19 PM jade.core.AgentContainerImpl joinPlatform
(...)
The very first line shows that the main
method was actually intercepted by AspectJ. Having said that, I really wonder why you want to check if it was invoked because if it was not the JADE container would not have been started anyway. ;-)