Allow single-user access syntax (getter / setter) using Checkstyle

We would like to have trivial Java property accessories using the syntax of one line, so they take up much less space and are more readable (in terms of quickly viewing a set of accessories). But we want to provide multi-line method syntax for everything else in our checkstyle configuration. But I'm not sure how to make this exception for accessories in the Checkstyle configuration and suspect that it might not be possible.

So, we would like our accessors to look something like this:

public String getFoo() { return foo; } public void setFoo(String foo) { this.foo = foo; } 

[In fact, we would prefer not to have trivial accessories at all, and instead just comment on private member variables using @Property or something else and have creators for us, since writing infinite get and set code does not give real benefits, but what general frustration in Java and aside how this issue goes.]

+4
source share
2 answers

If you want to solve this problem with Checkstyle, you must write one or more user checks. Your validation will be a subclass of the validation you are modifying. Then, for all affected checks, you must enter your own subclass check in the checkstyle configuration (and delete the original one).

You might be able to use the JavadocMethodCheck code, which already detects trivial accessors. It sounds like a lot of work, but it is not so much code, because the modification that you do in your subclass is always the same, and therefore it can be placed in a helper class.

I honestly don't think Checkstyle can do this out of the box, sorry!

0
source

I would recommend using SuppressionCommentFilter to disable Checkstyle checks for a block of code.

Then one could do the following to disable Checkstyle as follows:

 // CHECKSTYLE_OFF: ALL public String getFoo() { return foo; } public void setFoo(String foo) { this.foo = foo; } // CHECKSTYLE_ON: ALL 
-1
source

Source: https://habr.com/ru/post/1308454/


All Articles