Michael Berry
Michael Berry

Reputation: 72294

Javadoc warn on no comment

Is there a way (preferably via an argument, taglet, doclet or similar) to get Javadoc to generate a warning if there is no javadoc comment provided for a method or class? I've had a scout around at the options and Googled but can't see anything that stands out as being relevant. I'm currently working on a project where everything needs to have some form of Javadoc comment and this would be really useful for that purpose.

EDIT: I know such things can be enforced via code quality tools such as checkstyle, I was just wondering if there was a way to configure Javadoc to warn about different things such as this.

Upvotes: 5

Views: 11591

Answers (4)

Archie
Archie

Reputation: 5421

Newer JDK's support an explicit -Xdoclint:missing flag for this.

E.g. in JDK 17:

$ javadoc -X
...
    -Xdoclint:(all|none|[-]<group>)
                  Enable or disable specific checks for problems in javadoc
                  comments, where <group> is one of accessibility, html,
                  missing, reference, or syntax.

Note the missing option in there.

Upvotes: 2

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74760

If you really wanted to use Javadoc, a custom checking doclet would be the way to go.

Here is an example:

package de.fencing_game.paul.examples.doclet;

import com.sun.javadoc.*;

public class CheckingDoclet extends Doclet {

    private static void checkElement(ProgramElementDoc ped,
                                     DocErrorReporter err) {
        if(ped.commentText().equals("")) {
            err.printError(ped.position(), ped + " has no documentation!");
        }
    }

    private static void checkAll(ProgramElementDoc[] array,
                                 DocErrorReporter err) {
        for(ProgramElementDoc ped : array) {
           checkElement(ped, err);
        }
    }

    public static boolean start(RootDoc root) {
        for(ClassDoc clazz : root.classes()) {
           checkElement(clazz, root);
           checkAll(clazz.constructors(), root);
           checkAll(clazz.fields(), root);
           checkAll(clazz.enumConstants(), root);
           checkAll(clazz.methods(), root);
        }
        return true;
    }
}

Running the doclet on itself (with ant) gives this output:

doccheck.doclet:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source files for package de.fencing_game.paul.examples.doclet...
  [javadoc] Constructing Javadoc information...
  [javadoc] de/fencing_game/paul/examples/doclet/CheckingDoclet.java:7: error - de.fencing_game.paul.examples.doclet.CheckingDoclet has no documentation!
  [javadoc] de/fencing_game/paul/examples/doclet/CheckingDoclet.java:7: error - de.fencing_game.paul.examples.doclet.CheckingDoclet() has no documentation!
  [javadoc] de/fencing_game/paul/examples/doclet/CheckingDoclet.java:9: error - de.fencing_game.paul.examples.doclet.CheckingDoclet.checkElement(com.sun.javadoc.ProgramElementDoc, com.sun.javadoc.DocErrorReporter) has no documentation!
  [javadoc] de/fencing_game/paul/examples/doclet/CheckingDoclet.java:16: error - de.fencing_game.paul.examples.doclet.CheckingDoclet.checkAll(com.sun.javadoc.ProgramElementDoc[], com.sun.javadoc.DocErrorReporter) has no documentation!
  [javadoc] de/fencing_game/paul/examples/doclet/CheckingDoclet.java:23: error - de.fencing_game.paul.examples.doclet.CheckingDoclet.start(com.sun.javadoc.RootDoc) has no documentation!
  [javadoc] 5 errors

BUILD SUCCESSFUL
Total time: 2 seconds

If we want this to be not successful whenever one error is found, we should return false from the start-method in this case.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533530

This task is best done with a code analysis tool like PMD or FindBug (possibly check style) as these are designed find these sort of issues and much more.

IntelliJ has a builtin checker which can help populate missing javadoc content as well as sanity check/spell check it.

Upvotes: 1

Thomas
Thomas

Reputation: 88707

You might try checkstyle to enforce such conventions.

Upvotes: 9

Related Questions