Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to declare suppresswarnings to the declaration of the method?

I have a static method which returns a value which is a Magic Constant. Every time I access the method in my code I get a warning like this:

Must be one of: Toast.LENGTH_SHORT, Toast.LENGTH_LONG

when I add @SuppressWarnings("MagicConstant") to the method where I want to access my method which returns either Toast.LENGTH_SHORT or Toast.LENGTH_LONG the warning disappears. But I have to add this suppresswarnings every time I want to use my static method. and this is annoying. Is there a way to add this warning directly to my method which returns one of the both values?

I tried this:

@SuppressWarnings("MagicConstant")
    public static int getToastDuration() {
        return ((statement) ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
    }

but this doesn't work. I still have to add @SuppressWarnings("MagicConstant") to the method I want to use getToastDuration()

Example where I get this warning:

public void method() {
Toast.makeText(this,"text",Preferences.getToastDuration()).show();
}

The Toast class is the one of Android: http://developer.android.com/reference/android/widget/Toast.html

like image 770
maysi Avatar asked Nov 01 '22 16:11

maysi


1 Answers

To answer your actual question, no, because the warning is being fired on the makeText invocation where the code analyzer isn't properly identifying that Preferences.getToastDuration() does in fact return one of the approved int values. Whichever tool is giving you the warning (IDEA?) is failing to look deep enough into the code. I don't think that the MagicConstant warning is standard, and I'd open a bug report for this.

like image 149
chrylis -cautiouslyoptimistic- Avatar answered Nov 08 '22 04:11

chrylis -cautiouslyoptimistic-