Java Reference
In-Depth Information
type itself. You cannot use the annotation type Version as @Version(major=1, minor=2, toString="Hello") .
The Version annotation type does not declare toString as an element. It inherits the toString() method from the
Annotation interface.
Restriction #2
Method declarations in an annotation type cannot specify any parameters. A method declares an element for the
annotation type. An element in an annotation type lets you associate a data value to an annotation's instance.
A method declaration in an annotation is not called to perform any kind of processing. Think of an element as an
instance variable in a class having two methods, a setter and a getter, for that instance variable. For an annotation,
the Java runtime creates a proxy class that implements the annotation type (which is an interface). Each annotation
instance is an object of that proxy class. The method you declare in your annotation type becomes the getter method
for the value of that element you specify in the annotation. The Java runtime will take care of setting the specified
value for the annotation elements. Since the goal of declaring a method in an annotation type is to work with a data
element, you do not need to (and are not allowed to) specify any parameters in a method declaration. The following
declaration of an annotation type would not compile because it declares a concatenate() method, which accepts
two parameters:
// Won't compile
public @interface WrongVersion {
// Cannot have parameters
String concatenate(int major, int minor);
}
Restriction #3
Method declarations in an annotation type cannot have a throws clause. A method in an annotation type is defined
to represent a data element. Throwing an exception to represent a data value does not make sense. The following
declaration of an annotation type would not compile because the major() method has a throws clause:
// Won't compile
public @interface WrongVersion {
int major() throws Exception; // Cannot have a throws clause
int minor(); // OK
}
Restriction #4
The return type of a method declared in an annotation type must be one of the following types:
byte , short , int , long , float , double , boolean , and char
Any primitive type:
java.lang.String
java.lang.Class
enum type
An
An annotation type
String[] , int[] , etc. The return
type cannot be a nested array. For example, you cannot have a return type of String[][] or
int[][] .
An array of any of the above mentioned type, for example,
 
Search WWH ::




Custom Search