The first step in the post is defining an annotation type. This is pretty simple to do and looks familiar as well. An annotation-type declaration looks like an interface declaration except an "@" symbol precedes the interface keyword. The method declaration that goes between the braces of this declaration defines the elements of the annotation type. Of course, since we are annotating the code and not defining behavior, logically speaking, these methods shouldn't throw any exception. That means no throws clause. Another restriction is that the return type for these methods is restricted to primitives: String, Class, enums, annotations and arrays of the preceding types. The complete lists of restrictions are as follows:
No extends clause is permitted.Annotation types automatically extend a marker interface, java.lang.annotation.Annotation.
Methods must not have any parameters.
Methods must not have any type parameters (in other words, generic methods are prohibited).
Method return types are restricted to primitive types: String, Class, enum types, annotation types and arrays of the preceding types.
No throws clause is permitted.
Annotation types must not be parameterized.
The following code snippet defines an annotation type for a servlet. Presumably, we could use this definition to annotate a servlet and then have an annotation tool generate web.xml. Here we define no args methods that define the various XML attributes/elements found in web.xml. For conciseness we have left out elements like init, load on startup, icon etc.
[java] view plaincopy
public @interface Servlet {
String servletName();
String servletClass();
String displayName();
String description();
}
Declaring Annotation
Now that we have the annotation-type defined we can annotate our servlet using the defined annotation type. Annotation is a new kind of modifier that contains an annotation type with zero or more member-value pairs. If a member has a default value defined in the annotation-type member declaration then the value can be omitted, otherwise, annotation must provide a member-value pair for all members defined in the annotation type. Annotation can be used for modifiers in any declaration - class, interface, constructor, method, field, enum, even local variable. It can also be used on a package declaration provided only one annotation is permitted for a given package. In our case we are annotating at the class level and the annotation precedes the access modifier public.
[java] view plaincopy
@Servlet(
servletName="AnnotatedServet",
servletClass="com.wonder.servlet.AnnotatedServet",
displayName="AnnotatedServet",
description="This is an example Annotated Servlet"
)
public class AnnotatedServet extends HttpServlet{...}
Annotation Retention
The consumers of annotation fall into three categories.
Introspectors: Programs that query runtime-visible annotations of their own program elements. These programs will load both annotated classes and annotation interfaces into the virtual machine.
Specific Tools: Programs that query known annotation types of arbitrary external programs. Stub generators, for example, fall into this category. These programs will read annotated classes without loading them into the virtual machine, but will load annotation interfaces.
General Tools: Pr