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.
Notes:
Properties offCommentFormat and onCommentFormat must have equal
paren counts.
SuppressionWithPlainTextCommentFilter can suppress Checks that have Treewalker or Checker as parent module.
- Since:
- 8.6
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classThe class which represents the suppression.private static enumEnum 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 StringSpecify check pattern to suppress.private StringFile name that was suppressed.private final Collection<SuppressWithPlainTextCommentFilter.Suppression>List of suppressions from the file.private static final StringDefault check format to suppress.private static final StringComment format which turns checkstyle reporting off.private static final StringComment format which turns checkstyle reporting on.private StringSpecify check ID pattern to suppress.private StringSpecify message pattern to suppress.private PatternSpecify comment pattern to trigger filter to begin suppression.private PatternSpecify comment pattern to trigger filter to end suppression. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaccept(AuditEvent event) Determines whether or not a filtered AuditEvent is accepted.private voidcacheSuppressions(FileText fileText) Collects the list ofSuppressWithPlainTextCommentFilter.Suppressioninstances retrieved from the givenFileText.protected voidProvides a hook to finish the part of this component's setup that was not handled by the bean introspection.private static FileTextgetFileText(String fileName) CachesFileTextinstance created based on the given file name.private static SuppressWithPlainTextCommentFilter.SuppressiongetNearestSuppression(Collection<SuppressWithPlainTextCommentFilter.Suppression> suppressions, AuditEvent event) Finds the nearestSuppressWithPlainTextCommentFilter.Suppressioninstance which can suppress the givenAuditEvent.getSuppression(FileText fileText, int lineNo) Tries to extract the suppression from the given line.final voidsetCheckFormat(String format) Setter to specify check pattern to suppress.final voidsetIdFormat(String format) Setter to specify check ID pattern to suppress.final voidsetMessageFormat(String format) Setter to specify message pattern to suppress.final voidsetOffCommentFormat(Pattern pattern) Setter to specify comment pattern to trigger filter to begin suppression.final voidsetOnCommentFormat(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:
-
currentFileSuppressionCache
private final Collection<SuppressWithPlainTextCommentFilter.Suppression> currentFileSuppressionCacheList of suppressions from the file. By default, Its null. -
currentFileName
File name that was suppressed. By default, Its empty. -
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:FilterDetermines whether or not a filtered AuditEvent is accepted. -
finishLocalSetup
Description copied from class:AbstractAutomaticBeanProvides 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 classAbstractAutomaticBean
-
getFileText
CachesFileTextinstance created based on the given file name.- Parameters:
fileName- the name of the file.- Returns:
FileTextinstance.- Throws:
IllegalStateException- if the file could not be read.
-
cacheSuppressions
Collects the list ofSuppressWithPlainTextCommentFilter.Suppressioninstances retrieved from the givenFileText.- Parameters:
fileText-FileTextinstance.
-
getSuppression
private 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(Collection<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- collection ofSuppressWithPlainTextCommentFilter.Suppressioninstances.event-AuditEventinstance.- Returns:
SuppressWithPlainTextCommentFilter.Suppressioninstance.
-