Skip to content

Commit

Permalink
#656 Prohibit empty line at the start and at the end of javadoc: fix …
Browse files Browse the repository at this point in the history
…code review remarks part2
  • Loading branch information
dskalenko committed Feb 12, 2016
1 parent 3cb5f02 commit 7d85e5d
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
* Check for empty line at the start and at the end of javadoc.
* Check for empty line at the start and at the end of Javadoc.
* @author Denys Skalenko (d.skalenko@gmail.com)
* @version $Id$
* @since 0.17
Expand All @@ -46,6 +46,8 @@ public int[] getDefaultTokens() {
return new int[] {
TokenTypes.CLASS_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.ENUM_CONSTANT_DEF,
TokenTypes.VARIABLE_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.METHOD_DEF,
Expand All @@ -58,7 +60,7 @@ public void visitToken(final DetailAST ast) {
final int start = ast.getLineNo();
final int cstart =
JavadocEmptyLineCheck.findCommentStart(lines, start) + 1;
if (JavadocEmptyLineCheck.isNodeHasJavaDoc(ast, cstart)) {
if (JavadocEmptyLineCheck.isNodeHavingJavaDoc(ast, cstart)) {
if (JavadocEmptyLineCheck.isJavadocLineEmpty(lines[cstart])) {
this.log(cstart + 1, "Empty javadoc line at the beginning");
}
Expand All @@ -71,32 +73,40 @@ public void visitToken(final DetailAST ast) {
}

/**
* Check if javadoc Line Empty.
* @param line Java doc line
* @return Is Javadoc Line Empty
* Check if Javadoc Line Empty.
* @param line Javadoc line
* @return True when Javadoc Line is Empty
*/
private static boolean isJavadocLineEmpty(final String line) {
return null != line && "*".equals(line.trim());
}

/**
* Check if node has java doc.
* @param node Node to be checked for Java docs.
* Check if node has Javadoc.
* @param node Node to be checked for Javadoc.
* @param start Line number where comment starts.
* @return Is node has java doc
* @return True when node has Javadoc
*/
private static boolean isNodeHasJavaDoc(final DetailAST node,
private static boolean isNodeHavingJavaDoc(final DetailAST node,
final int start) {
int cprevious = 0;
return start > getLineNoOfPreviousNode(node);
}

/**
* Returns line number of previous node
* @param node - Current node
* @return Line number of previous node
*/
private static int getLineNoOfPreviousNode(final DetailAST node){
int start = 0;
final DetailAST previous = node.getPreviousSibling();
if (null != previous) {
cprevious = previous.getLineNo();
start = previous.getLineNo();
}
return start > cprevious;
return start;
}

/**
* Find javadoc starting comment.
* Find Javadoc starting comment.
* @param lines List of lines to check.
* @param start Start searching from this line number.
* @return Line number with found starting comment or -1 otherwise.
Expand All @@ -106,8 +116,8 @@ private static int findCommentStart(final String[] lines, final int start) {
}

/**
* Find javadoc ending comment.
* @param lines List of lines to check.
* Find Javadoc ending comment.
* @param lines Array of lines to check.
* @param start Start searching from this line number.
* @return Line number with found ending comment, or -1 if it wasn't found.
*/
Expand All @@ -117,7 +127,7 @@ private static int findCommentEnd(final String[] lines, final int start) {

/**
* Find a text in lines, by going up.
* @param lines List of lines to check.
* @param lines Array of lines to check.
* @param start Start searching from this line number.
* @param text Text to find.
* @return Line number with found text, or -1 if it wasn't found.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
*
* This is not a real Java class. It won't be compiled ever. It is used
* only as a text resource in integration.ChecksIT.
*/
public final class Invalid {

/**
*
* Empty javadoc line at the beginning.
*/
private static final int x = 0;

/**
*
* Empty javadoc line at the beginning.
*/
private int y = 0;

/**
* Empty javadoc line at the end.
*
*/
private int z = 0;

/**
*
* Empty javadoc line at the beginning.
*/
public Invalid() {}

/**
*
* Empty javadoc line
* @param param - param.
* @return Param.
*
*/
public String method(final String param) {
return param;
}

/**
*
* Empty javadoc line at the beginning.
*/
public void method2() {
}

/**
*
* Empty javadoc line at the beginning.
*/

public void method3() {
}

private static class InnerInvalid {

/**
* Empty javadoc line at the end.
*
*/
private final int x;

/**
*
* Empty javadoc line at the beginning.
* @param x - x
*/
public InnerInvalid(int x) {
this.x = x;
}

/**
*
* Empty javadoc line at the beginning.
*/
public void method() {
}
}
}

/**
* This is not a real interface class. It won't be compiled ever. It is used
* only as a text resource in integration.ChecksIT.
*
*/
interface I1 {

/**
*
* Empty javadoc line at the beginning
*/
void method();

/**
*
* Integer variable.
*/
int x = 0;

}

/**
*
* Empty javadoc line.
*
*/
enum InvalidEnum{

/**
*
* Empty javadoc line at the beginning.
*/
INVALID_ENUM("someValue");

/**
* Empty javadoc line at the end.
*
*/
private final String name;


/**
* Empty javadoc line at the end.
*
*/
InvalidEnum(final String name) {
this.name = name;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ public final class Valid {
public Valid() {}

/**
*
* There is no empty Javadoc line
* @param param - param.
* @return Param.
*
*/
public String method(final String param) {
return param;
Expand All @@ -50,6 +48,13 @@ public void method2() {
public void method3() {
}

/** Some text
*
*/
public void method4() {

}

private static class InnerValid {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
2:Empty javadoc line at the beginning
12:Empty javadoc line at the beginning
19:Empty javadoc line at the end
24:Empty javadoc line at the beginning
31:Empty javadoc line at the end
37:Empty javadoc line at the beginning
9:Empty javadoc line at the beginning
15:Empty javadoc line at the beginning
22:Empty javadoc line at the end
27:Empty javadoc line at the beginning
33:Empty javadoc line at the beginning
37:Empty javadoc line at the end
44:Empty javadoc line at the beginning
56:Empty javadoc line at the end
63:Empty javadoc line at the beginning
51:Empty javadoc line at the beginning
62:Empty javadoc line at the end
67:Empty javadoc line at the beginning
76:Empty javadoc line at the beginning
87:Empty javadoc line at the end
92:Empty javadoc line at the beginning
98:Empty javadoc line at the beginning
106:Empty javadoc line at the beginning
108:Empty javadoc line at the end
113:Empty javadoc line at the beginning
120:Empty javadoc line at the end
127:Empty javadoc line at the end

0 comments on commit 7d85e5d

Please sign in to comment.