Javadoc warns of any comments

Is there a way (preferably with an argument, taglet, doclet or similar) to force Javadoc to generate a warning if there is no javadoc comment for the method or class? I had a scout for options and Googled, but I donโ€™t see anything that stands out as relevant. I'm currently working on a project where everything should have some form of Javadoc comment, and that would be really useful for this purpose.

EDIT: I know that such things can be applied with code quality tools like checkstyle, I'm just wondering if there was a way to set up Javadoc to warn about things like this.

+4
source share
3 answers

If you really wanted to use Javadoc, you could choose the appropriate verification document.

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 fail when one error is detected, then we must return false from the start method.

+2
source

You can try checkstyle to enforce such agreements.

+7
source

This task is best accomplished with a code analysis tool such as PMD or FindBug (possibly for style checking), as they are designed to find such problems and more.

IntelliJ has a built-in check that can help fill in the missing javadoc content, as well as check spell checking.

+1
source

Source: https://habr.com/ru/post/1342967/


All Articles