Recipe8.4.Capturing the Value of a Field When It Is Modified


Recipe 8.4. Capturing the Value of a Field When It Is Modified

Problem

You want to capture the value of the field after it has been modified so it can be used in your corresponding advice.

Solution

Combine the args([Types | Identifiers]) pointcut with the set(Signature) pointcut to expose the new value of the field being set as an identifier on your pointcut that can be passed to the corresponding advice.

Discussion

Example 8-5 shows how the new value, that the MyClass.name attribute is being set to, can be passed to the before( ) advice.

Example 8-5. Accessing the new value of the field being set
public aspect CaptureModifiedFieldValue  {    pointcut setNamePointcut(String newValue) : set(String MyClass.name)                               && args(newValue);       // Advice declaration    before(String newValue) : setNamePointcut(newValue)    {       System.out.println(          "------------------- Aspect Advice Logic --------------------");       System.out.println(          "In the advice picked by " + "setNamePointcut( )");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));              System.out.println("Field Value set to: " + newValue);              System.out.println(          "------------------------------------------------------------");    } }

See Also

Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; The set(Signature) pointcut's syntax is examined in Recipe 8.4; the args([Types | Identifiers]) pointcut declaration is explained in Recipe 11.3; combining pointcut logic using a logical AND (&&) is shown in Recipe 12.2; Chapter 13 describes the different types of advice available in AspectJ.



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