<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<article id="JavaDoc">
  <articleinfo>
    <title>JavaDoc</title>
    <author>
      <firstname>Ashley</firstname>
      <othername>J.S</othername>
      <surname>Mills</surname>
      <affiliation>
 <address><email>ashley@ashleymills.com</email></address>
      </affiliation>
    </author>

    <copyright>
      <year>2005</year>
      <holder role="mailto:ashley@ashleymills.com">The University Of Birmingham</holder>
    </copyright>
  </articleinfo>

  <sect1 id="JavaDoc-Introduction"><title>JavaDoc</title>
    <para>
      JavaDoc is a tool which extracts information from a Java source file to create an API. It is specifically oriented toward this kind of documentation. To quote <ulink url="http://java.sun.com/j2se/javadoc/writingdoccomments/"><citetitle>How to Write Doc Comments for the JavadocTM Tool</citetitle></ulink>, an API should describe <quote>all aspects of the behavior of each method on which a caller can rely</quote>. One generally uses JavaDoc to document things such as <emphasis>Class</emphasis>es, <emphasis>Interface</emphasis>s and <emphasis>Method</emphasis>s but one can theoretically use it for anything written in Java by defining custom tags and creating custom <emphasis>DocLet</emphasis>s.
    </para>

    <para>
      JavaDoc is written within the Java source code for a particular project and then the <command>javadoc</command> tool is used to extract the JavaDoc-marked-up sections and create the <acronym>HTML</acronym> files that comprise the JavaDoc output.
    </para>
  </sect1>

  <sect1 id="JavaDoc-Installation"><title>Installation</title>
    <para>
      JavaDoc comes with the J2SE (Java 2 Platform Standard Edition), available from <ulink url="http://java.sun.com/j2se/">http://java.sun.com/j2se/</ulink>. You should be capable of following the instructions that come with this to install it; see <ulink url="../winenvars/winenvarshome.html"><citetitle>Configuring A Windows Working Environment</citetitle></ulink> and <ulink url="../unixenvars/unixenvarshome.html"><citetitle>Configuring A Unix Working Environment</citetitle></ulink>.
    </para>
  </sect1>

  <sect1 id="JavaDoc-Usage"><title>JavaDoc Usage</title>
    <sect2 id="JavaDoc-Usage-General"><title>General Format</title>
      <para>The general format of a JavaDoc marked up section is:</para>

      <programlisting>
/**
 * This is the description part of the doc comment.
 *
 * Additional details
 *
 * @tag1 Tag Content
 * @tag2 Tag Content
 *   .
 *   .
 *   .
 */
      </programlisting>

      <para>
        Take notice of the spacing used, the tags section must be separated from the description by a single blank line. The content of the comment starts one space in from the left hand edge of the comment. Tag lists may be separated into logical blocks by inserting a blank line.  Lines should preferably be below 80 columns in width. New paragraphs can be specified by using the <emphasis>&lt;p&gt;</emphasis> tag.
      </para>

      <para>
        The first sentence should summarize the overall behaviour of the Class, Interface or Method being described. It should be terminated with a full stop followed by either a space, tab or newline, it may span more than one line. To include multiple full stops in the first sentence, make sure that the ones that do not signify the end of the first sentence are not followed by a space, tab or newline. This can be achieved by using the <acronym>HTML</acronym> entity <emphasis>&amp;nsbp;</emphasis>:
      </para>

      <programlisting>
 /**
  * This is the part of the first sentence.&amp;nsbp;This is still part of the first sentence. This is not.
  *    
  */
      </programlisting>

      <para>One should write from the perspective of the third person:</para>

      <programlisting>
 /**
  * Correct   - Returns an Integer representing the mean of the numbers provided as parameters.
  *
  *             It has the special feature of blah blah blah...
  *
  * Incorrect - This method returns an Integer representing the mean of the numbers you provide as parameters. 
  *
  *             This has the special feature of blah blah blah...
  */
      </programlisting>

      <para>Tag inclusion should accord to the following order:</para>

      <para>
        <literallayout>
* @author       (classes and interfaces only, required)
* @version      (classes and interfaces only, required)
*               
* @param        (methods and constructors only)
* @return       (methods only)
* @exception    (@throws is a synonym added in Javadoc 1.2)
* @see          
* @since        
* @serial       (or @serialField or @serialData)
* @deprecated
        </literallayout>
      </para>

      <para>When using Java code in a description, one should use &lt;code&gt;blah&lt;/code&gt;:</para>

      <programlisting>
*
* Returns &lt;code&gt;true&lt;/code&gt; if blah otherwise returns &lt;code&gt;false&lt;/code&gt;
*
      </programlisting>

      <para>A description may contain any <acronym>HTML</acronym> tags:</para>

      <programlisting>
/**
 * This is the &lt;strong&gt;description&lt;/strong&gt; part of the doc comment.
 * &lt;p&gt;
 *   blah blah blah blah blah
 *   blah blah &lt;b&gt;blah&lt;/b&gt; &lt;i&gt;blah&lt;/i&gt;blah
 * &lt;/p&gt;
 * &lt;pre&gt;
 *     blah
 * blah    blah
 * &lt;/pre&gt;
 * 
 * @author Ashley &lt;strong&gt;Mills&lt;/strong&gt;
 * @version 1.2
 */
public class test {
         .
         .
         .
      </programlisting>

      <figure><title>The <acronym>HTML</acronym> output from the listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/tags.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>

    </sect2>

    <sect2 id="JavaDoc-Usage-Generating-Output"><title>Using <command>javadoc</command> To Produce The Output</title>
      <para>The command <command>javadoc</command> is used to generate the JavaDoc output:</para>
      <screen><userinput><command>javadoc</command> <replaceable>blah.java</replaceable></userinput></screen>
      <para>Creates JavaDoc for the file <filename>blah.java</filename>.</para>
      <screen><userinput><command>javadoc</command> <replaceable>*.java</replaceable></userinput></screen>
      <para>Creates JavaDoc for all <filename>java</filename> files in the current directory.</para>
      <screen><userinput><command>javadoc</command> <replaceable>-help</replaceable></userinput></screen>
      <para>Displays the command line options (so does not providing any).</para>

      <para>
        JavaDoc does not automatically link to the real online API, to achieve this one has to utilise the <option>-link</option> option provided by StandardDoclet. Assuming one desires to link to the Java 1.4 API, one would use:
      </para>

      <screen>
        <command>javadoc</command> <option>-link</option> http://java.sun.com/j2se/1.4/docs/api <replaceable>*.java</replaceable>
      </screen>

      <para>
        This causes all references to Java 1.4 classes in the generated <acronym>HTML</acronym> to be resolved.
      </para>

      <tip><para>Use the <option>-d</option> option to direct generated JavaDoc to a specific directory.</para></tip>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-author"><title>@author</title>
      <para>@author <replaceable>author name</replaceable></para>
      <para>Specifies the author of the class or interface:</para>

      <programlisting>
 * 
 * @author Jimmy Petronas
 * @author Mickey Block 
 * @author Sally Finch
 *
      </programlisting>

      <para>
        Not included in generated <acronym>HTML</acronym> using standard Doclet, unless turned on by specifying <option>-author</option> option when running <command>javadoc</command>
      </para>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-version"><title>@version</title>
      <para>@version <replaceable>version information</replaceable></para>
      <para>Adds version information to a class.</para>

      <programlisting>
*
* @version 1.2
*
      </programlisting>

      <para>
        Only one per class or interface allowed. Not included in generated <acronym>HTML</acronym> using standard Doclet, unless turned on by specifying the <option>-version</option> option when running <command>javadoc</command>.
      </para>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-param"><title>@param</title>
      <para>@param parameter-name description</para>
      <para>Adds a parameter to the parameter list.</para>

      <programlisting>
/**
 * Returns the product of two integers.
 *
 * @param int operand one 
 * @param int operand two
 * @return an int 
 */
public int mul(int a, int b) {
   return a*b;
}
      </programlisting>

      <figure><title>The <acronym>HTML</acronym> output from the listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/mulmethod.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-return"><title>@return</title>
      <para>@return <replaceable>description of return value</replaceable></para>
      <para>Describes the value returned from a method.</para>

      <programlisting>
*
* @return A new BlahBlah Object with a field size of 100.
*
      </programlisting>

      <figure><title>The <acronym>HTML</acronym> output for the @return above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/return.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-exception"><title>@exception</title>
      <para>@exception <replaceable>class-name</replaceable> <replaceable>description</replaceable></para>
      <para>
        Describes the exceptions thrown by the constructor, method, class or interface. <replaceable>class-name</replaceable> is the name of the exception.
      </para>

      <programlisting>
   /**
    * Replaces test().
    * @throws BlahException unless blah blah blah
    * @exception BlahBlahException 
    */
   public test(int i) {
   }
      </programlisting>

      <figure><title>The <acronym>HTML</acronym> output for the listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/exception.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-see"><title>@see</title>
      <para>@see <replaceable>classname</replaceable></para>
      <para>Adds a hyperlink to the section referenced by the classname supplied.</para>

      <programlisting>
/**
 * Returns the product of the squares of a and b.
 *
 * @see #mul
 *
 * @param int operand one 
 * @param int operand two
 * @return an int 
 */
   public int squaredMul(int a, int b) {
      return mul(mul(a,a),mul(b,b));
   }
      </programlisting>

      <figure><title>The <acronym>HTML</acronym> output for listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/see.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>

      <para>
        The classname can be local like this example, prefixed with a hash '#' or a fully qualified class name like<emphasis>java.lang.String</emphasis> or <emphasis>java.lang.String#charAt</emphasis>.
      </para>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-since"><title>@since</title>
      <para>@since <replaceable>since-text</replaceable></para>
      <para>Specifies from which version of Java this class, method or interface has been available from.</para>

      <programlisting>
*
* @since JDK1.4
*
      </programlisting>
      <figure><title>The <acronym>HTML</acronym> output for listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/since.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>
    </sect2>

    <sect2 id="JavaDoc-Usage-Tags-deprecated"><title>@deprecated</title>
      <para>@deprecated  <replaceable>deprecated-text</replaceable></para>
      <para>Marks a class, interface or method as deprecated.</para>

      <programlisting>
/**
 * @deprecated Replaced by &lt;code&gt;test(int)&lt;/code&gt;
 * @see #test(int)
 */
public test() {
}

/**
 * Replaces test().
 */
public test(int i) {
}
      </programlisting>
      <figure><title>The <acronym>HTML</acronym> output for listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/deprecated.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>
    </sect2>

    <sect2 id="JavaDoc-Usage-More"><title>More</title>
      <para>
        More detail pertaining to the available tags can be found at the JavaDoc reference page: <ulink url="http://java.sun.com/j2se/1.4/docs/tooldocs/windows/javadoc.html">http://java.sun.com/j2se/1.4/docs/tooldocs/windows/javadoc.html</ulink>.
      </para>
    </sect2>
  </sect1>

  <sect1 id="JavaDoc-Example"><title>Simple Example</title>
    <para>
      Here is a simple example showing a few of the features of JavaDoc:
    </para>

    <programlisting>
/**
 * Represents a &quot;fuzzy&quot; integer, in that the integer is
 * not exact, but is instead specified as being between a range.
 *
 * @author Ashley Mills
 * @version 1.0b
 */
public class FuzzyInteger {
   private int lowerVal, upperVal;

   /**
    * Creates a new &lt;code&gt;FuzzyInteger&lt;/code&gt; object based on the
    * range specified.
    * 
    * @param lowerVal The lowest value that the integer could be.
    * @param upperVal The highest value that the integer could be.
    */
   public FuzzyInteger(int lowerVal, int upperVal) {
      this.lowerVal = lowerVal;
      this.upperVal = upperVal;
   }

   /**
    * Doubles the value of the &lt;code&gt;FuzzyInteger&lt;/code&gt;.
    */
   public void doubleFuzzy() {
      lowerVal = lowerVal+lowerVal;
      upperVal = upperVal+upperVal;
   }
}
    </programlisting>

      <figure><title>The <acronym>HTML</acronym> output for listing above</title>
        <mediaobject>
          <imageobject><imagedata fileref="files/images/example.png" format="PNG"/></imageobject>
        </mediaobject>
      </figure>
  </sect1>

  <sect1 id="JavaDoc-References"><title>References (And Links You May Find Useful)</title>
    <itemizedlist>
      <listitem>
        <para><ulink url="http://java.sun.com/j2se/javadoc/writingdoccomments/index.html">http://java.sun.com/j2se/javadoc/writingdoccomments/index.html</ulink></para>
        <para>
          <literallayout>
How to Write Doc Comments
for the JavadocTM Tool
Maintained by javadoc-tool@sun.com 
          </literallayout>
        </para>
      </listitem>

      <listitem>
        <para><ulink url="http://java.sun.com/j2se/1.4/docs/tooldocs/solaris/javadoc.html">http://java.sun.com/j2se/1.4/docs/tooldocs/solaris/javadoc.html</ulink></para>
        <para>JavaDoc reference page</para>
      </listitem>

      <listitem>
        <para><ulink url="http://java.sun.com/j2se/javadoc/faq/index.html">http://java.sun.com/j2se/javadoc/faq/index.html</ulink></para> 
        <para>JavaDoc FAQ</para>
      </listitem>
    </itemizedlist>
  </sect1>
</article>

