Skip to content
Advertisement

AspectJ pointcut expression match parameter annotations at any position

I’m trying to define a pointcut expression to match methods which contain a parameter annotated with a specific annotation, regardless of what position the parameter is in. In my case I’m looking for the @Constraint annotation. For example:

Matching methods:

JavaScript

So far I’ve tried the following expressions with no luck:

JavaScript

Can someone point me to the right solution? is it even possible?

Advertisement

Answer

You cannot bind an argument at an arbitrary position via args() in AspectJ because this could lead to ambiguity. Just imagine you have two or more parameters of the same type (or annotated by the same annotation type in this case). Which one of them should be bound to the named args() parameter? So while

JavaScript

is possible as a stand-alone expression (please note the parentheses around the star), it is not possible in combination with args(). So if you do not just want to intercept the method execution itself, but also find the first or all parameters with the given annotation you need to do just what I showed in the other article. I am kinda repeating myself, but so be it in order for the answer not to be deleted again:

JavaScript
JavaScript
JavaScript

As you can see, it is a bit trickier to get the annotations of a given parameter than just its declared type, but basically it works the same way as in my previous post, i.e. by iterating over the list of arguments.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement