JavadocMethod

Since Checkstyle 3.0

Description

Checks the Javadoc of a method or constructor.

Violates parameters and type parameters for which no param tags are present can be suppressed by defining property allowMissingParamTags.

Violates methods which return non-void but for which no return tag is present can be suppressed by defining property allowMissingReturnTag.

Violates exceptions which are declared to be thrown (by throws in the method signature or by throw new in the method body), but for which no throws tag is present by activation of property validateThrows. Note that throw new is not checked in the following places:

  • Inside a try block (with catch). It is not possible to determine if the thrown exception can be caught by the catch block as there is no knowledge of the inheritance hierarchy, so the try block is ignored entirely. However, catch and finally blocks, as well as try blocks without catch, are still checked.
  • Local classes, anonymous classes and lambda expressions. It is not known when the throw statements inside such classes are going to be evaluated, so they are ignored.

ATTENTION: Checkstyle does not have information about hierarchy of exception types so usage of base class is considered as separate exception type. As workaround, you need to specify both types in javadoc (parent and exact type).

Javadoc is not required on a method that is tagged with the @Override annotation. However, under Java 5 it is not possible to mark a method required for an interface (this was corrected under Java 6). Hence, Checkstyle supports using the convention of using a single {@inheritDoc} tag instead of all the other tags.

Note that only inheritable items will allow the {@inheritDoc} tag to be used in place of comments. Static methods at all visibilities, private non-static methods and constructors are not inheritable.

For example, if the following method is implementing a method required by an interface, then the Javadoc could be done as:

/** {@inheritDoc} */
public int checkReturnTag(final int aTagIndex,
                          JavadocTag[] aTags,
                          int aLineNo)
        

Properties

name description type default value since
accessModifiers Specify the access modifiers where Javadoc comments are checked. AccessModifierOption[] public, protected, package, private 8.42
allowMissingParamTags Control whether to ignore violations when a method has parameters but does not have matching param tags in the javadoc. boolean false 3.1
allowMissingReturnTag Control whether to ignore violations when a method returns non-void type and does not have a return tag in the javadoc. boolean false 3.1
allowedAnnotations Specify annotations that allow missed documentation. String[] Override 6.0
validateThrows Control whether to validate throws tags. boolean false 6.0
tokens tokens to check subset of tokens METHOD_DEF , CTOR_DEF , ANNOTATION_FIELD_DEF , COMPACT_CTOR_DEF . METHOD_DEF , CTOR_DEF , ANNOTATION_FIELD_DEF , COMPACT_CTOR_DEF . 3.0

Examples

To configure the default check:

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

Example:

public class Example1 {

  /** */
  Example1(int x) {}
  // violation above, 'Expected @param tag for 'x''
  /** */
  public int m1(int p1) { return p1; }
  // 2 violations above:
  //    '@return tag should be present'
  //    'Expected @param tag for 'p1''

  /**
   * @param p1 The first number
   */
  @Deprecated
  private int m2(int p1) { return p1; }
  // violation 2 lines above '@return tag should be present'

  /** */
  void m3(int p1) {}
  // violation above, 'Expected @param tag for 'p1''
}
        

To configure the check for only public modifier, ignoring any missing param tags is:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocMethod">
      <property name="accessModifiers" value="public"/>
      <property name="allowMissingParamTags" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {

  /** */
  Example2(int x) {}

  /** */
  public int m1(int p1) { return p1; }
  // 1 violations above:
  //    '@return tag should be present'
  // OK, No missing param tag violation

  /**
   * @param p1 The first number
   */
  @Deprecated
  private int m2(int p1) { return p1; }
  // OK, only public methods are checked

  /** */
  void m3(int p1) {}

}
        

To configure the check for methods which are in private and package, but not any other modifier:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocMethod">
      <property name="accessModifiers" value="private, package"/>
    </module>
  </module>
</module>
        

Example:

public class Example3 {

  /** */
  Example3(int x) {}

  /** */
  public int m1(int p1) { return p1; }
  // OK, No missing param tag violation
  // OK, No missing @return tag violation
  // only private, package are checked

  /**
   * @param p1 The first number
   */
  @Deprecated
  private int m2(int p1) { return p1; }
  // OK, No missing @return tag violation

  /** */
  void m3(int p1) {}

}
        

To configure the check to ignore any missing return tags:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocMethod">
        <property name="allowMissingReturnTag" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example4 {

  /** */
  Example4(int x) {}
  // violation above, 'Expected @param tag for 'x''
  /** */
  public int m1(int p1) { return p1; }
  // 1 violations above:
  //    'Expected @param tag for 'p1''
  // OK, No missing @return tag violation

  /**
   * @param p1 The first number
   */
  @Deprecated
  private int m2(int p1) { return p1; }
  // OK, No missing @return tag violation

  /** */
  void m3(int p1) {}
  // violation above, 'Expected @param tag for 'p1''
}
        

To configure the check to ignore Methods with annotation Deprecated:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocMethod">
        <property name="allowedAnnotations" value="Deprecated"/>
    </module>
  </module>
</module>
        

Example:

public class Example5 {

  /** */
  Example5(int x) {}
  // violation above, 'Expected @param tag for 'x''
  /** */
  public int m1(int p1) { return p1; }
  // 2 violations above:
  //    '@return tag should be present'
  //    'Expected @param tag for 'p1''

  /**
   * @param p1 The first number
   */
  @Deprecated
  private int m2(int p1) { return p1; }
  // OK, No missing @return tag violation

  /** */
  void m3(int p1) {}
  // violation above, 'Expected @param tag for 'p1''
}
        

To configure the check only for tokens which are Constructor Definitions:

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocMethod">
        <property name="tokens" value="CTOR_DEF"/>
    </module>
  </module>
</module>
        

Example:

public class Example6 {

  /** */
  Example6(int x) {}
  // violation above, 'Expected @param tag for 'x''
  /** */
  public int m1(int p1) { return p1; }
  // OK, No missing param tag violation
  // OK, No missing @return tag violation
  // only constructors are checked

  /**
   * @param p1 The first number
   */
  @Deprecated
  private int m2(int p1) { return p1; }
  // OK, No missing @return tag violation

  /** */
  void m3(int p1) {}

}
        

To configure the check to validate throws tags, you can use following config.

<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocMethod">
      <property name="validateThrows" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example7 {

  /**
   * Actual exception thrown is child class of class that is declared in throws.
   * It is limitation of checkstyle (as checkstyle does not know type hierarchy).
   * Javadoc is valid not declaring FileNotFoundException
   * BUT checkstyle can not distinguish relationship between exceptions.
   * @param file some file
   * @throws IOException if some problem
   */
  public void doSomething8(File file) throws IOException {
    if (file == null) {
      // violation below, 'Expected @throws tag for 'FileNotFoundException''
      throw new FileNotFoundException();
    }
  }

  /**
   * Exact throw type referencing in javadoc even first is parent of second type.
   * It is a limitation of checkstyle (as checkstyle does not know type hierarchy).
   * This javadoc is valid for checkstyle and for javadoc tool.
   * @param file some file
   * @throws IOException if some problem
   * @throws FileNotFoundException if file is not found
   */
  public void doSomething9(File file) throws IOException {
    if (file == null) {
      throw new FileNotFoundException();
    }
  }

  /**
   * Ignore try block, but keep catch and finally blocks.
   *
   * @param s String to parse
   * @return A positive integer
   */
  public int parsePositiveInt(String s) {
    try {
      int value = Integer.parseInt(s);
      if (value <= 0) {
        throw new NumberFormatException(value + " is negative/zero"); // ok, try
      }
      return value;
    } catch (NumberFormatException ex) {
      // violation below, 'Expected @throws tag for 'IllegalArgumentException''
      throw new IllegalArgumentException("Invalid number", ex);
    } finally {
      // violation below, 'Expected @throws tag for 'IllegalStateException''
      throw new IllegalStateException("Should never reach here");
    }
  }

  /**
   * Try block without catch is not ignored.
   *
   * @return a String from standard input, if there is one
   */
  public String readLine() {
    try (Scanner sc = new Scanner(System.in)) {
      if (!sc.hasNext()) {
        // violation below, 'Expected @throws tag for 'IllegalStateException''
        throw new IllegalStateException("Empty input");
      }
      return sc.next();
    }
  }

  /**
   * Lambda expressions are ignored.
   *
   * @param s a String to be printed at some point in the future
   * @return a Runnable to be executed when the string is to be printed
   */
  public Runnable printLater(String s) {
    return () -> {
      if (s == null) {
        throw new NullPointerException();
      }
      System.out.println(s);
    };
  }
}
        

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