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

Hi Ramnivas,

Ramnivas Laddad wrote:

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

A possible solution to my problem, having access to fields being set or got within around advice is to force these fields to be public, so that the around advice can access them using Java reflection, but to prevent them to be used publicly using an error declaration. It is not pretty, but it works (though you can now break encapsulation using Java reflection).