Notice that no parentheses follow str in this assignment. When an annotation member is
given a value, only its name is used. Thus, annotation members look like fields in this context.
Specifying a Retention Policy
Before exploring annotations further, it is necessary to discuss annotation retention policies.
A retention policy determines at what point an annotation is discarded. Java defines three
such policies, which are encapsulated within the java.lang.annotation.RetentionPolicy
enumeration. They are SOURCE, CLASS, and RUNTIME.
An annotation with a retention policy of SOURCE is retained only in the source file
and is discarded during compilation.
An annotation with a retention policy of CLASS is stored in the .class file during
compilation. However, it is not available through the JVM during run time.
An annotation with a retention policy of RUNTIME is stored in the .class file during
compilation and is available through the JVM during run time. Thus, RUNTIME retention
offers the greatest annotation persistence.
A retention policy is specified for an annotation by using one of Java's built-in annotations:
@Retention. Its general form is shown here:
@Retention(retention-policy)
Here, retention-policy must be one of the previously discussed enumeration constants. If no
retention policy is specified for an annotation, then the default policy of CLASS is used.
The following version of MyAnno uses @Retention to specify the RUNTIME retention
policy. Thus, MyAnno will be available to the JVM during program execution.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
String str();
int val();
}
Obtaining Annotations at Run Time by Use of Reflection
Although annotations are designed mostly for use by other development or deployment tools,
if they specify a retention policy of RUNTIME, then they can be queried at run time by any
Java program through the use of reflection. Reflection is the feature that enables information
about a class to be obtained at run time. The reflection API is contained in the java.lang.reflect
package. There are a number of ways to use reflection, and we won't examine them all here.
We will, however, walk through a few examples that apply to annotations.
The first step to using reflection is to obtain a Class object that represents the class
whose annotations you want to obtain. Class is one of Java's built-in classes and is defined
in java.lang. It is described in detail in Part II. There are various ways to obtain a Class
object. One of the easiest is to call getClass( ), which is a method defined by Object. Its
general form is shown here:
final Class getClass( )
It returns the Class object that represents the invoking object. (getClass( ) and several other
reflection-related methods make use of the generics feature. However, because generics are not
discussed until Chapter 14, these methods are shown and used here in their raw form. As a result,
you will see a warning message when you compile the following programs. In Chapter 14, you
will learn about generics in detail.)
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home