Search code examples
javareflectionmaven-failsafe-plugin

Finding annotated methods in application code from maven integration test


I have a custom annotation in my program that I apply to methods. I would like to find all methods with a particular annotation. From my application code, I can use the Reflections package to do so:

new Reflections(
    new ConfigurationBuilder().setUrls(
        ClasspathHelper.forPackage("com.my.package")
    ).setScanners(new MethodAnnotationsScanner())
    ).getMethodsAnnotatedWith(MyAnnotation.class);

However, when I try to do the same from an integration test invoked via failsafe to find the annotated methods in the application (not in the test), no methods are found. How can I get my annotated methods from an integration test?


Solution

  • There were two things I had to change to get things to work:

    1. I was using version 0.9.9-RC1. I was careless enough to first discover Reflections at https://code.google.com/archive/p/reflections/, which lists 0.9.9-RC1 as the latest version, and did not notice the small print mentioning that the project had moved to GitHub. I assumed 0.9.9-RC1 was the latest version, which was not the case. The latest as of the time of writing was 0.9.11.
    2. I changed the Reflections instantiation as follows:

      new Reflections("com.my.package", new MethodAnnotationsScanner());