11

I am using IntDef from Android Support annotation in my code (but my question is wider in scope so please keep reading :) like this:

public class UiLockMode
{
    @IntDef({DEFAULT, NONE, VISIBLE, TRANSPARENT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface AllowedValues {}

    public static final int DEFAULT     = 0;
    public static final int NONE        = 1;
    public static final int VISIBLE     = 2;
    public static final int TRANSPARENT = 3;
}

Next, I got some other methods annotated with it like this:

protected void setLockMode(@UiLockMode.AllowedValues int lockMode) {
    ...

At that point is all fine and nice but the problem shows up whenever I'd like to pass return value from other methods to setLockMode(), like i.e. from Parcelable implementation:

private Foo(Parcel in) {
    ...
    setLockMode(in.getInt());

In such case my IDE complains that I am only allowed to use DEFAULT, NONE, VISIBLE, TRANSPARENT with setLockMode(). But getInt() is not my method so I cannot annotate its return value and make all this happy. I am also almost sure this is not unique use case but I failed to find the way to either temporarily disable AllowedValues annotation from complaining here or to "cast" return value from getInt() to make AllowedValue not complaining.

So my questions are: is there any way of solving this problem? Maybe I am missing something obvious about annotations but maybe I shall be creating bug report to make Google address this problem instead?

Any input or thought appreciated.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141

2 Answers2

13

You can suppress IntDef Lint warnings for a method by annotating it with the following:

@SuppressWarnings("ResourceType")

You can also disable these warnings for individual statements and whole classes - see the Android Tools site for further documentation.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
fractalwrench
  • 4,028
  • 7
  • 33
  • 49
  • Thanks for the answer, +1. Yes, I know I can suppress warnings, but I would like not to unless I got no other option, so basically I wonder if here is any other way of solve my issue. – Marcin Orlowski Jul 30 '15 at 21:13
  • I suppose you could try extending Parcel to add the myGetInt method in there. – fractalwrench Jul 30 '15 at 21:16
  • Just checked and extending is not a way to go as you will still see complains as annotation makes it expect `int` not a method. – Marcin Orlowski Jul 30 '15 at 22:44
7

If you only suppress the warning for this single statement by inserting //noinspection ResourceType above it, isn't this equivalent to "making it understand the value returned by getInt() at this point is correct"?

Alternatively, you could add to UiLockMode a simple method translating from int to @UiLockMode, e.g. something along the lines of:

public @UiLockMode.AllowedValues static int lockModeTranslate(int val)
{
    switch(val)
    {
        case 0: return UiLockMode.DEFAULT;
        case 1: return UiLockMode.NONE;
        case 2: return UiLockMode.TRANSPARENT;
        case 3: return UiLockMode.VISIBLE;
    }

    throw new SomethingHorrible;
}

Then a call like setLockMode(UiLockMode.lockModeTranslate(in.getInt())); will no longer cause a warning.

Miloslaw Smyk
  • 1,305
  • 10
  • 15
  • Well, suppressing is not equivalent way of saying "this is a correct value". It's often a way of saying "I cannot check that really" or "I do not care it's valid or not" (mostly as result of "I cannot check that really" :). Your code would shut the complaints but it is rather a work around not a solution. If I'd have this thing auto-generated (with i.e. other annotation) then maybe that would be ok, but otherwise suppressing is more clear. Too bad, of course... – Marcin Orlowski Jul 31 '15 at 07:46
  • 2
    Black magic: public @UiLockMode.AllowedValues static int lockModeTranslate(int val) { return val; } – hector6872 Jan 26 '16 at 20:46
  • @hrules6872 but what if you pass unsupported value? – Leo DroidCoder Apr 11 '17 at 14:11
  • Love this solution, since its throwns an SomethingHorribleException instead of unexcpected results – Marcos Vasconcelos Dec 31 '19 at 21:25