Aspectj Cookbook

Recipe 7.4. Capturing When an Object Is About to Be Initialized

Problem

You want to capture when an object is about to be initialized using a constructor that matches a specific signature.

Solution

Use the preinitialization(Signature) pointcut. The syntax of the preinitializa-tion(Signature) pointcut is:

pointcut <pointcut name>(<any values to be picked up>) : preinitialization(<optional modifier> <class>. new(<parameter types>));

Discussion

The preinitialization(Signature) pointcut has five key characteristics:

  1. The preinitialization(Signature) pointcut must contain the new keyword.

  2. Join points caught by the preinitialization(Signature) pointcut occur after the caught constructor is entered and before any super class constructors are called.

  3. The Signature must resolve to a constructor.

  4. The preinitialization(Signature) pointcut provides compile-time checking that a constructor is being referenced.

  5. Due to a compiler limitation in the AspectJ compiler, the preinitializa-tion(Signature) pointcut cannot be used when associated with around( ) advice.

Example 7-4 shows the preinitialization(Signature) pointcut capturing join points before the initialization of an object using the MyClass constructor with an int and a String as parameters.

Example 7-4. Using the preinitialization(Signature) pointcut to capture join points before the execution of a specific constructor

public aspect PreInitializationRecipe { /* Specifies calling advice just before an object initializes using a constructor that meets the following signature rules: Class Name: MyClass Method Name: new (This is a keyword indicating the constructor call) Method Parameters: an int followed by a String */ pointcut myClassIntStringObjectPreInitializationPointcut( ) : preinitialization(MyClass.new (int, String)); // Advice declaration before( int number, String name) : myClassIntStringObjectPreInitializationPointcut( ) && args(number, name) { System.out.println( "-------------- Aspect Advice Logic ---------------"); System.out.println( "In the advice picked by " + "anyMyClassObjectInitializationPointcut( )"); System.out.println( "The current type of object under construction is: "); System.out.println(thisJoinPoint.getThis( )); System.out.println( "The values passed in were: " + number + ", " + name); System.out.println( "Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println( "--------------------------------------------------"); } }

Figure 7-4 shows how the preinitialization(Signature) pointcut is applied to pick join points before an object is initialized.

Figure 7-4. How the preinitialization(Signature) pointcut is applied

See Also

Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; The adviceexecution() pointcut is covered in Recipe 6.1; Chapter 13 describes the different types of advice available in AspectJ.

Категории