RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Doclets: Decipher and Document Java Code Easily : Page 2

Developers are increasingly required to take over and maintain multiple projects. Doclets, a new addition to Java SE 6, provides a mechanism for learning the source code of Java projects with relative ease.

Doclets in Depth
A simple Doclet can scan through all classes and print only the fields, methods, and super class information—very handy for segregating the main logic-processing classes from the support classes. To make a developer's life even easier, the Doclet API's Doc class has methods to retrieve the position of a particular line in a given file.

MyDoclet.java in the source code download is an example of a simple Doclet. It walks through each class and prints all the methods and their method signatures. However, the Doclet API provides various additional classes and methods to customize processing further. All custom Doclets, such as those included in the source code download, must extend from com.sun.javadoc.Doclet and implement the following start method:

public static boolean start(RootDoc root)

Figure 1. Output from Running Doclet Example: The –private option processes the private methods of the class, while the –Doclet option forces Javadoc to use the example Doclet.

The root.classes() method of RootDoc grants access to an array of ClassDocs, which in turn could be used to navigate the class structures (for example, methods, constructors, fields, etc.). The typeParamTags() method of the ClassDoc or MethodDoc interfaces retrieves all the existing parameter tags documented in the code. The MethodDoc.tags(String tagName) method allows you to retrieve your custom tags by passing a custom tag name.

Run the Doclets
To run the Doclets in the source code download, take the following steps:

  1. Include tools.jar in your classpath.
  2. In the command prompt, type the following commands:
    $>javadoc -private -doclet MyDocletMissingDocumentation -docletpath . TestNoDocumentation.java

The –private option processes the private methods of the class. The –Doclet option forces Javadoc to use your Doclet as opposed to the standard Doclet. Figure 1 shows the output.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date