Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Re: Accessing fields

Hi Manuel,

Use the following aspect:

aspect GetSetTest {
    before(Object field) : set(* *.*) 
                           && args(field) && !within(GetSetTest) {
	System.out.println("Set: " + field);
    }

    after() returning(Object field) : get(* *.*) 
                                      && !within(GetSetTest) {
	System.out.println("Get:" + field);
    }
}

The key to remember is get()/set() pointcuts behave much 
like call/execution pointcut on equivalent getter/setter methods. 
Notice how "after returning" is used to collect the 
return value when using get() pointcut (exactly as you
would for pointcuts capturing getter methods).

-Ramnivas 

Ramnivas Laddad, 
Author, AspectJ in Action 
http://www.manning.com/laddad
http://www.amazon.com/exec/obidos/ASIN/1930110936

Check out my aspect-oriented refactoring articles:
http://tinyurl.com/yqm96
http://tinyurl.com/288nn

--- Manuel Menezes de Sequeira <Manuel.Sequeira@xxxxxxxx> wrote:
> Hi Miguel,
> 
> Thanks for your answer.  My problem is, I think, a bit more 
> complicated.  I want to capture all accesses to all instance fields
> of a 
> class or set of classes.  Hence, within the around advice, I do not
> know:
> a) What is the name of the field I'm accessing.
> b) What is the possible set of names, since the set of classes to
> which 
> I'm applying the Aspect is open.
> 
> I can, of course, obtain the field name using thisJointPoint (e.g., 
> thisJoinPointStaticPart.getSignature().getName()).  But accessing the
> 
> field itself is harder, if at all possible.  I tried using Java 
> reflection, but it (wisely) prohibits access to non-public fields. 
> The 
> ideal would be to have some way of exposing the field, just as
> target() 
> exposes the object which contains the field.  Is there any obvious 
> solution to this problem that I'm missing?
> 
> Thanks,
> 
> Manuel
> 
> mmonteiro@xxxxxxxxxxxxxxxxx wrote:
> 
> > Hi Manuel,
> > You can access the field by capturing the object that holds it.
> > Taka look to the following example, which I made while
> experimenting 
> > with the pointcut protocol:
> >
> > README.txt:
> > ---------------------------
> > This experiment ilustrates how both caller and callee objects can
> > be captured from a set() pointcut, as well as both old and new
> > values from the assignement.
> > The example includes (in class Callee) both a primitive field and
> > an object field.
> > Run Caller
> >
>
------------------------------------------------------------------------
> > Caller.java:
> > ---------------------------
> > /*
> > * Created on 19/Ago/2003
> > */
> > package capture_set;
> > /**
> > * @author Miguel Pessoa Monteiro
> > **/
> > public class Caller {
> >     private Callee myCallee;
> >     public int myField = 19;
> >     public Caller() {
> >         myCallee = new Callee("Bilbo", 5);
> >     }
> >     public void setName(String newstr) {
> >         myCallee.name = newstr;
> >     }
> >     public void setValue(int newval) {
> >         myCallee.value = newval;
> >     }
> >     public static void main(String[] args) {
> >         Caller caller = new Caller();
> >         caller.setValue(11);
> >         caller.setName("Frodo");
> >     }
> > }
> >
>
------------------------------------------------------------------------
> > Callee.java:
> > ---------------------------
> > /*
> > * Created on 19/Ago/2003
> > */
> > package capture_set;
> > /**
> > * @author Miguel Pessoa Monteiro
> > **/
> > public class Callee {
> >     protected String name;
> >     protected int value;
> >     public Callee(String s, int i) {
> >         name = s;
> >         value = i;
> >     }
> > }
> >
>
------------------------------------------------------------------------
> > ---------------------------
> > /*
> > * Created on 19/Ago/2003
> > */
> > package capture_set;
> > /**
> > * @author Miguel Pessoa Monteiro
> > **/
> > public aspect GetContext1 {
> >     pointcut setStr(String str, Callee obj):
> >         set(String Callee.name)
> >         && withincode(public void Caller.setName(..))
> >         && target(obj)
> >         && args(str);
> >     before(String str, Callee obj): setStr(str, obj) {
> >         System.out.print("Old value = \"" + obj.name +"\", ");
> >         System.out.println("new value = \"" + str +"\"");
> >     }
> >     pointcut setInt(int val, Callee obj, Caller caller):
> >         set(int Callee.value)
> >         && withincode(public void Caller.setValue(..))
> >         && target(obj)
> >         && this(caller)
> >         && args(val);
> >     before(int val, Callee obj, Caller caller):
> >             setInt(val, obj, caller) {
> >         System.out.print("Old value = " + obj.value +", ");
> >         System.out.print("new value = " + val);
> >         System.out.println(" and caller has " + caller.myField);
> >     }
> > /*
> >     //Reflective implementation:
> >     pointcut reflectiveGetValue(): set(public int field2);
> >     before(): reflectiveGetValue() {
> >         Object[] arguments = thisJoinPoint.getArgs();
> >         System.out.println("thisJoinPoint =\""+thisJoinPoint+"\"");
> >         System.out.println("arguments.length = " +
> arguments.length);
> >         Integer valObj = (Integer)arguments[0];
> >         int value = valObj.intValue();
> >         System.out.println("New value = " + value);
> >     }
> > */
> > }
> >
>
------------------------------------------------------------------------
> >
> > ----Original message:-----
> > Date: Fri, 23 Jan 2004 19:19:52 +0000
> > From: Manuel Menezes de Sequeira <Manuel.Sequeira@xxxxxxxx>
> > Organization: ISCTE
> > To: aspectj-users@xxxxxxxxxxx
> > Subject: [aspectj-users] Accessing fields
> > Reply-To: aspectj-users@xxxxxxxxxxx
> > I'm using an around advice for set and get pointcuts.  Can I have
> access
> > to field being set or got?
> >
> > Miguel J. T. Pessoa Monteiro
> > Ph.D. student Minho University,Portugal
> > eMail: mmonteiro@xxxxxxxxxxxx
> > URL:http://gec.di.uminho.pt/mpm/
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > http://dev.eclipse.org/mailman/listinfo/aspectj-users
> >
> >
> 
> -- 
> Manuel Menezes de Sequeira                  Manuel.Sequeira@xxxxxxxx
> Tel: +351(21)7903909                         Mobile: +351(96)2337428
> Fax: +351(21)7903099                 URL: http://home.iscte.pt/~mms/
> Yahoo: Manuel_Menezes_de_Sequeira  ICQ: 2313850  MSN/AIM: MMSequeira
> Gab. D6.18   ISCTE   Av. Forças Armadas   1649-026 LISBOA   Portugal
> 
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/


Back to the top