THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

An annotation type is a special kind of interface, designated by the @ character preceding the interface
keyword.[1] Annotations are applied to program elements, such as a class or field declaration. What appear to
be methods in the annotation type are actually special annotation element declarations. An annotation element
is rather like a field, and has a value for each program element that has that type of annotation. So a
ClassInfo annotation contains four string elements and one int element. When you annotate (apply an
annotation to) a program element, you supply values for each of the annotation elements. Annotations are
modifiers and can appear anywhere a modifier is allowed. Here's how you would use ClassInfo with class
Foo:


[1] The @ character was chosen because it is pronounced "at": A-T, short for Annotation Type.

@ClassInfo (
created = "Jan 31 2005",
createdBy = "James Gosling",
lastModified = "Feb 9 2005",
lastModifiedBy = "Ken Arnold",
revision = 3
)
public class Foo {
// ...
}


The annotation is introduced using the @ character again, followed by the name of the annotation type. Values
for the annotation elements are provided by a comma-separated list of name=value statements within
parentheses. As you can see, an annotation can contain a lot of textual information that can easily obscure the
actual program, so it is strongly recommended that you follow a consistent practice of always specifying
annotations first, ahead of any other modifiers, and always on a separate line.


To use the ClassInfo annotation, a programmer should update the value for each changed element
whenever they edit the source for Foo. Such updates could be automated through development tools that
understand annotations.


15.2. Annotation Types


Annotation types are a special kind of interface, declared, as you have seen, with the interface keyword
preceded by the @ character. Annotation types can be declared anywhere an interface can be declaredthat is, as
a top-level annotation type or nested within another typeand can have the same modifiers applied as
interfaces. Characterizing annotation types as interfaces is a little misleading, however, as aside from
borrowing some syntax and some associated usage rules, annotation types bear little resemblance to interfaces
in normal use.[2]


[2] Except when accessed reflectively at runtimesee "Annotation Queries" on page 414.

The methods declared in an annotation type are known as the elements of the annotation type. These elements
are constrained by strict rules:


The type of the element can only be a primitive type, String, an enum type, another annotation
type, Class (or a specific generic invocation of Class), or an array of one of the preceding types.



  • The element cannot declare any parameters.

  • The element cannot have a throws clause.

  • The element cannot define a type parameter (that is, it can't be a generic method).

Free download pdf