Search code examples
javaaopaspectj

Why is the below code giving me "adviceDidNotMatch"


I am trying to learn AspectJ and so far I am getting used to the concepts well. So here I am trying to write the aspect class for validation on an Object. But the below code gives me adviceDidNotMatch thing.

before(com.message.pojo.Entity entity) : call(*public com.message.helper.Processor.process(com.message.pojo.Entity))
     && args(entity)
        && target(com.message.helper.MessageProcessor){
        ValidationUtil validation = new ValidationUtil();
        validation.validate(entity);
    }

Now all the qualified names here I check is correct. Please check the screenshot of my java project structure.

[Project Structure


Solution

  • You have a little syntax problem in your pointcut: Instead of call(* public it should be call(public *, then it works.

    BTW, you can also replace your fully qualified class names by imports in native AspectJ syntax. The FQDN are only necessary in annotation-based @AspectJ syntax. Assuming that we are talking about ValidationAspect from your screenshot, for the two classes in the same package you do not even need any imports. Try it like this:

    package com.message.helper;
    
    import com.message.pojo.Entity;
    
    public aspect ValidationAspect {
      before(Entity entity) :
        call(public * Processor.process(Entity)) &&
        args(entity) &&
        target(MessageProcessor)
      {
        ValidationUtil validation = new ValidationUtil();
        validation.validate(entity);
      }
    }