Chapter 9. Capturing Join Points Within Programmatic Scope

Introduction

A common approach in the design of pointcut definitions is to limit the range of join points captured based on the programmatic scope of interest. This gives you some immediate control over what join points are going to be involved in further pointcut definitions.

This chapter describes the set of pointcuts that allow to you explicitly limit the join points captured based upon their programmatic scope. These pointcuts enable you to specify a method right through to package scope.

The pointcuts in this chapter are fairly simple to master, and they are some of the most commonly used elements of AspectJ. For example, a popular use of the within(TypePattern) pointcut (discussed in Recipe 5.1) is to use it in the !within(%THIS_ASPECT%) form. This AspectJ idiom limits the scope to every join point outside of the current aspect, providing protection against the advice triggering a recursive call to the same advice block and resulting in an infinite loop. The concepts in this chapter are basic to AspectJ programming and form the backbone of many of the operations detailed later in the book.

9.1. Capturing All Join Points Within a Particular Class

Problem

You want to capture all the join points within a particular class.

Solution

Use the within(TypePattern) pointcut, using the TypePattern to specify the particular class type pattern. The syntax of the within(TypePattern) pointcut is:

pointcut <pointcut name>(<any values to ...

Get AspectJ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.