Aspectj Cookbook
Recipe 4.2. Capturing the Parameter Values Passed on a Method Call
Problem
You want to capture and use the parameters passed on a method call. Solution
Create a pointcut that specifies the parameters that you want to capture as identifiers in its signature. Use the call(Signature) and args([TypePatterns | Identifiers]) pointcuts to capture the call to the method and then to bind the required identifiers to the values of the method's parameters. Discussion
Example 4-2 shows the call(Signature) pointcut being used to declare an interest in all methods that match the signature MyClass.foo(int,String). The captureCallParameters(int,String) pointcut requires an int and a String as specified by the value and name identifiers. Those identifiers are then bound to the methods parameters by the args([Types | Identifiers]) pointcut. Example 4-2. Capturing the int and String values that are passed on a call to the MyClass.foo(..) method
public aspect CaptureCallParametersRecipe { /* Specifies calling advice whenever a method matching the following rules gets called: Class Name: MyClass Method Name: foo Method Return Type: void Method Parameters: an int followed by a String */ pointcut captureCallParameters(int value, String name) : call(void MyClass.foo(int, String)) && args(value, name); // Advice declaration before(int value, String name) : captureCallParameters(value, name) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice attached to the call point cut"); System.out.println("Captured int parameter on method: " + value); System.out.println("Captured String parameter on method: " + name); System.out.println( "------------------------------------------------------------"); } } The before( ) advice can access the identifiers declared on the captureCallParame-ters(int,String) pointcut by including the value and name identifiers in its signature and then binding those identifiers to the captureCallParameters(int,String) pointcut. See Also
The call(Signature) pointcut is described in Recipe 4.1; Recipe also Recipe 4.1 shows some of the wildcard variations that can be used in a Signature; Recipe 11.3 discusses the args([Types | Identifiers]) pointcut; combining pointcut logic using a logical AND (&&) is shown in Recipe 12.2; the before() form of advice is shown in Recipe 13.3; the calling context that is available to advice is covered in Chapter 13. |