Class SuppressionCommentFilter
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter
-
- All Implemented Interfaces:
Configurable,Contextualizable,Filter
public class SuppressionCommentFilter extends AutomaticBean implements Filter
A filter that uses comments to suppress audit events.
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.
Usage: This check only works in conjunction with the FileContentsHolder module since that module makes the suppression comments in the .java files available sub rosa.
- See Also:
FileContentsHolder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSuppressionCommentFilter.TagA Tag holds a suppression comment and its location, and determines whether the suppression turns checkstyle reporting on or off.
-
Field Summary
Fields Modifier and Type Field Description private booleancheckCWhether to look in comments of the C type.private booleancheckCPPWhether to look in comments of the C++ type.private java.lang.StringcheckFormatThe check format to suppress.private static java.lang.StringDEFAULT_CHECK_FORMATControl all checks.private static java.lang.StringDEFAULT_OFF_FORMATTurns checkstyle reporting off.private static java.lang.StringDEFAULT_ON_FORMATTurns checkstyle reporting on.private java.lang.ref.WeakReference<FileContents>fileContentsReferenceReferences the current FileContents for this filter.private java.lang.StringmessageFormatThe message format to suppress.private java.util.regex.PatternoffCommentFormatParsed comment regexp that turns checkstyle reporting off.private java.util.regex.PatternonCommentFormatParsed comment regexp that turns checkstyle reporting on.private java.util.List<SuppressionCommentFilter.Tag>tagsTagged comments.
-
Constructor Summary
Constructors Constructor Description SuppressionCommentFilter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaccept(AuditEvent event)Determines whether or not a filtered AuditEvent is accepted.private voidaddTag(java.lang.String text, int line, int column, boolean reportingOn)Adds aTagto the list of all tags.private SuppressionCommentFilter.TagfindNearestMatch(AuditEvent event)Finds the nearest comment text tag that matches an audit event.FileContentsgetFileContents()Returns FileContents for this filter.voidsetCheckC(boolean checkC)Set whether to look in C comments.voidsetCheckCPP(boolean checkCpp)Set whether to look in C++ comments.voidsetCheckFormat(java.lang.String format)Set the format for a check.voidsetFileContents(FileContents fileContents)Set the FileContents for this filter.voidsetMessageFormat(java.lang.String format)Set the format for a message.voidsetOffCommentFormat(java.util.regex.Pattern pattern)Set the format for a comment that turns off reporting.voidsetOnCommentFormat(java.util.regex.Pattern pattern)Set the format for a comment that turns on reporting.private voidtagCommentLine(java.lang.String text, int line, int column)Tags a string if it matches the format for turning checkstyle reporting on or the format for turning reporting off.private voidtagSuppressions()Collects all the suppression tags for all comments into a list and sorts the list.private voidtagSuppressions(java.util.Collection<TextBlock> comments)Appends the suppressions in a collection of comments to the full set of suppression tags.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
DEFAULT_OFF_FORMAT
private static final java.lang.String DEFAULT_OFF_FORMAT
Turns checkstyle reporting off.- See Also:
- Constant Field Values
-
DEFAULT_ON_FORMAT
private static final java.lang.String DEFAULT_ON_FORMAT
Turns checkstyle reporting on.- See Also:
- Constant Field Values
-
DEFAULT_CHECK_FORMAT
private static final java.lang.String DEFAULT_CHECK_FORMAT
Control all checks.- See Also:
- Constant Field Values
-
tags
private final java.util.List<SuppressionCommentFilter.Tag> tags
Tagged comments.
-
checkC
private boolean checkC
Whether to look in comments of the C type.
-
checkCPP
private boolean checkCPP
Whether to look in comments of the C++ type.
-
offCommentFormat
private java.util.regex.Pattern offCommentFormat
Parsed comment regexp that turns checkstyle reporting off.
-
onCommentFormat
private java.util.regex.Pattern onCommentFormat
Parsed comment regexp that turns checkstyle reporting on.
-
checkFormat
private java.lang.String checkFormat
The check format to suppress.
-
messageFormat
private java.lang.String messageFormat
The message format to suppress.
-
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.
-
-
Method Detail
-
setOffCommentFormat
public final void setOffCommentFormat(java.util.regex.Pattern pattern)
Set the format for a comment that turns off reporting.- Parameters:
pattern- a pattern.
-
setOnCommentFormat
public final void setOnCommentFormat(java.util.regex.Pattern pattern)
Set the format for a comment that turns on 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- aStringvalue
-
setMessageFormat
public void setMessageFormat(java.lang.String format)
Set the format for a message.- Parameters:
format- aStringvalue
-
setCheckCPP
public void setCheckCPP(boolean checkCpp)
Set whether to look in C++ comments.- Parameters:
checkCpp-trueif C++ comments are checked.
-
setCheckC
public void setCheckC(boolean checkC)
Set whether to look in C comments.- Parameters:
checkC-trueif C comments are checked.
-
accept
public boolean accept(AuditEvent event)
Description copied from interface:FilterDetermines whether or not a filtered AuditEvent is accepted.
-
findNearestMatch
private SuppressionCommentFilter.Tag findNearestMatch(AuditEvent event)
Finds the nearest comment text tag that matches an audit event. The nearest tag is before the line and column of the event.- Parameters:
event- theAuditEventto match.- Returns:
- The
Tagnearest event.
-
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, int column)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.column- the column number of text.
-
addTag
private void addTag(java.lang.String text, int line, int column, boolean reportingOn)Adds aTagto the list of all tags.- Parameters:
text- the text of the tag.line- the line number of the tag.column- the column number of the tag.reportingOn-trueif the tag turns checkstyle reporting on.
-
-