Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why empty pointcut?

 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm total newbie to AspectJ - so I might be missing something very obvious.

I went through few articles online and found a typical practice:

There is an empty pointcut 'p' (i.e. an empty method with @Pointcut annotation) - having an expression 'e' (which matches a set of methods). Further, there is an advice 'a' - which is written for pointcut 'p'.

My question is - what is purpose of empty pointcut? I tried and found that I do not need empty pointcut. I can directly write an advice 'a' for an expression 'e'.

Thanks in advance.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do not define that empty @Pointcut method (which does not necessarily have to be empty), but instead define it on the advice it is referred to as an anonymous or un-named pointcut. You can draw a parallel to an anonymous class in Java. This is fine for simple point cuts but it suffers the same downsides of an anonymous class specifically it cannot be reused and is not as readable or maintainable. When you use the empty method it can be referenced from many advices and the name of the method can clearly specify the purpose of the pointcut expression.

HTH
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill. Its quite clear now.

So, by defining a pointcut, I don't have to copy-paste expression for all my advises right? (and in future, if I want to change the expression, I would change it only at one place)
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats correct you would do something like this:



Its clear now that someAdvice is applied to allOperations just by looking at it, and other advices can use the Pointcut as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic