Class SuppressWithNearbyCommentFilter
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.filters.SuppressWithNearbyCommentFilter
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,TreeWalkerFilter
public class SuppressWithNearbyCommentFilter extends AbstractAutomaticBean implements TreeWalkerFilter
Filter
SuppressWithNearbyCommentFilter
uses nearby comments to suppress audit events.Rationale: Same as
SuppressionCommentFilter
. Whereas the SuppressionCommentFilter uses matched pairs of filters to turn on/off comment matching,SuppressWithNearbyCommentFilter
uses single comments. This requires fewer lines to mark a region, and may be aesthetically preferable in some contexts.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.SuppressWithNearbyCommentFilter 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
commentFormat
- Specify comment pattern to trigger filter to begin suppression. Type isjava.util.regex.Pattern
. Default value is"SUPPRESS CHECKSTYLE (\w+)"
. -
Property
idFormat
- Specify check ID pattern to suppress. Type isjava.util.regex.Pattern
. Default value isnull
. -
Property
influenceFormat
- Specify negative/zero/positive value that defines the number of lines preceding/at/following the suppression comment. Type isjava.lang.String
. Default value is"0"
. -
Property
messageFormat
- Define message pattern to suppress. Type isjava.util.regex.Pattern
. Default value isnull
.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
- Since:
- 5.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SuppressWithNearbyCommentFilter.Tag
A Tag holds a suppression comment and its location.-
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 Pattern
commentFormat
Specify comment pattern to trigger filter to begin suppression.private static String
DEFAULT_CHECK_FORMAT
Default regex for checks that should be suppressed.private static String
DEFAULT_COMMENT_FORMAT
Format to turn checkstyle reporting off.private static String
DEFAULT_INFLUENCE_FORMAT
Default regex for lines that should be suppressed.private WeakReference<FileContents>
fileContentsReference
References the current FileContents for this filter.private String
idFormat
Specify check ID pattern to suppress.private String
influenceFormat
Specify negative/zero/positive value that defines the number of lines preceding/at/following the suppression comment.private String
messageFormat
Define message pattern to suppress.private List<SuppressWithNearbyCommentFilter.Tag>
tags
Tagged comments.
-
Constructor Summary
Constructors Constructor Description SuppressWithNearbyCommentFilter()
-
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)
Adds a comment suppressionTag
to the list of all tags.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.private boolean
matchesTag(TreeWalkerAuditEvent event)
Whether current event matches any tag fromtags
.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.void
setCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.private void
setFileContents(FileContents fileContents)
Set the FileContents for this filter.void
setIdFormat(String format)
Setter to specify check ID pattern to suppress.void
setInfluenceFormat(String format)
Setter to specify negative/zero/positive value that defines the number of lines preceding/at/following the suppression comment.void
setMessageFormat(String format)
Setter to define message pattern to suppress.private void
tagCommentLine(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(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_COMMENT_FORMAT
private static final String DEFAULT_COMMENT_FORMAT
Format to turn checkstyle reporting off.- See Also:
- Constant Field Values
-
DEFAULT_CHECK_FORMAT
private static final String DEFAULT_CHECK_FORMAT
Default regex for checks that should be suppressed.- See Also:
- Constant Field Values
-
DEFAULT_INFLUENCE_FORMAT
private static final String DEFAULT_INFLUENCE_FORMAT
Default regex for lines that should be suppressed.- See Also:
- Constant Field Values
-
tags
private final List<SuppressWithNearbyCommentFilter.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 (//
).
-
commentFormat
private Pattern commentFormat
Specify comment pattern to trigger filter to begin suppression.
-
checkFormat
private String checkFormat
Specify check pattern to suppress.
-
messageFormat
private String messageFormat
Define message pattern to suppress.
-
influenceFormat
private String influenceFormat
Specify negative/zero/positive value that defines the number of lines preceding/at/following the suppression comment.
-
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
-
SuppressWithNearbyCommentFilter
public SuppressWithNearbyCommentFilter()
-
-
Method Detail
-
setCommentFormat
public final void setCommentFormat(Pattern pattern)
Setter to specify comment pattern to trigger filter to begin suppression.- Parameters:
pattern
- a pattern.- Since:
- 5.0
-
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:
- 5.0
-
setMessageFormat
public void setMessageFormat(String format)
Setter to define message pattern to suppress.- Parameters:
format
- aString
value- Since:
- 5.0
-
setIdFormat
public void setIdFormat(String format)
Setter to specify check ID pattern to suppress.- Parameters:
format
- aString
value- Since:
- 8.24
-
setInfluenceFormat
public final void setInfluenceFormat(String format)
Setter to specify negative/zero/positive value that defines the number of lines preceding/at/following the suppression comment.- Parameters:
format
- aString
value- Since:
- 5.0
-
setCheckCPP
public void setCheckCPP(boolean checkCpp)
Setter to control whether to check C++ style comments (//
).- Parameters:
checkCpp
-true
if C++ comments are checked.- Since:
- 5.0
-
setCheckC
public void setCheckC(boolean checkC)
Setter to control whether to check C style comments (/* ... */
).- Parameters:
checkC
-true
if C comments are checked.- Since:
- 5.0
-
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.
-
matchesTag
private boolean matchesTag(TreeWalkerAuditEvent event)
Whether current event matches any tag fromtags
.
-
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)
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.
-
-