I have a “working” AspectJ Weaver pattern for matching all the setters inside a certain type (Item):
public pointcut setter(Item item) :
target(item)
&& execution(void Item+.set*(*));
It works just swell for all the potential setters, but it also matches methods with names like “setup” that takes one argument.
So my solution right now is to check in the method that intercepts all setters if the 4th character is lowercase or not (based on thisJoinPoint.getSignature().getName()), and based on that not continue with the code.
But is there a better way to exclude all methods that have a lowercase character after “set”? As far as I know regular expressions are not part of AspectJ patterns, right?
I don’t know if this is less ugly, but you could introduce an annotation @NoSetter and use this in another
pointcut noSetterto exclude the methods likesetup(...)where you do not want the aspect applied:Modify the pointcut
setteras follows:I’ve used this approach in a situation where setter methods needed to fire a certain event. We could not generate the code and doing this by hand was too tedious and error-prone. Also, on some setters the event was to be explicitly not fired at all.
[The above syntax is untested, don’t have an IDE right now to verify this]