Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4536

Valid @SuppressWarnings Warning Names

$
0
0

1. Overview

In this tutorial, we'll take a look at the different warning names that work with the @SuppressWarnings Java annotation, which allows us to suppress compiler warnings. These warning names allow us to suppress particular warnings. The warning names available will depend on our IDE or Java compiler. The Eclipse IDE is our reference for this article.

2. Warning Names

Below is a list of valid warning names available in the @SuppressWarnings annotation:

  • all: this is sort of a wildcard that suppresses all warnings
  • boxing: suppresses warnings related to boxing/unboxing operations
  • unused: suppresses warnings of unused code
  • cast: suppresses warnings related to object cast operations
  • deprecation: suppresses warnings related to deprecation, such as a deprecated class or method
  • restriction: suppresses warnings related to the usage of discouraged or forbidden references
  • dep-ann: suppresses warnings relative to deprecated annotations
  • fallthrough: suppresses warnings related to missing break statements in switch statements
  • finally: suppresses warnings related to finally blocks that don't return
  • hiding: suppresses warnings relative to locals that hide variables
  • incomplete-switch: suppresses warnings relative to missing entries in a switch statement (enum case)
  • nls: suppresses warnings related to non-nls string literals
  • null: suppresses warnings related to null analysis
  • serial: suppresses warnings related to the missing serialVersionUID field, which is typically found in a Serializable class
  • static-access: suppresses warnings related to incorrect static variable access
  • synthetic-access: suppresses warnings related to unoptimized access from inner classes
  • unchecked: suppresses warnings related to unchecked operations
  • unqualified-field-access: suppresses warnings related to unqualified field access
  • javadoc: suppresses warnings related to Javadoc
  • rawtypes: suppresses warnings related to the usage of raw types
  • resource: suppresses warnings related to the usage of resources of type Closeable
  • super: suppresses warnings related to overriding a method without super invocations
  • sync-override: suppresses warnings due to missing synchronize when overriding a synchronized method

3. Using Warning Names

This section will show examples of the use of different warning names.

3.1. @SuppressWarnings(“unused”)

In the example below, the warning name suppresses the warning of the unusedVal in the method:

@SuppressWarnings("unused")
void suppressUnusedWarning() {
    int usedVal = 5;
    int unusedVal = 10;  // no warning here
    List<Integer> list = new ArrayList<>();
    list.add(usedVal);
}

3.2. @SuppressWarnings(“deprecated”)

In the example below, the warning name suppresses the warning of the usage of the @deprecated method:

@SuppressWarnings("deprecated")
void suppressDeprecatedWarning() {
    ClassWithSuppressWarningsNames cls = new ClassWithSuppressWarningsNames();
    cls.deprecatedMethod(); // no warning here
}
@Deprecated
String deprecatedMethod() {
    return "deprecated method";
}

3.3. @SuppressWarnings(“fallthrough”)

In the example below, the warning name suppresses the warning of the missing break statements — we've included them here, commented out, to show where we would otherwise get the warning:

@SuppressWarnings("fallthrough")
String suppressFallthroughWarning() {
    int day = 5;
    switch (day) {
        case 5:
            return "This is day 5";
//          break; // no warning here
        case 10:
            return "This is day 10";
//          break; // no warning here   
        default:
            return "This default day";
    }
}

3.4. @SuppressWarnings(“serial”)

This warning name is placed at the class level. In the example below, the warning name suppresses the warning of the missing serialVersionUID (which we've commented out) in a Serializable class:

@SuppressWarnings("serial")
public class ClassWithSuppressWarningsNames implements Serializable {
//    private static final long serialVersionUID = -1166032307853492833L; // no warning even though this is commented

4. Combining Multiple Warning Names

The @SuppressWarnings annotation expects an array of Strings, so we can combine multiple warning names:

@SuppressWarnings({"serial", "unchecked"})

5. Conclusion

This article provides a list of valid @SuppressWarnings warning names. As usual, all code samples shown in this tutorial are available over on GitHub.

The post Valid @SuppressWarnings Warning Names first appeared on Baeldung.
       

Viewing all articles
Browse latest Browse all 4536

Trending Articles