166

So the issue is being able to combine multple warning suppressions so that each item doesn't need its own @SuppressWarnings annotation.

So for example:

public class Example
    public Example() {
        GO go = new GO();  // unused
        ....
        List<String> list = ( List<String> ) go.getList(); // unchecked
    }
    ...
    // getters/setters/other methods
}

Now instead of having two @SuppressWarnings I want to have one at the class level for those two warnings, so like this:

@SuppressWarnings( "unused", "unchecked" )
public class Example
    public Example() {
        GO go = new GO();  // unused - suppressed
        ....
        List<String> list = ( List<String> ) go.getList(); // unchecked - suppressed
    }
    ...
    // getters/setters/other methods
}

But that is not a valid syntax, is there a way to do this?

danronmoon
  • 3,814
  • 5
  • 34
  • 56
knownasilya
  • 5,998
  • 4
  • 36
  • 59
  • @SuppressWarnings( "unused", "unchecked" ) does not work please modify it to @SuppressWarnings( { "unused", "unchecked" }) – Raj Mar 24 '17 at 16:08

2 Answers2

342

Use the following: @SuppressWarnings({"unused", "unchecked"})

LuGo
  • 4,877
  • 1
  • 18
  • 19
  • Is this the normal convention for all annotations in Eclipse? – knownasilya Oct 25 '12 at 14:16
  • 1
    No. This is for a list parameter. When using an SDK to run Eclipse (or when having the Java sources attached), you can simply hit F3 on any annotation to see its source declaration, thereby also seeing how many (and which) parameters it needs. – Bananeweizen Oct 25 '12 at 14:24
  • 8
    That would be an array, as in `String[] value()`. Lists don't have special syntax in Java, but arrays can be defined using braces. – Maarten Bodewes Aug 05 '14 at 15:40
23

If you take a look inside the annotation you will see this:

public @interface SuppressWarnings {
    String[] value();
}

as you see, the value parameter is an array of Strings... so the parameter in the annotation can be: value1, value2 or value3 where

final String[] value1 = { "a1" };
final String[] value2 = { "a1", "a2" };
final String[] value3 = { "a1", "a2", "a3" };

i.e.:

@SuppressWarnings({"unused"})
@SuppressWarnings({"unused", "javadoc"})

you can oft see something like

@SuppressWarnings("unused") 

and this is a particular case allowing one element wit no "{ }"

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97