Datasheet

The Retention is set to SOURCE, which means this annotation is not available during compile time and
runtime. The doclet API is used to access source level annotations. The
Target is set to TYPE (for
classes/interfaces/enums) and
METHOD for methods. A compiler error is generated if the CodeTag anno-
tation is applied to any other source code element. The first two annotation elements are
authorName
and lastModificationDate, both of which are mandatory. The bugFixes element defaults to the
empty string if not specified. Following is an example class that utilizes the
CodeTag annotation:
import java.lang.annotation.*;
@CodeTag(authorName=”Dilbert”,
lastModificationDate=”May 7, 2006”)
public class ServerCommandProcessor {
@CodeTag(authorName=”Dilbert”,
lastModificationDate=”May 10, 2006”,
bugFixes=”BUG0170”)
public void setParams(String serverName)
{
// ...
}
public void executeCommand(String command, Object... params)
{
// ...
}
}
Note how annotation is used to mark who modified the source and when. The method was last modi-
fied a day after the class because of the bug fix. This custom annotation can be used to track this infor-
mation as part of keeping up with source code modifications. To view or process these source code
annotations, the doclet API must be used.
The doclet API (aka Javadoc API) has been extended to support the processing of annotations in the
source code. To use the doclet API, include the
tools.jar file (located in lib directory of a default JDK
install, version 5 or higher) in your classpath. You use the doclet API by writing a Java class that extends
com.sun.javadoc.Doclet. The start method must be implemented because this is the method that
Javadoc invokes on a doclet to perform custom processing. A simple doclet to print out all classes and
methods in a Java source file follows:
import com.sun.javadoc.*;
public class ListClasses extends Doclet {
public static boolean start(RootDoc root) {
ClassDoc[] classes = root.classes();
for (ClassDoc cd : classes) {
System.out.println(“Class [“ + cd + “] has the following methods”);
for(MemberDoc md : cd.methods()) {
System.out.println(“ “ + md);
}
}
return true;
}
}
28
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 28