Recipe11.2.Capturing When a Join Point s Target Object Is a Specific Type


Recipe 11.2. Capturing When a Join Point's Target Object Is a Specific Type

Problem

You want to capture all join points encountered when a join point's target object, if any, is of a specific type.

Solution

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

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

Discussion

The target([Type | Identifier]) pointcut has five key characteristics:

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

  2. A Type definition parameter must resolve to a valid class to pick up on the relevant join points. This is different from a TypePattern where wildcards may be employed.

  3. An Identifier is used to examine the type of the runtime object referenced as the target at the captured join point and to expose that object to the advice if required.

  4. Using a * wildcard allows you to state there must be a target for the join point, but you are not interested in what type it is.

  5. Join points that occur on exception handling using the handler(TypePattern) pointcut, static class initialization using the staticinitialization(TypePattern) and interestingly the object preinitialization using the preinitialization(Signature) pointcut do not have any target context to expose using the target([Type | Identifier]) pointcut.

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

  • Capture join points when the target is a MyClass object indicated by Identifier

  • Capture join points where the type of the target of a method call is AnotherClass specified by Type

Example 11-2. Using the target([Type | Identifier]) pointcut to capture join points based on the type of a methods target
public aspect TargetRecipe  {    /*        Specifies calling advice whenever the target of a methods        is of a type that matches the following rules:            Identifier/s: MyClass object    */    pointcut targetIdentifierMyClassPointcut(MyClass object) : target(object);    /*        Specifies calling advice whenever the target of a methods        is of a type that matches the following rules:                Type Pattern: AnotherClass    */    pointcut targetTypePatternAnotherClassPointcut( ) : target(AnotherClass);    // Advice declaration    before(MyClass object) : targetIdentifierMyClassPointcut(object)       && !within(TargetRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by targetIdentifierMyClassPointcut");       System.out.println("Join Point Kind: " + thisJoinPoint.getKind( ));       System.out.println("Object referenced by Target passed by Identifier: "           + object);       System.out.println(          "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getStaticPart( )          .getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    }    // Advice declaration    before( ) : targetTypePatternAnotherClassPointcut( )     && !within(TargetRecipe +)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by targetTypePatternAnotherClassPointcut");       System.out.println("Join Point Kind: " + thisJoinPoint.getKind( ));       System.out.println(          "Object referenced by Target: " + thisJoinPoint.getTarget( ));       System.out.println(          "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: " + thisJoinPoint.getStaticPart( )          .getSourceLocation( ));       System.out.println(          "------------------------------------------------------------");    } }

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

Figure 11-2. How the target(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