Documenting Java Code Javadoc The Tool and the

  • Slides: 16
Download presentation
Documenting Java Code Javadoc: The Tool and the Legend

Documenting Java Code Javadoc: The Tool and the Legend

Comments in Java l Regular Java comments: /* */ • for programmers who must

Comments in Java l Regular Java comments: /* */ • for programmers who must read or modify your code. l “One Liners”: // • for programmers who must read or modify your code. l Javadoc comments: /** */ • for those who must use your code.

General Form of a Doc Comment l The first line is indented to line

General Form of a Doc Comment l The first line is indented to line up with the code below the comment, and starts with the begin-comment symbol (/**) followed by a return l Subsequent lines start with an asterisk *. They are indented an additional space so the asterisks line up. A space separates the asterisk from the descriptive text or tag that follows it. l Insert a blank comment line between the description and the list of tags, as shown. l Insert additional blank lines to create "blocks" of related tags l The last line begins with the end-comment symbol (*/) indented so the asterisks line up and followed by a/**return. * This is the description part of a doc Note that the end-comment symbol contains comment only a single asterisk (*). * * @tag Comment for the tag

General Form of a Doc Comment (cont) l Break any doc-comment lines exceeding 80

General Form of a Doc Comment (cont) l Break any doc-comment lines exceeding 80 characters in length, if possible. If you have more than one paragraph in the doc comment, separate the paragraphs with a <p> paragraph tag. l Comments can contain HTML tags (including links)

First Sentence l should be a summary sentence, containing a concise but complete description

First Sentence l should be a summary sentence, containing a concise but complete description of the API item l The Javadoc tool copies this first sentence to the appropriate member, class/interface or package summary l This sentence ends at the first period that is followed by a blank, tab, or line terminator, or at the first tag (as defined below). For example, this first sentence ends at/**"Prof. ": * This is a simulation of Prof. Knuth's MIX computer. */ l you can work around this by typing an HTML meta-character such as "&" or "<" immediately after/**the period, such as: * This is a simulation of Prof.   Knuth's MIX computer. */

Tagged Paragraphs l l l Begin with @ followed by keyword End at the

Tagged Paragraphs l l l Begin with @ followed by keyword End at the next tag or end of the comment Identify information that has routine structure

@see l Used to specify a crossreference • @see java. lang. String • @see

@see l Used to specify a crossreference • @see java. lang. String • @see java. io. Input. Stream; • @see String#equals • @see java. lang. Object#wait(int) • @see java. io. Random. Access. File#Random. Access. File(File, String) • @see Character#MAX_RADIX • @see <a href="spec. html">Java Spec</a>

@author l May be used for class and interface declarations @author Mary Wollstonecraft l

@author l May be used for class and interface declarations @author Mary Wollstonecraft l A documentation comment may contain more than one @author tag

@version l May be used for class and interface declarations @version 493. 0. 1

@version l May be used for class and interface declarations @version 493. 0. 1 beta l A documentation comment may contain at most one version tag

@param l May be used in comments for method and constructor declarations @param file

@param l May be used in comments for method and constructor declarations @param file the file to be searched l Parameters should be documented in the order in which they are declared

@return l may be used in documentation comments for declarations of methods whose result

@return l may be used in documentation comments for declarations of methods whose result type is not void @return the number of widgets to be used l Use to provide a short description of the return value

@exception (@throws) l may be used in documentation comments for method and constructor declarations

@exception (@throws) l may be used in documentation comments for method and constructor declarations @exception Index. Out. Of. Bounds. Exception the matrix is too large l The name of the class followed by a short description of when it is thrown

@deprecated-text • Adds a comment indicating that this API should no longer be used

@deprecated-text • Adds a comment indicating that this API should no longer be used (even though it may continue to work). Javadoc moves the deprecated-text ahead of the description, placing it in italics and preceding it with a bold warning: "Deprecated".

Java. Doc Example /** * The root of the Class hierarchy. Every Class in

Java. Doc Example /** * The root of the Class hierarchy. Every Class in the * system has Object as its ultimate parent. Every variable * and method defined here is available in every Object. * @see Class * @version 1. 37, 26 Jun 1996 */ public class Object { /** * Compares two Objects for equality. * Returns a boolean that indicates whether this Object * is equivalent to the specified Object. This method is * used when an Object is stored in a hashtable. * @param obj the Object to compare with public boolean equals(Object obj) { return (this == obj); } /** * Creates a clone of the object. A new instance is * allocated and a bitwise clone of the current object * is placed in the new object. * @return a clone of this Object. * @exception Out. Of. Memory. Error If there is * memory. * @exception Clone. Not. Supported. Exception * Object explicitly does not want to * cloned, or it does not support * the Cloneable interface. */

Running Javadoc l Synopsis: javadoc [ options ] [ packagenames ] [ sourcefiles ]

Running Javadoc l Synopsis: javadoc [ options ] [ packagenames ] [ sourcefiles ] [ @files ] l Example javadoc -sourcepath /java/my/src/share/classes -d /java/my/api me. pack 1 me. pack 2 l Generates Output in /java/my/api

Links l l l http: //java. sun. com/j 2 se/javadoc/ writingdoccomments/index. html http: //java.

Links l l l http: //java. sun. com/j 2 se/javadoc/ writingdoccomments/index. html http: //java. sun. com/docs/books/jl s/html/18. doc. html http: //java. sun. com/products/jdk/ 1. 2/docs/tooldocs/win 32/javadoc. html