|
Chapter 13. Annotations
Annotations are tags that you insert into your source code for processing by tools. The tags can be processed at the source level, or the compiler can include them in class files. Annotations do not change the way in which your programs are compiled. (The JDK 5.0 compiler uses a couple of annotations to generate or suppress warnings, but it generates the same class files with or without the annotations. These annotations are simple and somewhat atypical since third parties can't add new annotations to the compiler.) In order to benefit from annotations, you need to select a processing tool, insert annotations into your code that your processing tool understands, and then apply the processing tool. As this chapter is written, no industrial-strength processing tools exist. Thus, we try to give you a flavor of what tools might emerge, and what capabilities you can expect of them. There is a wide range of possible uses for annotations, and that generality can be initially confusing. Here are some possible uses for annotations:
In this chapter, we start out with the basic concepts and put them to use in a concrete example: We mark methods as event listeners for AWT components, and show you an annotation processor that analyzes the annotations and hooks up the listeners. We then discuss the syntax rules in detail. We finish the chapter with two advanced examples for annotation processing. One of them uses apt, an annotation processing tool that is part of the JDK. The other uses the Apache Bytecode Engineering Library to inject additional bytecodes into annotated methods. |
|