diff --git a/src/main/java/de/chw/MyCommentUtil.java b/src/main/java/de/chw/MyCommentUtil.java index 8677a138d051bd07f4c054a27e7a92de17092afc..2293e16797307d9f40bea783bc53ad7be4fa731c 100644 --- a/src/main/java/de/chw/MyCommentUtil.java +++ b/src/main/java/de/chw/MyCommentUtil.java @@ -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()) { diff --git a/src/main/java/de/chw/NoEmptyLineAfterMethodHeader.java b/src/main/java/de/chw/NoEmptyLineAfterMethodHeader.java index 06fab1ed9c008529a3bae0dafce4078538ba9c2d..391c7d60ad4ee5c1edd73a0df27f384f9607052f 100644 --- a/src/main/java/de/chw/NoEmptyLineAfterMethodHeader.java +++ b/src/main/java/de/chw/NoEmptyLineAfterMethodHeader.java @@ -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 diff --git a/src/test/java/de/chw/NoEmptyLineAfterMethodHeaderTest.java b/src/test/java/de/chw/NoEmptyLineAfterMethodHeaderTest.java index 6cc8bc5fa4225440b62a3f425a5dcd09d7133e27..cfdeacf251bbd6e97ebc1498fe4d255f9c47e604 100644 --- a/src/test/java/de/chw/NoEmptyLineAfterMethodHeaderTest.java +++ b/src/test/java/de/chw/NoEmptyLineAfterMethodHeaderTest.java @@ -55,6 +55,18 @@ public class NoEmptyLineAfterMethodHeaderTest { assertThat(ruleViolation.getClassName(), is(equalTo("EmptyLineAfterMethodHeader"))); 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)); } diff --git a/src/test/resources/EmptyLineAfterMethodHeader.txt b/src/test/resources/EmptyLineAfterMethodHeader.txt index 96679a26c4349ed9665625b65c58b144c49357af..3e8804de98e89127ee94bb421587ef1997d874f7 100644 --- a/src/test/resources/EmptyLineAfterMethodHeader.txt +++ b/src/test/resources/EmptyLineAfterMethodHeader.txt @@ -6,7 +6,7 @@ public class EmptyLineAfterMethodHeader { } public void emptyLine() throws Exception { - + System.out.println(""); } @@ -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