Supported Values for @SuppressWarnings

Update July 2011: This list has been reviewed and made current with the most recent Eclipse 3.7 release.

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.7), 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.

Tags: , , , , , ,

About Riyad Kalla

Software development, video games, writing, reading and anything shiny. I ultimately just want to provide a resource that helps people and if I can't do that, then at least make them laugh.

, , , , , ,

41 Responses to “Supported Values for @SuppressWarnings”

  1. ken krueger May 5, 2008 at 12:35 pm #

    Thanks! Been lookin’ for that!

  2. Riyad Kalla May 5, 2008 at 12:53 pm #

    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. rakesh May 15, 2008 at 5:07 am #

    Wow, thats super cool :)

  4. Anonymous August 13, 2008 at 4:45 pm #

    cool. thanx a bunch to folks who got this online

  5. BlaqPoet August 26, 2008 at 2:46 am #

    Thanks for this list :)

  6. Editor August 26, 2008 at 4:36 am #

    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. Alex September 1, 2008 at 4:07 am #

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

  8. Paulo September 10, 2008 at 7:34 am #

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

  9. Editor September 10, 2008 at 11:00 am #

    Paulo, glad it helped!

  10. Mike Nelson October 8, 2008 at 7:14 am #

    Rock. Thanks!

  11. Anil November 9, 2008 at 11:37 pm #

    Thanks. For your information

  12. PRK November 21, 2008 at 6:23 am #

    Thanks for your valuable information

  13. Riyad Kalla November 21, 2008 at 7:47 am #

    PRK,

    Not a problem, glad it helped!

  14. Cyrus January 13, 2009 at 8:21 pm #

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

  15. Riyad Kalla January 13, 2009 at 8:28 pm #

    Glad to hear it Cyrus!

  16. ThemePark March 17, 2009 at 5:46 am #

    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. jack April 13, 2009 at 1:39 pm #

    Man, so useful.

  18. Aar May 11, 2009 at 11:21 pm #

    Thanks Riyad for this very useful information. :)

  19. hosmia June 7, 2009 at 7:56 am #

    thx riyad, this is so useful

  20. Pierre December 7, 2009 at 10:07 am #

    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.

    • Riyad Kalla December 10, 2009 at 9:10 am #

      Pierre, thanks for the heads up. I’ll add it to the article.

  21. medopal January 19, 2010 at 6:45 am #

    Great list, thanks.

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

    • Riyad Kalla January 19, 2010 at 7:21 pm #

      medopal, glad it helped! That’s all I wanted to create, a quick-reference so people didn’t have to dig and dig.

  22. Przemek March 4, 2010 at 2:43 pm #

    Hi Riyad,

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

    Thanks for a help.

    • Riyad Kalla March 4, 2010 at 3:41 pm #

      Przemek, I think you can… atleast I don’t get compile errors in IJ for this:

      @SuppressWarnings(“null”) String guid

      Have you given it a try?

  23. Przemek March 5, 2010 at 12:42 am #

    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();
    }
    }

    • Riyad Kalla March 5, 2010 at 9:24 am #

      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. Przemek March 5, 2010 at 10:33 am #

    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.

    • Riyad Kalla March 5, 2010 at 11:31 am #

      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. Przemek March 5, 2010 at 11:41 am #

    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. Przemek March 5, 2010 at 12:39 pm #

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

  27. Andrew Violette May 11, 2010 at 5:11 am #

    I also like the RedundantIfStatement suppression in IntelliJ for handling generated equals() methods so that it doesn’t force you to “simplify” the equals expression to be a one-line command.

  28. Ayok June 15, 2012 at 4:36 am #

    Even after 4 years this list helps some people like me.
    Thank you!!

Trackbacks/Pingbacks

  1. Jan Tietjens - June 2, 2009

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

  2. bubbl - November 8, 2009

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

  3. bubbl - November 21, 2009

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

  4. Delicious Over 50 - November 28, 2009

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

  5. The Buzz Media - January 20, 2010

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

  6. One Coder - 一个热爱技术,热爱分享的coder的博客 - June 24, 2012

    [...] 一般来说,@SuppressWarnings注解的使用,直接根据开发工具的提示即可。例如:@SuppressWarnings("unused"),即不在警告未使用的变量。那么@SuppressWarnings支持的值究竟有哪些呢?在这篇文章中,给出了比较详细的解释:http://www.thebuzzmedia.com/supported-values-for-suppresswarnings/ 虽然是英文的,不过还是比较容易看懂的。 [...]

  7. Java SupressWarnings | vkostin.blog - June 24, 2012

    [...] вот тут  This entry was posted in java, noobcamp and tagged java by admin. Bookmark the [...]

  8. PreferenceActivity | アンドロイドな日々 - June 11, 2013

    [...] 参考 Android アプリ設定画面を作成する – yan note – 設定画面を作る(PreferenceActivity ) – Supported Values for @SuppressWarnings [...]

Leave a Reply


6 − 2 =