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

Yes it does matter. Execution join points wiil not work because at the
time of the execution of the method you are in the method
implementation (of your implementation class), and at this point no
annotation infomration is available because the annotations on the
method are not inherited.

So, if only execution joint point is available to you (for example if
you only want to use sprin'ts aspect support) then your only option is
to weave annotations into your implementation methods. However, this
will require you either to compile time weave such aspects or use Load
time weaving which requires -javaagent option to be specified to the
JVM. Option 1 will require modifying your build process and probably
the way your work with your IDE; and option 2 would require a change
to the parameters to the JVM, which may be an issue if your are using
some application servers that spawn JVMs for every .ear deployed.

Monal Daxini

On 9/27/06, David Hatton (AT/LMI) <david.hatton@xxxxxxxxxxxx> wrote:
Thanks Monal,

This may help, but remember I cannot use a call join point and must use
execution join points.

Will this matter??

/David



-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Monal Daxini
Sent: 27 September 2006 17:30
To: aspectj-users@xxxxxxxxxxx
Subject: 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
> >
> >
> >
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top