Recipe 15.2. Implementing Abstract PointcutsProblemYou want to declare and reference a pointcut but leave the definition of the pointcut logic to be implemented by specialized subaspects. SolutionUse the abstract keyword when declaring the pointcut and the surrounding aspect, and do not supply any pointcut logic. DiscussionExample 15-3 shows an abstract pointcut being declared and used throughout an aspect. Example 15-3. Declaring abstract pointcuts for implementation by derived aspectspublic abstract aspect BaseAbstractAspect { /* Specifies an abstract pointcut placeholder for derived aspects to specify */ public abstract pointcut abstractBasePointcut( ); /* Specifies calling advice whenever a jhin point picked by the abstractBasePointcut (specified by specialized aspects) is encountered, and not within this aspect or any inheriting aspects. */ pointcut runAdvicePointcut( ) : abstractBasePointcut( ) && !within(BaseAbstractAspect +); } Example 15-4 shows how the abstract pointcut declared in Example 15-3 can be fully defined in the subaspect. Example 15-4. Implementing the full pointcut logic of the inherited abstract base pointcutpublic aspect AbstractImplementationAspect extends BaseAbstractAspect { /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: foo Method Return Type: void Method Parameters: an int followed by a String */ public pointcut abstractBasePointcut( ) : call(void MyClass.foo(int, String)); // Advice declaration before( ) : runAdvicePointcut( ) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice attached to the call point cut"); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } Figure 15-2 shows the inheritance relationship between the aspects in Examples Example 15-3 and Example 15-4. Figure 15-2. Implementing abstract pointcutsAn abstract pointcut declaration is useful when you want to postpone the definition of specific pointcut logic while still declaring more generic and potentially reusable pointcuts. Using this approach, generic abstract aspects can be created that can be extended specifically for a particular application. This can lead to libraries of reusable aspects. See AlsoRecipe 4.1 shows the definition of the call(Signature) pointcut; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4; Chapter 17 through Chapter 19 use the techniques shown in this example to define reusable aspects when implementing object-oriented design patterns. |