public class SuppressWithPlainTextCommentFilter extends AutomaticBean implements Filter
Filter SuppressWithPlainTextCommentFilter uses plain text to suppress
audit events. The filter can be used only to suppress audit events received
from the checks which implement FileSetCheck interface. In other words, the
checks which have Checker as a parent module. The filter knows nothing about
AST, it treats only plain text comments and extracts the information required
for suppression from the plain text comments. Currently the filter supports
only single line comments.
Please, be aware of the fact that, it is not recommended to use the filter for Java code anymore, however you still are able to use it to suppress audit events received from the checks which implement FileSetCheck interface.
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.
Properties offCommentFormat and onCommentFormat must have equal
paren counts.
SuppressionWithPlainTextCommentFilter can suppress Checks that have Treewalker or Checker 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.util.regex.Pattern.
Default value is ".*".
messageFormat - Specify message pattern to suppress.
Type is java.util.regex.Pattern.
Default value is null.
idFormat - Specify check ID pattern to suppress.
Type is java.util.regex.Pattern.
Default value is null.
To configure a filter to suppress audit events between a comment containing
CHECKSTYLE:OFF and a comment containing CHECKSTYLE:ON:
<module name="Checker"> ... <module name="SuppressWithPlainTextCommentFilter"/> ... </module>
To configure a filter to suppress audit events between a comment containing
line BEGIN GENERATED CONTENT and a comment containing line
END GENERATED CONTENT(Checker is configured to check only properties files):
<module name="Checker">
<property name="fileExtensions" value="properties"/>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="BEGIN GENERATED CONTENT"/>
<property name="onCommentFormat" value="END GENERATED CONTENT"/>
</module>
</module>
//BEGIN GENERATED CONTENT my.property=value1 // No violation events will be reported my.property=value2 // No violation events will be reported //END GENERATED CONTENT . . .
To configure a filter so that -- stop tab check and -- resume tab check
marks allowed tab positions (Checker is configured to check only sql files):
<module name="Checker">
<property name="fileExtensions" value="sql"/>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="stop tab check"/>
<property name="onCommentFormat" value="resume tab check"/>
<property name="checkFormat" value="FileTabCharacterCheck"/>
</module>
</module>
-- stop tab check SELECT * FROM users // won't warn here if there is a tab character on line -- resume tab check SELECT 1 // will warn here if there is a tab character on line
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 (Checker is configured to check only xml files):
<module name="Checker">
<property name="fileExtensions" value="xml"/>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="CSOFF\: ([\w\|]+)"/>
<property name="onCommentFormat" value="CSON\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>
</module>
// CSOFF: RegexpSinglelineCheck // RegexpSingleline check won't warn any lines below here if the line matches regexp <condition property="checkstyle.ant.skip"> <isset property="checkstyle.ant.skip"/> </condition> // CSON: RegexpSinglelineCheck // RegexpSingleline check will warn below here if the line matches regexp <property name="checkstyle.pattern.todo" value="NOTHingWillMatCH_-"/>
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 (Checker is configured to check only java files):
<module name="Checker">
<property name="fileExtensions" value="java"/>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat"
value="CHECKSTYLE_OFF: ALMOST_ALL"/>
<property name="onCommentFormat"
value="CHECKSTYLE_ON: ALMOST_ALL"/>
<property name="checkFormat"
value="^((?!(FileTabCharacterCheck)).)*$"/>
</module>
</module>
// CHECKSTYLE_OFF: ALMOST_ALL public static final int array []; private String [] strArray; // CHECKSTYLE_ON: ALMOST_ALL private int array1 [];
To configure a filter to suppress Check's violation message which matches
specified message in messageFormat(so suppression will not be only by
Check's name, but also by message text, as the same Check can report violations
with different message format) between a comment containing stop and
comment containing resume:
<module name="Checker">
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="stop"/>
<property name="onCommentFormat" value="resume"/>
<property name="checkFormat" value="FileTabCharacterCheck"/>
<property name="messageFormat"
value="^File contains tab characters (this is the first instance)\.$"/>
</module>
</module>
It is possible to specify an ID of checks, so that it can be leveraged by the
SuppressWithPlainTextCommentFilter 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="count"/>
<property name="format" value="^.*COUNT(*).*$"/>
<property name="message"
value="Don't use COUNT(*), use COUNT(1) instead."/>
</module>
<module name="RegexpSinglelineJava">
<property name="id" value="join"/>
<property name="format" value="^.*JOIN\s.+\s(ON|USING)$"/>
<property name="message"
value="Don't use JOIN, use sub-select instead."/>
</module>
Example of SuppressWithPlainTextCommentFilter 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="Checker">
<property name="fileExtensions" value="sql"/>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="CSOFF (\w+) \(\w+\)"/>
<property name="onCommentFormat" value="CSON (\w+)"/>
<property name="idFormat" value="$1"/>
</module>
</module>
-- CSOFF join (it is ok to use join here for performance reasons) SELECT name, job_name FROM users AS u JOIN jobs AS j ON u.job_id = j.id -- CSON join -- CSOFF count (test query execution plan) EXPLAIN SELECT COUNT(*) FROM restaurants -- CSON count
Example of how to configure the check to suppress more than one check (Checker is configured to check only sql files).
<module name="Checker">
<property name="fileExtensions" value="sql"/>
<module name="SuppressWithPlainTextCommentFilter">
<property name="offCommentFormat" value="@cs-\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>
</module>
-- @cs-: RegexpSinglelineCheck -- @cs-: FileTabCharacterCheck CREATE TABLE STATION ( ID INTEGER PRIMARY KEY, CITY CHAR(20), STATE CHAR(2), LAT_N REAL, LONG_W REAL);
Parent is com.puppycrawl.tools.checkstyle.Checker
| Modifier and Type | Class and Description |
|---|---|
private static class |
SuppressWithPlainTextCommentFilter.Suppression
The class which represents the suppression.
|
private static class |
SuppressWithPlainTextCommentFilter.SuppressionType
Enum which represents the type of the suppression.
|
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
checkFormat
Specify check pattern to suppress.
|
private static java.lang.String |
DEFAULT_CHECK_FORMAT
Default check format to suppress.
|
private static java.lang.String |
DEFAULT_OFF_FORMAT
Comment format which turns checkstyle reporting off.
|
private static java.lang.String |
DEFAULT_ON_FORMAT
Comment format which turns checkstyle reporting on.
|
private java.lang.String |
idFormat
Specify check ID pattern to suppress.
|
private java.lang.String |
messageFormat
Specify message pattern to suppress.
|
private java.util.regex.Pattern |
offCommentFormat
Specify comment pattern to trigger filter to begin suppression.
|
private java.util.regex.Pattern |
onCommentFormat
Specify comment pattern to trigger filter to end suppression.
|
| Constructor and Description |
|---|
SuppressWithPlainTextCommentFilter() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(AuditEvent event)
Determines whether or not a filtered AuditEvent 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.
|
private static FileText |
getFileText(java.lang.String fileName)
Returns
FileText instance created based on the given file name. |
private static SuppressWithPlainTextCommentFilter.Suppression |
getNearestSuppression(java.util.List<SuppressWithPlainTextCommentFilter.Suppression> suppressions,
AuditEvent event)
Finds the nearest
SuppressWithPlainTextCommentFilter.Suppression instance which can suppress
the given AuditEvent. |
private java.util.Optional<SuppressWithPlainTextCommentFilter.Suppression> |
getSuppression(FileText fileText,
int lineNo)
Tries to extract the suppression from the given line.
|
private java.util.List<SuppressWithPlainTextCommentFilter.Suppression> |
getSuppressions(FileText fileText)
Returns the list of
SuppressWithPlainTextCommentFilter.Suppression instances retrieved from the given FileText. |
void |
setCheckFormat(java.lang.String format)
Setter to specify check pattern to suppress.
|
void |
setIdFormat(java.lang.String format)
Setter to specify check ID pattern to suppress.
|
void |
setMessageFormat(java.lang.String format)
Setter to specify message pattern to suppress.
|
void |
setOffCommentFormat(java.util.regex.Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.
|
void |
setOnCommentFormat(java.util.regex.Pattern pattern)
Setter to specify comment pattern to trigger filter to end suppression.
|
configure, contextualize, getConfiguration, setupChildprivate static final java.lang.String DEFAULT_OFF_FORMAT
private static final java.lang.String DEFAULT_ON_FORMAT
private static final java.lang.String DEFAULT_CHECK_FORMAT
private java.util.regex.Pattern offCommentFormat
private java.util.regex.Pattern onCommentFormat
private java.lang.String checkFormat
private java.lang.String messageFormat
private java.lang.String idFormat
public SuppressWithPlainTextCommentFilter()
public final void setOffCommentFormat(java.util.regex.Pattern pattern)
pattern - off comment format pattern.public final void setOnCommentFormat(java.util.regex.Pattern pattern)
pattern - on comment format pattern.public final void setCheckFormat(java.lang.String format)
format - pattern for check format.public final void setMessageFormat(java.lang.String format)
format - pattern for message format.public final void setIdFormat(java.lang.String format)
format - pattern for check ID formatpublic boolean accept(AuditEvent event)
Filterprotected void finishLocalSetup()
AutomaticBeanThe default implementation does nothing.
finishLocalSetup in class AutomaticBeanprivate static FileText getFileText(java.lang.String fileName)
FileText instance created based on the given file name.fileName - the name of the file.FileText instance.java.lang.IllegalStateException - if the file could not be read.private java.util.List<SuppressWithPlainTextCommentFilter.Suppression> getSuppressions(FileText fileText)
SuppressWithPlainTextCommentFilter.Suppression instances retrieved from the given FileText.fileText - FileText instance.SuppressWithPlainTextCommentFilter.Suppression instances.private java.util.Optional<SuppressWithPlainTextCommentFilter.Suppression> getSuppression(FileText fileText, int lineNo)
fileText - FileText instance.lineNo - line number.Optional of SuppressWithPlainTextCommentFilter.Suppression.private static SuppressWithPlainTextCommentFilter.Suppression getNearestSuppression(java.util.List<SuppressWithPlainTextCommentFilter.Suppression> suppressions, AuditEvent event)
SuppressWithPlainTextCommentFilter.Suppression instance which can suppress
the given AuditEvent. The nearest suppression is the suppression which scope
is before the line and column of the event.suppressions - SuppressWithPlainTextCommentFilter.Suppression instance.event - AuditEvent instance.SuppressWithPlainTextCommentFilter.Suppression instance.Copyright © 2001-2022. All Rights Reserved.