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