Search code examples
javaaspectjagents-jade

why can't i get the agent from behaviour instance?


i am working with JADE in Eclipse. i tried to capture the action method for each executed behaviour using an aspect. it works so well, i even got the instance of the executed behaviour. however this instance doesn't allow me to get the Agent which added this behaviour. because in http://jade.tilab.com/doc/api/jade/core/behaviours/Behaviour.html Behaviour allows us to know which Agent added this behaviour. the following pic shows my error

enter image description here

thanks.


Solution

  • Thanks for not updating the question, not posting the actual error message and even hiding your class's imports from your readers' view. :-7

    Anyway: The code you posted should work, for me it does in Eclipse without any red underlinings. Here are two variants for your pointcut and advice, on like yours with an ugly cast and usage of getThis() and one more elegant by direct and type-safe parameter binding:

    package de.scrum_master.aspect;
    
    import jade.core.behaviours.Behaviour;
    
    public aspect ActionAspect {
        before() :
            execution(* Behaviour.action(..))
        {
            System.out.println(thisJoinPoint);
            Behaviour behaviour = (Behaviour) thisJoinPoint.getThis();
            behaviour.getAgent();
        }
    
        before(Behaviour behaviour) :
            execution(* Behaviour.action(..)) && this(behaviour)
        {
            System.out.println(thisJoinPoint);
            behaviour.getAgent();
        }
    }