Aspectj Cookbook
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:
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. |