Recipe 7.5. Capturing When a Class Is InitializedProblemYou want to capture when a class is initialized. SolutionUse the staticinitialization(TypePattern) pointcut. The syntax of the staticinit-ialization(TypePattern) pointcut is: pointcut <pointcut name>(<any values to be picked up>) : staticinitialization(<class>); DiscussionThe 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 classpublic 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 appliedSee AlsoRecipe 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. |