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.
FileContentsHolder
Modifier and Type | Class and Description |
---|---|
static class |
SuppressionCommentFilter.Tag
A Tag holds a suppression comment and its location, and determines
whether the suppression turns checkstyle reporting on or off.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
checkC
Whether to look in comments of the C type.
|
private boolean |
checkCPP
Whether to look in comments of the C++ type.
|
private java.lang.String |
checkFormat
The check format to suppress.
|
private static java.lang.String |
DEFAULT_CHECK_FORMAT
Control all checks.
|
private static java.lang.String |
DEFAULT_OFF_FORMAT
Turns checkstyle reporting off.
|
private static java.lang.String |
DEFAULT_ON_FORMAT
Turns checkstyle reporting on.
|
private java.lang.ref.WeakReference<FileContents> |
fileContentsReference
References the current FileContents for this filter.
|
private java.lang.String |
messageFormat
The message format to suppress.
|
private java.util.regex.Pattern |
offCommentFormat
Parsed comment regexp that turns checkstyle reporting off.
|
private java.util.regex.Pattern |
onCommentFormat
Parsed comment regexp that turns checkstyle reporting on.
|
private java.util.List<SuppressionCommentFilter.Tag> |
tags
Tagged comments.
|
Constructor and Description |
---|
SuppressionCommentFilter() |
Modifier and Type | Method and Description |
---|---|
boolean |
accept(AuditEvent event)
Determines whether or not a filtered AuditEvent is accepted.
|
private void |
addTag(java.lang.String text,
int line,
int column,
boolean reportingOn)
Adds a
Tag to the list of all tags. |
private SuppressionCommentFilter.Tag |
findNearestMatch(AuditEvent event)
Finds the nearest comment text tag that matches an audit event.
|
FileContents |
getFileContents()
Returns FileContents for this filter.
|
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 |
setFileContents(FileContents fileContents)
Set the FileContents for this filter.
|
void |
setMessageFormat(java.lang.String format)
Set the format for a message.
|
void |
setOffCommentFormat(java.util.regex.Pattern pattern)
Set the format for a comment that turns off reporting.
|
void |
setOnCommentFormat(java.util.regex.Pattern pattern)
Set the format for a comment that turns on reporting.
|
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.
|
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.
|
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
private static final java.lang.String DEFAULT_OFF_FORMAT
private static final java.lang.String DEFAULT_ON_FORMAT
private static final java.lang.String DEFAULT_CHECK_FORMAT
private final java.util.List<SuppressionCommentFilter.Tag> tags
private boolean checkC
private boolean checkCPP
private java.util.regex.Pattern offCommentFormat
private java.util.regex.Pattern onCommentFormat
private java.lang.String checkFormat
private java.lang.String messageFormat
private java.lang.ref.WeakReference<FileContents> fileContentsReference
public final void setOffCommentFormat(java.util.regex.Pattern pattern)
pattern
- a pattern.public final void setOnCommentFormat(java.util.regex.Pattern pattern)
pattern
- a pattern.public FileContents getFileContents()
public void setFileContents(FileContents fileContents)
fileContents
- the FileContents for this filter.public final void setCheckFormat(java.lang.String format)
format
- a String
valuepublic void setMessageFormat(java.lang.String format)
format
- a String
valuepublic void setCheckCPP(boolean checkCpp)
checkCpp
- true
if C++ comments are checked.public void setCheckC(boolean checkC)
checkC
- true
if C comments are checked.public boolean accept(AuditEvent event)
Filter
private SuppressionCommentFilter.Tag findNearestMatch(AuditEvent event)
event
- the AuditEvent
to match.Tag
nearest event.private void tagSuppressions()
private void tagSuppressions(java.util.Collection<TextBlock> comments)
comments
- the set of comments.private void tagCommentLine(java.lang.String text, int line, int column)
text
- the string to tag.line
- the line number of text.column
- the column number of text.private void addTag(java.lang.String text, int line, int column, boolean reportingOn)
Tag
to the list of all tags.text
- the text of the tag.line
- the line number of the tag.column
- the column number of the tag.reportingOn
- true
if the tag turns checkstyle reporting on.