Search code examples
javaaopaspectj

Emulate annnotation inheritance with @AspectJ code style


As you know with AspectJ 5 you can write aspects inside *.java files. I.e.: public aspect Aspects {} now can be written as following: @Aspect public class Aspects {}.

Is there any way to write the code below inside Java file? Code:

public aspect AnnotationInheritor { 
    declare @method : void ItemRepository+.getById(..) : @MyAnnotation;
}

P.S. I found @DeclareAnnotation that appears at version 1.5.3, but looks like it haven't been implemented yet.

@DeclareAnnotation("void ItemRepository+.getById(..)")
@MyAnnotation void inEmptyListMethod() {}

Solution

  • No, there is not way as of today. Even if there was, you would still need the AspectJ compiler or a load-time weaving agent in order to make it work. Where is the benefit in using clunky annotation syntax, putting everything inside a string? IMO the native syntax is much more clear and elegant. If you want to use AspectJ, learn its syntax. ;-)