Search code examples
javaaspectjspring-aop

How do I create a pointcut only matching methods with no arguments?


I am trying to only match methods with no parameters.

So far, I can match any method by using something like @PointCut("execution(* * (..))") but that is too broad. I tried execution(* * ()) but that never triggered. Right now I am checking the length of the parameter array of the JoinPoint but I would like a more straightforward solution.

Edit: It was working indeed (thanks everyone for checking!) and I was looking at a different problem but maybe this helps someone else. My setup is a request handler that calls two methods (one without arguments and one with) in a third class and I wanted to advise only one of those methods - missing that the pointcut matched the request method which had the same number of arguments as one of the methods in the third class. These methods are not matched at all but that is a different problem.


Solution

  • What you are describing seems to be incorrect. I cannot reproduce this problem. Both pointcuts work as expected for me. Probably you have another problem, your situation being different from the simplified version you have shown here.

    I would, however, recommend to use execution(* *()) without the extra space between method name wildcard and opening parenthesis, because this is how AspectJ pointcuts are usually written. It does not make a difference in your case, though, it just looks strange.