Recipe8.1.Capturing When an Object s Attribute Is Accessed


Recipe 8.1. Capturing When an Object's Attribute Is Accessed

Problem

You want to capture when an object's attribute is accessed.

Solution

Use the get(Signature) pointcut. The syntax of the get(Signature) pointcut is:

pointcut <pointcut name>(<any values to be picked up>) :              get(<optional modifier> <type> <class>.<field>);

Discussion

The get(Signature) has four key characteristics:

  1. The get(Signature) pointcut triggers advice where the attribute is directly accessed, not just on a call to an accessor method.

  2. The get(Signature) pointcut cannot capture access to static attributes although it is perfectly legal within the syntax of AspectJ to define a pointcut in this way.

  3. The Signature must resolve to an attribute of a particular class.

  4. The Signature can include wildcard characters to select a range of join points on different attributes.

Example 8-1 shows the get(Signature) pointcut being used to capture join points that are encountered whenever the String MyClass.name attribute is accessed.

Example 8-1. Using the get(Signature) pointcut to capture access to attributes
public aspect GetRecipe  {    /*            Specifies calling advice whenever an attribute             matching the following rules is accessed:                        Type: String            Class Name: MyClass            Attribute Name: name    */    pointcut getNamePointcut( ) : get(String MyClass.name);    // Advice declaration    before( ) : getNamePointcut( )    {       System.out.println(          "-------------- Aspect Advice Logic ---------------");       System.out.println(          "In the advice picked by " + "getNamePointcut( )");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));       System.out.println(          "--------------------------------------------------");    } }

Figure 8-1 shows how the get(Signature) pointcut is applied to a simple class.

Figure 8-1. How the get(Signature) pointcut is applied


You might expect that where a class has a constant attribute defined, using the static and final keywords, you could capture when that constant is accessed using the get(Signature) pointcut. The getConstrantPointcut() pointcut in Example 8-2 attempts to capture when the MyClass.CONSTANT attribute is accessed for just this purpose.

Example 8-2. Using the get(Signature) pointcut to capture when a constant is accessed
public aspect GetConstant  {    /*           Specifies calling advice whenever an attribute            matching the following rules is accessed.                      Type: String           Class Name: MyClass           Attribute Name: CONSTANT   */   pointcut getConstantPointcut( ) : get(public static final    String MyClass.CONSTANT);      // Advice declaration   before( ) : getConstantPointcut( )   {      System.out.println(         "-------------- Aspect Advice Logic ---------------");      System.out.println(         "In the advice picked by " + "getConstantPointcut( )");      System.out.println(         "Signature: "            + thisJoinPoint.getStaticPart( ).getSignature( ));      System.out.println(         "Source Line: "            + thisJoinPoint.getStaticPart( ).getSourceLocation( ));      System.out.println(         "--------------------------------------------------");   } }

In fact, this form of constant access is not caught by the getConstantPointcut( ) pointcut even though this is acceptable AspectJ syntax. Though this is a valid get(Signature) pointcut declaration, unlike regular variable attributes, the Java compiler "inlines" static final attributes; therefore, they do not exist in a form suitable for access with a pointcut.

Take care when using the get(Signature) pointcut as it breaks the encapsulation of attributes that may be declared private. This breaking of encapsulation could lead to brittle software. Because you are specifying pointcuts based on variable names, you must remember that this can add detrimental tighter coupling to your design.


See Also

Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; Chapter 13 describes the different types of advice available in AspectJ; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is 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