Chapter13.Annotations


Chapter 13. Annotations

  • Addition of Metadata to Programs

  • An Example: Annotating Event Handlers

  • Annotation Syntax

  • Standard Annotations

  • The apt Tool for Source-Level Annotation Processing

  • Bytecode Engineering

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:

  • Automatic generation of auxiliary files, such as deployment descriptors or bean information classes.

  • Automatic generation of code for testing, logging, transaction semantics, and so on.

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.



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net