An annotation processor is a plug-in for the Java compiler. An annotation processor can do such things as analyze declarations, cause compilation errors and generate new compilation units.
An annotation processor is a plug-in for the Java compiler.
An annotation processor is an instance of javax.annotation.processing.Processor
whose process
method is invoked during compilation.
Once invoked, the processor is then able to do such things as:
- Analyze parts of the abstract syntax tree, primarily declarations via
javax.lang.model.element
and types viajavax.lang.model.type
. - Cause compilation errors and warnings (similar to how the
@Override
annotation works) via theMessager
. - Generate new compilation units (i.e. source code files) via the
Filer
which are subsequently compiled.
Notes on the current limitations of annotation processors
- Annotation processors may only generate new compilation units, not modify existing compilation units. Some annotation processors, such as Project Lombok, achieve the latter via undocumented internal Javac API.
- The existing API in Java Platform SE is limited to only analyzing declarations, such as class declarations and method declarations. The Compiler Tree API exists, which allows one to analyze the full abstract syntax tree, but it's largely undocumented and not yet a part of SE.
Resources