Recipe9.2.Capturing All Join Points Within a Particular Package


Recipe 9.2. Capturing All Join Points Within a Particular Package

Problem

You want to capture all the join points within classes that are, in turn, within a particular package scope.

Solution

Use the within(TypePattern) pointcut using the TypePattern to specify a package.

Discussion

The 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 package
public 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 Also

Recipe 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.



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