O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required