Recipe 13.1. Accessing Class MembersProblemYou want to access attributes and methods of a particular object from within your advice. SolutionPass the appropriate object to the advice as a parameter on your pointcut declaration. DiscussionExample 13-1 shows how to access public and private members of a class whose object is available as the call join point is encountered. Example 13-1. Passing an object to advice for access to its methods and attributespublic privileged aspect MemberAccessRecipe { /* Specifies calling advice whenever a method matching the following rules gets executed: Class Name: MyClass Method Name: foo Method Return Type: void Method Parameters: an int followed by a String */ pointcut executionOfFooPointCut( ) : execution( void MyClass.foo(int, String)); // Advice declaration after(MyClass myClass) : executionOfFooPointCut( ) && this(myClass) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "Accessing the set(float) member of the MyClass object"); System.out.println( "Privileged access not required for this method call as it is public"); myClass.setF(2.0f); System.out.println( "Using the privileged aspect access to the private f member variable"); System.out.print("The current value of f is: "); System.out.println(myClass.f); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } An object of the MyClass class is made available to the advice by using the this(Identifier) pointcut definition. The this(Identifier) pointcut definition effectively exposes the advice to the object that is pointed at by the this reference at the triggering join point. The setF(float) method is called from within the advice and shows access to the MyClass object's public methods. To gain access to the private MyClass.f attribute, the aspect has to have some additional changes made to its structure. The aspect is attempting to break encapsulation by accessing the private member directly, and, therefore, the aspect must be declared as privileged because it is committing a potentially intrusive act. AspectJ provides the privileged keyword to be used where an aspect requires full and unrestricted access to the classes it is applied to, including those member variables and methods that are not declared on the class's public interface. The privileged status of an aspect should serve as a warning that care must be taken in any changes to that aspect, or the classes to which it is applied, as these changes can potentially cause other problems throughout the application. See AlsoThe call(Signature) pointcut is described in Recipe 4.1; the within(TypePattern) pointcut is described in Recipe 9.1; the this(TypePattern or Identifier) pointcut is described in Recipe 11.1; the Appendix contains a quick reference for the JoinPoint class and its subclasses. |