Bug 239649 - Access to this or the aspect instance in an if()
Summary: Access to this or the aspect instance in an if()
Status: NEW
Alias: None
Product: AspectJ
Classification: Tools
Component: Runtime (show other bugs)
Version: 1.6.1   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-04 12:55 EDT by Marijn Meijles CLA
Modified: 2013-06-24 11:06 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marijn Meijles CLA 2008-07-04 12:55:16 EDT
The construction of thisJoinPoint is an expensive operation which should be avoided when possible. If the aspect is oinly needed a small percentage of the time depending on state in the this() instance, it would be great to have access to this() or the current aspect instance in an if().
Comment 1 Ramnivas Laddad CLA 2008-07-04 14:35:47 EDT
I am guessing this is more about optimizing if(), since you can already get access to either. Right?

For example, you may access this() using something like:

pointcut pc(MyClass mc) : execution(* *(..)) && this(mc) && if(mc.someCondition());

And the aspect instance using something like:
pointcut pc() : execution(* *(..)) && if(MyAspect.aspectOf().someCondition());
Comment 2 Marijn Meijles CLA 2008-07-05 07:30:16 EDT
Yes, this is about optimizing if()
Comment 3 Andrew Clement CLA 2011-08-12 14:23:15 EDT
Now implemented access to aspect instance with the special name 'thisAspectInstance' available in the if clause():

aspect X {
  boolean doit() {
    System.out.println("In instance check method doit()");
    return true;
  }

  before():execution(* m(..))  && if(thisAspectInstance.doit()){
    System.out.println(thisJoinPoint);
  }
}

It only works for singletons right now - I'll address others as use cases come up.

It guards the creation of the tjp object.  It works for subclassing too:

abstract aspect X {
abstract pointcut p();

boolean doit() {
    return true;
  }

  before():p()  && if(thisAspectInstance.doit()){
    System.out.println(thisJoinPoint);
  }
}

aspect Y extends X {

  pointcut p(): execution(* m(..));

}

When doit() runs the instance will be of type Y.
Comment 4 Andrew Clement CLA 2013-06-24 11:06:35 EDT
unsetting the target field which is currently set for something already released