Recipe7.5.Capturing When a Class Is Initialized


Recipe 7.5. Capturing When a Class Is Initialized

Problem

You want to capture when a class is initialized.

Solution

Use the staticinitialization(TypePattern) pointcut. The syntax of the staticinit-ialization(TypePattern) pointcut is:

pointcut <pointcut name>(<any values to be picked up>) :              staticinitialization(<class>);

Discussion

The staticinitialization(TypePattern) pointcut has two key characteristics:

  1. There are limitations on the environment available to advice picked by the staticinitialization(TypePattern) pointcut. There is no parent object triggering the static initialization; therefore, there is no this reference. There is also no instance object involved, and, therefore, there is no target reference.

  2. The TypePattern can include wildcard characters to select a range of different classes.

Example 7-5 shows the staticinitialization(TypePattern) pointcut capturing join points in the static initialization of the MyClass class.

Example 7-5. Using the staticinitialization(TypePattern) pointcut to capture join points on the static initialization of a specific class
public aspect StaticInitializationRecipe  {    /*        Specifies calling advice when a class is initialized        that meets the following type pattern rules:            Class Name: MyClass    */    pointcut myClassStaticInitializationPointcut( ) :        staticinitialization(MyClass);    // Advice declaration    before( ) : myClassStaticInitializationPointcut( )    {       System.out.println(          "-------------- Aspect Advice Logic ---------------");       System.out.println(          "In the advice picked by "             + "myClassStaticInitializationPointcut( )");       System.out.println(          "Join Point Kind: "             + thisJoinPoint.getStaticPart( ).getKind( ));       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));       System.out.println(          "--------------------------------------------------");    } }

Figure 7-5 shows how the staticinitialization(TypePattern) pointcut is applied.

Figure 7-5. How the staticinitialization(TypePattern) pointcut is applied


See Also

Recipe 5.1 shows some of the wildcard variations that can be used in a TypePattern; Chapter 13 describes the different types of advice available in AspectJ including the associated different forms of environment that they expose.



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