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)
Best Answer
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
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.
Or even better, use this more tweaked version:
which allows you to turn off specific checks for specific lines of code:
*Note: you'll also have to add the
FileContentsHolder
:See also
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:
You can turn it off in your suppression xml file with:
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
andSuppressWarningsHolder
) in your configuration file:Then, within your code you can do the following:
or, for multiple suppressions:
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.