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.3. Capturing When the Arguments to a Join Point Are a Certain Number, Type, and Ordering

Problem

You want to capture all join points encountered when a join point’s arguments, if any, are of a specific type, number, and ordering.

Solution

Use the args([Types | Identifiers]) pointcut. The syntax for this pointcut is:

pointcut <pointcut name>(<any values to be picked up>) : 
            args(<types> or <identifiers> | .. | *, <repeat>);

Discussion

The args([Types | Identifiers]) pointcut has seven key characteristics:

  1. Any combination of Types and Identifiers can be used to narrow to the right join points and expose the right context to the advice.

  2. An Identifier is used to examine the type of the runtime objects that are the arguments at the captured join point and to expose those objects to the advice if required.

  3. Using .. allows you some flexibility in the number of arguments that a particular join point must have to match against your args([Types | Identifiers]) declaration.

  4. Without the .., the args([Types | Identifiers]) pointcut picks all join points that exactly match the types of the arguments specified. This is to say that the pointcut will only match join points on methods that have the same order, number, and types of parameters at runtime.

    With the .., a “best fit” policy is used by the pointcut. This means the statement args(MyClass,..,float) would result in any join points being matched that have two arguments, starting with a MyClass object and followed by any number of arguments that ...

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