Search code examples
aoppointcuts

Pointcuts and Aspect-Oriented Programming


How are pointcuts used in aspect-oriented programming language to add functionality into an existing program?

To my understanding, from this Wikipedia article - http://en.wikipedia.org/wiki/Pointcut

Pointcuts are placed into a specific spot in a piece of code, and when that point is reached, based on the evaluation of the pointcut, more code can be executed at a specific point somewhere in the code based on the evaluation of the pointcut. Is this a correct understanding?

If so, then that would add functionality because the programmer can execute different piece of code based off that evaluation.


Solution

  • For example, I have an application with a number of service objects and I want to time every method. Using AspectJ notation:

    class MyAspect
    {
        @Around("execution(public * my.service.package.*(..))")
        public Object aroundAdvice(JoinPoint jp)
        {
           // start timer
           Object o = jp.proceed();
           // stop timer, etc.
           return o;
        }
    }
    

    Here, the "execution(public * my.service.package.*(..))" is the pointcut: it specifies the set of join points for which the advice will be executed (the execution of all methods in all classes in the service package).