Java Reference
In-Depth Information
has really overridden the method if it comes across a @Override annotation for a method. Annotations are also
available at runtime so that a program can read and use it for any purpose it wants. For example, a tool can read
annotations and generate boilerplate code. If you have worked with Enterprise JavaBeans (EJB), you know the pain of
keeping all the interfaces and classes in sync and adding entries to XML configuration files. EJB 3.0 uses annotations
to generate the boilerplate code, which makes EJB development painless for programmers. Another example of an
annotation being used in a framework/tool is JUnit version 4.0. JUnit is a unit test framework for Java programs. It
uses annotations to mark methods that are test cases. Before that, you had to follow a naming convention for the test
case methods. Annotations have a variety of uses, which are documentation, verification, and enforcement by the
compiler, the runtime validation, code generation by frameworks/tools, etc.
To make an annotation available to the compiler and the runtime, an annotation has to follow rules. In fact, an
annotation is another type like a class and an interface. As you have to declare a class type or an interface type before
you can use it, you must also declare an annotation type.
An annotation does not change the semantics (or meaning) of the program element that it annotates. In that
sense, an annotation is like a comment, which does not affect the way the annotated program element works. For
example, the @Override annotation for the setSalary() method did not change the way the method works. You
(or a tool/framework) can change the behavior of a program based on an annotation. In such cases, you make use of
the annotation rather than the annotation doing anything on its own. The point is that an annotation by itself is
always passive.
Declaring an Annotation Type
Declaring an annotation type is similar to declaring an interface type, except for some restrictions. According to Java
specification, an annotation type declaration is a special kind of interface type declaration. You use the interface
keyword, which is preceded by the @ sign (at sign) to declare an annotation type. The following is the general syntax
for declaring an annotation type:
<modifiers> @ interface <annotation-type-name> {
// Annotation type body goes here
}
The <modifiers> for an annotation declaration is the same as for an interface declaration. For example, you can
declare an annotation type as public or package level. The @ sign and the interface keyword may be separated by
whitespaces or they can be placed together. By convention, they are placed together as @interface . The interface
keyword is followed by an annotation type name. It should be a valid Java identifier. The annotation type body is
placed within braces.
Suppose you want to annotate your program elements with the version information, so you can prepare a
report about new program elements added in a specific release of your product. To use a custom annotation type (as
opposed to built-in annotation, such as @Override ), you must declare it first. You want to include the major and the
minor versions of the release in the version information. Listing 1-1 has the complete code for your first annotation
declaration.
Listing 1-1. The Declaration of an Annotation Type Named Version
// Version.java
package com.jdojo.annotation;
public @interface Version {
int major();
int minor();
}
 
Search WWH ::




Custom Search