public class SuppressWarningsHolder extends AbstractCheck
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.
You can also define aliases for check names that need to be suppressed.
aliasList - Specify aliases for check names that can be used in code
within SuppressWarnings.
Type is java.lang.String[].
Default value is null.
To prevent FooCheck violations from being reported write:
@SuppressWarnings("foo") interface I { }
@SuppressWarnings("foo") enum E { }
@SuppressWarnings("foo") InputSuppressWarningsFilter() { }
Some real check examples:
This will prevent from invocation of the MemberNameCheck:
@SuppressWarnings({"membername"})
private int J;
You can also use a checkstyle prefix to prevent compiler from
processing this annotations. For example this will prevent ConstantNameCheck:
@SuppressWarnings("checkstyle:constantname")
private static final int m = 0;
The general rule is that the argument of the @SuppressWarnings will be
matched against class name of the checker in lower case and without Check
suffix if present.
If aliasList property was provided you can use your own names e.g below
code will work if there was provided a ParameterNumberCheck=paramnum in
the aliasList:
@SuppressWarnings("paramnum")
public void needsLotsOfParameters(@SuppressWarnings("unused") int a,
int b, int c, int d, int e, int f, int g, int h) {
...
}
It is possible to suppress all the checkstyle warnings with the argument "all":
@SuppressWarnings("all")
public void someFunctionWithInvalidStyle() {
//...
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
| Modifier and Type | Class and Description |
|---|---|
private static class |
SuppressWarningsHolder.Entry
Records a particular suppression for a region of a file.
|
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and 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.
|
private 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 SuppressWarnings
|
private static java.util.regex.Pattern |
NEWLINE
Compiled pattern used to match preceding newline in text block content.
|
private static java.util.regex.Pattern |
WHITESPACE
Compiled pattern used to match whitespace in text block content.
|
| Constructor and Description |
|---|
SuppressWarningsHolder() |
| Modifier and Type | Method and Description |
|---|---|
private static void |
addSuppressions(java.util.List<java.lang.String> values,
DetailAST targetAST)
Method to populate list of suppression entries.
|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
void |
destroy()
Destroy the check.
|
private static java.util.List<java.lang.String> |
findAllExpressionsInChildren(DetailAST parent)
Method looks at children and returns list of expressions in strings.
|
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 java.util.Optional<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.
|
private static java.lang.String |
getContentWithoutPrecedingWhitespace(java.lang.String textBlockContent)
Remove preceding newline and whitespace from the content of a text block.
|
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.
|
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.
|
private 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)
Setter to specify aliases for check names that can be used in code
within
SuppressWarnings. |
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearViolations, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildprivate static final java.lang.String CHECKSTYLE_PREFIX
FallThroughCheck 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".private static final java.lang.String JAVA_LANG_PREFIX
private static final java.lang.String CHECK_SUFFIX
private static final java.lang.String ALL_WARNING_MATCHING_ID
private static final java.util.Map<java.lang.String,java.lang.String> CHECK_ALIAS_MAP
private static final java.lang.ThreadLocal<java.util.List<SuppressWarningsHolder.Entry>> ENTRIES
private static final java.util.regex.Pattern WHITESPACE
private static final java.util.regex.Pattern NEWLINE
public SuppressWarningsHolder()
public static java.lang.String getDefaultAlias(java.lang.String sourceName)
sourceName - the source name of the check (generally the class
name)public static java.lang.String getAlias(java.lang.String sourceName)
setAliasList(String...), that
alias is returned; otherwise, the default alias is used.sourceName - the source name of the check (generally the class
name)private static void registerAlias(java.lang.String sourceName, java.lang.String checkAlias)
sourceName - the source name of the check (generally the class
name)checkAlias - the alias used in SuppressWarnings annotationspublic void setAliasList(java.lang.String... aliasList)
SuppressWarnings.aliasList - the list of comma-separated alias assignmentsjava.lang.IllegalArgumentException - when alias item does not have '='public static boolean isSuppressed(AuditEvent event)
event - audit event.private static boolean isSuppressedAfterEventStart(int line, int column, SuppressWarningsHolder.Entry entry)
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.private static boolean isSuppressedBeforeEventEnd(int line, int column, SuppressWarningsHolder.Entry entry)
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.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate static void addSuppressions(java.util.List<java.lang.String> values, DetailAST targetAST)
values - - list of check namestargetAST - - annotation targetprivate static java.lang.String removeCheckstylePrefixIfExists(java.lang.String checkName)
checkName - - name of the checkprivate static java.util.List<java.lang.String> getAllAnnotationValues(DetailAST ast)
ast - annotation tokenjava.lang.IllegalArgumentException - if there is an unknown annotation value type.private static java.util.Optional<DetailAST> getAnnotationTarget(DetailAST ast)
ast - the AST node to get the child ofjava.lang.IllegalArgumentException - if there is an unexpected container type.private static DetailAST getNthChild(DetailAST ast, int index)
ast - the AST node to get the child ofindex - the index of the child to getnull if noneprivate static java.lang.String getIdentifier(DetailAST ast)
ast - an AST node for an IDENT or DOTjava.lang.IllegalArgumentException - if the AST is invalidprivate static java.lang.String getStringExpr(DetailAST ast)
ast - an AST node for an EXPRjava.lang.IllegalArgumentException - if the AST is invalidprivate static java.util.List<java.lang.String> getAnnotationValues(DetailAST ast)
ast - an AST node for an EXPR or ANNOTATION_ARRAY_INITjava.lang.IllegalArgumentException - if the AST is invalidprivate static java.util.List<java.lang.String> findAllExpressionsInChildren(DetailAST parent)
parent - ast, that contains childrenprivate static java.lang.String getContentWithoutPrecedingWhitespace(java.lang.String textBlockContent)
textBlockContent - the actual text in a text block.public void destroy()
AbstractCheckdestroy in class AbstractCheckCopyright © 2001-2022. All Rights Reserved.