Skip to content

Commit

Permalink
Issue #3426: remove warning on PACKAGE_DEF predicted by javadoc not s…
Browse files Browse the repository at this point in the history
…eparated by line
  • Loading branch information
kazachka committed Nov 12, 2016
1 parent 548ecd7 commit 92224dc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Expand Up @@ -404,7 +404,7 @@ && hasNotAllowedTwoEmptyLinesBefore(ast)) {
* @param nextToken next token
*/
private void processPackage(DetailAST ast, DetailAST nextToken) {
if (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)) {
if (ast.getLineNo() > 1 && !(hasEmptyLineBefore(ast) || isPredictedByJavadoc(ast))) {
log(ast.getLineNo(), MSG_SHOULD_BE_SEPARATED, ast.getText());
}
if (!hasEmptyLineAfter(ast)) {
Expand Down Expand Up @@ -533,6 +533,34 @@ private boolean hasEmptyLineBefore(DetailAST token) {
return lineBefore.trim().isEmpty();
}

/**
* Check if token is predicted by javadoc comment
* @param token token.
* @return true, if token is predicted by javadoc comment.
*/
private boolean isPredictedByJavadoc(DetailAST token) {
final int lineNo = token.getLineNo();
boolean result = false;

if (lineNo != 1) {
// [lineNo - 2] is the number of the previous line because the numbering starts
// from zero.
String lineBefore;
int lineIndex = 2;
do {
lineBefore = getLines()[lineNo - lineIndex];
if (lineBefore.trim().startsWith("/**")) {
result = true;
}
else if (!lineBefore.trim().startsWith("*")) {
break;
}
lineIndex++;
} while (lineNo - lineIndex >= 0);
}
return result;
}

/**
* If variable definition is a type field.
* @param variableDef variable definition.
Expand Down
Expand Up @@ -54,15 +54,15 @@ public void testDefault() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(EmptyLineSeparatorCheck.class);

final String[] expected = {
"21: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
"35: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
"38: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
"39: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
"43: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
"57: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
"62: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"79: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"110: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
"25: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
"39: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
"42: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
"43: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
"47: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
"61: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
"66: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"83: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"114: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
};
verify(checkConfig, getPath("InputEmptyLineSeparator.java"), expected);
}
Expand All @@ -74,14 +74,14 @@ public void testAllowNoEmptyLineBetweenFields() throws Exception {
checkConfig.addAttribute("allowNoEmptyLineBetweenFields", "true");

final String[] expected = {
"21: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
"35: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
"39: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
"43: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
"57: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
"62: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"79: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"110: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
"25: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
"39: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
"43: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
"47: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
"61: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
"66: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"83: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
"114: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
};
verify(checkConfig, getPath("InputEmptyLineSeparator.java"), expected);
}
Expand Down
Expand Up @@ -17,6 +17,10 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

/**
* Some javadoc before PACKAGE_DEF to test that there isn't warning
* when javadoc and package definition aren't separate with line.
*/
package com.puppycrawl.tools.checkstyle.checks.whitespace;
import java.io.Serializable;
import java.util.ArrayList;
Expand Down

0 comments on commit 92224dc

Please sign in to comment.