public class SuppressionCommentFilter extends AutomaticBean implements TreeWalkerFilter
Filter SuppressionCommentFilter
uses pairs of comments to suppress audit events.
Rationale: Sometimes there are legitimate reasons for violating a check. When this is a matter of the code in question and not personal preference, the best place to override the policy is in the code itself. Semi-structured comments can be associated with the check. This is sometimes superior to a separate suppressions file, which must be kept up-to-date as the source file is edited.
Note that the suppression comment should be put before the violation. You can use more than one suppression comment each on separate line.
Attention: This filter may only be specified within the TreeWalker module
(<module name="TreeWalker"/>
) and only applies to checks which are also
defined within this module. To filter non-TreeWalker checks like RegexpSingleline
, a
SuppressWithPlainTextCommentFilter or similar filter must be used.
offCommentFormat
and onCommentFormat
must have equal
paren counts.
SuppressionCommentFilter can suppress Checks that have Treewalker as parent module.
offCommentFormat
- Specify comment pattern to
trigger filter to begin suppression.
Type is java.util.regex.Pattern
.
Default value is "CHECKSTYLE:OFF"
.
onCommentFormat
- Specify comment pattern to trigger filter to end suppression.
Type is java.util.regex.Pattern
.
Default value is "CHECKSTYLE:ON"
.
checkFormat
- Specify check pattern to suppress.
Type is java.lang.String
.
Default value is ".*"
.
messageFormat
- Specify message pattern to suppress.
Type is java.lang.String
.
Default value is null
.
idFormat
- Specify check ID pattern to suppress.
Type is java.lang.String
.
Default value is null
.
checkCPP
- Control whether to check C++ style comments (//
).
Type is boolean
.
Default value is true
.
checkC
- Control whether to check C style comments (/* ... */
).
Type is boolean
.
Default value is true
.
To configure a filter to suppress audit events between a comment containing
CHECKSTYLE:OFF
and a comment containing CHECKSTYLE:ON
:
<module name="TreeWalker"> ... <module name="SuppressionCommentFilter"/> ... </module>
To configure a filter to suppress audit events between a comment containing line
BEGIN GENERATED CODE
and a comment containing line END GENERATED CODE
:
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="BEGIN GENERATED CODE"/> <property name="onCommentFormat" value="END GENERATED CODE"/> </module>
//BEGIN GENERATED CODE @Override public boolean equals(Object obj) { ... } // No violation events will be reported @Override public int hashCode() { ... } // No violation events will be reported //END GENERATED CODE . . .
To configure a filter so that // stop constant check
and
// resume constant check
marks legitimate constant names:
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="stop constant check"/> <property name="onCommentFormat" value="resume constant check"/> <property name="checkFormat" value="ConstantNameCheck"/> </module>
//stop constant check public static final int someConstant; // won't warn here //resume constant check public static final int someConstant; // will warn here as constant's name doesn't match the // pattern "^[A-Z][A-Z0-9]*$"
To configure a filter so that UNUSED OFF: <i>var</i>
and
UNUSED ON: <i>var</i>
marks a variable or parameter known not to be
used by the code by matching the variable name in the message:
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="UNUSED OFF\: (\w+)"/> <property name="onCommentFormat" value="UNUSED ON\: (\w+)"/> <property name="checkFormat" value="Unused"/> <property name="messageFormat" value="^Unused \w+ '$1'.$"/> </module>
private static void foo(int a, int b) // UNUSED OFF: b { System.out.println(a); } private static void foo1(int a, int b) // UNUSED ON: b { System.out.println(a); }
To configure a filter so that name of suppressed check mentioned in comment
CSOFF: <i>regexp</i>
and CSON: <i>regexp</i>
mark a matching check:
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="CSOFF\: ([\w\|]+)"/> <property name="onCommentFormat" value="CSON\: ([\w\|]+)"/> <property name="checkFormat" value="$1"/> </module>
public static final int lowerCaseConstant; // CSOFF: ConstantNameCheck public static final int lowerCaseConstant1; // CSON: ConstantNameCheck
To configure a filter to suppress all audit events between a comment containing
CHECKSTYLE_OFF: ALMOST_ALL
and a comment containing
CHECKSTYLE_OFF: ALMOST_ALL
except for the EqualsHashCode check:
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="CHECKSTYLE_OFF: ALMOST_ALL"/> <property name="onCommentFormat" value="CHECKSTYLE_ON: ALMOST_ALL"/> <property name="checkFormat" value="^((?!(EqualsHashCode)).)*$"/> </module>
public static final int array []; // CHECKSTYLE_OFF: ALMOST_ALL private String [] strArray; private int array1 []; // CHECKSTYLE_ON: ALMOST_ALL
To configure a filter to suppress Check's violation message
which matches specified message in messageFormat
(so suppression will be not only by Check's name, but by message text
additionally, as the same Check could report different by message format violations)
between a comment containing stop
and comment containing resume
:
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="stop"/> <property name="onCommentFormat" value="resume"/> <property name="checkFormat" value="IllegalTypeCheck"/> <property name="messageFormat" value="^Declaring variables, return values or parameters of type 'GregorianCalendar' is not allowed.$"/> </module>
Code before filter above is applied with Check's audit events:
... // Warning below: Declaring variables, return values or parameters of type 'GregorianCalendar' // is not allowed. GregorianCalendar calendar; // Warning below here: Declaring variables, return values or parameters of type 'HashSet' // is not allowed. HashSet hashSet; ...
Code after filter is applied:
... //stop GregorianCalendar calendar; // No warning here as it is suppressed by filter. HashSet hashSet; // Warning above here: Declaring variables, return values or parameters of type 'HashSet' //is not allowed. //resume ...
It is possible to specify an ID of checks, so that it can be leveraged by the
SuppressionCommentFilter to skip validations. The following examples show how
to skip validations near code that is surrounded with // CSOFF <ID> (reason)
and // CSON <ID>
, where ID is the ID of checks you want to suppress.
Examples of Checkstyle checks configuration:
<module name="RegexpSinglelineJava"> <property name="id" value="ignore"/> <property name="format" value="^.*@Ignore\s*$"/> <property name="message" value="@Ignore should have a reason."/> </module> <module name="RegexpSinglelineJava"> <property name="id" value="systemout"/> <property name="format" value="^.*System\.(out|err).*$"/> <property name="message" value="Don't use System.out/err, use SLF4J instead."/> </module>
Example of SuppressionCommentFilter configuration (checkFormat which is set to '$1' points that ID of the checks is in the first group of offCommentFormat and onCommentFormat regular expressions):
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="CSOFF (\w+) \(\w+\)"/> <property name="onCommentFormat" value="CSON (\w+)"/> <property name="idFormat" value="$1"/> </module>
// CSOFF ignore (test has not been implemented yet) @Ignore // should NOT fail RegexpSinglelineJava @Test public void testMethod() { } // CSON ignore // CSOFF systemout (debug) public static void foo() { System.out.println("Debug info."); // should NOT fail RegexpSinglelineJava } // CSON systemout
Example of how to configure the check to suppress more than one checks.
<module name="SuppressionCommentFilter"> <property name="offCommentFormat" value="@cs-\: ([\w\|]+)"/> <property name="checkFormat" value="$1"/> </module>
// @cs-: ClassDataAbstractionCoupling // @cs-: MagicNumber @Service // no violations from ClassDataAbstractionCoupling here @Transactional public class UserService { private int value = 10022; // no violations from MagicNumber here }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Modifier and Type | Class and Description |
---|---|
static class |
SuppressionCommentFilter.TagType
Enum to be used for switching checkstyle reporting for tags.
|
AutomaticBean.OutputStreamOptions
Constructor and Description |
---|
SuppressionCommentFilter() |
Modifier and Type | Method and Description |
---|---|
boolean |
accept(TreeWalkerAuditEvent event)
Determines whether or not a filtered
TreeWalkerAuditEvent is accepted. |
protected void |
finishLocalSetup()
Provides a hook to finish the part of this component's setup that
was not handled by the bean introspection.
|
void |
setCheckC(boolean checkC)
Setter to control whether to check C style comments (
/* ... */ ). |
void |
setCheckCPP(boolean checkCpp)
Setter to control whether to check C++ style comments (
// ). |
void |
setCheckFormat(String format)
Setter to specify check pattern to suppress.
|
void |
setFileContents(FileContents fileContents)
Set the FileContents for this filter.
|
void |
setIdFormat(String format)
Setter to specify check ID pattern to suppress.
|
void |
setMessageFormat(String format)
Setter to specify message pattern to suppress.
|
void |
setOffCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.
|
void |
setOnCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to end suppression.
|
configure, contextualize, getConfiguration, setupChild
public SuppressionCommentFilter()
public final void setOffCommentFormat(Pattern pattern)
pattern
- a pattern.public final void setOnCommentFormat(Pattern pattern)
pattern
- a pattern.public void setFileContents(FileContents fileContents)
fileContents
- the FileContents for this filter.public final void setCheckFormat(String format)
format
- a String
valuepublic void setMessageFormat(String format)
format
- a String
valuepublic void setIdFormat(String format)
format
- a String
valuepublic void setCheckCPP(boolean checkCpp)
//
).checkCpp
- true
if C++ comments are checked.public void setCheckC(boolean checkC)
/* ... */
).checkC
- true
if C comments are checked.protected void finishLocalSetup()
AutomaticBean
The default implementation does nothing.
finishLocalSetup
in class AutomaticBean
public boolean accept(TreeWalkerAuditEvent event)
TreeWalkerFilter
TreeWalkerAuditEvent
is accepted.accept
in interface TreeWalkerFilter
event
- the TreeWalkerAuditEvent to filter.Copyright © 2001–2020. All rights reserved.