Permalink
Browse files

Issue #3908: SummaryJavadoc need special processing of inheritDoc tag

  • Loading branch information...
sagarshah94 committed Mar 5, 2017
1 parent f26c614 commit 5656075cc6d822f7ed81a44c42851ed6b16a5f38
@@ -19,6 +19,7 @@
package com.puppycrawl.tools.checkstyle.checks.javadoc;
import java.util.Objects;
import java.util.regex.Pattern;
import com.google.common.base.CharMatcher;
@@ -77,10 +78,16 @@
* This regexp is used to convert multiline javadoc to single line without stars.
*/
private static final Pattern JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN =
Pattern.compile("\n[ ]+(\\*)|^[ ]+(\\*)");
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.
@@ -135,7 +142,10 @@ 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)
&& !isValidInheritDocAfterRemovingAsteric(firstSentence)) {
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
}
}
@@ -147,6 +157,32 @@ public void visitJavadocToken(DetailNode ast) {
}
}
/**
* Finds if inheritDoc is placed properly in java doc.
* @param firstSentence first sentence of java doc.
* @return true if inheritDoc is valid or fasle.
*/
private static boolean isValidInheritDocAfterRemovingAsteric(String firstSentence) {
final int begin = firstSentence.indexOf(INHERIT_DOC_LITERAL);
final int end = firstSentence.indexOf("<EOF>");
boolean result = false;
if (begin == -1) {
result = false;
}
final StringBuilder innerContent = new StringBuilder();
for (int index = 0; index < end; index++) {
if (firstSentence.charAt(index) != '*' && firstSentence.charAt(index) != ' '
&& firstSentence.charAt(index) != '\n') {
innerContent.append(firstSentence.charAt(index));
}
}
if (Objects.equals(innerContent.toString().trim(), INHERIT_DOC_LITERAL)) {
result = true;
}
return result;
}
/**
* Finds and returns first sentence.
* @param ast Javadoc root node.
@@ -177,7 +213,7 @@ private static String getFirstSentence(DetailNode ast) {
*/
private boolean containsForbiddenFragment(String firstSentence) {
String javadocText = JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN
.matcher(firstSentence).replaceAll(" ");
.matcher(firstSentence).replaceAll(" ");
javadocText = CharMatcher.WHITESPACE.trimAndCollapseFrom(javadocText, ' ');
return forbiddenSummaryFragments.matcher(javadocText).find();
}
@@ -76,6 +76,7 @@ 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),
};
verify(checkConfig, getPath("InputIncorrectSummaryJavaDoc.java"), expected);
}
@@ -106,6 +107,7 @@ 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),
};
createChecker(checkConfig);
@@ -32,6 +32,22 @@ void foo6() {}
/** {@inheritDoc} */
void foo7() {}
/**
* {@inheritDoc} */
void foo8() {}
/**
* {@inheritDoc}
*/
void foo9() {}
/**
*
*
* {@inheritDoc}
*/
void foo10() {}
/**
* <a href="mailto:vlad@htmlbook.ru"/>
*/
@@ -112,5 +112,10 @@ void foo5() {}
* An especially short bit of Javadoc.
*/
void foo6() {}
/**
* mm{@inheritDoc}
*/
void foo7() {}
};
}

0 comments on commit 5656075

Please sign in to comment.