Recipe8.2.Capturing the Value of the Field Being Accessed


Recipe 8.2. Capturing the Value of the Field Being Accessed

Problem

You want to capture the value of the field being accessed so that it can be used in your corresponding advice.

Solution

Use the after( ) returning(<ReturnValue>) form of advice with an identifier in the returning( ) part of the declaration to contain the value that has been accessed.

Discussion

Example 8-3 shows how the value of the MyClass.name attribute can be passed to the after( ) returning(<ReturnValue>) advice as triggered when the MyClass.name is accessed.

Example 8-3. Accessing the value of a field as it is returned when it is accessed
public aspect CaptureAccessedFieldValue  {    pointcut getNamePointcut( ) : get(String MyClass.name);    // Advice declaration    after( ) returning(String value) : 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("Value being accessed is " + value);              System.out.println(          "--------------------------------------------------");    } }

See Also

Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; The get(Signature) pointcut's syntax is examined in Recipe 8.1; the after( ) returning form of advice is shown in Recipe 13.6.



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