Class SuppressionCommentFilter
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,TreeWalkerFilter
public class SuppressionCommentFilter extends AbstractAutomaticBean implements TreeWalkerFilter
FilterSuppressionCommentFilter
uses pairs of 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.
Note that the suppression comment should be put before the violation. You can use more than one suppression comment each on separate line.
Attention: This filter may only be specified within the TreeWalker module (
<module name="TreeWalker"/>
) and only applies to checks which are also defined within this module. To filter non-TreeWalker checks likeRegexpSingleline
, a SuppressWithPlainTextCommentFilter or similar filter must be used.offCommentFormat
andonCommentFormat
must have equal paren counts.SuppressionCommentFilter can suppress Checks that have Treewalker as parent module.
-
Property
checkC
- Control whether to check C style comments (/* ... */
). Type isboolean
. Default value istrue
. -
Property
checkCPP
- Control whether to check C++ style comments (//
). Type isboolean
. Default value istrue
. -
Property
checkFormat
- Specify check pattern to suppress. Type isjava.util.regex.Pattern
. Default value is".*"
. -
Property
idFormat
- Specify check ID pattern to suppress. Type isjava.util.regex.Pattern
. Default value isnull
. -
Property
messageFormat
- Specify message pattern to suppress. Type isjava.util.regex.Pattern
. Default value isnull
. -
Property
offCommentFormat
- Specify comment pattern to trigger filter to begin suppression. Type isjava.util.regex.Pattern
. Default value is"CHECKSTYLE:OFF"
. -
Property
onCommentFormat
- Specify comment pattern to trigger filter to end suppression. Type isjava.util.regex.Pattern
. Default value is"CHECKSTYLE:ON"
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
- Since:
- 3.5
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SuppressionCommentFilter.Tag
A Tag holds a suppression comment and its location, and determines whether the suppression turns checkstyle reporting on or off.static class
SuppressionCommentFilter.TagType
Enum to be used for switching checkstyle reporting for tags.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
checkC
Control whether to check C style comments (/* ... */
).private boolean
checkCPP
Control whether to check C++ style comments (//
).private String
checkFormat
Specify check pattern to suppress.private static String
DEFAULT_CHECK_FORMAT
Control all checks.private static String
DEFAULT_OFF_FORMAT
Turns checkstyle reporting off.private static String
DEFAULT_ON_FORMAT
Turns checkstyle reporting on.private WeakReference<FileContents>
fileContentsReference
References the current FileContents for this filter.private String
idFormat
Specify check ID pattern to suppress.private String
messageFormat
Specify message pattern to suppress.private Pattern
offCommentFormat
Specify comment pattern to trigger filter to begin suppression.private Pattern
onCommentFormat
Specify comment pattern to trigger filter to end suppression.private List<SuppressionCommentFilter.Tag>
tags
Tagged comments.
-
Constructor Summary
Constructors Constructor Description SuppressionCommentFilter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
accept(TreeWalkerAuditEvent event)
Determines whether or not a filteredTreeWalkerAuditEvent
is accepted.private void
addTag(String text, int line, int column, SuppressionCommentFilter.TagType reportingOn)
Adds aTag
to the list of all tags.private SuppressionCommentFilter.Tag
findNearestMatch(TreeWalkerAuditEvent event)
Finds the nearest comment text tag that matches an audit event.protected void
finishLocalSetup()
Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.private FileContents
getFileContents()
Returns FileContents for this filter.void
setCheckC(boolean checkC)
Setter to control whether to check C style comments (/* ... */
).void
setCheckCPP(boolean checkCpp)
Setter to control whether to check C++ style comments (//
).void
setCheckFormat(String format)
Setter to specify check pattern to suppress.private void
setFileContents(FileContents fileContents)
Set the FileContents for this filter.void
setIdFormat(String format)
Setter to specify check ID pattern to suppress.void
setMessageFormat(String format)
Setter to specify message pattern to suppress.void
setOffCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.void
setOnCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to end suppression.private void
tagCommentLine(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(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.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
DEFAULT_OFF_FORMAT
private static final String DEFAULT_OFF_FORMAT
Turns checkstyle reporting off.- See Also:
- Constant Field Values
-
DEFAULT_ON_FORMAT
private static final String DEFAULT_ON_FORMAT
Turns checkstyle reporting on.- See Also:
- Constant Field Values
-
DEFAULT_CHECK_FORMAT
private static final String DEFAULT_CHECK_FORMAT
Control all checks.- See Also:
- Constant Field Values
-
tags
private final List<SuppressionCommentFilter.Tag> tags
Tagged comments.
-
checkC
private boolean checkC
Control whether to check C style comments (/* ... */
).
-
checkCPP
private boolean checkCPP
Control whether to check C++ style comments (//
).
-
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.
-
fileContentsReference
private 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 are reassigned to the next FileContents, at which time filtering for the current FileContents is finished.
-
-
Constructor Detail
-
SuppressionCommentFilter
public SuppressionCommentFilter()
-
-
Method Detail
-
setOffCommentFormat
public final void setOffCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.- Parameters:
pattern
- a pattern.- Since:
- 3.5
-
setOnCommentFormat
public final void setOnCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to end suppression.- Parameters:
pattern
- a pattern.- Since:
- 3.5
-
getFileContents
private FileContents getFileContents()
Returns FileContents for this filter.- Returns:
- the FileContents for this filter.
-
setFileContents
private void setFileContents(FileContents fileContents)
Set the FileContents for this filter.- Parameters:
fileContents
- the FileContents for this filter.
-
setCheckFormat
public final void setCheckFormat(String format)
Setter to specify check pattern to suppress.- Parameters:
format
- aString
value- Since:
- 3.5
-
setMessageFormat
public void setMessageFormat(String format)
Setter to specify message pattern to suppress.- Parameters:
format
- aString
value- Since:
- 3.5
-
setIdFormat
public void setIdFormat(String format)
Setter to specify check ID pattern to suppress.- Parameters:
format
- aString
value- Since:
- 8.24
-
setCheckCPP
public void setCheckCPP(boolean checkCpp)
Setter to control whether to check C++ style comments (//
).- Parameters:
checkCpp
-true
if C++ comments are checked.- Since:
- 3.5
-
setCheckC
public void setCheckC(boolean checkC)
Setter to control whether to check C style comments (/* ... */
).- Parameters:
checkC
-true
if C comments are checked.- Since:
- 3.5
-
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 classAbstractAutomaticBean
-
accept
public boolean accept(TreeWalkerAuditEvent event)
Description copied from interface:TreeWalkerFilter
Determines whether or not a filteredTreeWalkerAuditEvent
is accepted.- Specified by:
accept
in interfaceTreeWalkerFilter
- Parameters:
event
- the TreeWalkerAuditEvent to filter.- Returns:
- true if the event is accepted.
-
findNearestMatch
private SuppressionCommentFilter.Tag findNearestMatch(TreeWalkerAuditEvent 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
- theTreeWalkerAuditEvent
to match.- Returns:
- The
Tag
nearest event.
-
tagSuppressions
private void tagSuppressions()
Collects all the suppression tags for all comments into a list and sorts the list.
-
tagSuppressions
private void tagSuppressions(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(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(String text, int line, int column, SuppressionCommentFilter.TagType reportingOn)
Adds aTag
to 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
-true
if the tag turns checkstyle reporting on.
-
-