O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.4. Executing Advice Around a Join Point

Problem

You want advice to execute around the join points that trigger it.

Solution

Use the around( ) type of advice.

Discussion

Example 13-4 shows how to execute advice around the void MyClass.foo(int, String) method.

Example 13-4. Executing advice around a method call

public aspect AroundAdviceRecipe 
{
   /*
           Specifies calling advice whenever a method
           matching the following rules gets called:
   
           Class Name: MyClass
           Method Name: bar
           Method Return Type: int
           Method Parameters: 
           */
   pointcut callFooPointCut( ) : call(int MyClass.foo( ));

   /*
    Specifies calling advice whenever a method
    matching the following rules gets called:
   
    Class Name: MyClass
    Method Name: bar2
    Method Return Type: int
    Method Parameters: int
    */
   pointcut callBarPointCut(int value) : call(int MyClass.bar(int))
      && args(value);
   
   /*
          Specifies calling advice whenever a method
          matching the following rules gets called:
  
          Class Name: MyClass
          Method Name: baz
          Method Return Type: int
          Method Parameters: 
          */
   pointcut callBazPointCut( ) : call(int MyClass.baz( ));

    // Advice declaration
        // This advice will be executed before the pointcut that picks it
   int around( ) : callFooPointCut( ) && !within(AroundAdviceRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Location: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "------------------------------------------------------------"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required