JavadocStyle

Since Checkstyle 3.2

Description

Validates Javadoc comments to help ensure they are well formed.

The following checks are performed:

  • Ensures the first sentence ends with proper punctuation (That is a period, question mark, or exclamation mark, by default). Note that this check is not applied to inline @return tags, because the Javadoc tools automatically appends a period to the end of the tag content. Javadoc automatically places the first sentence in the method summary table and index. Without proper punctuation the Javadoc may be malformed. All items eligible for the {@inheritDoc} tag are exempt from this requirement.
  • Check text for Javadoc statements that do not have any description. This includes both completely empty Javadoc, and Javadoc with only tags such as @param and @return.
  • Check text for incomplete HTML tags. Verifies that HTML tags have corresponding end tags and issues an "Unclosed HTML tag found:" error if not. An "Extra HTML tag found:" error is issued if an end tag is found without a previous open tag.
  • Check that a package Javadoc comment is well-formed (as described above).
  • Check for allowed HTML tags. The list of allowed HTML tags is "a", "abbr", "acronym", "address", "area", "b", "bdo", "big", "blockquote", "br", "caption", "cite", "code", "colgroup", "dd", "del", "dfn", "div", "dl", "dt", "em", "fieldset", "font", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "kbd", "li", "ol", "p", "pre", "q", "samp", "small", "span", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "tt", "u", "ul", "var".

These checks were patterned after the checks made by the DocCheck doclet available from Sun. Note: Original Sun's DocCheck tool does not exist anymore.

Properties

name description type default value since
checkEmptyJavadoc Control whether to check if the Javadoc is missing a describing text. boolean false 3.4
checkFirstSentence Control whether to check the first sentence for proper end of sentence. boolean true 3.2
checkHtml Control whether to check for incomplete HTML tags. boolean true 3.2
endOfSentenceFormat Specify the format for matching the end of a sentence. Pattern "([.?!][ \t\n\r\f<])|([.?!]$)" 5.0
excludeScope Specify the visibility scope where Javadoc comments are not checked. Scope null 3.4
scope Specify the visibility scope where Javadoc comments are checked. Scope private 3.2
tokens tokens to check subset of tokens ANNOTATION_DEF , ANNOTATION_FIELD_DEF , CLASS_DEF , CTOR_DEF , ENUM_CONSTANT_DEF , ENUM_DEF , INTERFACE_DEF , METHOD_DEF , PACKAGE_DEF , VARIABLE_DEF , RECORD_DEF , COMPACT_CTOR_DEF . ANNOTATION_DEF , ANNOTATION_FIELD_DEF , CLASS_DEF , CTOR_DEF , ENUM_CONSTANT_DEF , ENUM_DEF , INTERFACE_DEF , METHOD_DEF , PACKAGE_DEF , VARIABLE_DEF , RECORD_DEF , COMPACT_CTOR_DEF . 3.2

Examples

To configure the default check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle"/>
  </module>
</module>
        

Example1:

/**
 * Some description here
 */
public class Example1 {
  Example1() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here.
   */
  private void testMethod1() {}
  // ok below, @return tag automatically inserts a period after the text
  /**
   * {@return {@code true} if this object
   * has been initialized, {@code false} otherwise}
   */
  private boolean testMethod2() {
    return true;
  }
  /**
   * Some description here
   */
  private void testMethod3() {
    // violation 4 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   */
  public void testMethod4() {
    // violation 4 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   * Second line of description
   */
  private void testMethod5() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   * <p
   */
  private void testMethod6() { // violation 4 lines above 'should end with a period'
    // violation 3 lines above 'Incomplete HTML tag found'
  }
}
        

To configure the check for public scope:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle">
    <property name="scope" value="public"/>
    </module>
  </module>
</module>
        

Example2:

/**
 * Some description here
 */
public class Example2 {
  Example2() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here.
   */
  private void testMethod1() {}
  // ok below, @return tag automatically inserts a period after the text
  /**
   * {@return {@code true} if this object
   * has been initialized, {@code false} otherwise}
   */
  private boolean testMethod2() {
    return true;
  }
  /**
   * Some description here
   */
  private void testMethod3() {

  }
  /**
   * Some description here
   */
  public void testMethod4() {
    // violation 4 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   * Second line of description
   */
  private void testMethod5() {

  }
  /**
   * Some description here
   * <p
   */
  private void testMethod6() {

  }
}
        

To configure the check for javadoc which is in private, but not in package scope:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle">
    <property name="scope" value="private"/>
    <property name="excludeScope" value="package"/>
    </module>
  </module>
</module>
        

Example3:

/**
 * Some description here
 */
public class Example3 {
  Example3() {

  }
  /**
   * Some description here.
   */
  private void testMethod1() {}
  // ok below, @return tag automatically inserts a period after the text
  /**
   * {@return {@code true} if this object
   * has been initialized, {@code false} otherwise}
   */
  private boolean testMethod2() {
    return true;
  }
  /**
   * Some description here
   */
  private void testMethod3() {
    // violation 4 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   */
  public void testMethod4() {

  }
  /**
   * Some description here
   * Second line of description
   */
  private void testMethod5() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   * <p
   */
  private void testMethod6() { // violation 4 lines above 'should end with a period'
    // violation 3 lines above 'Incomplete HTML tag'
  }
}
        

To configure the check to turn off first sentence checking:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle">
    <property name="checkFirstSentence" value="false"/>
    </module>
  </module>
</module>
        

Example4:

/**
 * Some description here
 */
public class Example4 {
  Example4() {

  }
  /**
   * Some description here.
   */
  private void testMethod1() {}
  // ok below, @return tag automatically inserts a period after the text
  /**
   * {@return {@code true} if this object
   * has been initialized, {@code false} otherwise}
   */
  private boolean testMethod2() {
    return true;
  }
  /**
   * Some description here
   */
  private void testMethod3() {

  }
  /**
   * Some description here
   */
  public void testMethod4() {

  }
  /**
   * Some description here
   * Second line of description
   */
  private void testMethod5() {

  }
  /**
   * Some description here
   * <p
   */
  private void testMethod6() {
    // violation 3 lines above 'Incomplete HTML tag'
  }
}
        

To configure the check to turn off validation of incomplete html tags:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle">
    <property name="checkHtml" value="false"/>
    </module>
  </module>
</module>
        

Example5:

/**
 * Some description here
 */
public class Example5 {
  Example5() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here.
   */
  private void testMethod1() {}
  // ok below, @return tag automatically inserts a period after the text
  /**
   * {@return {@code true} if this object
   * has been initialized, {@code false} otherwise}
   */
  private boolean testMethod2() {
    return true;
  }
  /**
   * Some description here
   */
  private void testMethod3() {
    // violation 4 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   */
  public void testMethod4() {
    // violation 4 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   * Second line of description
   */
  private void testMethod5() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here
   * <p
   */
  private void testMethod6() { // violation 4 lines above 'should end with a period'

  }
}
        

To configure the check for only class definitions:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle">
    <property name="tokens" value="CLASS_DEF"/>
    </module>
  </module>
</module>
        

Example6:

/**
 * Some description here
 */
public class Example6 {
  Example6() {
    // violation 5 lines above 'First sentence should end with a period'
  }
  /**
   * Some description here.
   */
  private void testMethod1() {}
  // ok below, @return tag automatically inserts a period after the text
  /**
   * {@return {@code true} if this object
   * has been initialized, {@code false} otherwise}
   */
  private boolean testMethod2() {
    return true;
  }
  /**
   * Some description here
   */
  private void testMethod3() {

  }
  /**
   * Some description here
   */
  public void testMethod4() {

  }
  /**
   * Some description here
   * Second line of description
   */
  private void testMethod5() {

  }
  /**
   * Some description here
   * <p
   */
  private void testMethod6() {

  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.javadoc

Parent Module

TreeWalker