AspectJ warning 'Xlint:adviceDidNotMatch' for 'target()' pointcut against static method

Viewed 2040

I am new to AspectJ and still learning hot to write code. I am trying to make a pointcut and advice for nextYear() method, but i keep getting "Warning:(39, 0) ajc: advice defined in ChristkindAspect has not been applied [Xlint:adviceDidNotMatch]". I understand that it is probably because an Object is not being directly created, and that i cannot use target(christkindenter code here), but i cannot find a solution. Can anybody help? Thank you in advance!

pointcut nextYear(Christkind christkind, Population population):
  call(* *Christkind.nextYear(Population))&& args(population) && 
  target(christkind);


before(Christkind christkind,Population population): nextYear(christkind,population) {
    System.out.println("New Year");
    populationSize(population);
    numbOfWIshes(population);
    //wishStrength(population); }

In main method i have:

Christkind.nextYear(population);

enter image description here

2 Answers

Warning Xlint:adviceDidNotMatch means that no code in the scope of the aspect weaver or compiler was found matched by the pointcut in your aspect. Thus, the advice in question was not applied to (i.e. woven into) your target application code.

In your screenshot I see that your target method is static, also the way your main method calls it implies that:

Christkind.nextYear(population);

The target() pointcut designator checks if the target object is an instance of whatever you specify. But for a static method there is no instance, thus the name static method, not instance method. Consequently, your pointcut will not match. So either you make your method non-static (if you want to do something with the target instance object) or you need to use another pointcut designator such as within(Christkind) instead. It seems as though you are not even using the supposed Christkind instance in your advice code, only the Population instance. Thus, just get rid of the target() part or replace it by within().

Feel free to ask related(!) follow-up questions.

before(): execution(* Packages.*.*(..))
    {
         //packages is com
        System.out.println(" TEST");    

    }

Try this to find your correct advice

Related