Permalink
Browse files

Issue #3908: SummaryJavadoc need special processing of inheritDoc tag

  • Loading branch information...
sagarshah94 committed Mar 18, 2017
1 parent 1b5a154 commit 9b82f93078710e7109f7d160d681e1360bca1698
@@ -19,6 +19,9 @@
package com.puppycrawl.tools.checkstyle.checks.javadoc;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.base.CharMatcher;
@@ -92,6 +95,13 @@
*/
private String period = PERIOD;
/**
* Stores allowed values in document for inherit doc literal.
*/
private static final Set<Integer> ALLOWED_TYPES = new HashSet<>
(Arrays.asList(JavadocTokenTypes.NEWLINE,
JavadocTokenTypes.LEADING_ASTERISK,
JavadocTokenTypes.EOF));
/**
* Sets custom value of regular expression for forbidden summary fragments.
* @param pattern a pattern.
@@ -135,7 +145,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 (!isOnlyInheritDoc(ast)) {
log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
}
}
@@ -147,6 +157,38 @@ 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 false.
*/
private static boolean isOnlyInheritDoc(DetailNode ast) {
boolean extraTextFound = false;
boolean containsInheritDoc = false;
for (DetailNode child : ast.getChildren()) {
if (child.getType() == JavadocTokenTypes.TEXT) {
if (!child.getText().trim().isEmpty()) {
extraTextFound = true;
}
}
else if (child.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG) {
if (child.getChildren()[1].getType() == JavadocTokenTypes.INHERIT_DOC_LITERAL) {
containsInheritDoc = true;
}
else {
extraTextFound = true;
}
}
else if (!ALLOWED_TYPES.contains(child.getType())) {
extraTextFound = true;
}
if (extraTextFound) {
break;
}
}
return containsInheritDoc && !extraTextFound;
}
/**
* Finds and returns first sentence.
* @param ast Javadoc root node.
@@ -76,6 +76,10 @@ 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),
"126: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"131: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
};
verify(checkConfig, getPath("InputIncorrectSummaryJavaDoc.java"), expected);
}
@@ -106,6 +110,10 @@ 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),
"126: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
"131: " + 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,25 @@ void foo5() {}
* An especially short bit of Javadoc.
*/
void foo6() {}
/**
* mm{@inheritDoc}
*/
void foo7() {}
/**
* {@inheritDoc}mm
*/
void foo9() {}
/**
* {@link #setBounds(int,int,int,int)}
*/
void foo8() {}
/**
*
*/
void foo10() {}
};
}

0 comments on commit 9b82f93

Please sign in to comment.