6

In my Result class I annotated with @IntDef first integer parameter in newInstance() method like this:

public class Result {
    public static final int SUCCESS = 0;
    public static final int FAIL = 1;
    public static final int UNKNOWN = 2;

    // ...

    private Result(@Status int status, Uri uri) {
        mStatus = status;
        mUri = uri;
    }

    public static Result newInstance(@Status int status, Uri uri) {
        return new Result(status, uri);
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({ SUCCESS, FAIL, UNKNOWN })
    @interface Status {}
}

Next, in my Utils class I invoke that method and pass correct constant as parameter. I ensure that I use specific set of constants like this:

public static Result foo() {
    // ...
    return Result.newInstance(Result.SUCCESS, contentUri); // line 45
}

But lint fails the build with Security error

"WrongConstant: Incorrect constant"

../../src/main/java/my/package/Utils.java:45: Must be one of: 0, 1, 2

I know that this error can be simply suppressed. But I'd like to know what's wrong with my code? Or maybe it's another issue?

Community
  • 1
  • 1
dmitriyzaitsev
  • 716
  • 9
  • 17

2 Answers2

5

I had a similar issue with a @StringDef constant. I guess this particular Lint check has some issues.

In the meantime, you can use the @SuppressLint annotation as a workaround:

public static Result foo() {
    // ...
    @SuppressLint("WrongConstant")
    return Result.newInstance(Result.SUCCESS, contentUri);
}

Edit: This issue seems to be fixed with gradle plugin version 1.4.0-beta1
Issue 182179 - android - Lint gives erroneous @StringDef errors in androidTests

nicopico
  • 3,606
  • 1
  • 28
  • 30
-1

Like the error says, the value must be 0,1,2.

Result.SUCCESS has a value of -1

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
  • 1
    No, _Result.SUCCESS_ is the constant declared in my own class. It's definitely equal to 0. You've posted the link to Activity.RESULT_OK which is out of scope. – dmitriyzaitsev Aug 27 '15 at 21:28
  • Are you sure there isn't an import that uses a different Result? – ci_ Aug 27 '15 at 21:31