2

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 secureMethod();
}

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.

Community
  • 1
  • 1
soc
  • 27,983
  • 20
  • 111
  • 215
  • Strictly speaking, the question duplicates [@AspectJ pointcut for methods that override an interface method with an annotation](http://stackoverflow.com/questions/7178782/aspectj-pointcut-for-methods-that-override-an-interface-method-with-an-annotatio) – alehro Sep 12 '11 at 07:50

2 Answers2

1

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).

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • I just tried it with `Inherited`, but that doesn't seem to work :-/ – soc Sep 11 '11 at 17:59
  • Check out http://stackoverflow.com/questions/2847640/spring-aop-pointcut-that-matches-annotation-on-interface – Shivan Dragon Sep 11 '11 at 18:07
  • Interesting, but doesn't help me in my situation, because I don't actually know the classes in my aspect. – soc Sep 11 '11 at 19:11
  • 1
    `@Inherited` doesn't work here, because it is restricted to classes (not interfaces) and only works for class-level targets and not method/parameter targets. – soc Sep 11 '11 at 19:14
  • `AnnotationUtils.findAnnotation` was able to find the parent method's annotation for me.https://stackoverflow.com/questions/23994338/how-to-get-annotations-of-interface-or-abstract-class-methods-in-java – louis1204 Aug 15 '22 at 23:59
0
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.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • The actual idea is that I would add an annotation to the interface to force all implementing classes to go through the security checks. The fact that it works for class-level annotations made me hope that it would also work for methods. I could imagine that using a class-level annotation on the superclass and then going through all the methods in the superclass and checking those for `@Secure` annotations and then calling the implementation from the subclass could work. – soc Sep 11 '11 at 21:18