Java – How to disable a particular checkstyle rule for a particular line of code

checkstylejava

Yes, I saw this question:

How to disable a particular checkstyle rule for a particular line of code?

But I can not comment and I wanted to ask if it is possible to do what the OP asked:

How to disable one particular rule for a line of code

I also saw this:
Is there a way to force Checkstyle to ignore particular warning in the source code?

I guess the answer to my question is to use
SuppressWithNearbyCommentFilter

I just don't understand what my comments in java should look like.

Say I have the following code:

public class MyDataContainer {
  /** the name */
  public String name;
}

and I want to get rid of the "Visibility Modifier" warning and only that warning.

What do I have to enable (using the eclipse–cs.sf.net plugin)?

And how do my comments have to look?

I managed to enable Suppresion Comment Filter and surround my whole class with

//CHECKSTYLE:OFF
//CHECKSTYLE:ON

but I don't really like this solution.

Best Answer

It is a bit late, for future reference:

you can use the SuppressWithNearbyCommentFilter this way:

<module name="SuppressWithNearbyCommentFilter">
   <property name="commentFormat" value="CHECKSTYLE DISABLE ([\w\|]+) FOR (-?\d+) LINES"/>
   <property name="checkFormat" value="$1"/>
   <property name="influenceFormat" value="$2"/>
</module>

On the line where you want to disable the warning you can write:

// CHECKSTYLE DISABLE <WarningName> FOR <# of lines> LINES

for example

// CHECKSTYLE DISABLE MagicNumber FOR 2 LINES

The number can also be negative (if the warning is in some automatically generated code that you cannot touch).