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 o开发者_StackOverflow社区ut 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.
You might try checkstyle to enforce such conventions.
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.
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.
精彩评论