Skip to content
Snippets Groups Projects
Commit 6ee9daca authored by Christian Wulf's avatar Christian Wulf
Browse files

added ctors to NoEmptyLineAfterMethodHeader

parent f6e51729
No related branches found
No related tags found
No related merge requests found
Pipeline #353 failed
......@@ -17,6 +17,7 @@ package de.chw;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode;
import net.sourceforge.pmd.lang.java.ast.Comment;
final class MyCommentUtil {
......@@ -38,7 +39,7 @@ final class MyCommentUtil {
return null;
}
public static boolean isCommentInLine(final ASTMethodDeclaration node, final int lineNumber) {
public static boolean isCommentInLine(final AbstractJavaAccessNode node, final int lineNumber) {
ASTCompilationUnit compilationUnit = node.getParentsOfType(ASTCompilationUnit.class).get(0);
for (Comment comment : compilationUnit.getComments()) {
if (comment.getBeginLine() <= lineNumber && lineNumber <= comment.getEndLine()) {
......
......@@ -17,7 +17,10 @@ package de.chw;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.Comment;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
......@@ -26,6 +29,7 @@ public class NoEmptyLineAfterMethodHeader extends AbstractJavaRule {
public NoEmptyLineAfterMethodHeader() {
addRuleChainVisit(ASTMethodDeclaration.class);
addRuleChainVisit(ASTConstructorDeclaration.class);
}
@Override
......@@ -37,7 +41,7 @@ public class NoEmptyLineAfterMethodHeader extends AbstractJavaRule {
int lineDiff = firstBlockStatement.getBeginLine() - block.getBeginLine();
boolean commentInLine = MyCommentUtil.isCommentInLine(node, block.getBeginLine() + 1);
if (lineDiff > 1 && !commentInLine) {
String fullQualifiedMethodName = node.getMethodName();
String fullQualifiedMethodName = node.getName();
addViolation(data, node, fullQualifiedMethodName);
}
}
......@@ -45,6 +49,30 @@ public class NoEmptyLineAfterMethodHeader extends AbstractJavaRule {
return super.visit(node, data);
}
@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
ASTFormalParameters formalParameters = node.getFirstChildOfType(ASTFormalParameters.class);
ASTBlockStatement blockStatement = node.getFirstChildOfType(ASTBlockStatement.class);
if (blockStatement != null) {
int lineDiff = blockStatement.getBeginLine() - formalParameters.getEndLine();
boolean commentInLine = MyCommentUtil.isCommentInLine(node, blockStatement.getBeginLine() + 1);
if (lineDiff > 1 && !commentInLine) {
String fullQualifiedMethodName = node.getImage();
addViolation(data, node, fullQualifiedMethodName);
}
} else {
int lineDiff = node.getEndLine() - formalParameters.getEndLine();
boolean commentInLine = MyCommentUtil.isCommentInLine(node, formalParameters.getEndLine() + 1);
if (lineDiff > 1 && !commentInLine) {
String fullQualifiedMethodName = node.getImage();
addViolation(data, node, fullQualifiedMethodName);
}
}
return super.visit(node, data);
}
/*
* We copied this method from MyCommentUtil since MyCommentUtil is not loaded by
* the class loader from our PMD Eclipse plugin. As long as our
......
......@@ -56,6 +56,18 @@ public class NoEmptyLineAfterMethodHeaderTest {
assertThat(ruleViolation.getMethodName(), is(equalTo("twoEmptyLines")));
assertThat(ruleViolation.getBeginLine(), is(13));
ruleViolation = iter.next();
assertThat(ruleViolation.getRule(), is(instanceOf(NoEmptyLineAfterMethodHeader.class)));
assertThat(ruleViolation.getClassName(), is(equalTo("EmptyLineAfterMethodHeader")));
assertThat(ruleViolation.getMethodName(), is(equalTo("EmptyLineAfterMethodHeader")));
assertThat(ruleViolation.getBeginLine(), is(23));
ruleViolation = iter.next();
assertThat(ruleViolation.getRule(), is(instanceOf(NoEmptyLineAfterMethodHeader.class)));
assertThat(ruleViolation.getClassName(), is(equalTo("EmptyLineAfterMethodHeader")));
assertThat(ruleViolation.getMethodName(), is(equalTo("EmptyLineAfterMethodHeader")));
assertThat(ruleViolation.getBeginLine(), is(27));
assertThat(iter.hasNext(), is(false));
}
......
......@@ -20,4 +20,16 @@ public class EmptyLineAfterMethodHeader {
System.out.println("");
}
public EmptyLineAfterMethodHeader() {
}
public EmptyLineAfterMethodHeader(Integer value) {
System.out.println("");
}
public EmptyLineAfterMethodHeader(String value) {
System.out.println("");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment