Class SuppressWithPlainTextCommentFilter
- All Implemented Interfaces:
Configurable
,Contextualizable
,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.
-
Property
checkFormat
- Specify check pattern to suppress. Type isjava.util.regex.Pattern
. Default value is".*"
. -
Property
idFormat
- Specify check ID pattern to suppress. Type isjava.util.regex.Pattern
. Default value isnull
. -
Property
messageFormat
- Specify message pattern to suppress. Type isjava.util.regex.Pattern
. Default value isnull
. -
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"
.
Parent is com.puppycrawl.tools.checkstyle.Checker
- Since:
- 8.6
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
The class which represents the suppression.private static enum
Enum which represents the type of the suppression.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
Specify check pattern to suppress.private static final String
Default check format to suppress.private static final String
Comment format which turns checkstyle reporting off.private static final String
Comment format which turns checkstyle reporting on.private String
Specify check ID pattern to suppress.private String
Specify message pattern to suppress.private Pattern
Specify comment pattern to trigger filter to begin suppression.private Pattern
Specify comment pattern to trigger filter to end suppression. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
accept
(AuditEvent event) Determines whether or not a filtered AuditEvent is accepted.protected void
Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.private static FileText
getFileText
(String fileName) ReturnsFileText
instance created based on the given file name.private static SuppressWithPlainTextCommentFilter.Suppression
getNearestSuppression
(Collection<SuppressWithPlainTextCommentFilter.Suppression> suppressions, AuditEvent event) Finds the nearestSuppressWithPlainTextCommentFilter.Suppression
instance which can suppress the givenAuditEvent
.getSuppression
(FileText fileText, int lineNo) Tries to extract the suppression from the given line.getSuppressions
(FileText fileText) Returns the list ofSuppressWithPlainTextCommentFilter.Suppression
instances retrieved from the givenFileText
.final void
setCheckFormat
(String format) Setter to specify check pattern to suppress.final void
setIdFormat
(String format) Setter to specify check ID pattern to suppress.final void
setMessageFormat
(String format) Setter to specify message pattern to suppress.final void
setOffCommentFormat
(Pattern pattern) Setter to specify comment pattern to trigger filter to begin suppression.final void
setOnCommentFormat
(Pattern pattern) Setter to specify comment pattern to trigger filter to end suppression.Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
DEFAULT_OFF_FORMAT
Comment format which turns checkstyle reporting off.- See Also:
-
DEFAULT_ON_FORMAT
Comment format which turns checkstyle reporting on.- See Also:
-
DEFAULT_CHECK_FORMAT
Default check format to suppress. By default, the filter suppress all checks.- See Also:
-
offCommentFormat
Specify comment pattern to trigger filter to begin suppression. -
onCommentFormat
Specify comment pattern to trigger filter to end suppression. -
checkFormat
Specify check pattern to suppress. -
messageFormat
Specify message pattern to suppress. -
idFormat
Specify check ID pattern to suppress.
-
-
Constructor Details
-
SuppressWithPlainTextCommentFilter
public SuppressWithPlainTextCommentFilter()
-
-
Method Details
-
setOffCommentFormat
Setter to specify comment pattern to trigger filter to begin suppression.- Parameters:
pattern
- off comment format pattern.- Since:
- 8.6
-
setOnCommentFormat
Setter to specify comment pattern to trigger filter to end suppression.- Parameters:
pattern
- on comment format pattern.- Since:
- 8.6
-
setCheckFormat
Setter to specify check pattern to suppress.- Parameters:
format
- pattern for check format.- Since:
- 8.6
-
setMessageFormat
Setter to specify message pattern to suppress.- Parameters:
format
- pattern for message format.- Since:
- 8.6
-
setIdFormat
Setter to specify check ID pattern to suppress.- Parameters:
format
- pattern for check ID format- Since:
- 8.24
-
accept
Description copied from interface:Filter
Determines whether or not a filtered AuditEvent is accepted. -
finishLocalSetup
Description copied from class:AbstractAutomaticBean
Provides 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:
finishLocalSetup
in classAbstractAutomaticBean
-
getFileText
ReturnsFileText
instance created based on the given file name.- Parameters:
fileName
- the name of the file.- Returns:
FileText
instance.- Throws:
IllegalStateException
- if the file could not be read.
-
getSuppressions
Returns the list ofSuppressWithPlainTextCommentFilter.Suppression
instances retrieved from the givenFileText
.- Parameters:
fileText
-FileText
instance.- Returns:
- list of
SuppressWithPlainTextCommentFilter.Suppression
instances.
-
getSuppression
private Optional<SuppressWithPlainTextCommentFilter.Suppression> getSuppression(FileText fileText, int lineNo) Tries to extract the suppression from the given line.- Parameters:
fileText
-FileText
instance.lineNo
- line number.- Returns:
Optional
ofSuppressWithPlainTextCommentFilter.Suppression
.
-
getNearestSuppression
private static SuppressWithPlainTextCommentFilter.Suppression getNearestSuppression(Collection<SuppressWithPlainTextCommentFilter.Suppression> suppressions, AuditEvent event) Finds the nearestSuppressWithPlainTextCommentFilter.Suppression
instance which can suppress the givenAuditEvent
. The nearest suppression is the suppression which scope is before the line and column of the event.- Parameters:
suppressions
- collection ofSuppressWithPlainTextCommentFilter.Suppression
instances.event
-AuditEvent
instance.- Returns:
SuppressWithPlainTextCommentFilter.Suppression
instance.
-