Class SuppressWithNearbyCommentFilter

  • All Implemented Interfaces:
    Configurable, Contextualizable, Filter

    public class SuppressWithNearbyCommentFilter
    extends AutomaticBean
    implements Filter

    A filter that uses nearby comments to suppress audit events.

    This check is philosophically similar to SuppressionCommentFilter. Unlike SuppressionCommentFilter, this filter does not require pairs of comments. This check may be used to suppress warnings in the current line:

        offendingLine(for, whatever, reason); // SUPPRESS ParameterNumberCheck
     
    or it may be configured to span multiple lines, either forward:
        // PERMIT MultipleVariableDeclarations NEXT 3 LINES
        double x1 = 1.0, y1 = 0.0, z1 = 0.0;
        double x2 = 0.0, y2 = 1.0, z2 = 0.0;
        double x3 = 0.0, y3 = 0.0, z3 = 1.0;
     
    or reverse:
       try {
         thirdPartyLibrary.method();
       } catch (RuntimeException ex) {
         // ALLOW ILLEGAL CATCH BECAUSE third party API wraps everything
         // in RuntimeExceptions.
         ...
       }
     

    See SuppressionCommentFilter for usage notes.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean checkC
      Whether to look for trigger in C-style comments.
      private boolean checkCPP
      Whether to look for trigger in C++-style comments.
      private java.lang.String checkFormat
      The comment pattern that triggers suppression.
      private java.util.regex.Pattern commentFormat
      Parsed comment regexp that marks checkstyle suppression region.
      private static java.lang.String DEFAULT_CHECK_FORMAT
      Default regex for checks that should be suppressed.
      private static java.lang.String DEFAULT_COMMENT_FORMAT
      Format to turns checkstyle reporting off.
      private static java.lang.String DEFAULT_INFLUENCE_FORMAT
      Default regex for lines that should be suppressed.
      private java.lang.ref.WeakReference<FileContents> fileContentsReference
      References the current FileContents for this filter.
      private java.lang.String influenceFormat
      The influence of the suppression comment.
      private java.lang.String messageFormat
      The message format to suppress.
      private java.util.List<SuppressWithNearbyCommentFilter.Tag> tags
      Tagged comments.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean accept​(AuditEvent event)
      Determines whether or not a filtered AuditEvent is accepted.
      private void addTag​(java.lang.String text, int line)
      Adds a comment suppression Tag to the list of all tags.
      FileContents getFileContents()
      Returns FileContents for this filter.
      private boolean matchesTag​(AuditEvent event)
      Whether current event matches any tag from tags.
      void setCheckC​(boolean checkC)
      Set whether to look in C comments.
      void setCheckCPP​(boolean checkCpp)
      Set whether to look in C++ comments.
      void setCheckFormat​(java.lang.String format)
      Set the format for a check.
      void setCommentFormat​(java.util.regex.Pattern pattern)
      Set the format for a comment that turns off reporting.
      void setFileContents​(FileContents fileContents)
      Set the FileContents for this filter.
      void setInfluenceFormat​(java.lang.String format)
      Set the format for the influence of this check.
      void setMessageFormat​(java.lang.String format)
      Set the format for a message.
      private void tagCommentLine​(java.lang.String text, int line)
      Tags a string if it matches the format for turning checkstyle reporting on or the format for turning reporting off.
      private void tagSuppressions()
      Collects all the suppression tags for all comments into a list and sorts the list.
      private void tagSuppressions​(java.util.Collection<TextBlock> comments)
      Appends the suppressions in a collection of comments to the full set of suppression tags.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_COMMENT_FORMAT

        private static final java.lang.String DEFAULT_COMMENT_FORMAT
        Format to turns checkstyle reporting off.
        See Also:
        Constant Field Values
      • DEFAULT_CHECK_FORMAT

        private static final java.lang.String DEFAULT_CHECK_FORMAT
        Default regex for checks that should be suppressed.
        See Also:
        Constant Field Values
      • DEFAULT_INFLUENCE_FORMAT

        private static final java.lang.String DEFAULT_INFLUENCE_FORMAT
        Default regex for lines that should be suppressed.
        See Also:
        Constant Field Values
      • checkC

        private boolean checkC
        Whether to look for trigger in C-style comments.
      • checkCPP

        private boolean checkCPP
        Whether to look for trigger in C++-style comments.
      • commentFormat

        private java.util.regex.Pattern commentFormat
        Parsed comment regexp that marks checkstyle suppression region.
      • checkFormat

        private java.lang.String checkFormat
        The comment pattern that triggers suppression.
      • messageFormat

        private java.lang.String messageFormat
        The message format to suppress.
      • influenceFormat

        private java.lang.String influenceFormat
        The influence of the suppression comment.
      • fileContentsReference

        private java.lang.ref.WeakReference<FileContents> fileContentsReference
        References the current FileContents for this filter. Since this is a weak reference to the FileContents, the FileContents can be reclaimed as soon as the strong references in TreeWalker and FileContentsHolder are reassigned to the next FileContents, at which time filtering for the current FileContents is finished.
    • Constructor Detail

      • SuppressWithNearbyCommentFilter

        public SuppressWithNearbyCommentFilter()
    • Method Detail

      • setCommentFormat

        public final void setCommentFormat​(java.util.regex.Pattern pattern)
        Set the format for a comment that turns off reporting.
        Parameters:
        pattern - a pattern.
      • getFileContents

        public FileContents getFileContents()
        Returns FileContents for this filter.
        Returns:
        the FileContents for this filter.
      • setFileContents

        public void setFileContents​(FileContents fileContents)
        Set the FileContents for this filter.
        Parameters:
        fileContents - the FileContents for this filter.
      • setCheckFormat

        public final void setCheckFormat​(java.lang.String format)
        Set the format for a check.
        Parameters:
        format - a String value
      • setMessageFormat

        public void setMessageFormat​(java.lang.String format)
        Set the format for a message.
        Parameters:
        format - a String value
      • setInfluenceFormat

        public final void setInfluenceFormat​(java.lang.String format)
        Set the format for the influence of this check.
        Parameters:
        format - a String value
      • setCheckCPP

        public void setCheckCPP​(boolean checkCpp)
        Set whether to look in C++ comments.
        Parameters:
        checkCpp - true if C++ comments are checked.
      • setCheckC

        public void setCheckC​(boolean checkC)
        Set whether to look in C comments.
        Parameters:
        checkC - true if C comments are checked.
      • 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.
      • matchesTag

        private boolean matchesTag​(AuditEvent event)
        Whether current event matches any tag from tags.
        Parameters:
        event - AuditEvent to test match on tags.
        Returns:
        true if event matches any tag from tags, false otherwise.
      • tagSuppressions

        private void tagSuppressions()
        Collects all the suppression tags for all comments into a list and sorts the list.
      • tagSuppressions

        private void tagSuppressions​(java.util.Collection<TextBlock> comments)
        Appends the suppressions in a collection of comments to the full set of suppression tags.
        Parameters:
        comments - the set of comments.
      • tagCommentLine

        private void tagCommentLine​(java.lang.String text,
                                    int line)
        Tags a string if it matches the format for turning checkstyle reporting on or the format for turning reporting off.
        Parameters:
        text - the string to tag.
        line - the line number of text.
      • addTag

        private void addTag​(java.lang.String text,
                            int line)
        Adds a comment suppression Tag to the list of all tags.
        Parameters:
        text - the text of the tag.
        line - the line number of the tag.