Class SuppressWarningsHolder
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class SuppressWarningsHolder extends AbstractCheck
Maintains a set of check suppressions fromSuppressWarnings
annotations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SuppressWarningsHolder.Entry
Records a particular suppression for a region of a file.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
ALL_WARNING_MATCHING_ID
Special warning id for matching all the warnings.private static java.util.Map<java.lang.String,java.lang.String>
CHECK_ALIAS_MAP
A map from check source names to suppression aliases.private static java.lang.String
CHECK_SUFFIX
Suffix to be removed from subclasses of Check.static java.lang.String
CHECKSTYLE_PREFIX
Optional prefix for warning suppressions that are only intended to be recognized by checkstyle.private static java.lang.ThreadLocal<java.util.List<SuppressWarningsHolder.Entry>>
ENTRIES
A thread-local holder for the list of suppression entries for the last file parsed.private static java.lang.String
JAVA_LANG_PREFIX
Java.lang namespace prefix, which is stripped from SuppressWarningsstatic java.lang.String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description SuppressWarningsHolder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginTree(DetailAST rootAST)
Called before the starting to process a tree.private static java.util.List<java.lang.String>
findAllExpressionsInChildren(DetailAST parent)
Method looks at children and returns list of expressions in strings.private static DetailAST
getAcceptableParent(DetailAST child)
Returns parent of given ast if parent has one of the following types: ANNOTATION_DEF, PACKAGE_DEF, CLASS_DEF, ENUM_DEF, ENUM_CONSTANT_DEF, CTOR_DEF, METHOD_DEF, PARAMETER_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, TYPE, LITERAL_NEW, LITERAL_THROWS, TYPE_ARGUMENT, IMPLEMENTS_CLAUSE, DOT.int[]
getAcceptableTokens()
The configurable token set.static java.lang.String
getAlias(java.lang.String sourceName)
Returns the alias for the source name of a check.private static java.util.List<java.lang.String>
getAllAnnotationValues(DetailAST ast)
Get all annotation values.private static DetailAST
getAnnotationTarget(DetailAST ast)
Get target of annotation.private static java.util.List<java.lang.String>
getAnnotationValues(DetailAST ast)
Returns the annotation values represented by an AST.static java.lang.String
getDefaultAlias(java.lang.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" suffix removed.int[]
getDefaultTokens()
Returns the default token a check is interested in.private static java.lang.String
getIdentifier(DetailAST ast)
Returns the Java identifier represented by an AST.private static DetailAST
getNthChild(DetailAST ast, int index)
Returns the n'th child of an AST node.int[]
getRequiredTokens()
The tokens that this check must be registered for.private static java.lang.String
getStringExpr(DetailAST ast)
Returns the literal string expression represented by an AST.private static boolean
isAnnotationEmpty(java.util.List<java.lang.String> values)
Checks that annotation is empty.static boolean
isSuppressed(AuditEvent event)
Checks for a suppression of a check with the given source name and location in the last file processed.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.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.static void
registerAlias(java.lang.String sourceName, java.lang.String checkAlias)
Registers an alias for the source name of a check.private static java.lang.String
removeCheckstylePrefixIfExists(java.lang.String checkName)
Method removes checkstyle prefix (checkstyle:) from check name if exists.void
setAliasList(java.lang.String... aliasList)
Registers a list of source name aliases based on a comma-separated list ofsource=alias
items, such ascom.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck= paramnum
.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
CHECKSTYLE_PREFIX
public static final java.lang.String CHECKSTYLE_PREFIX
Optional prefix for warning suppressions that are only intended to be recognized by checkstyle. For instance, to suppressFallThroughCheck
only 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:
- Constant Field Values
-
JAVA_LANG_PREFIX
private static final java.lang.String JAVA_LANG_PREFIX
Java.lang namespace prefix, which is stripped from SuppressWarnings- See Also:
- Constant Field Values
-
CHECK_SUFFIX
private static final java.lang.String CHECK_SUFFIX
Suffix to be removed from subclasses of Check.- See Also:
- Constant Field Values
-
ALL_WARNING_MATCHING_ID
private static final java.lang.String ALL_WARNING_MATCHING_ID
Special warning id for matching all the warnings.- See Also:
- Constant Field Values
-
CHECK_ALIAS_MAP
private static final java.util.Map<java.lang.String,java.lang.String> CHECK_ALIAS_MAP
A map from check source names to suppression aliases.
-
ENTRIES
private static final java.lang.ThreadLocal<java.util.List<SuppressWarningsHolder.Entry>> ENTRIES
A thread-local holder for the list of suppression entries for the last file parsed.
-
-
Method Detail
-
getDefaultAlias
public static java.lang.String getDefaultAlias(java.lang.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" suffix removed.- Parameters:
sourceName
- the source name of the check (generally the class name)- Returns:
- the default alias for the given check
-
getAlias
public static java.lang.String getAlias(java.lang.String sourceName)
Returns the alias for the source name of a check. If an alias has been explicitly registered viaregisterAlias(String, 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
public static void registerAlias(java.lang.String sourceName, java.lang.String checkAlias)
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 inSuppressWarnings
annotations
-
setAliasList
public void setAliasList(java.lang.String... aliasList)
Registers a list of source name aliases based on a comma-separated list ofsource=alias
items, such ascom.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck= paramnum
.- Parameters:
aliasList
- the list of comma-separated alias assignments
-
isSuppressed
public static boolean isSuppressed(AuditEvent event)
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
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
removeCheckstylePrefixIfExists
private static java.lang.String removeCheckstylePrefixIfExists(java.lang.String checkName)
Method removes checkstyle prefix (checkstyle:) from check name if exists.- Parameters:
checkName
- - name of the check- Returns:
- check name without prefix
-
getAllAnnotationValues
private static java.util.List<java.lang.String> getAllAnnotationValues(DetailAST ast)
Get all annotation values.- Parameters:
ast
- annotation token- Returns:
- list values
-
isAnnotationEmpty
private static boolean isAnnotationEmpty(java.util.List<java.lang.String> values)
Checks that annotation is empty.- Parameters:
values
- list of values in the annotation- Returns:
- whether annotation is empty or contains some values
-
getAnnotationTarget
private static DetailAST getAnnotationTarget(DetailAST ast)
Get target of annotation.- Parameters:
ast
- the AST node to get the child of- Returns:
- get target of annotation
-
getAcceptableParent
private static DetailAST getAcceptableParent(DetailAST child)
Returns parent of given ast if parent has one of the following types: ANNOTATION_DEF, PACKAGE_DEF, CLASS_DEF, ENUM_DEF, ENUM_CONSTANT_DEF, CTOR_DEF, METHOD_DEF, PARAMETER_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, TYPE, LITERAL_NEW, LITERAL_THROWS, TYPE_ARGUMENT, IMPLEMENTS_CLAUSE, DOT.- Parameters:
child
- an ast- Returns:
- returns ast - parent of given
-
getNthChild
private static DetailAST getNthChild(DetailAST ast, int index)
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
null
if none
-
getIdentifier
private static java.lang.String getIdentifier(DetailAST ast)
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:
java.lang.IllegalArgumentException
- if the AST is invalid
-
getStringExpr
private static java.lang.String getStringExpr(DetailAST ast)
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:
java.lang.IllegalArgumentException
- if the AST is invalid
-
getAnnotationValues
private static java.util.List<java.lang.String> getAnnotationValues(DetailAST ast)
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:
java.lang.IllegalArgumentException
- if the AST is invalid
-
findAllExpressionsInChildren
private static java.util.List<java.lang.String> findAllExpressionsInChildren(DetailAST parent)
Method looks at children and returns list of expressions in strings.- Parameters:
parent
- ast, that contains children- Returns:
- list of expressions in strings
-
-