Class SuppressWithPlainTextCommentFilter
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.filters.SuppressWithPlainTextCommentFilter
-
- All Implemented Interfaces:
Configurable,Contextualizable,Filter
public class SuppressWithPlainTextCommentFilter extends AbstractAutomaticBean 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
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 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.AbstractAutomaticBean
AbstractAutomaticBean.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.Collection<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.AbstractAutomaticBean
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.- Since:
- 8.6
-
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.- Since:
- 8.6
-
setCheckFormat
public final void setCheckFormat(java.lang.String format)
Setter to specify check pattern to suppress.- Parameters:
format- pattern for check format.- Since:
- 8.6
-
setMessageFormat
public final void setMessageFormat(java.lang.String format)
Setter to specify message pattern to suppress.- Parameters:
format- pattern for message format.- Since:
- 8.6
-
setIdFormat
public final void setIdFormat(java.lang.String format)
Setter to specify check ID pattern to suppress.- Parameters:
format- pattern for check ID format- Since:
- 8.24
-
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: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
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.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.
-
-