The problem here is not AspectJ, but the JVM. In Java annotations on
- interfaces
- methods or
- other annotations
never inherited
- implementing classes
- overriding methods or
- classes using annotated annotations.
Inheritance of annotations only works from classes to subclasses, but only if the type of annotation used in the superclass carries the @Inherited @Inherited , see the JavaDoc JDK .
AspectJ is a JVM language and therefore works within the limits of the JVM. There is no general solution to this problem, but for specific interfaces or methods for which you want to emulate annotation inheritance, you can use a workaround similar to the following:
package de.scrum_master.aspect; import de.scrum_master.app.Marker; import de.scrum_master.app.MyInterface; public aspect MarkerAnnotationInheritor {
Note: With this aspect, you can remove (literal) annotations from the interface and from the annotated method, because the AspectJ ITD mechanics (defining between types) add them back to the interface plus to all the implementation / redefinition of classes / methods.
Now, the console log when starting Application says:
execution(de.scrum_master.app.Application()) execution(void de.scrum_master.app.Application.two())
By the way, you can also embed an aspect directly into the interface so that everything is in one place. Just be careful, rename MyInterface.java to MyInterface.aj to help the AspectJ compiler recognize that it needs to do some work here.
package de.scrum_master.app; public interface MyInterface { void one(); void two(); public static aspect MarkerAnnotationInheritor {
source share