0

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

steevn
  • 242
  • 2
  • 8
  • What is JADE? You could have provided a link. I assume that it is some kind of library, right? But I am just guessing and guessing is not good when trying to answer questions. So please edit your question, provide more information, e.g. some application code which should be captured by the aspect. Ideally, provide an [SSCCE](http://sscce.org/) making the problem reproduceable. – kriegaex Mar 15 '15 at 09:26
  • JADE : Java Agent DEvelopment framework, i think it is so popular, no need to introduce it – steevn Mar 15 '15 at 10:50
  • Firstly, popular or not, I do not know it. But I know a lot about AspectJ. Secondly, the description of the tag you have chosen here reads "High performance templating engine heavily influenced by Haml", thus my question. Google lists different topics for JADE as well. Thirdly, you still have not provided more context code. Be smart and help others helping you. It is for your own best, you want someting from the community not vice versa. As you can see from my reputation score I do like to help, but I need to know how. – kriegaex Mar 15 '15 at 11:48

1 Answers1

2

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,

  • an AspectJ project containing your aspect(s) and
  • a plain Java project containing your JADE agent(s).

It would look like this:

Eclipse project view

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

LTW run config - main class

2.) JADE program arguments

LTW run config - program args

3.) Use "Add projects" button to add the AspectJ project to the so-called aspectpath

LTW run config - LTW 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. ;-)

kriegaex
  • 63,017
  • 15
  • 111
  • 202