| [aspectj-users] Re: [NEWSDELIVER] pointcut all method calls on an object that implements an interface |
To:
undisclosed-recipients:
cc:
Subject:
[NEWSDELIVER]
pointcut all method calls on an object that implements an interface
Hi everybody,
Been toying with this idea for a day or so and hoping someone will be able
to come up with a better solution than i have!
I want to match on any method calls on an object that implements an
interface, not only the methods defined by that interface, but all other
method calls on the obj as well. Heres some class that should make it abit
clearer.
public class TheSubject implements TheInterface{
public void interfaceDeclared() {}
public void classDeclared(){}
public static void main(String[] args){
TheSubject
sub = new TheSubject();
System.out.println("Calling
subjects implementation of interface");
sub.interfaceDeclared();
System.out.println("\nCalling
subjects own method");
sub.classDeclared();
}
}
public interface TheInterface {
public void interfaceDeclared();
}
An now for the aspect
public aspect TheAspect {
pointcut interfaceMethod () : execution(void TheInterface.* ());
void around() : interfaceMethod(){
System.out.println("Interface
joinpoint triggered");
}
pointcut classMethod(): execution (void TheSubject.* ());
void around() : classMethod(){
System.out.println("Class
joinpoint triggered");
}
/*//This results in conditional advice around almost every method call
and does'nt match on the TheSubject calls....
pointcut anyMethod() : execution(void * ()) &&
if(implementsInter(TheInterface.class, thisJoinPoint.getThis()));
before() : anyMethod(){
System.out.println("Any
Method aspect triggered");
}
private static boolean implementsInter(Class inter, Object obj){
return
inter.isInstance(obj);
}*/
//This works but results in conditional advice around almost every method
called!
pointcut anyMethod() : target(TheInterface) && call(void * ( ..
));
before() : anyMethod(){
System.out.println("Any
Method joinpoint triggered");
}
/*//Does'nt work and results in many conditional advice markers
pointcut anyMethod() :execution(void * ( .. )) && this(TheInterface);
before() : anyMethod(){
System.out.println("Any
Method aspect triggered");
}*/
}
The output im looking for
Calling subjects implementation of interface
Any Method joinpoint triggered
Interface joinpoint triggered
Calling subjects own method
Any Method joinpoint triggered
Class joinpoint triggered
As you can see i have managed to make it work. However this results in
a
huge number of 'conditional' advice markers, which is something i'd rather
not have. Planning to use this idea in a medium sized project and
having
a conditional advice marker round every method is a little unacceptable
(Am i right in thinking that these conditionals with be tested at runtime
everytime a method is called?? thereby introducing abit of a slow dwn?).
So what im really looking for is a technique of matching all calls to on
object that implements a given interface at compile time.
Look forward to your ideas!
Tom
p.s. if anyones got the time i wld'nt mind hearing a little abit about
why
the other commented pointcuts don't work as well.....