after () returning (Audible au) : call(public * *.getNew*(..))
How to write the proper pointcut to match an interface returning object?
Question
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?
Solution
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow