Class SuppressWarningsHolder
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder
- All Implemented Interfaces:
Configurable,Contextualizable
Maintains a set of check suppressions from
@SuppressWarnings annotations.
It allows to prevent Checkstyle from reporting violations from parts of code that were
annotated with @SuppressWarnings and using name of the check to be excluded.
It is possible to suppress all the checkstyle warnings with the argument "all".
You can also use a checkstyle: prefix to prevent compiler
from processing these annotations.
You can also define aliases for check names that need to be suppressed.
-
Property
aliasList- Specify aliases for check names that can be used in code withinSuppressWarningsin a format of comma separated attribute=value entries. The attribute is the fully qualified name of the Check and value is its alias. Type isjava.lang.String[]. Default value is"".
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
- Since:
- 5.7
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classRecords a particular suppression for a region of a file.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringSpecial warning id for matching all the warnings.A map from check source names to suppression aliases.private static final StringSuffix to be removed from subclasses of Check.private static final StringOptional prefix for warning suppressions that are only intended to be recognized by checkstyle.private static final ThreadLocal<List<SuppressWarningsHolder.Entry>>A thread-local holder for the list of suppression entries for the last file parsed.private static final StringJava.lang namespace prefix, which is stripped from SuppressWarnings.private static final PatternCompiled pattern used to match preceding newline in text block content.private static final PatternCompiled pattern used to match whitespace in text block content. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidaddSuppressions(List<String> values, DetailAST targetAST) Method to populate list of suppression entries.voidCalled before the starting to process a tree.voiddestroy()Destroy the check.Method looks at children and returns list of expressions in strings.int[]The configurable token set.static StringReturns the alias for the source name of a check.Get all annotation values.Get target of annotation.Returns the annotation values represented by an AST.private static StringgetContentWithoutPrecedingWhitespace(String textBlockContent) Remove preceding newline and whitespace from the content of a text block.static StringgetDefaultAlias(String sourceName) Returns the default alias for the source name of a check, which is the source name in lower case with any dotted prefix or "Check"/"check" suffix removed.int[]Returns the default token a check is interested in.private static StringgetIdentifier(DetailAST ast) Returns the Java identifier represented by an AST.private static DetailASTgetNthChild(DetailAST ast, int index) Returns the n'th child of an AST node.int[]The tokens that this check must be registered for.private static StringgetSimpleNameAlias(String sourceName) Returns the alias of simple check name for a check, The alias is for the form of CheckNameCheck or CheckName.private static StringgetStringExpr(DetailAST ast) Returns the literal string expression represented by an AST.static booleanisSuppressed(AuditEvent event) Checks for a suppression of a check with the given source name and location in the last file processed.private static booleanisSuppressedAfterEventStart(int line, int column, SuppressWarningsHolder.Entry entry) Checks whether suppression entry position is after the audit event occurrence position in the source file.private static booleanisSuppressedBeforeEventEnd(int line, int column, SuppressWarningsHolder.Entry entry) Checks whether suppression entry position is before the audit event occurrence position in the source file.private static voidregisterAlias(String sourceName, String checkAlias) Registers an alias for the source name of a check.private static StringremoveCheckstylePrefixIfExists(String checkName) Method removes checkstyle prefix (checkstyle:) from check name if exists.voidsetAliasList(String... aliasList) Setter to specify aliases for check names that can be used in code withinSuppressWarningsin a format of comma separated attribute=value entries.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
CHECKSTYLE_PREFIX
Optional prefix for warning suppressions that are only intended to be recognized by checkstyle. For instance, to suppressFallThroughCheckonly in checkstyle (and not in javac), use the suppression"checkstyle:fallthrough"or"checkstyle:FallThrough". To suppress the warning in both tools, just use"fallthrough".- See Also:
-
JAVA_LANG_PREFIX
Java.lang namespace prefix, which is stripped from SuppressWarnings.- See Also:
-
CHECK_SUFFIX
Suffix to be removed from subclasses of Check.- See Also:
-
ALL_WARNING_MATCHING_ID
Special warning id for matching all the warnings.- See Also:
-
CHECK_ALIAS_MAP
A map from check source names to suppression aliases. -
ENTRIES
A thread-local holder for the list of suppression entries for the last file parsed. -
WHITESPACE
Compiled pattern used to match whitespace in text block content. -
NEWLINE
Compiled pattern used to match preceding newline in text block content.
-
-
Constructor Details
-
SuppressWarningsHolder
public SuppressWarningsHolder()
-
-
Method Details
-
getDefaultAlias
Returns the default alias for the source name of a check, which is the source name in lower case with any dotted prefix or "Check"/"check" suffix removed.- Parameters:
sourceName- the source name of the check (generally the class name)- Returns:
- the default alias for the given check
-
getSimpleNameAlias
Returns the alias of simple check name for a check, The alias is for the form of CheckNameCheck or CheckName.- Parameters:
sourceName- the source name of the check (generally the class name)- Returns:
- the alias of the simple check name for the given check
-
getAlias
Returns the alias for the source name of a check. If an alias has been explicitly registered viasetAliasList(String...), that alias is returned; otherwise, the default alias is used.- Parameters:
sourceName- the source name of the check (generally the class name)- Returns:
- the current alias for the given check
-
registerAlias
Registers an alias for the source name of a check.- Parameters:
sourceName- the source name of the check (generally the class name)checkAlias- the alias used inSuppressWarningsannotations
-
setAliasList
Setter to specify aliases for check names that can be used in code withinSuppressWarningsin a format of comma separated attribute=value entries. The attribute is the fully qualified name of the Check and value is its alias.- Parameters:
aliasList- comma-separated alias assignments- Throws:
IllegalArgumentException- when alias item does not have '='- Since:
- 5.7
-
isSuppressed
Checks for a suppression of a check with the given source name and location in the last file processed.- Parameters:
event- audit event.- Returns:
- whether the check with the given name is suppressed at the given source location
-
isSuppressedAfterEventStart
private static boolean isSuppressedAfterEventStart(int line, int column, SuppressWarningsHolder.Entry entry) Checks whether suppression entry position is after the audit event occurrence position in the source file.- Parameters:
line- the line number in the source file where the event occurred.column- the column number in the source file where the event occurred.entry- suppression entry.- Returns:
- true if suppression entry position is after the audit event occurrence position in the source file.
-
isSuppressedBeforeEventEnd
private static boolean isSuppressedBeforeEventEnd(int line, int column, SuppressWarningsHolder.Entry entry) Checks whether suppression entry position is before the audit event occurrence position in the source file.- Parameters:
line- the line number in the source file where the event occurred.column- the column number in the source file where the event occurred.entry- suppression entry.- Returns:
- true if suppression entry position is before the audit event occurrence position in the source file.
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
addSuppressions
Method to populate list of suppression entries.- Parameters:
values- - list of check namestargetAST- - annotation target
-
removeCheckstylePrefixIfExists
Method removes checkstyle prefix (checkstyle:) from check name if exists.- Parameters:
checkName- - name of the check- Returns:
- check name without prefix
-
getAllAnnotationValues
Get all annotation values.- Parameters:
ast- annotation token- Returns:
- list values
- Throws:
IllegalArgumentException- if there is an unknown annotation value type.
-
getAnnotationTarget
Get target of annotation.- Parameters:
ast- the AST node to get the child of- Returns:
- get target of annotation
- Throws:
IllegalArgumentException- if there is an unexpected container type.
-
getNthChild
Returns the n'th child of an AST node.- Parameters:
ast- the AST node to get the child ofindex- the index of the child to get- Returns:
- the n'th child of the given AST node, or
nullif none
-
getIdentifier
Returns the Java identifier represented by an AST.- Parameters:
ast- an AST node for an IDENT or DOT- Returns:
- the Java identifier represented by the given AST subtree
- Throws:
IllegalArgumentException- if the AST is invalid
-
getStringExpr
Returns the literal string expression represented by an AST.- Parameters:
ast- an AST node for an EXPR- Returns:
- the Java string represented by the given AST expression or empty string if expression is too complex
- Throws:
IllegalArgumentException- if the AST is invalid
-
getAnnotationValues
Returns the annotation values represented by an AST.- Parameters:
ast- an AST node for an EXPR or ANNOTATION_ARRAY_INIT- Returns:
- the list of Java string represented by the given AST for an expression or annotation array initializer
- Throws:
IllegalArgumentException- if the AST is invalid
-
findAllExpressionsInChildren
Method looks at children and returns list of expressions in strings.- Parameters:
parent- ast, that contains children- Returns:
- list of expressions in strings
-
getContentWithoutPrecedingWhitespace
Remove preceding newline and whitespace from the content of a text block.- Parameters:
textBlockContent- the actual text in a text block.- Returns:
- content of text block with preceding whitespace and newline removed.
-
destroy
Description copied from class:AbstractCheckDestroy the check. It is being retired from service.- Overrides:
destroyin classAbstractCheck
-