I am using IntDef from Android Support annotation in my code (but my question is wider in scope so please keep reading :) like this:
我在代码中使用来自Android Support annotation的IntDef(但我的问题范围更广,所以请继续阅读:)
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:
在这一点上,一切都很好,但是问题出现了,每当我想要将返回值从其他方法传递给setLockMode()时,比如从Parcelable实现:
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.
在这种情况下,我的IDE会抱怨我只能使用默认的、无的、可见的、透明的setLockMode()。但是getInt()不是我的方法,因此我无法注释它的返回值,并使所有这些都很满意。我也几乎确定这不是唯一的用例,但是我没能找到一种方法,要么暂时禁用这里的AllowedValues注释,要么从getInt()“转换”返回值,使AllowedValue不抱怨。
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?
我的问题是:有没有办法解决这个问题?也许我遗漏了一些关于注释的明显的东西,但是也许我应该创建bug报告来让谷歌解决这个问题?
Any input or thought appreciated.
任何输入或想法都被欣赏。
2 个解决方案
#1
13
You can suppress IntDef
Lint warnings for a method by annotating it with the following:
您可以通过以下注释来抑制方法的IntDef Lint警告:
@SuppressWarnings("ResourceType")
You can also disable these warnings for individual statements and whole classes - see the Android Tools site for further documentation.
您还可以为单个语句和整个类禁用这些警告——请参阅Android Tools站点以获得进一步的文档。
#2
5
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"?
如果您只是通过在该语句上面插入// /noinspection ResourceType来抑制该语句的警告,这是否等价于“让它理解getInt()此时返回的值是正确的”?
Alternatively, you could add to UiLockMode a simple method translating from int to @UiLockMode, e.g. something along the lines of:
或者,您可以向UiLockMode添加一个从int到@UiLockMode的简单方法,例如:
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.
然后调用setLockMode(UiLockMode.lockModeTranslate);将不再引起警告。
#1
13
You can suppress IntDef
Lint warnings for a method by annotating it with the following:
您可以通过以下注释来抑制方法的IntDef Lint警告:
@SuppressWarnings("ResourceType")
You can also disable these warnings for individual statements and whole classes - see the Android Tools site for further documentation.
您还可以为单个语句和整个类禁用这些警告——请参阅Android Tools站点以获得进一步的文档。
#2
5
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"?
如果您只是通过在该语句上面插入// /noinspection ResourceType来抑制该语句的警告,这是否等价于“让它理解getInt()此时返回的值是正确的”?
Alternatively, you could add to UiLockMode a simple method translating from int to @UiLockMode, e.g. something along the lines of:
或者,您可以向UiLockMode添加一个从int到@UiLockMode的简单方法,例如:
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.
然后调用setLockMode(UiLockMode.lockModeTranslate);将不再引起警告。