Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress all checks for a file in Checkstyle?

I'm doing an override for a third party class and I want to suppress all checks for it (since I'm only keeping it around until the patch is accepted).

Is there a way to suppress all checks for a file?

I tried using "*" but that fails.

like image 525
Alceu Costa Avatar asked Jun 18 '09 12:06

Alceu Costa


People also ask

How do I stop checkstyle warnings?

It is possible to suppress all the checkstyle warnings with the argument "all" . You can also use a checkstyle: prefix to prevent compiler from processing these annotations. You can also define aliases for check names that need to be suppressed.

What is checkstyle suppressions XML?

A suppressions XML document contains a set of suppress elements, where each suppress element can have the following attributes: files - a Pattern matched against the file name associated with an audit event. It is optional. checks - a Pattern matched against the name of the check associated with an audit event.

What is checkstyle XML?

Checkstyle obtains a configuration from an XML document whose elements specify the configuration's hierarchy of modules and their properties. You provide a file that contains the configuration document when you invoke Checkstyle at the command line, and when you run a Checkstyle task in ant.

How do I cancel my checkstyle account?

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. under the SuppressionFilter section on that same page, which allows you to turn off individual checks for pattern matched resources.


2 Answers

I was able to suppress the checks in a file by adding the SuppressionCommentFilter tag in my checks.xml:

First, I added the FileContentHolder tag as a child of TreeWalker tag (this step is not needed since version 8.1 of com.puppycrawl.tools:checkstyle):

<module name="TreeWalker">     ...     <module name="FileContentsHolder"/>     ... </module> 

Then I added the SuppressionCommentFilter in the checks.xml (since version 8.1 of com.puppycrawl.tools:checkstyle - under "TreeWalker" node):

<module name="SuppressionCommentFilter"/> 

In each file that I wanted to suppress the checks I inserted the following comment in the first line of the file:

// CHECKSTYLE:OFF 
like image 22
Alceu Costa Avatar answered Sep 17 '22 14:09

Alceu Costa


Don't know whether you're using command line or in an IDE, but you'll basically need a suppresions file. If you're manually editing the Checkstyle config file, add a new module to it:

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

Your mysuppression.xml can be something like:

<?xml version="1.0"?>  <!DOCTYPE suppressions PUBLIC     "-//Puppy Crawl//DTD Suppressions 1.1//EN"     "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">  <suppressions>     <suppress files="TheClassToIgnore\.java" checks="[a-zA-Z0-9]*"/> </suppressions> 

The value for "files" attribute is basically a regex for your source files, and the value for the "checks" attribute is regex for what checks to skip ("[a-zA-Z0-9]*" basically means skip everything). This is the pattern you're looking for I guess?

like image 58
aberrant80 Avatar answered Sep 17 '22 14:09

aberrant80