MissingJavadocMethod: false-positive on getters containing block comments when allowMissingPropertyJavadoc is enabled
I have read check documentation: https://checkstyle.org/checks/javadoc/missingjavadocmethod.html I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
/var/tmp $ javac Test.java
/var/tmp $ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="MissingJavadocMethod">
<property name="allowMissingPropertyJavadoc" value="true"/>
</module>
</module>
</module>
/var/tmp $ cat Test.java
public class Test {
private int x;
// Line comment: PASSED (no violation reported)
public int getFoo() {
// line comment
return x;
}
// Block comment: FALSE POSITIVE violation reported!
public int getBar() {
/* block comment */
return x;
}
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-14.2.0-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /var/tmp/Test.java:12:5: Missing a Javadoc comment for 'getBar'. [MissingJavadocMethod]
Audit done.
Checkstyle ends with 1 errors.Describe what you expect in detail.
When allowMissingPropertyJavadoc is set to true, getters and setters are allowed to omit Javadoc comments.
getFoo() and getBar() are both standard getter methods. Checkstyle correctly recognizes getFoo() (which contains a single-line // comment) as a getter, but fails on getBar() (which contains a /* ... */ block comment), emitting a false-positive violation.
Block comments should be skipped just like single-line comments when identifying property accessors.
Root Cause
In MissingJavadocMethodCheck.java:
In
isGetterMethod():DetailAST expr = slist.getFirstChild(); while (expr.getType() == TokenTypes.SINGLE_LINE_COMMENT) { expr = expr.getNextSibling(); } getterMethod = expr.getType() == TokenTypes.LITERAL_RETURN;Only
SINGLE_LINE_COMMENTis skipped in the while-loop;BLOCK_COMMENT_BEGINis not handled.In
getChildCount():while (child != null) { if (child.getType() != TokenTypes.SINGLE_LINE_COMMENT) { childCount += 1; } child = child.getNextSibling(); }Block comments are counted towards
childCount, which can causeisSetterMethod()andisGetterMethod()to reject valid accessors when block comments are present.
Source: checkstyle/checkstyle