Recipe12.6.Reusing Pointcuts


Recipe 12.6. Reusing Pointcuts

Problem

You want to reuse a pointcut expression.

Solution

Declare a pointcut that can be referenced by name in the places where it is to be reused.

Discussion

Example 12-7 shows an example where the foundationNamedPointcut( ) named pointcut is reused when declaring the reuseNamedPointcut( ).

Example 12-7. Reuse of named pointcuts
public aspect PointcutReuseRecipe  {    /*            A pointcut declaration that is to be used and reused:                        Anonymous Pointcuts: call(void MyClass.foo(int,String)        */    pointcut foundationNamedPointcut( ) : call(       void MyClass.foo(int, String));    /*            A pointcut declaration that is built up from two            pointcuts:                        Anonymous Pointcuts: !within(AnonymousPointcutRecipe +)            Named Pointcuts:         foundationNamedPointcut( )                                                        */    pointcut reuseNamedPointcut( ) : foundationNamedPointcut( )       && !within(PointcutReuseRecipe +);    /*            A pointcut declaration attached to the advice it will invoke,            built up from simple named and anonymous pointcuts:                         Anonymous Pointcuts: !within(LogicalOrRecipe +)            Named Pointcuts: foundationNamedPointcut( );    */    before( ) : foundationNamedPointcut( )       && !within(PointcutReuseRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by foundationNamedPointcut( ) and");       System.out.println("!within(AnonymousPointcutRecipe( ) +");       System.out.println(          "Signature: " + thisJoinPoint.getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    }    /*            A pointcut declaration attached to the advice it will invoke,            built up from complex pointcuts built reusing other pointcut            declarations:                         Named Pointcuts: reuseNamedPointcut    */    before( ) : reuseNamedPointcut( )    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by reuseNamedPointcut( )");       System.out.println(          "Signature: " + thisJoinPoint.getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    } }

A named pointcut is similar to a method. It has a signature and can be referenced throughout the rest of the pointcut declarations within a particular aspect or even in other aspects according to the named pointcut's access modifier. This becomes increasingly important when we consider inheritance between aspects where pointcuts can be declared abstract and the actual pointcut logic can be implemented by the inheriting aspects.

When declaring pointcuts for reuse it is important that you consider where your pointcut delcarations are going to be reused. You use access modifiers in relation to your pointcut declarations in order to control where your pointcut declarations are visible within your application.

In AspectJ, pointcut declarations have the same access modifiers as regular Java methods: public, the pointcut delcaration is visible throughout your entire applications aspects; default (no modifier specified), the pointcut declaration is visible to all other aspects in the same package; protected, the pointcut delcaration is visible only to subaspects; private, the pointcut delcaration is only visible in the aspect within it is declared.

See Also

Inheritance between aspects and the implications on pointcut declarations is discussed in Chapter 15; techniques for combining pointcut declarations are covered in Recipes 12.2 and 12.3; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.6; Chapter 13 describes the different types of advice available in AspectJ.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net