Search code examples
javaaspectjaspect

"adviceDidNotMatch" warning when i try adding a new advice to an aspectJ


I am trying to add a new advice to my aspectJ.

public aspect Aspect11 {

    pointcut namePC(String s, int i) : call (public String Simple.getName(String, int))&&args(s,i);

    pointcut getData(int j) : get(public int Simple.trial)&&args(j);

    after(String s, int i) : namePC(s, i) {
        // this is the advice
        System.out.println("Printing..." + s + "   " + i);
        System.out.println(thisJoinPoint.getSignature());
    }

    before(int j):getData(j)
    {
        System.out.println(j);
    }
}

In the code above, pointcut namePC() and its advice were already added. This was working as expected.

Following is my Simple.java

public class Simple {
private String name = "Aman";
String home;
int trial;
public String getName(String s, int i) {

    System.out.println("Hi in getName()" + s + "   " + i);
    return name;
}

public static void main(String args[]) {
    Simple simple = new Simple();
    simple.trial=8;
    System.out.println("AA" + simple.getName("In Simple", 1));
}

}

When i try adding the new point cut and its advice : getData(), i am getting a warning : "advice defined in Aspect11 has not been applied [Xlint:adviceDidNotMatch]" Am new to aspectJ and not getting a way to sort this out!!


Solution

  • Your edited version still does not work for two reasons:

    • Simple.trial is not public as your pointcut states.
    • Why should a get() pointcut match if you never read from the member? In your code there is only an assignment, i.e. a set() pointcut would match.

    Try this:

    public class Simple {
        private String name = "Aman";
        private int trial;
    
        public static void main(String args[]) {
            Simple simple = new Simple();
            simple.trial = 8;
            System.out.println(simple.getName("foo", 1));
        }
    
        public String getName(String s, int i) {
            System.out.println("getName(" + s + ", " + i + ")");
            return name;
        }
    }
    
    public aspect Aspect11 {
        pointcut namePC(String s, int i) :
            call (public String Simple.getName(String, int)) && args(s,i);
    
        pointcut setData(int j) :
            set(int Simple.trial) && args(j);
    
        after(String s, int i) : namePC(s, i) {
            System.out.println("namePC: " + thisJoinPoint.getSignature());
        }
    
        before(int j) : setData(j) {
            System.out.println("setData: " + j);
        }
    }