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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With