Since Checkstyle 5.7
@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.
name | description | type | default value | since |
---|---|---|---|---|
aliasList | Specify aliases for check names that can be used in code within SuppressWarnings in a format of comma separated attribute=value entries. The attribute is the fully qualified name of the Check and value is its alias. |
String[] | {} |
5.7 |
To use default module configuration:
<module name="Checker"> <module name="TreeWalker"> <module name="MemberName"/> <module name="ConstantName"/> <module name="ParameterNumber"> <property name="id" value="ParamNumberId"/> </module> <module name= "NoWhitespaceAfter"/> <module name="SuppressWarningsHolder"/> </module> <module name="SuppressWarningsFilter"/> </module>
Example:
class Example1 { private int K; // violation, 'Name 'K' must match pattern' @SuppressWarnings({"membername"}) private int J; // violation suppressed private static final int i = 0; // violation, 'Name 'i' must match pattern' @SuppressWarnings("checkstyle:constantname") private static final int m = 0; // violation suppressed @SuppressWarnings("ParamNumberId") public void needsLotsOfParameters1 (int a, // violation suppressed int b, int c, int d, int e, int f, int g, int h) { // ... } private int [] ARR; // violation ''int' is followed by whitespace' // violation above, 'Name 'ARR' must match pattern' @SuppressWarnings("all") private int [] ARRAY; // violations suppressed }
The general rule is that the argument of the @SuppressWarnings
will be
matched against class name of the check in any letter case. Adding check
suffix is also accepted.
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
:
<module name="Checker"> <module name="TreeWalker"> <module name="ParameterNumber"/> <module name="SuppressWarningsHolder"> <property name="aliasList" value= "com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck=paramnum"/> </module> </module> <module name="SuppressWarningsFilter"/> </module>
Example:
class Example2 { // violation below, 'More than 7 parameters (found 8)' public void needsLotsOfParameters (int a, int b, int c, int d, int e, int f, int g, int h) { // ... } @SuppressWarnings("paramnum") public void needsLotsOfParameters1 (int a, // violation suppressed int b, int c, int d, int e, int f, int g, int h) { // ... } }
com.puppycrawl.tools.checkstyle.checks