Recipe 8.2. Capturing the Value of the Field Being AccessedProblemYou want to capture the value of the field being accessed so that it can be used in your corresponding advice. SolutionUse 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. DiscussionExample 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 accessedpublic 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 AlsoRecipe 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. |