Skip to content

Commit

Permalink
Issue checkstyle#3908: SummaryJavadoc need special processing of inhe…
Browse files Browse the repository at this point in the history
…ritDoc tag
  • Loading branch information
sagarshah94 committed Mar 18, 2017
1 parent 1b5a154 commit 1a7dd41
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
Expand Up @@ -78,7 +78,6 @@ public class SummaryJavadocCheck extends AbstractJavadocCheck {
*/
private static final Pattern JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN =
Pattern.compile("\n[ ]+(\\*)|^[ ]+(\\*)");

/** Period literal. */
private static final String PERIOD = ".";

Expand Down Expand Up @@ -135,7 +134,7 @@ public void visitJavadocToken(DetailNode ast) {
String firstSentence = getFirstSentence(ast);
final int endOfSentence = firstSentence.lastIndexOf(period);
if (endOfSentence == -1) {
if (!firstSentence.trim().startsWith("{@inheritDoc}")) {
if (!isValidInheritDoc(ast)) {
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
}
}
Expand All @@ -147,6 +146,36 @@ public void visitJavadocToken(DetailNode ast) {
}
}

/**
* Finds if inheritDoc is placed properly in java doc.
* @param ast Javadoc root node.
* @return true if inheritDoc is valid or fasle.
*/
private static boolean isValidInheritDoc(DetailNode ast) {
boolean result = true;
for (DetailNode child : ast.getChildren()) {
if (child.getType() == JavadocTokenTypes.TEXT) {
if (!child.getText().trim().isEmpty()) {
result = false;
}
}
else if (child.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG) {
if (child.getChildren()[1].getType() != JavadocTokenTypes.INHERIT_DOC_LITERAL) {
result = false;
}
}
else if (child.getType() != JavadocTokenTypes.NEWLINE
&& child.getType() != JavadocTokenTypes.LEADING_ASTERISK
&& child.getType() != JavadocTokenTypes.EOF) {
result = false;
}
if (!result) {
break;
}
}
return result;
}

/**
* Finds and returns first sentence.
* @param ast Javadoc root node.
Expand Down
Expand Up @@ -76,6 +76,8 @@ public void testIncorrect() throws Exception {
"69: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"83: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
"103: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"116: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"121: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
};
verify(checkConfig, getPath("InputIncorrectSummaryJavaDoc.java"), expected);
}
Expand Down Expand Up @@ -106,6 +108,8 @@ public void testDefaultConfiguration() throws Exception {
"37: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"69: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"103: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"116: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"121: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
};

createChecker(checkConfig);
Expand Down
Expand Up @@ -32,6 +32,22 @@ void foo6() {}
/** {@inheritDoc} */
void foo7() {}

/**
* {@inheritDoc} */
void foo8() {}

/**
* {@inheritDoc}
*/
void foo9() {}

/**
*
*
* {@inheritDoc}
*/
void foo10() {}

/**
* <a href="mailto:vlad@htmlbook.ru"/>
*/
Expand Down
Expand Up @@ -112,5 +112,15 @@ void foo5() {}
* An especially short bit of Javadoc.
*/
void foo6() {}

/**
* mm{@inheritDoc}
*/
void foo7() {}

/**
* {@link #setBounds(int,int,int,int)}
*/
void foo8() {}
};
}

0 comments on commit 1a7dd41

Please sign in to comment.