Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a particular Checkstyle rule for a particular line of code?

I have a Checkstyle validation rule configured in my project, that prohibits to define class methods with more than 3 input parameters. The rule works fine for my classes, but sometimes I have to extend third-party classes, which do not obey this particular rule.

Is there a possibility to instruct Checkstyle that a certain method should be silently ignored?

BTW, I ended up with my own wrapper of Checkstyle: qulice.com (see Strict Control of Java Code Quality)

like image 226
yegor256 Avatar asked Oct 26 '10 11:10

yegor256


People also ask

How do I turn off Checkstyle?

Then to disable Checkstyle, you use //CHECKSTYLE:OFF and //CHECKSTYLE:ON (default values) in your code.

How do you ignore a Checkstyle file?

To skip all checks in all files under directories (packages) named "generated", you must use regex pattern <suppress checks="[a-zA-Z0-9]*" files="[\\/]generated[\\/]" /> It was really little hard for me to find out the right pattern for files parameter.

Is Checkstyle a coding standard?

Overview. Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

What is Checkstyle configuration?

A Checkstyle configuration specifies which modules to plug in and apply to Java source files. Modules are structured in a tree whose root is the Checker module. The next level of modules contains: FileSetChecks - modules that take a set of input files and fire violation messages.


1 Answers

Check out the use of the supressionCommentFilter at http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter. You'll need to add the module to your checkstyle.xml

<module name="SuppressionCommentFilter"/> 

and it's configurable. Thus you can add comments to your code to turn off checkstyle (at various levels) and then back on again through the use of comments in your code. E.g.

//CHECKSTYLE:OFF public void someMethod(String arg1, String arg2, String arg3, String arg4) { //CHECKSTYLE:ON 

Or even better, use this more tweaked version:

<module name="SuppressionCommentFilter">     <property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>     <property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>     <property name="checkFormat" value="$1"/> </module> 

which allows you to turn off specific checks for specific lines of code:

//CHECKSTYLE.OFF: IllegalCatch - Much more readable than catching 7 exceptions catch (Exception e) //CHECKSTYLE.ON: IllegalCatch 

*Note: you'll also have to add the FileContentsHolder:

<module name="FileContentsHolder"/> 

See also

<module name="SuppressionFilter">     <property name="file" value="docs/suppressions.xml"/> </module> 

under the SuppressionFilter section on that same page, which allows you to turn off individual checks for pattern matched resources.

So, if you have in your checkstyle.xml:

<module name="ParameterNumber">    <property name="id" value="maxParameterNumber"/>    <property name="max" value="3"/>    <property name="tokens" value="METHOD_DEF"/> </module> 

You can turn it off in your suppression xml file with:

<suppress id="maxParameterNumber" files="YourCode.java"/> 

Another method, now available in Checkstyle 5.7 is to suppress violations via the @SuppressWarnings java annotation. To do this, you will need to add two new modules (SuppressWarningsFilter and SuppressWarningsHolder) in your configuration file:

<module name="Checker">    ...    <module name="SuppressWarningsFilter" />    <module name="TreeWalker">        ...        <module name="SuppressWarningsHolder" />    </module> </module>  

Then, within your code you can do the following:

@SuppressWarnings("checkstyle:methodlength") public void someLongMethod() throws Exception { 

or, for multiple suppressions:

@SuppressWarnings({"checkstyle:executablestatementcount", "checkstyle:methodlength"}) public void someLongMethod() throws Exception { 

NB: The "checkstyle:" prefix is optional (but recommended). According to the docs the parameter name have to be in all lowercase, but practice indicates any case works.

like image 170
Chris Knight Avatar answered Sep 28 '22 14:09

Chris Knight