开发者

Pointcut matching methods which have been annotated directly or in an inherited interface?

Consider this @PointCut which gets triggered if a method is annotated with an @Secure annotation:

@Pointcut("execution(@Secure * *(..)) && @annotation(secure)")
public void accessOperation(final Access access) { }

This works perfectly well for methods like:

class Foo {
    @Secure
    public void secureMethod() { }
}

But is it possible to have a Pointcut which also gets triggered when the annotation only exists in a superclass/interface like this?

interface Foo {

    @Secure
    public void secureMetho开发者_C百科d();
}

class SubFoo implements Foo {

    @Override
    public void secureMethod() {  // <--- execution of this method should be caught
        /* .... */
    }
}

EDIT:

This seems to be very closely related to: @AspectJ pointcut for subclasses of a class with an annotation

The only difference is that they use a class-level annotation, whereas I need a method-level annotation.


I don't know how AspectJ deals with annotations in such a scenario, but if he only checks the implementing class for a certain annotation, and that annotation is only found on the interface that the class implements, Java will report that infact that annoation is not present on the class method. You should annotate your annotation with @Inherited:

http://download.oracle.com/javase/6/docs/api/java/lang/annotation/Inherited.html

maybe this will do the trick (though in this case you should make sure that your Advice is not called multiple times).


I don't actually know the classes in my aspect 

Considering what you are saying and the fact that @Inherited cannot be used on anything else but classes, you are implicitly hoping that AspectJ will do the job of finding out whether a method (or its overriden implementations and declarations) is annotated. This is much more than what is announced by AspectJ.

If your final objective is to secure some code, then all methods which should be secured should be properly annotated in the first place. So, the answer to your question is no, this is not possible in this context, especially if you know nothing about the classes.

However, there might be a workaround if you have access to an annotation processor. In this case, you could pick all @Secure annotation and work your way through the code with reflection to generate AspectJ code at compile time that would captures all method instances properly. Not easy, but possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜