Recipe 9.2. Capturing All Join Points Within a Particular PackageProblemYou want to capture all the join points within classes that are, in turn, within a particular package scope. SolutionUse the within(TypePattern) pointcut using the TypePattern to specify a package. DiscussionThe within(TypePattern) pointcut provides a useful means of specifying an interest in the join points that occur in every class within a package by using wildcards. Entire packages of join points can be included or excluded from the rest of the pointcut logic using the appropriate wildcards in the TypePattern. Example 9-2 shows the within(TypePattern) pointcut being used to capture all the join points within the packageA package. Example 9-2. Using the within(TypePattern) pointcut to capture join points within a specific packagepublic aspect WithinPackageRecipe { /* Specifies calling advice on any join point is encountered within the defined scope: Scope: packageA */ pointcut withinPackageA( ) : within(packageA.*); // Advice declaration before( ) : withinPackageA( ) && !within(WithinPackageRecipe +) { System.out.println("-------------- Aspect Advice Logic ---------------"); System.out.println("In the advice picked by " + "withinPackageA( )"); System.out.println("Join Point Kind: " + thisJoinPoint.getKind( )); System.out.println("Signature: " + thisJoinPoint.getStaticPart( ).getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getStaticPart( ).getSourceLocation( )); System.out.println("--------------------------------------------------"); } } The within(TypePattern) pointcut's key characteristics are listed in Recipe Recipe 6.1. See AlsoRecipe 5.1 shows some of the wildcard variations that can be used in a TypePattern; Recipe 9.1 shows the more traditional use of the within(TypePattern) pointcut definition for capturing the join points within a specific class; the mechanisms by which pointcuts can be combined are described in the recipes in Chapter 12; the within(TypePattern) pointcut is often negated to remove an aspect from the scope of the weaving using the unary NOT (!) operator as described in Recipe 12.4. |