I don't know if this is possible, but I am trying to write a pointcut which would match any method that returns an object implementing a specific interface.

Given the following:

public class User implements Auditable{
   private int id;

   private String name;

   public String getName(){
      return name;
   }
}

and the interface Auditable:

public interface Auditable{
   public String getName();
}

And some random class:

public class RandomClass{
    public User getNewUser(){
       User u = new User();
       return u;
    }
}

How can I write an "AfterReturning" pointcut that would catch any method called getNew* that implements Auditable?

The following works:

pointcut auditablePointcut(): call(public * *.getNew*(..))

however, that will match any returning type. The following does not work:

pointcut auditablePointcut(): call(public Auditable *.getNew*(..))

I presume I could write it using an if(), but that seems a little kludgy (I haven't tried it yet). Or is there a more elegant manner?

有帮助吗?

解决方案

after () returning (Audible au) : call(public * *.getNew*(..))

其他提示

I'm not sure about pointcut, but you can check return type of the method in aspects like this:

MethodSignature signature = (MethodSignature)joinPoint.getSignature();
signature.getReturnType(); // method return type
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top