Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lint fails the build with Security error "WrongConstant: Incorrect constant". IntDef annotation

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?

like image 495
dmitriyzaitsev Avatar asked Aug 27 '15 21:08

dmitriyzaitsev


1 Answers

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

like image 51
nicopico Avatar answered Sep 20 '22 23:09

nicopico