Recipe11.1.Capturing When the this Reference Is a Specific Type


Recipe 11.1. Capturing When the this Reference Is a Specific Type

Problem

You want to capture all join points where the this reference at a join point is of a specific type.

Solution

Use the this([Type | Identifier]) pointcut. The syntax of the this([Type | Identifier]) pointcut is:

pointcut <pointcut name>(<any values to be picked up>) :              this(<type> or <identifier> or *);

Discussion

The this([Type | Identifier]) pointcut examines the this reference at the captured join point to decide whether to invoke the associated advice and has five key characteristics:

  1. The this([Type | Identifier]) pointcut captures all join points when the executing object is of the specified type.

  2. A Type definition parameter must resolve to a valid class. This is not the same as a TypePattern, where wildcards may be employed.

  3. An Identifier is used to examine the type of the runtime object referenced to by this and to expose that object to the advice if required.

  4. Using a * wildcard allows you to state a valid object must be pointed to by the this reference at the join point, but you are not interested in what type it is.

  5. Join points that occur on exception handling do not have a value for using the handler(TypePattern) pointcut, when they use the handler(TypePattern) within any static block of code including static class initialization specified using the staticinitialization(TypePattern) pointcut, and interestingly the object pre-initialization using the preinitialization(Signature) pointcut, do not have a value for the this reference to expose using the this([Type | Identifier]) pointcut.

Example 11-1 shows two examples of the this([Type | Identifier]) pointcut being used to:

  • Capture join points where the this reference is pointing to an object of type MyClass using an Identifier

  • Capture when the this reference's type is AnotherClass using a Type specification

Example 11-1. Using the this(Type | Identifier) pointcut to capture join points based on the type of the this reference
public aspect ThisRecipe  {    /*        Specifies calling advice whenever the executing        object is of a type that matches the following rules:                Identifier/s: MyClass object    */    pointcut thisIdentifierMyClassPointcut(MyClass object) : this(object);    /*        Specifies calling advice whenever the executing        object is of a type that matches the following rules:                Type Pattern: AnotherClass    */    pointcut thisTypePatternAnotherClassPointcut( ) : this(AnotherClass);    // Advice declaration    before(MyClass object) : thisIdentifierMyClassPointcut(object)       && !within(ThisRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by thisIdentifierMyClassPointcut");       System.out.println("Join Point Kind: " + thisJoinPoint.getKind( ));       System.out.println(          "this reference as passed by Identifier " + object);       System.out.println(          "Object referenced by this: " + thisJoinPoint.getThis( ));       System.out.println(          "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    }    // Advice declaration    before( ) : thisTypePatternAnotherClassPointcut( ) && !within(ThisRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by thisTypePatternAnotherClassPointcut");       System.out.println("Join Point Kind: " + thisJoinPoint.getKind( ));       System.out.println(          "Type of executing object: "             + thisJoinPoint.getThis( ).getClass( ).getName( ));       System.out.println(          "Object referenced by this: " + thisJoinPoint.getThis( ));       System.out.println(          "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    } }

Figure 11-1 shows how the this([Type | Identifier]) pointcut is applied.

Figure 11-1. How the this([Type | Identifier]) pointcut is applied


See Also

The calling context that is available to advice is covered in Chapter 13; the handler(TypePattern) pointcut is described in Recipe 5.1; the preinitializa-tion(Signature) pointcut is described in Recipe 7.4; the staticinitializa-tion(TypePattern) pointcut is described in Recipe 7.5; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4.



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