Search code examples
javaannotationsannotation-processing

Practical examples of generating documentation/XML with Java's annotation processors


One of the often mentioned advantages of annotations is the ability to generate documentation, XML, config files, and more with compile/deployment-time annotation processors. I've seen several examples of annotation processors, but not about automatically generating docs or XMLs with them. I can see why that'd be useful, e.g. adding valuable information to a project's documentation from the annotations.

One way to include them in the documentation is to use the @Documented meta-annotation on annotation types to include them in the JavaDoc, but it merely copies the annotation with its values as it is, without changing, formatting or generating anything.

So, my question is: How can processors be used to generate documentation, config, XML files for a project, and what are some practical examples of this?


Solution

  • One annotation processor that generates documentation is for plume-lib's Options annotation.

    If a program uses the @Option annotation, its Javadoc contains a description of its command-line options. Here are two examples.

    In addition, the program's command-line options are automatically parsed, and a usage message is automatically generated. Thus, you only have to write the documentation once; the source code, manual, and usage messages always stay in sync.