Java Reference
In-Depth Information
// Pass the review without changes
@Review(status=ReviewStatus.PASSED)
Here is the sample code that annotates a Test class indicating that it passed the code review:
import com.jdojo.annotation.Review.ReviewStatus;
import com.jdojo.annotation.Review;
@Review(status=ReviewStatus.PASSED)
public class Test {
// Code goes here
}
Annotation Type
An annotation type can be used anywhere a type can be used in a Java program. For example, you can use an
annotation type as the return type for a method. You can also use an annotation type as the type of an element inside
another annotation type's declaration. Suppose you want to have a new annotation type called Description , which will
include the name of the author, version, and comments for a program element. You can reuse your Name and Version
annotation types as its name and version elements type. Listing 1-9 has code the for Description annotation type.
Listing 1-9. An Annotation Type Using Other Annotation Types as Data Type of Its Elements
// Description.java
package com.jdojo.annotation;
public @interface Description {
Name name();
Version version();
String comments() default "";
}
To provide a value for an element of an annotation type, you need to use the syntax that is used to create an
annotation type instance. For example, @Version(major=1, minor=2) creates an instance of the Version annotation.
Note the nesting of an annotation inside another annotation in the following snippet of code:
@Description(name=@Name(first="John", last="Jacobs"),
version=@Version(major=1, minor=2),
comments="Just a test class")
public class Test {
// Code goes here
}
Array Type Annotation Element
An annotation can have elements of an array type. The array type could be of one of the following types:
A primitive type
java.lang.String type
java.lang.Class type
enum type
An
An annotation type
 
Search WWH ::




Custom Search