pointcut all method calls on an object that implements an interface [message #61319] |
Wed, 25 January 2006 10:07  |
Eclipse User |
|
|
|
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.....
|
|
|
|
|
Re: pointcut all method calls on an object that implements an interface [message #591990 is a reply to message #61319] |
Fri, 27 January 2006 09:37  |
Eclipse User |
|
|
|
To match *calls* to a method, you need to use the call pointcut
designator rather than execution. The "+" qualifier on a type pattern
means "or any subtype thereof". So the type pattern:
TheInterface+
will match TheInterface or any type that is a subtype of (implements)
TheInterface.
Therefore the following pointcut will do what you need:
pointcut callToTheInterfaceOrAnyImplementorThereof() :
call(* TheInterface+.*(..));
Regards, Adrian.
Tom Coupland wrote:
> 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.....
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.05761 seconds