SuppressWarningsHolder

Since Checkstyle 5.7

Description

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.

Properties

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

Examples

To use default module configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="MemberName"/>
    <module name="ConstantName"/>
    <module name="ParameterNumber">
      <property name="id" value="ParamListShouldBeShort"/>
    </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

  @SuppressWarnings("ParamListShouldBeShort")
    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; // violation suppressed
  // violation 2 lines below 'More than 7 parameters (found 8)'
  @SuppressWarnings("paramcounter")
    public void needsLotsOfParameters2 (int a,
     int b, int c, int d, int e, int f, int g, int h) {}
}

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 com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck=paramcounter in the aliasList:


<module name="Checker">
  <module name="TreeWalker">
    <module name="MemberName"/>
    <module name="ConstantName"/>
    <module name="ParameterNumber">
      <property name="id" value="ParamListShouldBeShort"/>
    </module>
    <module name="SuppressWarningsHolder">
      <property name="aliasList"
        value="com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck=paramcounter"/>
    </module>
    <module name="NoWhitespaceAfter"/>
    <module name="SuppressWarningsHolder"/>
  </module>
  <module name="SuppressWarningsFilter"/>
</module>

Example:


class Example2 {
  private int K; // violation, 'Name 'K' must match pattern'
  @SuppressWarnings({"membername"})
  private int J; // violation suppressed

  @SuppressWarnings("ParamListShouldBeShort")
  public void needsLotsOfParameters1 (int a,
    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; // violation suppressed

  @SuppressWarnings("paramcounter")
  public void needsLotsOfParameters2 (int a, // violation suppressed
    int b, int c, int d, int e, int f, int g, int h) {}
}

You can also use simple check name like ParameterNumberCheck=paramcounter instead to use fully qualified name of check in the aliasList:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterNumber"/>
    <module name="SuppressWarningsHolder">
      <property name="aliasList" value="ParameterNumberCheck=paramcounter"/>
    </module>
  </module>
  <module name="SuppressWarningsFilter"/>
</module>

Example:


public class Example3 {

  private int K;

  @SuppressWarnings({"membername"})
  private int J;

  private static final int i = 0;

  @SuppressWarnings("checkstyle:constantname")
  private static final int m = 0;

  // 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) {}

  // violation 2 lines below 'More than 7 parameters (found 8)'
  @SuppressWarnings("ParamNumberId")
  public void needsLotsOfParameters1(
          int a, int b, int c, int d,
          int e, int f, int g, int h) {}

  @SuppressWarnings("paramcounter")
  public void needsLotsOfParameters2(
          int a, int b, int c, int d,
          int e, int f, int g, int h) {} // violation suppressed

  private int [] ARR;

  @SuppressWarnings("all")
  private int [] ARRAY;
}

The check can also be used without suffix of "Check" like ParameterNumber=paramcounter in the aliasList:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterNumber"/>
    <module name="SuppressWarningsHolder">
      <property name="aliasList" value="ParameterNumber=paramcounter"/>
    </module>
  </module>
  <module name="SuppressWarningsFilter"/>
</module>

Example:


public class Example4 {

  private int K;

  @SuppressWarnings({"membername"})
  private int J;

  private static final int i = 0;

  @SuppressWarnings("checkstyle:constantname")
  private static final int m = 0;

  // 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) {}

  // violation 2 lines below 'More than 7 parameters (found 8)'
  @SuppressWarnings("ParamNumberId")
  public void needsLotsOfParameters1(
          int a, int b, int c, int d,
          int e, int f, int g, int h) {}

  @SuppressWarnings("paramcounter")
  public void needsLotsOfParameters2(
          int a, int b, int c, int d,
          int e, int f, int g, int h) {} // violation suppressed

  private int [] ARR;

  @SuppressWarnings("all")
  private int [] ARRAY;
}

Example of Usage

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker