Class SuppressWithNearbyTextFilter

java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.filters.SuppressWithNearbyTextFilter
All Implemented Interfaces:
Configurable, Contextualizable, Filter

public class SuppressWithNearbyTextFilter extends AbstractAutomaticBean implements Filter
Filter SuppressWithNearbyTextFilter uses plain text to suppress nearby audit events. The filter can suppress all checks which have Checker as a parent module.

Notes: Setting .* value to nearbyTextPattern property will see any text as a suppression and will likely suppress all audit events in the file. It is best to set this to a key phrase not commonly used in the file to help denote it out of the rest of the file as a suppression. See the default value as an example.

Since:
10.10.0
  • Field Details

    • DEFAULT_NEARBY_TEXT_PATTERN

      private static final String DEFAULT_NEARBY_TEXT_PATTERN
      Default nearby text pattern to turn check reporting off.
      See Also:
    • DEFAULT_CHECK_PATTERN

      private static final String DEFAULT_CHECK_PATTERN
      Default regex for checks that should be suppressed.
      See Also:
    • DEFAULT_LINE_RANGE

      private static final String DEFAULT_LINE_RANGE
      Default number of lines that should be suppressed.
      See Also:
    • suppressions

      private final List<SuppressWithNearbyTextFilter.Suppression> suppressions
      Suppressions encountered in current file.
    • nearbyTextPattern

      private Pattern nearbyTextPattern
      Specify nearby text pattern to trigger filter to begin suppression.
    • checkPattern

      private String checkPattern
      Specify check name pattern to suppress. Property can also be a RegExp group index at nearbyTextPattern in format of $x and be picked from line that matches nearbyTextPattern.
    • messagePattern

      private String messagePattern
      Specify check violation message pattern to suppress.
    • idPattern

      private String idPattern
      Specify check ID pattern to suppress.
    • lineRange

      private String lineRange
      Specify negative/zero/positive value that defines the number of lines preceding/at/following the suppressing nearby text. Property can also be a RegExp group index at nearbyTextPattern in format of $x and be picked from line that matches nearbyTextPattern.
    • cachedFileAbsolutePath

      private String cachedFileAbsolutePath
      The absolute path to the currently processed file.
  • Constructor Details

    • SuppressWithNearbyTextFilter

      public SuppressWithNearbyTextFilter()
  • Method Details

    • setNearbyTextPattern

      public final void setNearbyTextPattern(Pattern pattern)
      Setter to specify nearby text pattern to trigger filter to begin suppression.
      Parameters:
      pattern - a Pattern value.
      Since:
      10.10.0
    • setCheckPattern

      public final void setCheckPattern(String pattern)
      Setter to specify check name pattern to suppress. Property can also be a RegExp group index at nearbyTextPattern in format of $x and be picked from line that matches nearbyTextPattern.
      Parameters:
      pattern - a String value.
      Since:
      10.10.0
    • setMessagePattern

      public void setMessagePattern(String pattern)
      Setter to specify check violation message pattern to suppress.
      Parameters:
      pattern - a String value.
      Since:
      10.10.0
    • setIdPattern

      public void setIdPattern(String pattern)
      Setter to specify check ID pattern to suppress.
      Parameters:
      pattern - a String value.
      Since:
      10.10.0
    • setLineRange

      public final void setLineRange(String format)
      Setter to specify negative/zero/positive value that defines the number of lines preceding/at/following the suppressing nearby text. Property can also be a RegExp group index at nearbyTextPattern in format of $x and be picked from line that matches nearbyTextPattern.
      Parameters:
      format - a String value.
      Since:
      10.10.0
    • accept

      public boolean accept(AuditEvent event)
      Description copied from interface: Filter
      Determines whether or not a filtered AuditEvent is accepted.
      Specified by:
      accept in interface Filter
      Parameters:
      event - the AuditEvent to filter.
      Returns:
      true if the event is accepted.
    • 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)
      Returns 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.
    • collectSuppressions

      private void collectSuppressions(FileText fileText)
      Collets all SuppressWithNearbyTextFilter.Suppression instances retrieved from the given FileText.
      Parameters:
      fileText - FileText instance.
    • getSuppression

      private SuppressWithNearbyTextFilter.Suppression getSuppression(FileText fileText, int lineNo)
      Tries to extract the suppression from the given line.
      Parameters:
      fileText - FileText instance.
      lineNo - line number.
      Returns:
      SuppressWithNearbyTextFilter.Suppression instance.
    • getNearestSuppression

      Finds the nearest SuppressWithNearbyTextFilter.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 SuppressWithNearbyTextFilter.Suppression instances.
      event - AuditEvent instance.
      Returns:
      SuppressWithNearbyTextFilter.Suppression instance.