Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom AbstractProcessor and integrating with Eclipse

Tags:

I'm trying to create a new annotation with which I'll do some runtime wiring, but, for a number of reasons, I'd like to verify at compile time that my wiring will be successful with some rudimentary checks.

Suppose I create a new annotation:

@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation{ } 

Now I want to do some kind of validation at compile time, like check the field that CustomAnnotation annotates is of a particular type: ParticularType. I'm working in Java 6, so I created an AbstractProcessor:

@SupportedAnnotationTypes("com.example.CustomAnnotation") public class CompileTimeAnnotationProcessor extends AbstractProcessor {      @Override     public boolean process(Set<? extends TypeElement> annotations,                             RoundEnvironment roundEnv) {         Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);         for(Element e : elements){             if(!e.getClass().equals(ParticularType.class)){                 processingEnv.getMessager().printMessage(Kind.ERROR,                      "@CustomAnnotation annotated fields must be of type ParticularType");             }         }         return true;     }  } 

Then, based on some instructions I found, I created a folder META-INF/services and created a file javax.annotation.processing.Processor with contents:

 com.example.CompileTimeAnnotationProcessor 

Then, I exported the project as a jar.

In another project, I built a simple test class:

public class TestClass {     @CustomAnnotation     private String bar; // not `ParticularType` } 

I configured the Eclipse project properties as follows:

  • Set Java Compiler -> Annotation Processing: "Enable annotation processing" and "Enable processing in editor"
  • Set Java Compiler -> Annotation Processing -> Factory Path to include my exported jar and checked under advanced that my fully qualified class shows up.

I clicked "apply" and Eclipse prompts to rebuild the project, I hit okay -- but no error is thrown, despite having the annotation processor.

Where did I go wrong?


I ran this using javac as

javac -classpath "..\bin;path\to\tools.jar" -processorpath ..\bin -processor com.example.CompileTimeAnnotationProcessor com\test\TestClass.java 

with output

@CustomAnnotation annotated fields must be of type ParticularType

like image 784
Mark Elliot Avatar asked Jul 13 '11 23:07

Mark Elliot


People also ask

What is Eclipse annotation processing?

The JDT-APT project provides plugins that add Java 5 annotation processing support to Eclipse. A Java annotation processor is a compiler plug-in that can gather information about source code as it is being compiled, generate additional Java types or other resource files, and post warnings and errors.

How does annotation processing work?

The annotation processing is done in multiple rounds. Each round starts with the compiler searching for the annotations in the source files and choosing the annotation processors suited for these annotations. Each annotation processor, in turn, is called on the corresponding sources.

How do I enable annotation processing in Intellij?

Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Compiler | Annotation Processors.


1 Answers

To have errors show up in the editor, the Element causing the error needs to be tagged in the printMessage function. For the example above, this means that the compile time check should use:

processingEnv.getMessager().printMessage(Kind.ERROR,     "@CustomAnnotation annotated fields must be of type ParticularType",     e); // note we explicitly pass the element "e" as the location of the error 
like image 141
Mark Elliot Avatar answered Sep 30 '22 12:09

Mark Elliot