Java @SuppressWarnings Annotation

@SuppressWarnings Annotation indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).
The @SuppressWarnings annotation type allows Java programmers to disable compilation warnings for a certain part of a program (type, field, method, parameter, constructor, and local variable). Normally warnings are good. However, in some cases, they would be inappropriate and annoying. So programmers can choose to tell the compiler ignoring such warnings if needed.
When you program with generics, you'll see a lot of compiler warnings:
  • unchecked cast warnings
  • unchecked method invocation warnings
  • unchecked generic array creation warnings
  • unchecked conversion warnings
Eliminate every unchecked warning that you can. If you eliminate all warnings, you are assured that the code is TYPESAFE.

SuppressWarnings Annotation Source Code

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();
}
From the above code, notice that @SuppressWarnings annotation applicable to a field, method, parameter, constructor, and local variable
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

SuppressWarnings Annotation Examples

Suppressing warnings on using unchecked generic types operations:
@SuppressWarnings("unchecked")
void uncheckedGenerics() {
    List words = new ArrayList();
 
    words.add("hello"); // this causes unchecked warning
}
If the above code is compiled without @SuppressWarnings("unchecked") annotation, the compiler will complain like this:
XYZ.java uses unchecked or unsafe operations.

Suppressing warnings on using deprecated APIs: 
```java
@SuppressWarnings("deprecation")
public void showDialog() {
    JDialog dialog = new JDialog();
    dialog.show();  // this is a deprecated method
}
Without the @SuppressWarnings("deprecation") annotation, the compiler will issue this warning:
XYZ.java uses or overrides a deprecated API.
Suppress all unchecked and deprecation warnings for all code inside the Foo class below:
@SuppressWarnings({"unchecked", "deprecation"})
class Foo {
    // code that may issue unchecked and deprecation warnings
}
Suppressing warnings on local variable declaration:
void foo(List inputList) {
    @SuppressWarnings("unchecked")
    List<String> list = (List<String>) inputList; // unsafe cast
}

Comments