Combining multiple @SuppressWarnings annotations - Eclipse Indigo

ε祈祈猫儿з 提交于 2019-12-03 00:56:08

问题


So the issue is being able to combine multple warning suppressions so that each item doesn't need it's 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?


回答1:


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




回答2:


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 "{ }"



来源:https://stackoverflow.com/questions/13070260/combining-multiple-suppresswarnings-annotations-eclipse-indigo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!