Class SuppressWithPlainTextCommentFilter
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.filters.SuppressWithPlainTextCommentFilter
-
- All Implemented Interfaces:
Configurable,Contextualizable,Filter
public class SuppressWithPlainTextCommentFilter extends AutomaticBean implements Filter
Filter
SuppressWithPlainTextCommentFilteruses 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
offCommentFormatandonCommentFormatmust have equal paren counts.SuppressionWithPlainTextCommentFilter can suppress Checks that have Treewalker or Checker as parent module.
-
Property
offCommentFormat- Specify comment pattern to trigger filter to begin suppression. Type isjava.util.regex.Pattern. Default value is"// CHECKSTYLE:OFF". -
Property
onCommentFormat- Specify comment pattern to trigger filter to end suppression. Type isjava.util.regex.Pattern. Default value is"// CHECKSTYLE:ON". -
Property
checkFormat- Specify check pattern to suppress. Type isjava.util.regex.Pattern. Default value is".*". -
Property
messageFormat- Specify message pattern to suppress. Type isjava.util.regex.Pattern. Default value isnull. -
Property
idFormat- Specify check ID pattern to suppress. Type isjava.util.regex.Pattern. Default value isnull.
To configure a filter to suppress audit events between a comment containing
CHECKSTYLE:OFFand a comment containingCHECKSTYLE:ON:<module name="Checker"> ... <module name="SuppressWithPlainTextCommentFilter"/> ... </module>
To configure a filter to suppress audit events between a comment containing line
BEGIN GENERATED CONTENTand a comment containing lineEND 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 checkand-- resume tab checkmarks 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>andCSON: <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_ALLand a comment containingCHECKSTYLE_OFF: ALMOST_ALLexcept 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
stopand comment containingresume:<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- Since:
- 8.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSuppressWithPlainTextCommentFilter.SuppressionThe class which represents the suppression.private static classSuppressWithPlainTextCommentFilter.SuppressionTypeEnum which represents the type of the suppression.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringcheckFormatSpecify check pattern to suppress.private static java.lang.StringDEFAULT_CHECK_FORMATDefault check format to suppress.private static java.lang.StringDEFAULT_OFF_FORMATComment format which turns checkstyle reporting off.private static java.lang.StringDEFAULT_ON_FORMATComment format which turns checkstyle reporting on.private java.lang.StringidFormatSpecify check ID pattern to suppress.private java.lang.StringmessageFormatSpecify message pattern to suppress.private java.util.regex.PatternoffCommentFormatSpecify comment pattern to trigger filter to begin suppression.private java.util.regex.PatternonCommentFormatSpecify comment pattern to trigger filter to end suppression.
-
Constructor Summary
Constructors Constructor Description SuppressWithPlainTextCommentFilter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaccept(AuditEvent event)Determines whether or not a filtered AuditEvent is accepted.protected voidfinishLocalSetup()Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.private static FileTextgetFileText(java.lang.String fileName)ReturnsFileTextinstance created based on the given file name.private static SuppressWithPlainTextCommentFilter.SuppressiongetNearestSuppression(java.util.List<SuppressWithPlainTextCommentFilter.Suppression> suppressions, AuditEvent event)Finds the nearestSuppressWithPlainTextCommentFilter.Suppressioninstance which can suppress the givenAuditEvent.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 ofSuppressWithPlainTextCommentFilter.Suppressioninstances retrieved from the givenFileText.voidsetCheckFormat(java.lang.String format)Setter to specify check pattern to suppress.voidsetIdFormat(java.lang.String format)Setter to specify check ID pattern to suppress.voidsetMessageFormat(java.lang.String format)Setter to specify message pattern to suppress.voidsetOffCommentFormat(java.util.regex.Pattern pattern)Setter to specify comment pattern to trigger filter to begin suppression.voidsetOnCommentFormat(java.util.regex.Pattern pattern)Setter to specify comment pattern to trigger filter to end suppression.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
DEFAULT_OFF_FORMAT
private static final java.lang.String DEFAULT_OFF_FORMAT
Comment format which turns checkstyle reporting off.- See Also:
- Constant Field Values
-
DEFAULT_ON_FORMAT
private static final java.lang.String DEFAULT_ON_FORMAT
Comment format which turns checkstyle reporting on.- See Also:
- Constant Field Values
-
DEFAULT_CHECK_FORMAT
private static final java.lang.String DEFAULT_CHECK_FORMAT
Default check format to suppress. By default the filter suppress all checks.- See Also:
- Constant Field Values
-
offCommentFormat
private java.util.regex.Pattern offCommentFormat
Specify comment pattern to trigger filter to begin suppression.
-
onCommentFormat
private java.util.regex.Pattern onCommentFormat
Specify comment pattern to trigger filter to end suppression.
-
checkFormat
private java.lang.String checkFormat
Specify check pattern to suppress.
-
messageFormat
private java.lang.String messageFormat
Specify message pattern to suppress.
-
idFormat
private java.lang.String idFormat
Specify check ID pattern to suppress.
-
-
Constructor Detail
-
SuppressWithPlainTextCommentFilter
public SuppressWithPlainTextCommentFilter()
-
-
Method Detail
-
setOffCommentFormat
public final void setOffCommentFormat(java.util.regex.Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.- Parameters:
pattern- off comment format pattern.
-
setOnCommentFormat
public final void setOnCommentFormat(java.util.regex.Pattern pattern)
Setter to specify comment pattern to trigger filter to end suppression.- Parameters:
pattern- on comment format pattern.
-
setCheckFormat
public final void setCheckFormat(java.lang.String format)
Setter to specify check pattern to suppress.- Parameters:
format- pattern for check format.
-
setMessageFormat
public final void setMessageFormat(java.lang.String format)
Setter to specify message pattern to suppress.- Parameters:
format- pattern for message format.
-
setIdFormat
public final void setIdFormat(java.lang.String format)
Setter to specify check ID pattern to suppress.- Parameters:
format- pattern for check ID format
-
accept
public boolean accept(AuditEvent event)
Description copied from interface:FilterDetermines whether or not a filtered AuditEvent is accepted.
-
finishLocalSetup
protected void finishLocalSetup()
Description copied from class:AutomaticBeanProvides a hook to finish the part of this component's setup that was not handled by the bean introspection.The default implementation does nothing.
- Specified by:
finishLocalSetupin classAutomaticBean
-
getFileText
private static FileText getFileText(java.lang.String fileName)
ReturnsFileTextinstance created based on the given file name.- Parameters:
fileName- the name of the file.- Returns:
FileTextinstance.- Throws:
java.lang.IllegalStateException- if the file could not be read.
-
getSuppressions
private java.util.List<SuppressWithPlainTextCommentFilter.Suppression> getSuppressions(FileText fileText)
Returns the list ofSuppressWithPlainTextCommentFilter.Suppressioninstances retrieved from the givenFileText.- Parameters:
fileText-FileTextinstance.- Returns:
- list of
SuppressWithPlainTextCommentFilter.Suppressioninstances.
-
getSuppression
private java.util.Optional<SuppressWithPlainTextCommentFilter.Suppression> getSuppression(FileText fileText, int lineNo)
Tries to extract the suppression from the given line.- Parameters:
fileText-FileTextinstance.lineNo- line number.- Returns:
OptionalofSuppressWithPlainTextCommentFilter.Suppression.
-
getNearestSuppression
private static SuppressWithPlainTextCommentFilter.Suppression getNearestSuppression(java.util.List<SuppressWithPlainTextCommentFilter.Suppression> suppressions, AuditEvent event)
Finds the nearestSuppressWithPlainTextCommentFilter.Suppressioninstance which can suppress the givenAuditEvent. The nearest suppression is the suppression which scope is before the line and column of the event.- Parameters:
suppressions-SuppressWithPlainTextCommentFilter.Suppressioninstance.event-AuditEventinstance.- Returns:
SuppressWithPlainTextCommentFilter.Suppressioninstance.
-
-