SuppressionSingleFilter
Since Checkstyle 8.23
Description
SuppressionSingleFilter
suppresses audit events for
Checks violations in the specified file, class, checks, message, module id,
lines, and columns.
Rationale: To allow users to use suppressions configured in the same config as other
modules. SuppressionFilter
and SuppressionXpathFilter
require
a separate file.
Advice: If checkstyle configuration is used for several projects, single suppressions on common files/folders is better to put in checkstyle configuration as common rule. All suppression that are for specific file names is better to keep in project specific config file.
Attention: This filter only supports single suppression, and will need multiple instances if users wants to suppress multiple violations.
Notes
SuppressionSingleFilter
can suppress Checks that have
Treewalker
or Checker
as parent module.
Properties
name | description | type | default value | since |
---|---|---|---|---|
checks | Define the RegExp for matching against the name of the check associated with an audit event. | Pattern | null |
8.23 |
columns | Specify a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer. | String | null |
8.23 |
files | Define the RegExp for matching against the file name associated with an audit event. | Pattern | null |
8.23 |
id | Specify a string matched against the ID of the check associated with an audit event. | String | null |
8.23 |
lines | Specify a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer. | String | null |
8.23 |
message | Define the RegExp for matching against the message of the check associated with an audit event. | Pattern | null |
8.23 |
Examples
To configure a filter to suppress violations of JavadocStyle
and
MagicNumber
checks in Example1.java
for specific line ranges
using SuppressionSingleFilter
:
<module name="Checker">
<module name="TreeWalker">
<module name="JavadocStyle"/>
<module name="MagicNumber"/>
</module>
<module name="SuppressionSingleFilter">
<property name="checks" value="JavadocStyle|MagicNumber"/>
<property name="files" value="Example1.java"/>
<property name="lines" value="1,5-100"/>
</module>
<module name="SuppressionSingleFilter">
<property name="message" value="Missing a Javadoc comment"/>
</module>
</module>
public class Example1 {
public void exampleMethod() {
int value = 100; // filtered violation ''100' is a magic number'
}
}
To configure a filter to suppress violations of JavadocMethod
and
EqualsAvoidNull
checks in Example2.java
using
SuppressionSingleFilter
:
<module name="Checker">
<module name="TreeWalker">
<module name="JavadocMethod"/>
<module name="EqualsAvoidNull"/>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="Example2.java"/>
<property name="checks" value="JavadocMethod|EqualsAvoidNull"/>
</module>
</module>
public class Example2 {
public void checkStringEquality(String s) {
// filtered violation below 'String literal expressions should be on the left'
s.equals("M");
}
/**
* @param p1 The first number
*/
// filtered violation below '@return tag should be present'
private int m2(int p1) { return p1; }
}
To configure a filter to suppress violations of RegexpSinglelineCheck
in Example3.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="RegexpSingleline">
<property name="format" value="example"/>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="Example3.java"/>
<property name="checks" value="RegexpSinglelineCheck"/>
</module>
</module>
public class Example3 {
public void printExample() {
System.out.println(
"example" // filtered violation 'Line matches the illegal pattern 'example''
);
}
public void noViolation() {
System.out.println(
"RegexpSingleline is case sensitive by default. 'Example' in not matching."
);
}
}
To configure a filter to suppress violations of NoWhitespaceAfter
in Example4.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="TreeWalker">
<module name="NoWhitespaceAfter"/>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="Example4.java"/>
<property name="checks" value="NoWhitespaceAfter"/>
</module>
</module>
public class Example4 {
public void exampleMethod(int a, int b) {
// filtered violation below ''.' is followed by whitespace'
Integer. parseInt("3");
}
public void exampleMethod2() {
int [] x; // filtered violation ''int' is followed by whitespace'
}
}
To configure a filter to suppress violations of MethodName
in Example5.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="MethodName"/>
<module name="SuppressionSingleFilter">
<property name="files" value="Example5.java"/>
<property name="checks" value="MethodName"/>
</module>
</module>
public class Example5 {
// filtered violation below 'Name 'example_Method' must match pattern'
public void example_Method() {
}
// filtered violation below 'Name 'Another_Method' must match pattern'
public void Another_Method() {
}
}
To configure a filter to suppress violations of ConstantName
in Example6.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="ConstantName"/>
<module name="SuppressionSingleFilter">
<property name="files" value="Example6.java"/>
<property name="checks" value="ConstantName"/>
</module>
</module>
public class Example6 {
// filtered violation below 'Name 'myConstant' must match pattern'
private static final int myConstant = 42;
}
To configure a filter to suppress violations of MemberName
and MethodName
in Example7.java
using
SuppressionSingleFilter
:
<module name="Checker">
<module name="MemberName"/>
<module name="MethodName"/>
<module name="SuppressionSingleFilter">
<property name="files" value="Example7.java"/>
<property name="checks" value="MemberName|MethodName"/>
</module>
</module>
public class Example7 {
// filtered violation below 'Name 'MyVariable' must match pattern'
private int MyVariable = 5;
// filtered violation below 'Name 'MyMethod' must match pattern'
public void MyMethod() {
}
}
To configure a filter to suppress ParameterNumber
violations
in Example8.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="TreeWalker">
<module name="ParameterNumber">
<property name="max" value="5"/>
</module>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="Example8.java"/>
<property name="checks" value="ParameterNumber"/>
</module>
</module>
public class Example8 {
// filtered violation below 'More than 5 parameters (found 6)'
public void exampleMethod(
int param1, int param2, int param3, int param4, int param5, int param6
) {}
}
To configure a filter to suppress FileLength
violations
in Example9.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="TreeWalker">
<module name="FileLength">
<property name="max" value="1"/>
</module>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="Example9.java"/>
<property name="checks" value="FileLength"/>
</module>
</module>
/* filtered violation on 1st line 'File length is 4 lines (max allowed is 1)' */
public class Example9 {
}
To configure a filter to suppress MemberName
violations
in Example10.java
using SuppressionSingleFilter
:
<module name="Checker">
<module name="MemberName">
<property name="format" value="^[A-Z][a-zA-Z0-9]*$"/>
</module>
<module name="SuppressionSingleFilter">
<property name="files" value="Example10.java"/>
<property name="message" value="Name 'log' must match pattern"/>
</module>
</module>
public class Example10 {
// filtered violation below 'Name 'log' must match pattern'
private String log = "Some log message";
}
Example of Usage
Package
com.puppycrawl.tools.checkstyle.filters