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:
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
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.
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.
Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Compiler | Annotation Processors.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With