Recipe7.1.Capturing a Call to a Constructor


Recipe 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 call(Signature) pointcut with the additional new keyword as part of the signature. The syntax for using the call(Signature) pointcut in relation to constructors is:

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

Discussion

The call(Signature) pointcut has three key characteristics when used to capture calls to constructors:

  1. The call(Signature) pointcut with the new keyword captures join points when a class is instantiated into an object.

  2. By using the around( ) form of advice, the call(Signature) pointcut can override the type of returned object, within the bounds of the normal inheritance rules of Java.

  3. The specified Signature is not checked by the compiler to correspond to an actual constructor.

Example 7-1 shows how to use the call(Signature) pointcut to capture calls to the constructor on the MyClass class that takes an int and String as parameters.

Example 7-1. Using the call(Signature) pointcut to capture join points when a constructor is called
public aspect CallNewRecipe  {    /*        Specifies calling advice when any constructor is called        that meets the following signature rules:            Class Name: MyClass        Method Name: new (This is a keyword indicating the constructor call)        Method Parameters: int, String    */    pointcut myClassConstructorWithIntAndStringPointcut( ) :                 call(MyClass.new (int, String));    // Advice declaration    before( ) : myClassConstructorWithIntAndStringPointcut( )    {       System.out.println(          "-------------- Aspect Advice Logic ---------------");       System.out.println(          "In the advice picked by "             + "myClassConstructorWithIntAndOthersPointcut( )");       System.out.println(          "The current type of object under construction is: ");       System.out.println(thisJoinPoint.getThis( ));       System.out.println(          "Signature: "             + thisJoinPoint.getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getSourceLocation( ));       System.out.println(          "--------------------------------------------------");    } }

Figure 7-1 shows how the call(Signature) pointcut is applied.

Figure 7-1. How the call(Signature) pointcut is applied to constructors


See Also

The execution(Signature) and call(Signature) pointcuts in Recipes Recipe 4.1 and Recipe 4.4 respectively deal with attaching advice to methods other than constructors; Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; Chapter 13 deals with the environments available to the advice when picked by the different pointcuts; Recipe 20.2 describes in more detail the use of the around( ) advice, specifically in dealing with overriding constructors to return a different type of object.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net