Checkstyle rule to enforce empty line separation for class sections











up vote
1
down vote

favorite












From time to time I see on code reviews such definitions



public class EntityConverter {
private static final int FIRST_VERSION = 1;
@Autowired
private EntityRepository repository;

public void someMethod() {
...
}
...
}


and/or



public class EntityConverter {
private static final int FIRST_VERSION = 1;

@Autowired
private EntityRepository repository;
public void someMethod() {
...
}
...
}


How to enforce guys to add 1 empty line between class sections (between constants, variables, methods, etc.)?










share|improve this question
























  • what is ur IDE?
    – Salman Lashkarara
    Oct 31 '17 at 16:47










  • @Salman it doesn't matter. This check has to be at CI server.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:48










  • It is a clean code measurement. Why a CI server should be able to detect it? Do you have any solution to define it for a CI?
    – Salman Lashkarara
    Oct 31 '17 at 16:50










  • @Salman cause we have Checkstyle checks like GitHub PR code style pre-checks at CI. All PRs must pass Checkstyle rules successfully before be merged into the main brunch.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:53















up vote
1
down vote

favorite












From time to time I see on code reviews such definitions



public class EntityConverter {
private static final int FIRST_VERSION = 1;
@Autowired
private EntityRepository repository;

public void someMethod() {
...
}
...
}


and/or



public class EntityConverter {
private static final int FIRST_VERSION = 1;

@Autowired
private EntityRepository repository;
public void someMethod() {
...
}
...
}


How to enforce guys to add 1 empty line between class sections (between constants, variables, methods, etc.)?










share|improve this question
























  • what is ur IDE?
    – Salman Lashkarara
    Oct 31 '17 at 16:47










  • @Salman it doesn't matter. This check has to be at CI server.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:48










  • It is a clean code measurement. Why a CI server should be able to detect it? Do you have any solution to define it for a CI?
    – Salman Lashkarara
    Oct 31 '17 at 16:50










  • @Salman cause we have Checkstyle checks like GitHub PR code style pre-checks at CI. All PRs must pass Checkstyle rules successfully before be merged into the main brunch.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:53













up vote
1
down vote

favorite









up vote
1
down vote

favorite











From time to time I see on code reviews such definitions



public class EntityConverter {
private static final int FIRST_VERSION = 1;
@Autowired
private EntityRepository repository;

public void someMethod() {
...
}
...
}


and/or



public class EntityConverter {
private static final int FIRST_VERSION = 1;

@Autowired
private EntityRepository repository;
public void someMethod() {
...
}
...
}


How to enforce guys to add 1 empty line between class sections (between constants, variables, methods, etc.)?










share|improve this question















From time to time I see on code reviews such definitions



public class EntityConverter {
private static final int FIRST_VERSION = 1;
@Autowired
private EntityRepository repository;

public void someMethod() {
...
}
...
}


and/or



public class EntityConverter {
private static final int FIRST_VERSION = 1;

@Autowired
private EntityRepository repository;
public void someMethod() {
...
}
...
}


How to enforce guys to add 1 empty line between class sections (between constants, variables, methods, etc.)?







java checkstyle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 31 '17 at 18:34

























asked Oct 31 '17 at 16:18









Andriy Kryvtsun

1,7091123




1,7091123












  • what is ur IDE?
    – Salman Lashkarara
    Oct 31 '17 at 16:47










  • @Salman it doesn't matter. This check has to be at CI server.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:48










  • It is a clean code measurement. Why a CI server should be able to detect it? Do you have any solution to define it for a CI?
    – Salman Lashkarara
    Oct 31 '17 at 16:50










  • @Salman cause we have Checkstyle checks like GitHub PR code style pre-checks at CI. All PRs must pass Checkstyle rules successfully before be merged into the main brunch.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:53


















  • what is ur IDE?
    – Salman Lashkarara
    Oct 31 '17 at 16:47










  • @Salman it doesn't matter. This check has to be at CI server.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:48










  • It is a clean code measurement. Why a CI server should be able to detect it? Do you have any solution to define it for a CI?
    – Salman Lashkarara
    Oct 31 '17 at 16:50










  • @Salman cause we have Checkstyle checks like GitHub PR code style pre-checks at CI. All PRs must pass Checkstyle rules successfully before be merged into the main brunch.
    – Andriy Kryvtsun
    Oct 31 '17 at 16:53
















what is ur IDE?
– Salman Lashkarara
Oct 31 '17 at 16:47




what is ur IDE?
– Salman Lashkarara
Oct 31 '17 at 16:47












@Salman it doesn't matter. This check has to be at CI server.
– Andriy Kryvtsun
Oct 31 '17 at 16:48




@Salman it doesn't matter. This check has to be at CI server.
– Andriy Kryvtsun
Oct 31 '17 at 16:48












It is a clean code measurement. Why a CI server should be able to detect it? Do you have any solution to define it for a CI?
– Salman Lashkarara
Oct 31 '17 at 16:50




It is a clean code measurement. Why a CI server should be able to detect it? Do you have any solution to define it for a CI?
– Salman Lashkarara
Oct 31 '17 at 16:50












@Salman cause we have Checkstyle checks like GitHub PR code style pre-checks at CI. All PRs must pass Checkstyle rules successfully before be merged into the main brunch.
– Andriy Kryvtsun
Oct 31 '17 at 16:53




@Salman cause we have Checkstyle checks like GitHub PR code style pre-checks at CI. All PRs must pass Checkstyle rules successfully before be merged into the main brunch.
– Andriy Kryvtsun
Oct 31 '17 at 16:53












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Well, your best bet would be the EmptyLineSeparator check. According to its docs, it:




checks for empty line separators after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.




Configure like this:



<module name="EmptyLineSeparator"/>


The tokens property controls where you want the empty lines. (At least you can choose from a list of places.)



You need at least Checkstyle 5.8, better 6.18 or later.






share|improve this answer





















  • It's very close what I need but it doesn't distinguish static final constants and just fields.
    – Andriy Kryvtsun
    Oct 31 '17 at 21:37










  • I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
    – Thomas Jensen
    Nov 1 '17 at 7:00












  • Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
    – rveach
    Nov 1 '17 at 17:07


















up vote
0
down vote













I did not find anything already implemented, so I wrote a regex for this check.



Quick solution with multiline regex (the code should be well formatted):



<module name="RegexpMultiline">
<property name="format" value="^([^n ]+ )*(class|interface|enum) [^{]*{n[^n}]"/>
<property name="message" value="Leave empty row after class/interface/enum definition!"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="groovy,java"/>
</module>

<module name="RegexpMultiline">
<property name="format" value="[^n{]n}n"/>
<property name="message" value="Leave empty row before end of class/interface/enum!"/>
<property name="severity" value="error"/>
<property name="fileExtensions" value="groovy,java"/>
</module>


There are a lot of threads and discussions about this:




  • https://github.com/checkstyle/checkstyle/issues/5313

  • https://github.com/checkstyle/checkstyle/issues/3379

  • https://github.com/checkstyle/checkstyle/issues/3923






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47040002%2fcheckstyle-rule-to-enforce-empty-line-separation-for-class-sections%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Well, your best bet would be the EmptyLineSeparator check. According to its docs, it:




    checks for empty line separators after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.




    Configure like this:



    <module name="EmptyLineSeparator"/>


    The tokens property controls where you want the empty lines. (At least you can choose from a list of places.)



    You need at least Checkstyle 5.8, better 6.18 or later.






    share|improve this answer





















    • It's very close what I need but it doesn't distinguish static final constants and just fields.
      – Andriy Kryvtsun
      Oct 31 '17 at 21:37










    • I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
      – Thomas Jensen
      Nov 1 '17 at 7:00












    • Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
      – rveach
      Nov 1 '17 at 17:07















    up vote
    1
    down vote













    Well, your best bet would be the EmptyLineSeparator check. According to its docs, it:




    checks for empty line separators after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.




    Configure like this:



    <module name="EmptyLineSeparator"/>


    The tokens property controls where you want the empty lines. (At least you can choose from a list of places.)



    You need at least Checkstyle 5.8, better 6.18 or later.






    share|improve this answer





















    • It's very close what I need but it doesn't distinguish static final constants and just fields.
      – Andriy Kryvtsun
      Oct 31 '17 at 21:37










    • I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
      – Thomas Jensen
      Nov 1 '17 at 7:00












    • Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
      – rveach
      Nov 1 '17 at 17:07













    up vote
    1
    down vote










    up vote
    1
    down vote









    Well, your best bet would be the EmptyLineSeparator check. According to its docs, it:




    checks for empty line separators after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.




    Configure like this:



    <module name="EmptyLineSeparator"/>


    The tokens property controls where you want the empty lines. (At least you can choose from a list of places.)



    You need at least Checkstyle 5.8, better 6.18 or later.






    share|improve this answer












    Well, your best bet would be the EmptyLineSeparator check. According to its docs, it:




    checks for empty line separators after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.




    Configure like this:



    <module name="EmptyLineSeparator"/>


    The tokens property controls where you want the empty lines. (At least you can choose from a list of places.)



    You need at least Checkstyle 5.8, better 6.18 or later.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 31 '17 at 20:03









    Thomas Jensen

    10.6k75797




    10.6k75797












    • It's very close what I need but it doesn't distinguish static final constants and just fields.
      – Andriy Kryvtsun
      Oct 31 '17 at 21:37










    • I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
      – Thomas Jensen
      Nov 1 '17 at 7:00












    • Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
      – rveach
      Nov 1 '17 at 17:07


















    • It's very close what I need but it doesn't distinguish static final constants and just fields.
      – Andriy Kryvtsun
      Oct 31 '17 at 21:37










    • I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
      – Thomas Jensen
      Nov 1 '17 at 7:00












    • Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
      – rveach
      Nov 1 '17 at 17:07
















    It's very close what I need but it doesn't distinguish static final constants and just fields.
    – Andriy Kryvtsun
    Oct 31 '17 at 21:37




    It's very close what I need but it doesn't distinguish static final constants and just fields.
    – Andriy Kryvtsun
    Oct 31 '17 at 21:37












    I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
    – Thomas Jensen
    Nov 1 '17 at 7:00






    I understand, but that‘s all you‘re gonna get unless you‘re willing to write your own custom check.
    – Thomas Jensen
    Nov 1 '17 at 7:00














    Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
    – rveach
    Nov 1 '17 at 17:07




    Besides writing your own check, you can ask Checkstyle to incorporate your changes into it if it makes sense for others or you can extend the existing check yourself.
    – rveach
    Nov 1 '17 at 17:07












    up vote
    0
    down vote













    I did not find anything already implemented, so I wrote a regex for this check.



    Quick solution with multiline regex (the code should be well formatted):



    <module name="RegexpMultiline">
    <property name="format" value="^([^n ]+ )*(class|interface|enum) [^{]*{n[^n}]"/>
    <property name="message" value="Leave empty row after class/interface/enum definition!"/>
    <property name="severity" value="error"/>
    <property name="fileExtensions" value="groovy,java"/>
    </module>

    <module name="RegexpMultiline">
    <property name="format" value="[^n{]n}n"/>
    <property name="message" value="Leave empty row before end of class/interface/enum!"/>
    <property name="severity" value="error"/>
    <property name="fileExtensions" value="groovy,java"/>
    </module>


    There are a lot of threads and discussions about this:




    • https://github.com/checkstyle/checkstyle/issues/5313

    • https://github.com/checkstyle/checkstyle/issues/3379

    • https://github.com/checkstyle/checkstyle/issues/3923






    share|improve this answer

























      up vote
      0
      down vote













      I did not find anything already implemented, so I wrote a regex for this check.



      Quick solution with multiline regex (the code should be well formatted):



      <module name="RegexpMultiline">
      <property name="format" value="^([^n ]+ )*(class|interface|enum) [^{]*{n[^n}]"/>
      <property name="message" value="Leave empty row after class/interface/enum definition!"/>
      <property name="severity" value="error"/>
      <property name="fileExtensions" value="groovy,java"/>
      </module>

      <module name="RegexpMultiline">
      <property name="format" value="[^n{]n}n"/>
      <property name="message" value="Leave empty row before end of class/interface/enum!"/>
      <property name="severity" value="error"/>
      <property name="fileExtensions" value="groovy,java"/>
      </module>


      There are a lot of threads and discussions about this:




      • https://github.com/checkstyle/checkstyle/issues/5313

      • https://github.com/checkstyle/checkstyle/issues/3379

      • https://github.com/checkstyle/checkstyle/issues/3923






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I did not find anything already implemented, so I wrote a regex for this check.



        Quick solution with multiline regex (the code should be well formatted):



        <module name="RegexpMultiline">
        <property name="format" value="^([^n ]+ )*(class|interface|enum) [^{]*{n[^n}]"/>
        <property name="message" value="Leave empty row after class/interface/enum definition!"/>
        <property name="severity" value="error"/>
        <property name="fileExtensions" value="groovy,java"/>
        </module>

        <module name="RegexpMultiline">
        <property name="format" value="[^n{]n}n"/>
        <property name="message" value="Leave empty row before end of class/interface/enum!"/>
        <property name="severity" value="error"/>
        <property name="fileExtensions" value="groovy,java"/>
        </module>


        There are a lot of threads and discussions about this:




        • https://github.com/checkstyle/checkstyle/issues/5313

        • https://github.com/checkstyle/checkstyle/issues/3379

        • https://github.com/checkstyle/checkstyle/issues/3923






        share|improve this answer












        I did not find anything already implemented, so I wrote a regex for this check.



        Quick solution with multiline regex (the code should be well formatted):



        <module name="RegexpMultiline">
        <property name="format" value="^([^n ]+ )*(class|interface|enum) [^{]*{n[^n}]"/>
        <property name="message" value="Leave empty row after class/interface/enum definition!"/>
        <property name="severity" value="error"/>
        <property name="fileExtensions" value="groovy,java"/>
        </module>

        <module name="RegexpMultiline">
        <property name="format" value="[^n{]n}n"/>
        <property name="message" value="Leave empty row before end of class/interface/enum!"/>
        <property name="severity" value="error"/>
        <property name="fileExtensions" value="groovy,java"/>
        </module>


        There are a lot of threads and discussions about this:




        • https://github.com/checkstyle/checkstyle/issues/5313

        • https://github.com/checkstyle/checkstyle/issues/3379

        • https://github.com/checkstyle/checkstyle/issues/3923







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 5:23









        Nagy Attila

        19619




        19619






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47040002%2fcheckstyle-rule-to-enforce-empty-line-separation-for-class-sections%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?