diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/SummaryJavadocCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/SummaryJavadocCheck.java index 4e5b77586a32..eae2c349db34 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/SummaryJavadocCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/SummaryJavadocCheck.java @@ -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. @@ -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); @@ -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. diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputCorrectSummaryJavaDoc.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputCorrectSummaryJavaDoc.java index 0c5a130e7601..6ec07423e10d 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputCorrectSummaryJavaDoc.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/InputCorrectSummaryJavaDoc.java @@ -32,6 +32,15 @@ void foo6() {} /** {@inheritDoc} */ void foo7() {} + /** + * {@inheritDoc} */ + void foo8() {} + + /** + * {@inheritDoc} + */ + void foo9() {} + /** * */