Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] pointcut to get outer class object

I'm afraid I don't have a pretty answer for you (I should but we don't have the syntax for it right now).  Or at least I can't think of a pretty answer right now..

For now you need to exploit what happens in the bytecode when you compile that kind of code.

The inner class has two characteristics when compiled like this:
- it has a new field called this$0 that maps to the outer class
- the constructor takes an additional argument that is the outer instance (which is then places in this$0).

We can grab the this$0:

before(): execution(boolean B+.someFunc()) {
  Object t = thisJoinPoint.getTarget();
  try {
    Object outerInstance = t.getClass().getDeclaredField("this$0").get(t);
  } catch (Exception e) {}
}

We can grab it on the way into the constructor and remember it:

before(A a,B b): execution(B+.new(..)) && args(a) && this(b) { // this pointcut will vary if your inner takes params on the ctor
  mymap.put(b,a);
}

before(): execution(boolean B+.someFunc()) {
  Object t = thisJoinPoint.getTarget();
  Object outerInstance = mymap.get(t);
}

Neither are ideal, I'll agree...

cheers
Andy


On 22 January 2013 18:05, bob doe <dumpforjunk@xxxxxxxxx> wrote:
I have the following class:

package Test;
public class A
{
    private B b = new B()
    {
       @Override
       public boolean someFunc() {return false;}
    }
 }
What is the AspectJ pointcut to capture execution of someFunc, and at
the same time get a reference to outer class A?

I tried:

pointcut captureExec(): within(Test.A) && execution(boolean Test.B+.someFunc());
before(): captureExec()
{
    //here thisJoinPount.getTarget() returns object to class B,
    //but I need reference object to the outer class A
}

Thanks
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top