I have an @MyAnnotation annotation and I can annotate any type (class) with it. Then I have a class called AnnotatedClassRegister , and I would like it to register all classes annotated with @MyAnnotation , so I can access them later. And I would like to register these classes automatically after creating AnnotatedClassRegister , if possible, and, most importantly, before creating annotated classes.
I have AspectJ and Guice at my disposal. The only solution I have encountered so far is to use Guice to insert a single-screen instance of AnnotatedClassRegister into an aspect that searches for all classes annotated with @MyAnnotation , and it adds the code needed to register such a class in its constructor, The disadvantage of this The solution is that I need to instantiate each annotated class so that the code added by AOP actually runs, so I cannot use the lazy instantiation of these classes.
A simplified pseudo-code example of my solution:
What approach should be used to solve this problem? Is there an easy way to get all such annotated classes in the class path through reflection so that I don't need to use AOP at all? Or any other solution?
Any ideas are greatly appreciated, thanks!
source share