Recipe13.1.Accessing Class Members


Recipe 13.1. Accessing Class Members

Problem

You want to access attributes and methods of a particular object from within your advice.

Solution

Pass the appropriate object to the advice as a parameter on your pointcut declaration.

Discussion

Example 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 attributes
public 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 Also

The 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.



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