Java Reference
In-Depth Information
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD,
ElementType.PACKAGE, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Version {
int major();
int minor();
}
Suppose you annotate a Test class with your Version annotation type as follows:
package com.jdojo.annotation;
@Version(major=1, minor=0)
public class Test {
// Code for Test class goes here
}
When you generate documentation for the Test class using the Javadoc tool, the Version annotation on the
Test class declaration is also generated as part of the documentation. If you remove the Documented annotation from
the Version annotation type declaration, the Test class documentation would not contain information about its
Version annotation.
The Repeatable Annotation
Prior to Java 8, it was not allowed to repeat an annotation in the same context. For example, the following repeated use
of the Version annotation would generate a compile-time error:
@Version(major=1, minor=1)
@Version(major=1, minor=2)
public class Test {
// Code goes here
}
Java 8 added a Repeatable meta-annotation type. An annotation type declaration must be annotated with a
@Repeatable annotation if its repeated use is to be allowed. The Repeatable annotation type has only one element
named value whose type is a class type of another annotation type.
Creating a repeatable annotation type is a two-step process:
Declare an annotation type (say
T ) and annotate it with the Repeatable meta-annotation.
Specify the value for the annotation as another annotation that is known as containing
annotation for the repeatable annotation type being declared.
Declare the containing annotation type with one element that is an array of the repeatable
annotation.
Listing 1-14 and Listing 1-15 contain declarations for ChangeLog and ChangeLogs annotation types. ChangeLog is
annotated with the @Repeatable(ChangeLogs.class) annotation, which means that it is a repeatable annotation type
and its containing annotation type is ChangeLogs .
 
Search WWH ::




Custom Search