Java Annotation Processors

Plugging into the Java Compiler

Presentation slides Pdf


Java Compiler commandline options for annotation processing

 javac <options> <source files>
  -verbose                   Output messages about what the compiler is doing
  -proc:{none,only}          Control whether annotation processing and/or compilation is done
  -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -XprintRounds              Print information about rounds of annotation processing
  -XprintProcessorInfo       Print information about which annotations a processor i asked to process

The interface for an annotation processor is provided from javax.annotation.processing.Processor. The java compiler will discover annotation processor implementations on the classpath using the java.util.ServiceLoader mechanism if provided - See Auto Service.

The annotation processing is performed in one or more rounds until all input files are consumed that contain matching annotations, since newly generated files may contain annotations.

e.g


Annotation Processing wraps the Java Compiler and compilation errors may cause the javac process to exit with a non-zero value failing any dependent build.

When multiple annotation processors are invoked any duplicate source/class files that may have be aggregated will cause the Java Filer process to fail. e.g When two anotation processors output code for the same annotation.