5

Is there a way to setup checkstyle to prevent leading, multiple or trailing empty lines in method bodies:

e.g.

private void a() {
-
int a = 1;
doStuff(a);
-
-
doMoreStuff(a);
-
}

In the example above, I have denoted empty lines with - characters.

I'd like to be able to to prevent the leading line, trailing line and more than one line in the method body.

2
  • Use an automatic code formatter. Commented Jan 5, 2018 at 17:50
  • 2
    The aim is to enforce this via checkstyle rules so that an automated build can validate the formatting and perhaps fail a CI build if someone were to forget to apply the formatter. Commented Jan 9, 2018 at 14:15

7 Answers 7

3

To prevent leading empty lines in method bodies, you can use:

<module name="RegexpMultiline">
    <property name="message" value="Blank line at start of method should be removed"/>
    <property name="format" value="\(.*\)\s*\{\s*\n\s*\n"/>
</module>

We can find method by parentheses.

Sign up to request clarification or add additional context in comments.

Comments

1

To enforce no blank lines at the beginning and end of any block, you can use multi-line regular expression checks:

<module name="RegexpMultiline">
    <property name="message" value="Blank line at start of block should be removed" />
    <property name="format" value="(?&lt;=\{\s{0,99}$)^$" />
    <property name="fileExtensions" value="groovy,java" />
</module>
<module name="RegexpMultiline">
    <property name="message" value="Blank line at end of block should be removed" />
    <property name="format" value="(?&lt;!\{\s{0,99}$)^$(?=^\s{0,99}\})" />
    <property name="fileExtensions" value="groovy,java" />
</module>

"^$" signifies the blank line.

3 Comments

Looks promising but seems to match all newlines, even before or after methods or field declarations.
Here is a more robust expression for the start of a block: <module name="RegexpMultiline"> <property name="message" value="A blank line at the start of a block should be removed."/> <property name="format" value="\{\s*[\r]?\n\s*[\r]?\n"/> <property name="fileExtensions" value="java"/> </module>
Here is a more robust expression for the end of a block: <module name="RegexpMultiline"> <property name="message" value="A blank line at the end of a block should be removed."/> <property name="format" value="[\r]?\n\s*[\r]?\n\s*\}"/> <property name="fileExtensions" value="java"/> </module>
0

To prevent multiple empty lines you can use the EmptyLineSeparator check. Its primary purpose is to ensure that there is an empty line between members in a file, but it also has a allowMultipleEmptyLines property which you can set to "false" to disallow them.

There is however currently a bug with the check that means it doesn't correctly detect multiple empty lines between methods where there is a comment (including JavaDoc) between the methods. I am working on a fix for this at the moment.

As for checking for new lines at the beginning or end of a block, I think the RegexpMultiline check would be the only option as mentioned in Pankaj's answer.

Comments

0

Can use it:

<module name="Regexp">
    <property name="message" value="Blank line at start of block is not allowed"/>
    <property name="format" value="\{\s*$^\s*$"/>
    <property name="ignoreComments" value="true"/>
    <property name="illegalPattern" value="true"/>
</module>
<module name="Regexp">
    <property name="message" value="Blank line at end of block is not allowed"/>
    <property name="format" value="^\s*$^\s*\}"/>
    <property name="ignoreComments" value="true"/>
    <property name="illegalPattern" value="true"/>
</module>

Comments

0

This is my suggestion for checking for blank lines at the beginning of method

<module name="RegexpMultiline">
    <property name="id" value="RegexpMultilineEmptyRowBeforeStartOfMethod"/>
    <property name="message" value="Blank line at start of method or block should be removed"/>
    <property name="format" value="\)(\s)*\{\r?\n(\s)*\n"/>
    <property name="fileExtensions" value="java"/>
</module>

Comments

0

check-tfij-style contains a checkstyle check to restrict any empty lines in methods or constructors. Just add maven/gradle dependency to checkstyle plugin:

plugins {
    java
    checkstyle
}

dependencies {
    checkstyle("pl.tfij:check-tfij-style:1.5.1")
}

Then add the check to your checkstyle config:

<module name="MethodEmptyLines"/>

Comments

0

The answers here discuss preventing leading and trailing lines at the beginning and ends of method blocks, but don't answer the part of the question that asks how to prevent multiple repeating empty lines (non-leading or trailing) within a method block.

To prevent that we can use:

<module name="EmptyLineSeparator">
  <property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/>
</module>

https://checkstyle.sourceforge.io/version/8.40/config_whitespace.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.