63

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.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Alceu Costa
  • 9,733
  • 19
  • 65
  • 83

7 Answers7

73

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?

aberrant80
  • 12,815
  • 8
  • 45
  • 68
  • 1
    This is exactly what I'm looking for. But it didn't work. Do I have to set a checkstyle configuration somewhere to use the 'files' and 'checks' attributes as regular expressions? I can see that in the already existant suppres tags doesn't use the 'file' attribute as regex: i.e. files="MyClass.java" and not files="MyClass\.java". – Alceu Costa Jun 19 '09 at 11:56
  • Hmm... are you using an IDE? I'm afraid I can't help you more because the above is basically a simplified version of what I've set up for my current work - basically there was a group of similarly-named generated classes that we want Checkstyle to ignore, so we ignored a particularly pattern in the name. – aberrant80 Jun 20 '09 at 04:04
  • Doesn't help me. Don't have access to the server and the architects aren't likely to give it. Was hoping for something like @SupressWarnings("com.puppycrawl.tools.*") – user447607 Jan 31 '13 at 18:42
  • 2
    To skip all checks in all files under directories (packages) named "generated", you must use regex pattern `` It was really little hard for me to find out the right pattern for **files** parameter. – Rib47 Jan 19 '17 at 09:53
  • 3
    `[a-zA-Z0-9]*` seems excessive, why don' you just use `.*`? – rveach Jul 22 '17 at 20:49
  • How can I add multiple specific checks into the `checks="..."` parameter? How are they separated? – AndroidNoob Dec 11 '17 at 15:37
  • Try making the path to the mysuppressions.xml file from the project root . ... ie ... ./codequality/mysuppressions.xml – slipperypete Nov 06 '18 at 19:41
  • i'm getting file is not found. it is placed right at the rool of "modules" folder, nearby checkstyle.xml. – Simon Logic Jul 20 '21 at 12:23
33

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
Nishi
  • 10,634
  • 3
  • 27
  • 36
Alceu Costa
  • 9,733
  • 19
  • 65
  • 83
  • Henrique, note that `FileContentsHolder` should be under `TreeWalker` while `SuppressionCommentFilter` should be under `Checker`. If you put them both under `TreeWalker` or both under `Checker` - this would not work. For more information, see http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter – MatanRubin Mar 08 '17 at 18:40
  • 1
    Since this tripped me up just now -- @MatanRubin's comment is accurate for versions of checkstyle up to 8.0. However, checkstyle 8.1 and 8.2 made some backwards-incompatible changes: the correct thing to do now is to have `SuppressionCommentFilter` be under `TreeWalker`. The `FileContentsHolder` module seems to have been removed entirely -- it's no longer necessary. – Michael0x2a Sep 24 '17 at 01:08
  • Thanks for the info @Michael0x2a – MatanRubin Sep 24 '17 at 07:55
19

aberrant80's answer helped a lot. With the help of his hint - using regex pattern matching, I was also able to suppress checkstyle for an entire package by adding this to the checkstyle-suppressions.xml file. For eg. to skip checkstyle checks on all auto generated java files under the jaxb package,

    <suppressions>
        <suppress checks="[a-zA-Z0-9]*" files="[\\/]jaxb[\\/]" />
    </suppressions>
Inxsible
  • 700
  • 5
  • 27
  • 3
    You can just use `.*` for `checks` and `/jaxb/` for `files` if your path separator character is `/`. – erwaman Apr 28 '17 at 16:52
8

If you're using the Checkclipse Eclipse plugin for Checkstyle, you can include or exclude file patterns (including directories) by going to the Checkclipse > File Filter tab under project properties. For example, my project contains src and test directories at the top level. I want Checkstyle applied to only files in the src directory (omitting test), so I added an include pattern that looks like this:

src/.+java$

As you can see, it uses a regex-style syntax for pattern specification.

Rob H
  • 14,502
  • 8
  • 42
  • 45
3

@aberrant80's answer is very inspiring although it didn't work in my project. In order to suppress any warnings that checkstyle looks for in Foo.java, according to link, if maven is used:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    ...
        <configuration>
            ...
            <configLocation>checkstyle.xml</configLocation>
            <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
            ...
        </configuration>                
    ...
</plugin>

And in checkstyle-suppressions.xml

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

So far the highest version is 1.1 (1.2 does not exist).

Tiina
  • 4,285
  • 7
  • 44
  • 73
0

There is an option to ignore write protected files. Or files in a package.

akarnokd
  • 69,132
  • 14
  • 157
  • 192
0

If you wish to not have a group of files within a project inspected, you can filter these files out so they are not inspected by creating a file filter

The file filter uses regex to determine what files to exclude. The regex operates on the complete file name - because of this, you could also exclude whole folders. In this case you could exclude the whole package if you wished.

If you google around a bit - you could probably find some Checkstyle Configuration Propertie files that have examples of what you're looking for. I would suggest after you do so - save it as a bit of a template so you can refer to it in future situations

ist_lion
  • 3,149
  • 9
  • 43
  • 73