Datasheet
Several types of annotations are defined in this package. These are listed in the following table. Each
of these annotations inherits from the
Annotation interface, which defines an equals method and a
toString method.
Annotation Class Name Description
Target Specifies to which program elements an annotation type is applicable.
Each program element can appear only once.
Documented Specifies annotations should be documented by javadoc or other
documentation tools. This can only be applied to annotations.
Inherited Inherits annotations from superclasses, but not interfaces. The
policy on this annotation is
RUNTIME, and it can be applied only to
annotations.
Retention Indicates how long annotations on this program element should be
available. See
RetentionPolicy discussed previously. The policy on
this annotation is
RUNTIME, and it can be applied only to annotations.
Deprecated Marks a program element as deprecated, telling developers they
should no longer use it. Retention policy is
SOURCE.
Overrides Indicates that a method is meant to override the method in a parent
class. If the override does not actually exist, the compiler will generate
an error message. This can be applied only to methods.
Two useful source level annotations come with JDK 5,
@deprecated and @overrides. The @deprecated
annotation is used to mark a method as deprecated— that is, it shouldn’t be used by client program-
mers. The compiler will issue a warning when encountering this annotation on a class method that a
programmer uses. The other annotation,
@overrides, is used to mark a method as overriding a method
in the parent class. The compiler will ensure that a method marked as
@overrides does indeed override
a method in the parent class. If the method in the child class doesn’t override the one in the parent class,
the compiler will issue an error alerting the programmer to the fact that the method signature does not
match the method in the parent class.
Developing a custom annotation isn’t difficult. Create a
CodeTag annotation that stores basic author and
modification date information, and also stores any bug fixes applied to that piece of code. The annota-
tion will be limited to classes and methods:
import java.lang.annotation.*;
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface CodeTag {
String authorName();
String lastModificationDate();
String bugFixes() default “”;
}
27
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 27