*
The moose likes Aspect Oriented Programming and the fly likes Why empty pointcut? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Aspect Oriented Programming
Bookmark "Why empty pointcut?" Watch "Why empty pointcut?" New topic
Author

Why empty pointcut?

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

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


[How To Ask Questions][Read before you PM me]
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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

Joined: Mar 07, 2010
Posts: 1648
    
    7

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.
 
wood burning stoves
 
subject: Why empty pointcut?
 
Similar Threads
Spring transaction using AOP
Spring AOP, How does this example do it?
How to pass information from target object to Advice Object
Any good examples of using @Before instead of setup
want an advisor for many pointcut