The pre-Java 9 Doclet API

The pre-Java 9 Doclet API, or the com.sun.javadoc package, gives us access to look at Javadoc comments located in the source code. Invoking a Doclet is accomplished by using the start method. This method's signature is public static boolean start(RootDoc root). We will use the RootDoc instance as a container for the program's structure information.

In order to call Javadoc, we need to pass the following:

  • Package names
  • Source file names (for classes and interfaces)
  • An access control option—one of the following:
  • package
  • private
  • protected
  • public

When the preceding listed items are used to call javadoc, a documented set is provided as a filtered list. If our aim is to obtain a comprehensive, unfiltered list, we can use allClasses(false).

Let's review an example Doclet:

// Mandatory import statement
import com.sun.javadoc.*;

// We will be looking for all the @throws documentation tags
public class AllThrowsTags extends Doclet {

// This is used to invoke the Doclet.
public static boolean start(Rootdoc myRoot) {
// "ClassDoc[]" here refers to classes and interfaces.
ClassDoc[] classesAndInterfaces = myRoot.classesAndInterfaces();
for (int i = 0; i < classesAndInterfaces.length; ++i) {
ClassDoc tempCD = classesAndInterfaces[i];
printThrows(tempCD.contructors());
printThrows(tempCD.methods());
}
return true;
}

static void printThrows(ExecutableMemberDoc[] theThrows) {
for (int i = 0; i < theThrows.length; ++i) {
ThrowsTag[] throws = theThrows[i].throwsTags();
// Print the "qualified name" which will be
// the class or interface name
System.out.println(theThrows[i].qualifiedName());
// A loop to print all comments with the
// Throws Tag that belongs to the previously
// printed class or interface name
for (int j = 0; j < throws.length; ++j) {
// A println statement that calls three
// methods from the ThrowsTag Interface:
// exceptionType(), exceptionName(),
// and exceptionComment().
System.out.println("--> TYPE: " +
throws[j].exceptionType() +
" | NAME: " + throws[j].exceptionName() +
" | COMMENT: " + throws[j].exceptionComment());
}
}
}
}

As you can see by the thoroughly commented code, gaining access to the javadoc content is relatively easy. In our preceding example, we would invoke the AllThrows class by using the following code in the command line:

javadoc -doclet AllThrowsTags -sourcepath <source-location> java.util

The output of our result will consist of the following structure:

<class or interface name>
TYPE: <exception type> | NAME: <exception name> | COMMENT: <exception comment>
TYPE: <exception type> | NAME: <exception name> | COMMENT: <exception comment>
TYPE: <exception type> | NAME: <exception name> | COMMENT: <exception comment>
<class or interface name>
TYPE: <exception type> | NAME: <exception name> | COMMENT: <exception comment>
TYPE: <exception type> | NAME: <exception name> | COMMENT: <exception comment>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset