public class SuppressWithNearbyCommentFilter extends AutomaticBean implements TreeWalkerFilter
Filter SuppressWithNearbyCommentFilter
uses nearby comments to suppress audit events.
Rationale: Same as SuppressionCommentFilter
.
Whereas the SuppressionCommentFilter uses matched pairs of filters to turn
on/off comment matching, SuppressWithNearbyCommentFilter
uses single comments.
This requires fewer lines to mark a region, and may be aesthetically preferable in some contexts.
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.
SuppressWithNearbyCommentFilter can suppress Checks that have Treewalker as parent module.
commentFormat
- Specify comment pattern to trigger filter to begin suppression.
Type is java.util.regex.Pattern
.
Default value is "SUPPRESS CHECKSTYLE (\w+)"
.
checkFormat
- Specify check pattern to suppress.
Type is java.lang.String
.
Default value is ".*"
.
messageFormat
- Define 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
.
influenceFormat
- Specify negative/zero/positive value that
defines the number of lines preceding/at/following the suppression comment.
Type is java.lang.String
.
Default value is "0"
.
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 for check on any line
with a comment SUPPRESS CHECKSTYLE <i>check</i>
:
<module name="SuppressWithNearbyCommentFilter"/>
private int [] array; // SUPPRESS CHECKSTYLE
To configure a filter to suppress all audit events on any line containing
the comment CHECKSTYLE IGNORE THIS LINE
:
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="CHECKSTYLE IGNORE THIS LINE"/> <property name="checkFormat" value=".*"/> <property name="influenceFormat" value="0"/> </module>
public static final int lowerCaseConstant; // CHECKSTYLE IGNORE THIS LINE
To configure a filter so that // OK to catch (Throwable|Exception|RuntimeException) here
permits the current and previous line to avoid generating an IllegalCatch audit event:
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="OK to catch (\w+) here"/> <property name="checkFormat" value="IllegalCatchCheck"/> <property name="messageFormat" value="$1"/> <property name="influenceFormat" value="-1"/> </module>
. . . catch (RuntimeException re) { // OK to catch RuntimeException here } catch (Throwable th) { ... } . . .
To configure a filter so that CHECKSTYLE IGNORE <i>check</i> FOR NEXT
<i>var</i> LINES
avoids triggering any audits for the given check for
the current line and the next var lines (for a total of var+1 lines):
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="CHECKSTYLE IGNORE (\w+) FOR NEXT (\d+) LINES"/> <property name="checkFormat" value="$1"/> <property name="influenceFormat" value="$2"/> </module>
static final int lowerCaseConstant; // CHECKSTYLE IGNORE ConstantNameCheck FOR NEXT 3 LINES static final int lowerCaseConstant1; static final int lowerCaseConstant2; static final int lowerCaseConstant3; static final int lowerCaseConstant4; // will warn here
To configure a filter to avoid any audits on code like:
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="ALLOW (\\w+) ON PREVIOUS LINE"/> <property name="checkFormat" value="$1"/> <property name="influenceFormat" value="-1"/> </module>
private int D2; // ALLOW MemberName ON PREVIOUS LINE . . .
To configure a filter to allow suppress one or more Checks (separated by "|") and demand comment no less than 14 symbols:
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="@cs\.suppress \[(\w+(\|\w+)*)\] \w[-\.'`,:;\w ]{14,}"/> <property name="checkFormat" value="$1"/> <property name="influenceFormat" value="1"/> </module>
public static final int [] array; // @cs.suppress [ConstantName|NoWhitespaceAfter] A comment here
It is possible to specify an ID of checks, so that it can be leveraged by
the SuppressWithNearbyCommentFilter to skip validations. The following examples show how to skip
validations near code that has comment like // @cs-: <ID/> (reason)
,
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 SuppressWithNearbyCommentFilter configuration (idFormat which is set to '$1' points that ID of the checks is in the first group of commentFormat regular expressions):
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="@cs-: (\w+) \(.*\)"/> <property name="idFormat" value="$1"/> <property name="influenceFormat" value="0"/> </module>
@Ignore // @cs-: ignore (test has not been implemented yet) @Test public void testMethod() { } public static void foo() { System.out.println("Debug info."); // @cs-: systemout (should not fail RegexpSinglelineJava) }
Example of how to configure the check to suppress more than one checks. The influence format format is specified in the second regexp group.
<module name="SuppressWithNearbyCommentFilter"> <property name="commentFormat" value="@cs-\: ([\w\|]+) influence (\d+)"/> <property name="checkFormat" value="$1"/> <property name="influenceFormat" value="$2"/> </module>
// @cs-: ClassDataAbstractionCoupling influence 2 // @cs-: MagicNumber influence 4 @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
AutomaticBean.OutputStreamOptions
Constructor and Description |
---|
SuppressWithNearbyCommentFilter() |
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 |
setCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.
|
void |
setFileContents(FileContents fileContents)
Set the FileContents for this filter.
|
void |
setIdFormat(String format)
Setter to specify check ID pattern to suppress.
|
void |
setInfluenceFormat(String format)
Setter to specify negative/zero/positive value that defines the number
of lines preceding/at/following the suppression comment.
|
void |
setMessageFormat(String format)
Setter to define message pattern to suppress.
|
configure, contextualize, getConfiguration, setupChild
public SuppressWithNearbyCommentFilter()
public final void setCommentFormat(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 final void setInfluenceFormat(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.