[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ews.eclipse.technology.aspectj] Re: Inherited annotations, interfaces and execution() join points

It's a feature(?) of annotations in Java 5 that annotations on interfaces are never inherited by implementing types, even if those annotations are @Inheritable.

There's a brief discussion on this at
http://www.eclipse.org/aspectj/doc/next/adk15notebook/annotations.html#annotation-inheritance

Regards, Adrian.

Mario Scalas wrote:
I read in [1] that I could capture join points by using inherited
annotations. The example shown there uses class inheritance (and call()
join point) but I thought that the same idea could work for implementing
interfaces and with execution(). Though, it doesn't work and so before
surrendering I want to ask to the ng.

[My annotation]

@Target( ElementType.METHOD )
@Inherited
@Retention( RetentionPolicy.RUNTIME )
public @interface MyAnnot {
    String value() default "";
}

[Annotated interface]

public interface IMain {
    @MyAnnot( value = "bla" )
    void doSomething();
}

[Implementation (the implemented methods should inherit the annotation)]

public class Main implements IMain {
public static void main( String[] args ) {
IMain m = new Main();
m.doSomething(); // If I use the "call()" pointcut I capture this
}


    public void doSomething() {
        int c = 3 + 2;
        System.out.println( "c = " + c );
    }
}

[The aspect]

@Aspect
public class MyAspect {
    // If I use call() instead of execution() it works fine
    @Pointcut( "execution( @MyAnnot * *.*(..) ) && @annotation( a )" )
    protected void annotatedMethods( MyAnnot a ) {}

@Around( "annotatedMethods( a )" )
public void checkAnnotations( ProceedingJoinPoint jp, MyAnnot a ) throws Throwable {
if ("bla".equals( a.value() ))
System.out.println( "Bla!" );
jp.proceed();
}
}


So, am I triying to do something really stupid? Or I'm just implementing my
idea in the wrong way?

Thanks!
Mario


[1] http://www.eclipse.org/aspectj/doc/next/adk15notebook/annotations-pointcuts-and-advice.html