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.)?
java checkstyle
add a comment |
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.)?
java checkstyle
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
add a comment |
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.)?
java checkstyle
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
java checkstyle
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
add a comment |
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
add a comment |
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.
It's very close what I need but it doesn't distinguishstatic finalconstants 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
add a comment |
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
add a comment |
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.
It's very close what I need but it doesn't distinguishstatic finalconstants 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
add a comment |
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.
It's very close what I need but it doesn't distinguishstatic finalconstants 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
add a comment |
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.
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.
answered Oct 31 '17 at 20:03
Thomas Jensen
10.6k75797
10.6k75797
It's very close what I need but it doesn't distinguishstatic finalconstants 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
add a comment |
It's very close what I need but it doesn't distinguishstatic finalconstants 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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 13 at 5:23
Nagy Attila
19619
19619
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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