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 7, 2017
1 parent f26c614 commit cb31885
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Expand Up @@ -78,9 +78,15 @@ public class SummaryJavadocCheck extends AbstractJavadocCheck {
*/
private static final Pattern JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN =
Pattern.compile("\n[ ]+(\\*)|^[ ]+(\\*)");

/**
* This regexp is used to find occurence of first character of asteric.
*/
private static final Pattern FIRST_CHARACTER_ASTERIC_PATTERN =
Pattern.compile("^\\*");
/** Period literal. */
private static final String PERIOD = ".";
/** Inherit doc literal. */
private static final String INHERIT_DOC_LITERAL = "{@inheritDoc}";

/**
* Regular expression for forbidden summary fragments.
Expand Down Expand Up @@ -135,9 +141,12 @@ public void visitJavadocToken(DetailNode ast) {
String firstSentence = getFirstSentence(ast);
final int endOfSentence = firstSentence.lastIndexOf(period);
if (endOfSentence == -1) {
if (!firstSentence.trim().startsWith("{@inheritDoc}")) {
if (!firstSentence.trim().startsWith(INHERIT_DOC_LITERAL)
&& !FIRST_CHARACTER_ASTERIC_PATTERN.matcher(firstSentence.trim())
.replaceFirst("").trim().startsWith(INHERIT_DOC_LITERAL)
&& !isValidAInheritDocAfterRemovingAsteric(firstSentence)) {
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
}
}
}
else {
firstSentence = firstSentence.substring(0, endOfSentence);
Expand All @@ -147,6 +156,31 @@ public void visitJavadocToken(DetailNode ast) {
}
}

/**
* Removes asteric from start and end of sentence and determines sentence contains {@inheritDoc}
* @param firstSentence -first sentence of comment
* @return true if valid and false if invalid content in sentence.
*/
private boolean isValidAInheritDocAfterRemovingAsteric(String firstSentence) {
int begin = firstSentence.indexOf(INHERIT_DOC_LITERAL);
boolean result =true;
if(begin == -1) {
result = false;
}
StringBuilder innerContent = new StringBuilder();
for (int index = 0 ; index <= begin && result ; index++) {
if(firstSentence.charAt(index) != '*' || firstSentence.charAt(index) != ' '
|| firstSentence.charAt(index) != '\n') {
innerContent.append(firstSentence.charAt(index));
}
}
if(innerContent.equals(INHERIT_DOC_LITERAL)) {
result = true;
}

return result;
}

/**
* Finds and returns first sentence.
* @param ast Javadoc root node.
Expand Down
Expand Up @@ -32,6 +32,15 @@ void foo6() {}
/** {@inheritDoc} */
void foo7() {}

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

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

/**
* <a href="mailto:vlad@htmlbook.ru"/>
*/
Expand Down

0 comments on commit cb31885

Please sign in to comment.