Custom annotation processor not called by tomcat

Custom annotation processor is not called by tomcat. Below is the code of the Annotation processor I am using:

@SuppressWarnings("restriction")
@SupportedAnnotationTypes("io.strati.rs.cxf2.bindings.MyAnnotation")
@SupportedSourceVersion( SourceVersion.RELEASE_8 )
public class SimpleAnnotationProcessor extends AbstractProcessor {

    public static List<String> str = new ArrayList<>();

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

        System.out.println("Into annotation processor ... 2");

        for ( Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
            System.out.println("Method name is:"+element.getSimpleName());
            str.add(element.getSimpleName().toString());
        }   
        return false;
    }
}

      

The method name of all methods that have a custom annotation is stored here. This is what the annotation class looks like:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

      

I am trying to access a list box in tomcat application like this:

@GET
@Path("/dummy")
@MyAnnotation
@Produces({APPLICATION_JSON, APPLICATION_XML, TEXT_XML})
public void print(){
   System.out.println("List is:"+SimpleAnnotationProcessor.str);
}

      

The list is printed as empty, although the method is annotated. I have specified the annotation in the maven compiler plugin and also specified it in META-INF / services / javax.annotation.processing.Processor. Can anyone tell me what are the possible reasons why the annotation processor is not being called?

+3


source to share


1 answer


I doubt Tomcat has anything to do with this. Annotation processing is done at compile time and is often used to generate or modify code. An annotation processor can be thought of as a compiler plugin.

https://www.javacodegeeks.com/2015/09/java-annotation-processors.html

By following the retention policy, the annotation will be stored by the Java compiler in the class file during the compilation phase, but it will not (and should not) be available at runtime.



Probably the annotation processor actually adds the name of the print () method to the list (check the assembly output), but again this only happens when the code is compiled.

The deployed web service at runtime will never see the list filled with the processor at compile time, they are completely different environments.

+1


source







All Articles