Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Pointcuts for Annotated Interface Methods

However if you use

MyInterfaceImple1 mii1 = new MyInterfaceImpl1()
mii1.implementMe();

This code will not execute the advice because now the type of your
variable is not the interface and the annotation is defined only in
the method in the interface.

Monal

On 9/27/06, Monal Daxini <monaldax@xxxxxxxxx> wrote:
David,

You could do the following without having to add annotations to every
implemented methods in your implementors.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

public interface MyInterface {
    @MyAnnotation
    public void implementMe();
}

public class MyInterfaceImpl1() {
    public void implementMe() {
        System.out.println("I am implemented 1");
}

public class MyInterfaceImpl2() {
    public void implementMe() {
        System.out.println("I am implemented 2");
}

1  public class MyMain {
2      public static void main(String[] args) {
3           MyInterface mi1 = new MyInterfaceImpl1();
4           mi1.implementMe();
5           MyInterface mi2 = new MyInterfaceImpl2();
6           mi2.implementMe();
7      }
8  }

You could change the advice, but for illustrative purpose I am using
an after advice.

public aspect MyAspect {
    after() : call(@MyAnnotation * MyInterface+.testMe()) {
                System.out.println(" Advice - &&& Called MyInterface.implementMe()");
        }
}

Output is:
-------------
I am implemented 1
Advice - &&& Called MyInterface.implementMe()
I am implemented 2
Advice - &&& Called MyInterface.implementMe()

Conclusion:
As long as you have the type of the variable mi1 and mi2 to be of the
interface type (MyInterface) the after adivce will always get
executed. So, if you create a variable of the reference type, and
assign it to any implementation of the interface you can advice your
implemented methods withouth having to add annotations to all your
implemented methods.


Monal Daxini

On 9/27/06, David Hatton (AT/LMI) <david.hatton@xxxxxxxxxxxx> wrote:
>
>
>
> Hi,
>
> We would like to apply transactional control to certain public methods
> defined in our API … there are quite a lot of methods involved and a lot
> more implementations of these methods.
>
> I was hoping to put an annotation on the required methods and use an
> annotation based pointcut to select the methods which should have the
> relevant advice applied.
>
> BUT … annotations are not inherited from interfaces to their implementing
> classes so this will not work.
>
> Is there any obvious way to resolve this … I really don't want to go down
> the road of adding the Annotation to each and every implementing method.
>
> E.g. is there some AspectJ way to select an annotated method and
> implementers of that method??
>
> I tried using
>
>    pointcut tx():
>         @annotation(Transactional);
>
> But that only picks out calling join points, and we want to provide this on
> execution join points as the code which will be using our API is not
> available to us at build time.
>
> /David
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
>



Back to the top