Chapter 7. Capturing Join Points on Class Object Construction

Introduction

There are four initialization and construction stages that Java classes and objects go through before they can be used within your application, ignoring the actual loading of the class into your Java Virtual Machine (JVM) by its class loader.

The first step in the process is the invocation of a constructor using the new keyword. This invocation, if the class has not been previously used by the application, triggers static class initialization for the class of the object being constructed. Static class initialization is when the class itself is initialized so that all of its static variables and methods are properly constructed.

Once the class has been statically initialized, and before the constructor method can be executed, the object itself must be initialized. Object initialization first constructs the inheritance hierarchy for the object by executing all of the object’s superclass constructors. Finally, once the superclass constructors have returned successfully, the initialized object is all set to complete the execution of its constructor method.

AspectJ provides a specific pointcut for each of the stages involved in a class and object’s construction and initialization, and this chapter will walk you through the syntax and key characteristics of each.

7.1. Capturing a Call to a Constructor

Problem

You want to capture when a call to a constructor that matches a specific signature is invoked.

Solution

Use the ...

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.