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 SuppressWithPlainTextCommentFilter uses plain text to suppress audit events. 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.

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.

SuppressWithPlainTextCommentFilter can suppress Checks that have Treewalker or Checker as parent module.

Since:
8.6
  • Field Details

    • DEFAULT_OFF_FORMAT

      private static final String DEFAULT_OFF_FORMAT
      Comment format which turns checkstyle reporting off.
      See Also:
    • DEFAULT_ON_FORMAT

      private static final String DEFAULT_ON_FORMAT
      Comment format which turns checkstyle reporting on.
      See Also:
    • DEFAULT_CHECK_FORMAT

      private static final String DEFAULT_CHECK_FORMAT
      Default check format to suppress. By default, the filter suppress all checks.
      See Also:
    • currentFileSuppressionCache

      private final Collection<SuppressWithPlainTextCommentFilter.Suppression> currentFileSuppressionCache
      List of suppressions from the file. By default, Its null.
    • currentFileName

      private String currentFileName
      File name that was suppressed. By default, Its empty.
    • offCommentFormat

      private Pattern offCommentFormat
      Specify comment pattern to trigger filter to begin suppression.
    • onCommentFormat

      private Pattern onCommentFormat
      Specify comment pattern to trigger filter to end suppression.
    • checkFormat

      private String checkFormat
      Specify check pattern to suppress.
    • messageFormat

      private String messageFormat
      Specify message pattern to suppress.
    • idFormat

      private String idFormat
      Specify check ID pattern to suppress.
  • Constructor Details

    • SuppressWithPlainTextCommentFilter

      public SuppressWithPlainTextCommentFilter()
      Creates a new SuppressWithPlainTextCommentFilter instance.
  • Method Details

    • setOffCommentFormat

      public final void setOffCommentFormat(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(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(String format)
      Setter to specify check pattern to suppress. The pattern is matched against the fully qualified class name of the Check.
      Parameters:
      format - pattern for check format.
      Since:
      8.6
    • setMessageFormat

      public final void setMessageFormat(String format)
      Setter to specify message pattern to suppress.
      Parameters:
      format - pattern for message format.
      Since:
      8.6
    • setIdFormat

      public final void setIdFormat(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: Filter
      Determines whether or not a filtered AuditEvent is accepted. An accepted event is kept in the final report as a violation; a rejected event is filtered out. In other words, returning true makes the violation appear in the report, while returning false suppresses it.
      Specified by:
      accept in interface Filter
      Parameters:
      event - the AuditEvent to filter.
      Returns:
      true if the event is accepted and its violation should appear in the final report; false to suppress the event.
    • finishLocalSetup

      protected void 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 class AbstractAutomaticBean
    • getFileText

      private static FileText getFileText(String fileName)
      Caches FileText 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.
    • cacheSuppressions

      private void cacheSuppressions(FileText fileText)
      Collects the list of SuppressWithPlainTextCommentFilter.Suppression instances retrieved from the given FileText.
      Parameters:
      fileText - FileText instance.
    • 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 of SuppressWithPlainTextCommentFilter.Suppression.
    • getNearestSuppression

      Finds the nearest 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.
      Parameters:
      suppressions - collection of Suppression instances.
      event - AuditEvent instance.
      Returns:
      Suppression instance.