This post is the second part in my series about Code Generation using Annotation Processors in the Java language. In part 1 (read it here) we introduced what Annotations are in the Java language and some of their common uses.
Now in part 2 we are going to introduce Annotation Processors, how to build them and how to run them.
Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors
Annotations are great, sure. You can set any kind of metadata or configuration with them, with a well defined syntax and different types to use.
From what we have seen until now, annotations have advantages compared with Javadocs but not enough to justify their inclusion into the language. Therefore, is it possible to interact with annotations and get the most from them? Sure it is:
- At runtime, annotations with runtime retention policy are accessible through reflection. The methods getAnnotation() and getAnnotations() in Class class will do the magic (1).
- At compile time, Annotation Processors, a specialized type of classes, will handle the different annotations found in code being compiled.