6

Need help with annotation processor . I have created a simple annotation processor which uses @autoservice annotation that checks whether the field which is annotated is final. But it is not showing any compile time errors. This is my configuration

Annotation:

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface Store {

    int temp() default 0;
}

Annotation Processor:

@SupportedAnnotationTypes("com.self.Store")
@AutoService(Processor.class)
public class Process extends AbstractProcessor {

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

        for (Element element : roundEnv.getElementsAnnotatedWith(Store.class)) {

            TypeElement typeElement = (TypeElement) element;

            for (Element element2 : typeElement.getEnclosedElements()) {

                VariableElement variableElement = (VariableElement) element2;

                if (!variableElement.getModifiers().contains(Modifier.FINAL)) {

                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "it should be final");
                }

            }

        }

        return true;
    }

}

pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>annotations</groupId>
  <artifactId>annotations</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>annotation</name>
  <dependencies>
 <dependency>
      <groupId>com.google.auto.service</groupId>
      <artifactId>auto-service</artifactId>
      <version>1.0-rc2</version>
      <optional>true</optional>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
         </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Testing file:

public class Test {

     @Store
     public int id;
}
amt14779
  • 125
  • 1
  • 1
  • 5

2 Answers2

13

According to the Baeldung's topic on annotation processor development, you must configure your maven compiler plugin first and declare the auto-service annotation processor there. That is the missing step. FYI, if you are using Gradle you may declare it under dependencies closure:

dependencies {
    annotationProcessor 'com.google.auto.service:auto-service:1.0-rc5'
    compileOnly 'com.google.auto.service:auto-service:1.0-rc5'
}
Mohsents
  • 691
  • 11
  • 9
2

It looks like you are missing a step. Running Maven build of this project will invoke the Google AutoService annotation processor, create a registration file for your custom processor, and build a .jar with it. In order for your processor to work, that .jar must be included as a dependency before compiling the project that contains Test. Otherwise the registration file that must be picked up by Java ServiceLoader is generated during compilation and obviously not included in the compiler's classpath.

vempo
  • 3,093
  • 1
  • 14
  • 16
  • Your post is very strange. Missing a step is one thing, the problem of circular dependency is totally another one. What are you talking about? – Gangnus Apr 25 '19 at 13:07
  • 1
    @Gangnus, circular dependency? Why? IMO this is not a case of X depends on Y while Y depends on X (even with intermediaries). It's just about trying to run in one step something that should be done in two. I'm assuming some familiarity with Java annotation processing, Java SPI and Google's AutoService in order to understand my answer. You can't both generate an annotation processor and apply it in the same run. – vempo Apr 25 '19 at 17:37
  • Sorry, I still do not understand your post. "Otherwise the registration file that must be picked up by Java ServiceLoader is generated during compilation and obviously not included" - IMHO, it is about circular dependency. – Gangnus Apr 25 '19 at 19:29