Recipe15.4.Declaring Aspects Inside Classes


Recipe 15.4. Declaring Aspects Inside Classes

Problem

You want to declare an internal class-wide cross-cutting concern.

Solution

Use an inner aspect declaration.

Discussion

Example 15-7 shows how to define an inner aspect that is applied to its containing class. The call(TypePattern) pointcut is used to capture join points on all calls to the MyClass class.

Example 15-7. Declaring an inner aspect
public class MyClass {    /*       Specifies calling advice whenever a method       matching the following rules gets called:           Class Name: MyClass       Method Name: *       Method Return Type: * (any return type)       Method Parameters: .. (any parameters0       */    private static aspect CallMethods     {       pointcut callPointCut( ) : call(* MyClass.* (..));       // Advice declaration       before( ) : callPointCut( ) && !within(CallMethods +)       {          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(             "----------------------------------------------------");       }    }    public void foo(int number, String name)    {       System.out.println("Inside foo (int, String)");    }    public void bar(String name)    {       System.out.println("Inside bar (String)");       this.privateMethod( );    }    private void privateMethod( )    {       System.out.println("In privateMethod ( )");    }    public static void main(String[] args)    {       // Create an instance of MyClass       MyClass myObject = new MyClass( );       // Make the call to foo       myObject.foo(1, "Russ");       //                Make the call to bar       myObject.bar("Kim");    } }

Cross-cutting behavior is often thought of as system-wide in scope, but the classes may exhibit cross-cutting concerns internally. Any behavior, common across a group of methods in a class, is a candidate for being a cross-cutting concern especially if it is not core to the classes purpose within the business logic.

Inner aspects have three key characteristics:

  1. Inner aspects must be explicitly declared static as their instantiation is controlled by an aspect instantiation policy and not by the construction of the surrounding class.

  2. Inner aspects may have all of the regular access modifiers applied to them to allow encapsulation protection and static inheritance reuse where appropriate.

  3. An inner aspect is not restricted to being woven against the surrounding class. By declaring an inner aspect, you are implying that the aspect is strongly related to the surrounding class but the aspect is not constrained in that way.

Inner aspects are useful when refactoring your existing code. The first step is to identify behavior that is cross-cutting in nature, i.e., that it affects more than one area of your class and may not naturally sit with the fundamental business purpose of the class.

One or more inner aspects can then be declared that modularize this cross-cutting behavior. Some commonality may exist between the inner aspects in your application, so the next stage could be to refactor the inner aspects into more traditional system scoped aspects.

See Also

Recipe 4.1 shows the definition of the call(Signature) pointcut; the within(TypePat-tern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4; the before( ) form of advice is explained in Recipe 13.3; aspect instantiation policies are covered in Chapter 14; more refactoring concerns and approaches are shown in Recipe 15.3; a series of articles from Ramnivas Laddad, at http://www.theserverside.com/articles/article.tss?l=AspectOrientedRefactoringPart1/1 provides more information on aspect-oriented refactoring.



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