|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON General Java Harness the Power of Javadoc...
...to track your team's code reviews
By: James Scott
May. 5, 2004 12:00 AM
High-performing software teams use code reviews to improve the quality of their code and to keep team members informed of what's going on in the larger application. However, keeping track of which packages, classes, and methods have been reviewed can be a real headache, especially if a code review process is adopted for an existing codebase. Fortunately, Sun's Javadoc API documentation generator provides the basis for a simple but powerful tool that will help the team mark reviewed code and track the progress of reviews. Most software teams recognize the virtues of code reviews. Though techniques vary in formality and thoroughness, all code reviews intend to find bugs as early in the development life cycle as possible. A developer who reads code that someone else wrote is likely to find issues that functional testing will not. Reviews also have a side benefit: developers get exposure to the techniques employed by their colleagues. Going over the code is actually just the start of the code review process - someone has to keep track of which code has been reviewed and document the results of the reviews. This is not the typical developer's idea of a good time, but without this information the team will not be able to assess the effectiveness of their reviews. Spreadsheets, bug-tracking tools, and homegrown databases make adequate tools for tracking reviews, but they all suffer from a common weakness: they're separate from the code and therefore require an extra step after the review to keep the review statuses up-to-date. The ideal review-tracking tool would provide an easy way to mark the code as having been reviewed. As it happens, Sun provides just such a tool with the Java SDK: Javadoc. Javadoc 101 In the first versions of the JDK, Javadoc was hard coded to output HTML. Since JDK release 1.2, Javadoc has offered a public doclet API for producing custom output. Doclets control the format and content of Javadoc output. The standard doclet that ships with Javadoc generates the HTML-formatted documentation that you're probably familiar with, but third-party doclets are available to produce XML, PDF, and other formats. While doclets provide complete control over the output, sometimes they're overkill. As of J2SDK 1.3, Javadoc did not provide a means of making small alterations to the documentation content without having to rewrite the doclet. In J2SDK 1.4, Sun introduced the taglet API for Javadoc. This lightweight API enabled the introduction of custom tags for use with any doclet, including the standard doclet. For the first time, developers had an easy means of customizing the standard HTML output. In the simplest cases, custom tags can be introduced on the command line, without any coding at all. Below we'll see how to use both taglets and doclets to facilitate tracking of code reviews. The next section shows how to create a custom taglet that you can use to mark source code as reviewed. After that, we'll look at using a custom doclet to produce a management report. Taglet, You're It The Easy Way: -tag tagname:Xaoptcmf:"taghead" where tagname is the name of the tag (without the initial "@") and taghead is the header you want to appear in the output. Each letter in Xaoptcmf represents the set of elements where the tag should appear (see Table 1). Executing the command: Javadoc -tag review:tm "Last code review:" on a source file with a class documentation comment containing an "@review" tag will produce output similar to that shown in Figure 1. More Control: Extending SimpleTaglet SimpleTaglet offers a three-argument constructor that accepts the same inputs as the -tag command-line option - hardly surprising, since SimpleTaglet underlies the command line. The first argument is the name of the tag, the second is the header to use in the output, and the third is a string that contains a single-character code for each element that the tag may appear in. Listing 1 shows a class that implements the review tag by extending SimpleTaglet. The static method register(Map tagletMap) is called during Javadoc setup and makes the doclet aware of the taglet's existence. By default, SimpleTaglet puts the tag header in an HTML <DT> element and the tag comment goes in a corresponding <DD> element. Comments from multiple tags of the same type are concatenated into a comma-separated list in a single <DD> element. If this doesn't meet your needs, you need to override the toString(Tag) and toString (Tag[]) methods to provide the output you want. Javadoc calls one of these methods when it encounters a documentation element containing a tag handled by the taglet. Sun's documentation is a bit unclear on this point, but as of JDK 1.4.2, Javadoc always calls toString (Tag[]) for standalone tags, even if the documentation element contains only one tag of the appropriate type. Javadoc only invokes toString(Tag) when handling inline tags. Since SimpleTaglet is defined as a standalone tag, Javadoc will never invoke its toString(Tag) method. If you want significant differences in your processing of single and multiple tags - a table for multiple tags, for instance - check the array size and call toString (Tag) if the array contains only one tag. To compile the taglet, you'll need the J2SDK's tools.jar on your compile classpath, since it contains the taglet-related classes and interfaces. Once you have the class compiled, you can make Javadoc aware of the taglet with the following command: javadoc -tagletpath tagletdir -taglet ReviewTaglet -sourcepath sourcedir where tagletdir is the directory containing the taglet's class file and sourcedir is the directory holding the source code that you want to document. Implementing the Taglet Interface Introducing Doclets Doclets are the first link in the documentation-generation chain and provide total control over the content and format of Javadoc's output. Javadoc invokes a doclet every time the tool is run. If no doclet is specified on the command line (using the -doclet option), Javadoc invokes the standard doclet. Except for the taglet API, there's no easy way to extend or modify the behavior of the standard doclet. To create our report, we'll have to implement our own doclet. API Overview RootDoc provides access to all of the packages and classes that were specified in Javadoc command-line options. It declares methods returning PackageDoc or ClassDoc types, which (as you can probably imagine) describe the structure and documentation comments of a package, class, or interface. PackageDoc's methods return ClassDocs for the classes in the package, filtered in various ways depending on which method is invoked. ClassDoc models the contents of a class, and uses ConstructorDoc, MethodDoc, and FieldDoc to describe its constituent elements. All of these interfaces extend Doc, which provides access to such things as tags, text of a documentation comment, and information about the position and type of the element currently being processed. Code Review Report firstSentenceTags() returns an array of tags that represent the first sentence in the comment. If the first sentence contains any inline tags, the returned array contains one tag of kind = "Text" for each sentence fragment and one tag of the appropriate kind for each inline tag. This allows us to output a readable, plain-text version of the first sentence even if it contains markup. Once ReviewDoclet is compiled, it can be invoked with the following command: javadoc -docletpath docletdir -doclet ReviewDoclet -sourcepath sourcedir Custom Command-Line Options: This feature provides a useful extension to our code-review reports: date filtering. Some organizations specify that all code must be reviewed periodically. A report that shows which classes have been reviewed since a specified date will alert the reviewers to the parts of the codebase that need attention. By specifying a new custom tag called @reviewdate, we can use the doclet's custom command-line option to find out which classes have been reviewed since a specified date. Listing 3 shows the new methods and member variables for ReviewDoclet. The optionLength method is implemented to return 1 if a "-date" option is passed, and 0 otherwise. validOptions looks for a -date option and confirms that the associated argument contains a well-formed date. Since the doclet does not require a -date option, validOptions returns true if none is found. Listing 4 contains a modified inner loop for the start method, which looks for a reviewdate tag and parses the text associated with the tag as a date. If the review date is after the date specified on the command line, the report simply outputs "OK." If the review date is absent, not parseable, or before the specified date, ReviewDoclet prints an appropriate message. Conclusion: The Future of Javadoc The code review documentation tools described here are a useful extension of Javadoc, but they represent only one extension of Javadoc's functionality. I encourage you to find ways to apply Javadoc to the problems facing your team. No longer will you be "working for Javadoc" - inserting the missing tags and malformed comments that Javadoc complains about. You now know how to make Javadoc work for you. Resources LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||