dimanche 10 juillet 2022

How do you get Java reflections to look for multiple levels of annotation

I'm trying to get multiple levels of annotation in one go. I find it kind of hard to explain in words, so let's consider the following example.

@Controller
public @interface HttpController {
}

@Controller
public @interface ConsoleController {
}

public @interface Controller{
}

If I have the above annotations, I would like to get every class annotated with any of these with only needing to do reflections.getTypesAnnotatedWith(Controller.class);

I've tried reflections.getTypesAnnotatedWith(Controller.class, true); and I know that I could just search for each of them separately, but I'd love to just be able to add a new Controller annotation with only needing to annotate that annotation with @Controller.

I have currently built my Reflection like this:

new Reflections(
        new ConfigurationBuilder()
              .setUrls(
                      ClasspathHelper.forPackage(
                      rootClass.getPackageName()))
              .setScanners(new SubTypesScanner(false),
                      new TypeAnnotationsScanner())
              .filterInputsBy(it ->
                      it.startsWith(rootClass.getPackageName())));




Aucun commentaire:

Enregistrer un commentaire