Supported Values for @SuppressWarnings

Update #1: All these annotations are still valid in Eclipse 3.4 and 3.5, there have been no new SuppressWarning arguments added in those versions of the JDT compiler.

If you are a Java developer and use the new @SuppressWarnings annotation in your code from time-to-time to suppress compiler warnings you, like me, have wondered probably about a million times already just exactly what are the supported values that can be used with this annotation.

The reason the list isn’t easy to find is because it’s compiler specific, which means Sun may have a different set of supported values than say IBM, GCJ or Apache Harmony.

Fortunately for us, the Eclipse folks have documented the values they support (As of Eclipse 3.3), here they are for reference:

  • all to suppress all warnings
  • boxing to suppress warnings relative to boxing/unboxing operations
  • cast to suppress warnings relative to cast operations
  • dep-ann to suppress warnings relative to deprecated annotation
  • deprecation to suppress warnings relative to deprecation
  • fallthrough to suppress warnings relative to missing breaks in switch statements
  • finally to suppress warnings relative to finally block that don’t return
  • hiding to suppress warnings relative to locals that hide variable
  • incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • rawtypes to suppress warnings relative to un-specific types when using generics on class params
  • restriction to suppress warnings relative to usage of discouraged or forbidden references
  • serial to suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-access to suppress warnings relative to incorrect static access
  • synthetic-access to suppress warnings relative to unoptimized access from inner classes
  • unchecked to suppress warnings relative to unchecked operations
  • unqualified-field-access to suppress warnings relative to field access unqualified
  • unused to suppress warnings relative to unused code

TIP: For the folks that haven’t used @SuppressWarnings before, the syntax looks like this:

@SuppressWarnings(“unused”)

and can be placed above almost any piece of code that is causing a compiler warning to popup for your class.

Update #1: Thanks to Pierre for the addition of the ‘rawtypes’ argument and description.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

36 Responses to “Supported Values for @SuppressWarnings”

  1. Thanks! Been lookin’ for that!

  2. No problem, glad it helped!

    I couldn’t believe how hard it was to find a list when I originally went looking, so I knew I had to blog it ;)

  3. Wow, thats super cool :)

  4. cool. thanx a bunch to folks who got this online

  5. Thanks for this list :)

  6. No problem, glad it could help! (I know that I just keep this page bookmarked any time I’m coding now, they really should just add this to the auto-complete list in the editor IMO… or some agreed up on sub-set between Eclipse JDT/Sun Javac or something…)

  7. Wow – thx: likewise, can’t believe how hard this is to find …

  8. Big Thanks for the list! looked and found your blog ;)

  9. Paulo, glad it helped!

  10. Rock. Thanks!

  11. Thanks. For your information

  12. Thanks for your valuable information

  13. PRK,

    Not a problem, glad it helped!

  14. Thanks Riyad, your article just helped me out. Much appreciated!

  15. Glad to hear it Cyrus!

  16. This really is a great blog to have put up. Thanks a lot for making this list public, it’s very useful now that I have just started learning about and using SuppressWarnings.

  17. Man, so useful.

  18. Thanks Riyad for this very useful information. :)

  19. thx riyad, this is so useful

  20. Hi Riyad, there is one missing value for @SuppressWarnings you didn’t talked about:

    @SuppressWarnings (“rawtypes”)

    It helps you with generic classes when you don’t want them to have a parameter.

  21. Great list, thanks.

    BTW, this blog post is the second result when searching for SuppressWarnings on Google, that’s how i reached here.

  22. Hi Riyad,

    Do you know maybe how to apply @SuppressWarnings(“null”) on a method parameter? Does it work at all?

    Thanks for a help.

  23. Yes, I already have but it didn’t work. In addition to that I am getting the warning “Unnecessary @SuppressWarnings(“null”)” under eclipse.

    Dummy example that I have tried.

    public void test(@SuppressWarnings(“null”) String text) {
    if (text == null) {
    text.length();
    }
    }

    • Przemek, what warning are you trying to suppress with that annot btw? Maybe there is a misunderstanding about how these work?

      From the code snippet you posted, I made me think what you *want* is a way to specify that an arg is NotNull, much like JSR 305 is trying to do with @NotNull and a few other annots (click here for more info)

      And that’s not what @SuppressWarnings will do — it just tells the compiler to “shut it” when it comes to certain warnings, and in the case above, the compiler won’t emit a warning for ‘text’ potentially being null, so that annot is actually a no-op.

  24. By text.length() I am getting the warning “Null Pointer Access” which I want to suppress. You are right. @NotNull would solve the issue. In the real system however I am currently working on the snippet would look a bit different (@NotNull is not at stake):

    public void test(String text) {
    Assert.isTrue(text != null && text.length() > 0, “Error”);

    text.length();
    }

    where Assert.isTrue (not JUnit-Assert) throws AssertException where the condition is not met. The compiler doesn’t recognize that by the line with text.line() text cannot be null anymore. So it shows “Potential Null Pointer Access”. This is in fact what I want to suppress.

    • Ahhh… does the SuppressWarning annot do what you want when you throw it on the entire method and not just the parameter?

      This is in Eclipse right? I wonder if you are spending more time than it’s worth fighting the Eclipse Java compiler to do what you want instead of just turning off the “might be null” warning directly on the compiler settings prefs page?

      I don’t think javac shows this warning by default, but the Eclipse compiler might. Not sure.

  25. Right. On the method level @SuppressWarnings works perfectly but doing this would be too wide.

    Right. It’s possible to turn (P)NPA check off in eclipse compiler settings. I would like to avoid it as well. The (P)NPA warning is useful but often gives false alarms. I would like to switch it off case by case.

  26. I thought @SuppressWarnings(“null”) would solve the problem as it can be inserted at method parameter level.

Trackbacks/Pingbacks

  1. Jan Tietjens - 02. Jun, 2009

    Supported Values for @SuppressWarnings: http://squurl.com/97256/

  2. bubbl - 08. Nov, 2009

    Supported Values for @SuppressWarnings | The "Break it Down" Blog http://ff.im/-b9keg

  3. bubbl - 21. Nov, 2009

    Supported Values for @SuppressWarnings | The "Break it Down" Blog http://ff.im/-bOsD3

  4. Delicious Over 50 - 28. Nov, 2009

    Supported Values for @SuppressWarnings | The "Break it Down" Blog http://bit.ly/6L2vWR java annotations

  5. The Buzz Media - 20. Jan, 2010

    Supported Values for @SuppressWarnings http://ff.im/-b9keg

Leave a Reply